Skip to content

Commit 5848223

Browse files
committed
Add appVersion configuration option to track a context entity with the application version (#1373)
1 parent 5d58a0f commit 5848223

File tree

7 files changed

+82
-4
lines changed

7 files changed

+82
-4
lines changed

api-docs/docs/browser-tracker/browser-tracker.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ export type TrackerConfiguration = {
523523
cookieLifetime?: number;
524524
sessionCookieTimeout?: number;
525525
appId?: string;
526+
appVersion?: string;
526527
platform?: Platform;
527528
respectDoNotTrack?: boolean;
528529
crossDomainLinker?: (elt: HTMLAnchorElement | HTMLAreaElement) => boolean;

api-docs/docs/browser-tracker/markdown/browser-tracker.trackerconfiguration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type TrackerConfiguration = {
1818
cookieLifetime?: number;
1919
sessionCookieTimeout?: number;
2020
appId?: string;
21+
appVersion?: string;
2122
platform?: Platform;
2223
respectDoNotTrack?: boolean;
2324
crossDomainLinker?: (elt: HTMLAnchorElement | HTMLAreaElement) => boolean;
@@ -44,6 +45,5 @@ newTracker('sp1', 'collector.my-website.com', {
4445
plugins: [ PerformanceTimingPlugin(), AdTrackingPlugin() ],
4546
stateStorageStrategy: 'cookieAndLocalStorage'
4647
});
47-
4848
```
4949

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-tracker-core",
5+
"comment": "Add appVersion configuration option to track a context entity with the application version",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-tracker-core"
10+
}

libraries/browser-tracker-core/src/tracker/index.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import {
6565
emptyIdCookie,
6666
eventIndexFromIdCookie,
6767
} from './id_cookie';
68-
import { CLIENT_SESSION_SCHEMA, WEB_PAGE_SCHEMA, BROWSER_CONTEXT_SCHEMA } from './schemata';
68+
import { CLIENT_SESSION_SCHEMA, WEB_PAGE_SCHEMA, BROWSER_CONTEXT_SCHEMA, APPLICATION_CONTEXT_SCHEMA } from './schemata';
6969
import { getBrowserProperties } from '../helpers/browser_props';
7070
import { asyncCookieStorage, syncCookieStorage } from './cookie_storage';
7171

@@ -149,13 +149,13 @@ export function Tracker(
149149
if (typeof config.anonymousTracking === 'boolean') {
150150
return false;
151151
}
152-
return config.anonymousTracking?.withSessionTracking === true ?? false;
152+
return config.anonymousTracking?.withSessionTracking === true;
153153
},
154154
getAnonymousServerTracking = (config: TrackerConfiguration) => {
155155
if (typeof config.anonymousTracking === 'boolean') {
156156
return false;
157157
}
158-
return config.anonymousTracking?.withServerAnonymisation === true ?? false;
158+
return config.anonymousTracking?.withServerAnonymisation === true;
159159
},
160160
getAnonymousTracking = (config: TrackerConfiguration) => !!config.anonymousTracking,
161161
isBrowserContextAvailable = trackerConfiguration?.contexts?.browser ?? false,
@@ -203,6 +203,8 @@ export function Tracker(
203203
configPlatform = trackerConfiguration.platform ?? 'web',
204204
// Site ID
205205
configTrackerSiteId = trackerConfiguration.appId ?? '',
206+
// Application version
207+
configAppVersion = trackerConfiguration.appVersion,
206208
// Document URL
207209
configCustomUrl: string,
208210
// Document title
@@ -323,6 +325,20 @@ export function Tracker(
323325
core.addPayloadPair('cd', colorDepth);
324326
if (timeZone) core.addPayloadPair('tz', timeZone);
325327

328+
// Add the application version context entity
329+
if (configAppVersion) {
330+
core.addPlugin({
331+
plugin: {
332+
contexts: () => [
333+
{
334+
schema: APPLICATION_CONTEXT_SCHEMA,
335+
data: { version: configAppVersion },
336+
},
337+
],
338+
},
339+
});
340+
}
341+
326342
/*
327343
* Initialize tracker
328344
*/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const WEB_PAGE_SCHEMA = 'iglu:com.snowplowanalytics.snowplow/web_page/jsonschema/1-0-0';
22
export const BROWSER_CONTEXT_SCHEMA = 'iglu:com.snowplowanalytics.snowplow/browser_context/jsonschema/2-0-0';
33
export const CLIENT_SESSION_SCHEMA = 'iglu:com.snowplowanalytics.snowplow/client_session/jsonschema/1-0-2';
4+
export const APPLICATION_CONTEXT_SCHEMA = 'iglu:com.snowplowanalytics.snowplow/application/jsonschema/1-0-0';

libraries/browser-tracker-core/src/tracker/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ export type TrackerConfiguration = {
117117
sessionCookieTimeout?: number;
118118
/** The app id to send with each event */
119119
appId?: string;
120+
/**
121+
* Version of the application tracked as a context entity with all events.
122+
* Can be a semver-like structure (e.g 1.1.0) or a Git commit SHA hash.
123+
* Entity schema: iglu:com.snowplowanalytics.snowplow/application/jsonschema/1-0-0
124+
*/
125+
appVersion?: string;
120126
/**
121127
* The platform the event is being sent from
122128
* @defaultValue web
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { APPLICATION_CONTEXT_SCHEMA } from '../../src/tracker/schemata';
2+
import { createTracker } from '../helpers';
3+
4+
describe('Application context:', () => {
5+
it('Adds the entity when the appVersion option is configured', (done) => {
6+
const tracker = createTracker({
7+
appVersion: '1.0.2-beta.2',
8+
plugins: [
9+
{
10+
filter: (payload) => {
11+
const { data: payloadData } = JSON.parse(payload.co as string);
12+
const appContext = payloadData.find((context: any) => context.schema.match(APPLICATION_CONTEXT_SCHEMA));
13+
expect(appContext).toBeTruthy();
14+
expect(appContext.data.version).toBe('1.0.2-beta.2');
15+
done();
16+
return false;
17+
},
18+
},
19+
],
20+
});
21+
22+
tracker?.trackPageView();
23+
});
24+
25+
it('Does not attach the entity if not configured', (done) => {
26+
const tracker = createTracker({
27+
plugins: [
28+
{
29+
filter: (payload) => {
30+
const { data: payloadData } = JSON.parse(payload.co as string);
31+
const applicationContext = payloadData.find((context: any) =>
32+
context.schema.match(APPLICATION_CONTEXT_SCHEMA)
33+
);
34+
expect(applicationContext).toBeUndefined();
35+
done();
36+
return false
37+
},
38+
},
39+
],
40+
});
41+
42+
tracker?.trackPageView();
43+
});
44+
});

0 commit comments

Comments
 (0)