Skip to content

Feat/pixel adapter #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"dist/streamsql.esm.js": {
"bundled": 7741,
"minified": 3828,
"gzipped": 1575,
"bundled": 8133,
"minified": 3944,
"gzipped": 1606,
"treeshaked": {
"rollup": {
"code": 75,
"import_statements": 0
},
"webpack": {
"code": 4442
"code": 4558
}
}
},
"dist/streamsql.cjs.js": {
"bundled": 9122,
"minified": 4326,
"gzipped": 1641
"bundled": 9592,
"minified": 4470,
"gzipped": 1679
},
"dist/streamsql.min.js": {
"bundled": 10015,
"minified": 4153,
"gzipped": 1615
"bundled": 10483,
"minified": 4297,
"gzipped": 1655
}
}
2 changes: 1 addition & 1 deletion integration/fixtures/streamsql.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions integration/fixtures/template-fns.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="./streamsql.min.js"></script>
<title>Test Template Functions</title>
</head>
<body>
<h1>Alt Page</h1>
<input id="input-user-id" type="text" />
<button id="login-button">Login</button>
<button id="logout-button">Logout</button>
<div id="identify-response">
<!-- Plug/unplug user id here -->
</div>


<script type="application/javascript">
streamsql.init('0000-0000-0000-0000'); // create client with apiKey
// These should be called on (un)identify.
// Test by checking the text of user-id response
streamsql.onIdentify = function(userId) {
document.getElementById('identify-response').innerText = userId;
}
streamsql.onUnidentify = function() {
document.getElementById('identify-response').innerText = '';
}
document.getElementById('login-button').addEventListener('click', function () {
var userId = document.getElementById('input-user-id').value;
streamsql.identify(userId);
})
document.getElementById('logout-button').addEventListener('click', function () {
streamsql.unidentify() // clear the userid
})
</script>
</body>
</html>
36 changes: 26 additions & 10 deletions integration/index.umd.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const home = SERVER_URL + '/index.html'
const alt = SERVER_URL + '/alt.html'
const templatePage = SERVER_URL + '/template-fns.html'

describe('integration', () => {
let page
Expand Down Expand Up @@ -29,8 +30,8 @@ describe('integration', () => {
if (postData) {
postData = JSON.parse(postData)
}
expect(postData.streamName).toMatch('clickstream')
expect(postData.eventTimestamp).toBeGreaterThan(15e9)
expect(postData.stream).toMatch('clickstream')
expect(postData.data.timestamp).toBeGreaterThan(15e9)
}, 10000)

it('sends page context with url, title, and referrer', async () => {
Expand All @@ -41,7 +42,7 @@ describe('integration', () => {
await page.goto(home)
await page.click('button#count-button')
postData = postData && JSON.parse(postData)
expect(postData.context).toMatchObject({
expect(postData.data.context).toMatchObject({
url: await page.url(),
title: await page.title(),
referrer: '',
Expand All @@ -50,7 +51,7 @@ describe('integration', () => {
await Promise.all([page.waitForNavigation(), page.click('a')])
await page.click('button#send-button')
postData = postData && JSON.parse(postData)
expect(postData.context).toMatchObject({
expect(postData.data.context).toMatchObject({
url: await page.url(),
title: await page.title(),
referrer: expect.stringMatching(SERVER_URL),
Expand Down Expand Up @@ -107,27 +108,27 @@ describe('integration', () => {
await page.type('input#user-input', userId)
await page.click('button#login-button')
postData = postData && JSON.parse(postData)
expect(postData.user).toMatchObject({ id: userId })
expect(postData.data.user).toMatchObject({ id: userId })
postData = undefined // reset for next page

// visit the other page, should persist userId
await Promise.all([page.waitForNavigation(), page.click('a')])
await page.click('button#send-button')
postData = postData && JSON.parse(postData)
expect(postData.user).toMatchObject({ id: userId })
expect(postData.data.user).toMatchObject({ id: userId })
postData = undefined

// do logout, ensure logout sent event does NOT have user id
await page.click('#logout-button')
postData = postData && JSON.parse(postData)
expect(postData.user).toMatchObject({ id: '' })
expect(postData.data.user).toMatchObject({ id: '' })
postData = undefined

// go back and send event from home page, should NOT have user id
await page.goBack()
await page.click('#count-button')
postData = postData && JSON.parse(postData)
expect(postData.user).toMatchObject({ id: '' })
expect(postData.data.user).toMatchObject({ id: '' })
}, 20000)

it('persists user id when coming back to site', async () => {
Expand All @@ -144,7 +145,7 @@ describe('integration', () => {
await page.type('input#user-input', userId)
await page.click('button#login-button')
postData = postData && JSON.parse(postData)
expect(postData.user).toMatchObject({ id: userId })
expect(postData.data.user).toMatchObject({ id: userId })
postData = undefined // reset for next page

// visit the other page, should persist userId
Expand All @@ -153,6 +154,21 @@ describe('integration', () => {
await page.goto(alt)
await page.click('button#send-button')
postData = postData && JSON.parse(postData)
expect(postData.user).toMatchObject({ id: userId })
expect(postData.data.user).toMatchObject({ id: userId })
}, 8000)

it('calls template functions onIdentify and onUnidentify', async () => {
const userId = 'user-id'
const responseDivSelector = 'div#identify-response'
// simulate a login that should store id to send w request
await page.goto(templatePage)
await page.type('#input-user-id', userId)
await page.click('button#login-button')
const identifyResponse = await page.$(responseDivSelector)
expect(await identifyResponse.evaluate(node => node.innerText)).toBe(userId);

await page.click('button#logout-button')
const unidentifyResponse = await page.$(responseDivSelector)
expect(await unidentifyResponse.evaluate(node => node.innerText)).toBe('');
})
})
18 changes: 14 additions & 4 deletions src/StreamSQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ export default class StreamSQLClient implements CoreAPI {
public identify(userId: string): StreamSQLClient {
if (!this.identifier) this.throwNoInitError()
this.identifier.setUser(userId)
this.onIdentify(userId) // call template listener
return this
}

public unidentify(): StreamSQLClient {
if (!this.identifier) this.throwNoInitError()
this.identifier.deleteUser()
this.onUnidentify() // call template listener
return this
}

Expand All @@ -57,9 +59,8 @@ export default class StreamSQLClient implements CoreAPI {
if (!this.pageCtx || !this.identifier) {
this.throwNoInitError()
}
return {
streamName: streamName.toLowerCase(),
eventTimestamp: new Date().getTime(),
const streamsqlData = {
timestamp: new Date().getTime(),
context: {
url: this.pageCtx.location(),
referrer: this.pageCtx.referrer(),
Expand All @@ -69,11 +70,20 @@ export default class StreamSQLClient implements CoreAPI {
id: this.identifier.getUser(),
// FUTURE: ability to add other user props
},
data: data || {}
}
const _data = data ? Object.assign({}, streamsqlData, data) : streamsqlData
return {
stream: streamName.toLowerCase(),
data: _data
}
}

private throwNoInitError(): never {
throw streamsqlErr(`api key must be set first: streamsql.init(apiKey)`)
}

// Template functions. Originally built to allow pixel to listen for events.
// @ts-ignore
public onIdentify(userId: string) {}
public onUnidentify() {}
}
16 changes: 16 additions & 0 deletions src/test/streamsql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,20 @@ describe('StreamSQL Core', () => {
})
expect(() => streamsql.sendEvent('mystream')).not.toThrow()
})

it('calls template listeners for identify, unidentify', () => {
streamsql.init(apiKey)
const onIdentify = jest.fn((userId: string) => userId)
const onUnidentify = jest.fn()

streamsql.onIdentify = onIdentify
streamsql.onUnidentify = onUnidentify

streamsql.identify('user-id')
expect(onIdentify).toHaveBeenCalledTimes(1)
expect(onIdentify).toHaveBeenCalledWith('user-id')

streamsql.unidentify()
expect(onUnidentify).toHaveBeenCalledTimes(1)
})
})