Skip to content

Commit 48603ad

Browse files
authored
Merge pull request #2 from streamsqlio/feat/beta-data-compat
add api version, language, and ua to data
2 parents 3e8bf98 + ea3ee2e commit 48603ad

File tree

11 files changed

+126
-64
lines changed

11 files changed

+126
-64
lines changed

.size-snapshot.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
{
22
"dist/streamsql.esm.js": {
3-
"bundled": 8092,
4-
"minified": 3922,
5-
"gzipped": 1610,
3+
"bundled": 8662,
4+
"minified": 4273,
5+
"gzipped": 1705,
66
"treeshaked": {
77
"rollup": {
88
"code": 75,
99
"import_statements": 0
1010
},
1111
"webpack": {
12-
"code": 4536
12+
"code": 4866
1313
}
1414
}
1515
},
1616
"dist/streamsql.cjs.js": {
17-
"bundled": 9528,
18-
"minified": 4442,
19-
"gzipped": 1682
17+
"bundled": 10213,
18+
"minified": 4843,
19+
"gzipped": 1782
2020
},
2121
"dist/streamsql.min.js": {
22-
"bundled": 10419,
23-
"minified": 4269,
24-
"gzipped": 1653
22+
"bundled": 11154,
23+
"minified": 4647,
24+
"gzipped": 1748
2525
}
2626
}

integration/fixtures/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports.apiEndpoint = '/api/send/single'
1+
module.exports.apiEndpoint = '/api/send/pixel'
22
module.exports.apiEndpointBatch = '/api/send/batch'
33
module.exports.port = 9000
44
module.exports.httpsPort = 9001

integration/fixtures/streamsql.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/index.umd.test.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('integration', () => {
2020
expect(isResponseOK).toBe(true)
2121
})
2222

23-
it('sends streamname and timestamp', async () => {
23+
it('sends streamname, timestamp, and apiVersion', async () => {
2424
let postData
2525
await page.goto(home)
2626
page.on('request', req => {
@@ -32,20 +32,25 @@ describe('integration', () => {
3232
}
3333
expect(postData.stream).toMatch('clickstream')
3434
expect(postData.data.timestamp).toBeGreaterThan(15e9)
35+
expect(postData.data.apiVersion).toMatch(/v[0-9]+/)
3536
}, 10000)
3637

37-
it('sends page context with url, title, and referrer', async () => {
38+
it('sends page context with url, title, ref, lang, ua', async () => {
3839
let postData
3940
page.on('request', req => {
4041
if (req.method() === 'POST') postData = req.postData()
4142
})
43+
const ua = "Iceweasel/001"
44+
await page.setUserAgent(ua)
4245
await page.goto(home)
4346
await page.click('button#count-button')
4447
postData = postData && JSON.parse(postData)
4548
expect(postData.data.context).toMatchObject({
4649
url: await page.url(),
4750
title: await page.title(),
4851
referrer: '',
52+
userAgent: ua,
53+
language: expect.stringMatching('en'),
4954
})
5055
// visit the other page
5156
await Promise.all([page.waitForNavigation(), page.click('a')])
@@ -55,6 +60,8 @@ describe('integration', () => {
5560
url: await page.url(),
5661
title: await page.title(),
5762
referrer: expect.stringMatching(SERVER_URL),
63+
userAgent: ua,
64+
language: expect.stringMatching('en'),
5865
})
5966
}, 15000)
6067

@@ -80,7 +87,7 @@ describe('integration', () => {
8087
for (let i = 1; i <= 5; i += 1) {
8188
await page.click('button#count-button')
8289
postData = postData && JSON.parse(postData)
83-
expect(postData.data.count).toEqual(i)
90+
expect(postData.data.event.count).toEqual(i)
8491
}
8592
}, 12000)
8693

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "@streamsql/streamsql-js",
33
"author": "StreamSQL <ethan@streamsql.io>",
44
"version": "1.0.0-beta.0",
5+
"config": {
6+
"apiVersion": "v1-beta"
7+
},
58
"license": "MIT",
69
"description": "StreamSQL's javascript ingestion API",
710
"main": "streamsql.cjs.js",
@@ -13,7 +16,7 @@
1316
"prebuild": "rimraf dist",
1417
"rollup": "rollup -c",
1518
"rollup:test": "rollup --config rollup-test.config.js",
16-
"copy": "copyfiles -f package.json readme.md LICENSE streamsql.d.ts dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.prettier=undefined; this.jest=undefined; this.babel=undefined; this.np=undefined; this.publishConfig=undefined;\"",
19+
"copy": "copyfiles -f package.json readme.md LICENSE streamsql.d.ts dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.prettier=undefined; this.jest=undefined; this.babel=undefined; this.config=undefined; this.np=undefined; this.publishConfig=undefined;\"",
1720
"build": "npm-run-all --parallel rollup copy",
1821
"prepare": "npm run build",
1922
"serve:test": "cross-env NODE_ENV=test node integration/fixtures/server.js",
@@ -28,6 +31,7 @@
2831
"format": "npm run prettier -- --write",
2932
"check-format": "npm run prettier --list-different"
3033
},
34+
3135
"devDependencies": {
3236
"@babel/core": "^7.8.4",
3337
"@babel/plugin-proposal-class-properties": "^7.8.3",

src/Page.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
export interface PageContextProvider {
2-
location(): string
32
referrer(): string
43
title(): string
4+
language(): string
5+
userAgent(): string
6+
location(): string // url
57
}
68

79
export default class PageContext implements PageContextProvider {
@@ -26,6 +28,20 @@ export default class PageContext implements PageContextProvider {
2628
return ''
2729
}
2830

31+
public language(): string {
32+
if (this.isNavigator()) {
33+
return navigator.language || ''
34+
}
35+
return ''
36+
}
37+
38+
public userAgent(): string {
39+
if (this.isNavigator()) {
40+
return navigator.userAgent || ''
41+
}
42+
return ''
43+
}
44+
2945
protected isWindow(prop: string): boolean {
3046
return (
3147
typeof window !== 'undefined'
@@ -36,4 +52,8 @@ export default class PageContext implements PageContextProvider {
3652
protected isDocument(): boolean {
3753
return this.isWindow('document') && typeof document !== 'undefined'
3854
}
55+
56+
protected isNavigator(): boolean {
57+
return this.isWindow('navigator') && typeof navigator !== 'undefined'
58+
}
3959
}

src/StreamSQL.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import XHR, { Fetcher, RequestCallback } from './XHR'
22
import UserIdentifier, { Identifier } from './Identify'
33
import PageContext, { PageContextProvider } from './Page'
44
import { streamsqlErr, isValidAPIKeyFormat } from './utils'
5-
import { apiEndpoint } from './config'
5+
import { apiEndpoint, apiVersion } from './config'
66

77
export interface CoreAPI {
88
init(apiKey: string): CoreAPI
@@ -61,17 +61,22 @@ export default class StreamSQLClient implements CoreAPI {
6161
}
6262
const streamsqlData = {
6363
timestamp: new Date().getTime(),
64+
apiVersion: this.version(),
6465
context: {
65-
url: this.pageCtx.location(),
66+
language: this.pageCtx.language(),
6667
referrer: this.pageCtx.referrer(),
6768
title: this.pageCtx.title(),
69+
userAgent: this.pageCtx.userAgent(),
70+
url: this.pageCtx.location(),
6871
},
6972
user: {
7073
id: this.identifier.getUser(),
7174
// FUTURE: ability to add other user props
7275
},
7376
}
74-
const _data = data ? Object.assign({}, streamsqlData, data) : streamsqlData
77+
const _data = data
78+
? Object.assign({}, streamsqlData, { event: data })
79+
: streamsqlData
7580
return {
7681
stream: streamName.toLowerCase(),
7782
data: _data
@@ -82,6 +87,11 @@ export default class StreamSQLClient implements CoreAPI {
8287
throw streamsqlErr(`api key must be set first: streamsql.init(apiKey)`)
8388
}
8489

90+
// For pixel
91+
private version(): string {
92+
return apiVersion
93+
}
94+
8595
// Template functions. Originally built to allow pixel to listen for events.
8696
// @ts-ignore
8797
public onIdentify(userId: string) {}

src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export const apiEndpoint = `__APIHOST__` + `/api/send/single`
1+
export const apiEndpoint = `__APIHOST__` + `/api/send/pixel`
2+
export const apiVersion = `v1-beta` // FIXME: hardcoded

src/test/page.incompat.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ describe('Page Context - No Browser', () => {
1515
expect(new PageContext().referrer()).toBe('')
1616
expect(new PageContext().title()).toBe('')
1717
})
18+
19+
it('returns empty strings if navigator is not available', () => {
20+
expect(new PageContext().language()).toBe('')
21+
expect(new PageContext().userAgent()).toBe('')
22+
})
1823
})

src/test/page.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ describe('Page Context', () => {
2222
expect(pageCtx.title()).toBe('Home Page')
2323
window.document.title = ''
2424
})
25+
26+
it('returns the user agent', () => {
27+
const ua = "Mozilla/5.0" // beginning of jsDom default
28+
pageCtx = new PageContext()
29+
expect(pageCtx.userAgent()).toMatch(ua)
30+
})
31+
32+
it('returns the user agent', () => {
33+
const lang = 'en-US' // jsDom default
34+
pageCtx = new PageContext()
35+
expect(pageCtx.language()).toMatch(lang)
36+
})
2537
})

0 commit comments

Comments
 (0)