Skip to content

add api version, language, and ua to data #2

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 1 commit into from
Feb 28, 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": 8092,
"minified": 3922,
"gzipped": 1610,
"bundled": 8662,
"minified": 4273,
"gzipped": 1705,
"treeshaked": {
"rollup": {
"code": 75,
"import_statements": 0
},
"webpack": {
"code": 4536
"code": 4866
}
}
},
"dist/streamsql.cjs.js": {
"bundled": 9528,
"minified": 4442,
"gzipped": 1682
"bundled": 10213,
"minified": 4843,
"gzipped": 1782
},
"dist/streamsql.min.js": {
"bundled": 10419,
"minified": 4269,
"gzipped": 1653
"bundled": 11154,
"minified": 4647,
"gzipped": 1748
}
}
2 changes: 1 addition & 1 deletion integration/fixtures/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports.apiEndpoint = '/api/send/single'
module.exports.apiEndpoint = '/api/send/pixel'
module.exports.apiEndpointBatch = '/api/send/batch'
module.exports.port = 9000
module.exports.httpsPort = 9001
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.

13 changes: 10 additions & 3 deletions integration/index.umd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('integration', () => {
expect(isResponseOK).toBe(true)
})

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

it('sends page context with url, title, and referrer', async () => {
it('sends page context with url, title, ref, lang, ua', async () => {
let postData
page.on('request', req => {
if (req.method() === 'POST') postData = req.postData()
})
const ua = "Iceweasel/001"
await page.setUserAgent(ua)
await page.goto(home)
await page.click('button#count-button')
postData = postData && JSON.parse(postData)
expect(postData.data.context).toMatchObject({
url: await page.url(),
title: await page.title(),
referrer: '',
userAgent: ua,
language: expect.stringMatching('en'),
})
// visit the other page
await Promise.all([page.waitForNavigation(), page.click('a')])
Expand All @@ -55,6 +60,8 @@ describe('integration', () => {
url: await page.url(),
title: await page.title(),
referrer: expect.stringMatching(SERVER_URL),
userAgent: ua,
language: expect.stringMatching('en'),
})
}, 15000)

Expand All @@ -80,7 +87,7 @@ describe('integration', () => {
for (let i = 1; i <= 5; i += 1) {
await page.click('button#count-button')
postData = postData && JSON.parse(postData)
expect(postData.data.count).toEqual(i)
expect(postData.data.event.count).toEqual(i)
}
}, 12000)

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "@streamsql/streamsql-js",
"author": "StreamSQL <ethan@streamsql.io>",
"version": "1.0.0-beta.0",
"config": {
"apiVersion": "v1-beta"
},
"license": "MIT",
"description": "StreamSQL's javascript ingestion API",
"main": "streamsql.cjs.js",
Expand All @@ -13,7 +16,7 @@
"prebuild": "rimraf dist",
"rollup": "rollup -c",
"rollup:test": "rollup --config rollup-test.config.js",
"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;\"",
"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;\"",
"build": "npm-run-all --parallel rollup copy",
"prepare": "npm run build",
"serve:test": "cross-env NODE_ENV=test node integration/fixtures/server.js",
Expand All @@ -28,6 +31,7 @@
"format": "npm run prettier -- --write",
"check-format": "npm run prettier --list-different"
},

"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/plugin-proposal-class-properties": "^7.8.3",
Expand Down
22 changes: 21 additions & 1 deletion src/Page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export interface PageContextProvider {
location(): string
referrer(): string
title(): string
language(): string
userAgent(): string
location(): string // url
}

export default class PageContext implements PageContextProvider {
Expand All @@ -26,6 +28,20 @@ export default class PageContext implements PageContextProvider {
return ''
}

public language(): string {
if (this.isNavigator()) {
return navigator.language || ''
}
return ''
}

public userAgent(): string {
if (this.isNavigator()) {
return navigator.userAgent || ''
}
return ''
}

protected isWindow(prop: string): boolean {
return (
typeof window !== 'undefined'
Expand All @@ -36,4 +52,8 @@ export default class PageContext implements PageContextProvider {
protected isDocument(): boolean {
return this.isWindow('document') && typeof document !== 'undefined'
}

protected isNavigator(): boolean {
return this.isWindow('navigator') && typeof navigator !== 'undefined'
}
}
16 changes: 13 additions & 3 deletions src/StreamSQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import XHR, { Fetcher, RequestCallback } from './XHR'
import UserIdentifier, { Identifier } from './Identify'
import PageContext, { PageContextProvider } from './Page'
import { streamsqlErr, isValidAPIKeyFormat } from './utils'
import { apiEndpoint } from './config'
import { apiEndpoint, apiVersion } from './config'

export interface CoreAPI {
init(apiKey: string): CoreAPI
Expand Down Expand Up @@ -61,17 +61,22 @@ export default class StreamSQLClient implements CoreAPI {
}
const streamsqlData = {
timestamp: new Date().getTime(),
apiVersion: this.version(),
context: {
url: this.pageCtx.location(),
language: this.pageCtx.language(),
referrer: this.pageCtx.referrer(),
title: this.pageCtx.title(),
userAgent: this.pageCtx.userAgent(),
url: this.pageCtx.location(),
},
user: {
id: this.identifier.getUser(),
// FUTURE: ability to add other user props
},
}
const _data = data ? Object.assign({}, streamsqlData, data) : streamsqlData
const _data = data
? Object.assign({}, streamsqlData, { event: data })
: streamsqlData
return {
stream: streamName.toLowerCase(),
data: _data
Expand All @@ -82,6 +87,11 @@ export default class StreamSQLClient implements CoreAPI {
throw streamsqlErr(`api key must be set first: streamsql.init(apiKey)`)
}

// For pixel
private version(): string {
return apiVersion
}

// Template functions. Originally built to allow pixel to listen for events.
// @ts-ignore
public onIdentify(userId: string) {}
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const apiEndpoint = `__APIHOST__` + `/api/send/single`
export const apiEndpoint = `__APIHOST__` + `/api/send/pixel`
export const apiVersion = `v1-beta` // FIXME: hardcoded
5 changes: 5 additions & 0 deletions src/test/page.incompat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ describe('Page Context - No Browser', () => {
expect(new PageContext().referrer()).toBe('')
expect(new PageContext().title()).toBe('')
})

it('returns empty strings if navigator is not available', () => {
expect(new PageContext().language()).toBe('')
expect(new PageContext().userAgent()).toBe('')
})
})
12 changes: 12 additions & 0 deletions src/test/page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ describe('Page Context', () => {
expect(pageCtx.title()).toBe('Home Page')
window.document.title = ''
})

it('returns the user agent', () => {
const ua = "Mozilla/5.0" // beginning of jsDom default
pageCtx = new PageContext()
expect(pageCtx.userAgent()).toMatch(ua)
})

it('returns the user agent', () => {
const lang = 'en-US' // jsDom default
pageCtx = new PageContext()
expect(pageCtx.language()).toMatch(lang)
})
})
Loading