Skip to content

Commit b212eb7

Browse files
committed
Add setSessionId and setSessionIndex capability to Node.js tracker
close #1178
1 parent e718126 commit b212eb7

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
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/node-tracker",
5+
"comment": "Add setSessionId and setSessionIndex capability",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/node-tracker"
10+
}

trackers/node-tracker/docs/node-tracker.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ export type Timestamp = TrueTimestamp | DeviceTimestamp | number;
360360
export interface Tracker extends TrackerCore {
361361
setDomainUserId: (userId: string) => void;
362362
setNetworkUserId: (userId: string) => void;
363+
setSessionId: (sessionId: string) => void;
364+
setSessionIndex: (sessionIndex: string | number) => void;
363365
}
364366

365367
// @public

trackers/node-tracker/src/tracker.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ export interface Tracker extends TrackerCore {
4646
* @param userId - The network user id
4747
*/
4848
setNetworkUserId: (userId: string) => void;
49+
50+
/**
51+
* Set the session ID (`domain_sessionid` in the atomic events)
52+
*
53+
* @param sessionId - The session id
54+
*/
55+
setSessionId: (sessionId: string) => void;
56+
57+
/**
58+
* Set the session index (`domain_sessionidx` in the atomic events)
59+
*
60+
* @param sessionIndex - The session index
61+
*/
62+
setSessionIndex: (sessionIndex: string | number) => void;
4963
}
5064

5165
/**
@@ -64,6 +78,8 @@ export function tracker(
6478
): Tracker {
6579
let domainUserId: string;
6680
let networkUserId: string;
81+
let sessionId: string;
82+
let sessionIndex: string | number;
6783
let allEmitters: Array<Emitter>;
6884

6985
if (Array.isArray(emitters)) {
@@ -77,6 +93,8 @@ export function tracker(
7793
const addUserInformation = (payload: PayloadBuilder): void => {
7894
payload.add('duid', domainUserId);
7995
payload.add('nuid', networkUserId);
96+
payload.add('sid', sessionId);
97+
payload.add('vid', Number(sessionIndex));
8098
};
8199

82100
/**
@@ -107,9 +125,19 @@ export function tracker(
107125
networkUserId = userId;
108126
};
109127

128+
const setSessionId = function (currentSessionId: string) {
129+
sessionId = currentSessionId;
130+
};
131+
132+
const setSessionIndex = function (currentSessionIndex: string | number) {
133+
sessionIndex = currentSessionIndex;
134+
};
135+
110136
return {
111137
setDomainUserId,
112138
setNetworkUserId,
139+
setSessionId,
140+
setSessionIndex,
113141
...core,
114142
};
115143
}

trackers/node-tracker/test/tracker.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ for (const method of testMethods) {
422422
refr: 'http://google.com',
423423
p: 'web',
424424
uid: 'jacob',
425+
sid: 'sessionId',
426+
vid: '10',
425427
res: '400x200',
426428
vp: '500x800',
427429
cd: '24',
@@ -449,6 +451,8 @@ for (const method of testMethods) {
449451

450452
track.setPlatform('web');
451453
track.setUserId('jacob');
454+
track.setSessionId('sessionId');
455+
track.setSessionIndex(10);
452456
track.setScreenResolution('400', '200');
453457
track.setViewport('500', '800');
454458
track.setColorDepth('24');
@@ -604,4 +608,64 @@ for (const method of testMethods) {
604608
);
605609
});
606610
});
611+
612+
test(method + ' method: setSessionId should attach a sid property to event', async (t) => {
613+
const expected = {
614+
sid: 'sid-test-1234',
615+
};
616+
617+
await new Promise((resolve, reject) => {
618+
const e = gotEmitter(
619+
endpoint,
620+
HttpProtocol.HTTPS,
621+
undefined,
622+
method,
623+
0,
624+
undefined,
625+
undefined,
626+
function (error, response) {
627+
checkPayload(extractPayload(response?.body, method), expected, t);
628+
if (error) reject(error);
629+
else resolve(response);
630+
}
631+
);
632+
633+
const track = tracker(e, 'cf', 'cfe35', false);
634+
track.setSessionId(expected.sid);
635+
track.track(
636+
buildPageView({ pageUrl: 'http://www.example.com', pageTitle: 'example page', referrer: 'http://google.com' }),
637+
context
638+
);
639+
});
640+
});
641+
642+
test(method + ' method: setSessionIndex should attach a vid property to event', async (t) => {
643+
const expected = {
644+
vid: '1234',
645+
};
646+
647+
await new Promise((resolve, reject) => {
648+
const e = gotEmitter(
649+
endpoint,
650+
HttpProtocol.HTTPS,
651+
undefined,
652+
method,
653+
0,
654+
undefined,
655+
undefined,
656+
function (error, response) {
657+
checkPayload(extractPayload(response?.body, method), expected, t);
658+
if (error) reject(error);
659+
else resolve(response);
660+
}
661+
);
662+
663+
const track = tracker(e, 'cf', 'cfe35', false);
664+
track.setSessionIndex(expected.vid);
665+
track.track(
666+
buildPageView({ pageUrl: 'http://www.example.com', pageTitle: 'example page', referrer: 'http://google.com' }),
667+
context
668+
);
669+
});
670+
});
607671
}

0 commit comments

Comments
 (0)