@schibsted/pulse-sdk 8.0.0-rc.20 → 8.0.0-rc.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Tracker.js +170 -334
- package/dist/cjs/builders/session-browser.js +4 -79
- package/dist/cjs/session/store.js +96 -0
- package/dist/cjs/tpaas-builders/builders/device.js +42 -70
- package/dist/cjs/tpaas-builders/builders/pageInfo.js +2 -3
- package/dist/cjs/tpaas-builders/builders/session.js +2 -114
- package/dist/cjs/tpaas-builders/builders/utm.js +2 -8
- package/dist/cjs/tpaas-builders/builders.js +135 -148
- package/dist/cjs/tpaas-context/adapters.js +85 -0
- package/dist/cjs/tpaas-context/browser.js +51 -0
- package/dist/cjs/tpaas-context/index.js +6 -0
- package/dist/cjs/tpaas-context/prepare.js +177 -0
- package/dist/cjs/tpaas-context/session.js +23 -0
- package/dist/cjs/tpaas-schemas/constants.js +68 -75
- package/dist/cjs/tracker-proxy/consts.js +1 -2
- package/dist/cjs/version.js +1 -1
- package/dist/ejs/Tracker.js +171 -335
- package/dist/ejs/builders/session-browser.js +4 -77
- package/dist/ejs/session/store.js +90 -0
- package/dist/ejs/tpaas-builders/builders/device.js +42 -70
- package/dist/ejs/tpaas-builders/builders/pageInfo.js +2 -3
- package/dist/ejs/tpaas-builders/builders/session.js +1 -111
- package/dist/ejs/tpaas-builders/builders/utm.js +2 -8
- package/dist/ejs/tpaas-builders/builders.js +135 -147
- package/dist/ejs/tpaas-context/adapters.js +80 -0
- package/dist/ejs/tpaas-context/browser.js +47 -0
- package/dist/ejs/tpaas-context/index.js +1 -0
- package/dist/ejs/tpaas-context/prepare.js +137 -0
- package/dist/ejs/tpaas-context/session.js +19 -0
- package/dist/ejs/tpaas-schemas/constants.js +66 -73
- package/dist/ejs/tracker-proxy/consts.js +1 -2
- package/dist/ejs/version.js +1 -1
- package/dist/types/Tracker.d.ts +27 -23
- package/dist/types/builders/session-browser.d.ts +0 -13
- package/dist/types/session/store.d.ts +34 -0
- package/dist/types/tpaas-builders/builders/consents.d.ts +2 -2
- package/dist/types/tpaas-builders/builders/device.d.ts +2 -2
- package/dist/types/tpaas-builders/builders/experiments.d.ts +2 -2
- package/dist/types/tpaas-builders/builders/pageInfo.d.ts +2 -1
- package/dist/types/tpaas-builders/builders/pageviewId.d.ts +2 -2
- package/dist/types/tpaas-builders/builders/provider.d.ts +2 -2
- package/dist/types/tpaas-builders/builders/session.d.ts +1 -12
- package/dist/types/tpaas-builders/builders/trackerBuilder.d.ts +2 -2
- package/dist/types/tpaas-builders/builders/user.d.ts +2 -2
- package/dist/types/tpaas-builders/builders/utm.d.ts +2 -1
- package/dist/types/tpaas-builders/builders.d.ts +70 -70
- package/dist/types/tpaas-builders/index.d.ts +0 -1
- package/dist/types/tpaas-context/adapters.d.ts +26 -0
- package/dist/types/tpaas-context/browser.d.ts +8 -0
- package/dist/types/tpaas-context/index.d.ts +2 -0
- package/dist/types/tpaas-context/prepare.d.ts +38 -0
- package/dist/types/tpaas-context/session.d.ts +10 -0
- package/dist/types/tpaas-context/types.d.ts +66 -0
- package/dist/types/tpaas-schemas/constants.d.ts +66 -73
- package/dist/types/tpaas-schemas/types/ITpaasEvent.d.ts +2 -4
- package/dist/types/tpaas-schemas/types/schemas/{IAnonymousViewUIElement.d.ts → IAnonymousImpressionUIElement.d.ts} +2 -2
- package/dist/types/tpaas-schemas/types/schemas/IEngagementAudio.d.ts +6 -2
- package/dist/types/tpaas-schemas/types/schemas/IEngagementTeaser.d.ts +2 -2
- package/dist/types/tpaas-schemas/types/schemas/IEngagementTvModule.d.ts +4 -1
- package/dist/types/tpaas-schemas/types/schemas/IEngagementTvSearch.d.ts +4 -4
- package/dist/types/tpaas-schemas/types/schemas/IEngagementVideo.d.ts +6 -2
- package/dist/types/tpaas-schemas/types/schemas/IEngagementVideoAd.d.ts +10 -8
- package/dist/types/tpaas-schemas/types/schemas/IImpressionTeaser.d.ts +2 -2
- package/dist/types/tpaas-schemas/types/schemas/ILeaveWeather.d.ts +2 -0
- package/dist/types/tpaas-schemas/types/schemas/index.d.ts +1 -2
- package/dist/types/tpaas-schemas/types.d.ts +2 -2
- package/dist/types/tracker-proxy/consts.d.ts +1 -1
- package/dist/types/types.d.ts +2 -4
- package/dist/types/version.d.ts +1 -1
- package/package.json +1 -1
- package/dist/types/tpaas-builders/types.d.ts +0 -36
- package/dist/types/tpaas-schemas/types/schemas/IRequestAd.d.ts +0 -95
- /package/dist/cjs/{tpaas-builders → tpaas-context}/types.js +0 -0
- /package/dist/ejs/{tpaas-builders → tpaas-context}/types.js +0 -0
|
@@ -1,75 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
const { USE_LOCALSTORAGE } = cookieConfig;
|
|
4
|
-
const SESSION_STORAGE_KEY = '_pulsesession';
|
|
5
|
-
const generateSessionSDRN = (sessionId) => `sdrn:schibsted:session:${sessionId}`;
|
|
6
|
-
// Return parsed json from cookie (or localStorage if enabled), or undefined.
|
|
7
|
-
export const readAndParse = (storageKey) => {
|
|
8
|
-
try {
|
|
9
|
-
const storedSession = readCookie(storageKey);
|
|
10
|
-
if (storedSession) {
|
|
11
|
-
return JSON.parse(storedSession);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
catch (_e) {
|
|
15
|
-
// If localStorage/cookie is not available, we treat it as if
|
|
16
|
-
// the value is undefined.
|
|
17
|
-
// If data is unparseable, we also ignore it.
|
|
18
|
-
return undefined;
|
|
19
|
-
}
|
|
20
|
-
return undefined;
|
|
21
|
-
};
|
|
22
|
-
// Read and parse session data from localStorage.
|
|
23
|
-
// Any invalid or unreadable data is ignored.
|
|
24
|
-
export const readSession = () => {
|
|
25
|
-
const storedSession = readAndParse(SESSION_STORAGE_KEY);
|
|
26
|
-
if (storedSession) {
|
|
27
|
-
const [id, startTimeMs, lastEventTimeMs] = storedSession;
|
|
28
|
-
const startTime = new Date(startTimeMs);
|
|
29
|
-
const lastEventTime = new Date(lastEventTimeMs);
|
|
30
|
-
if (id &&
|
|
31
|
-
startTime &&
|
|
32
|
-
lastEventTime &&
|
|
33
|
-
!Number.isNaN(startTime.getTime()) &&
|
|
34
|
-
!Number.isNaN(lastEventTime.getTime())) {
|
|
35
|
-
return { id, startTime, lastEventTime };
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return undefined;
|
|
39
|
-
};
|
|
40
|
-
// Write session data to localStorage
|
|
41
|
-
const writeSession = (session) => {
|
|
42
|
-
const { id, startTime, lastEventTime } = session;
|
|
43
|
-
try {
|
|
44
|
-
const sessionToStore = [id, startTime.getTime(), lastEventTime.getTime()];
|
|
45
|
-
const halfHourInDays = 1.0 / (24 * 2);
|
|
46
|
-
writeCookie(SESSION_STORAGE_KEY, JSON.stringify(sessionToStore), getRootDomain(), halfHourInDays);
|
|
47
|
-
// Delete old localStorage-based session if needed
|
|
48
|
-
if (!USE_LOCALSTORAGE && isBrowser && window.localStorage) {
|
|
49
|
-
// removeItem will do nothing to a non-existing item.
|
|
50
|
-
window.localStorage.removeItem(SESSION_STORAGE_KEY);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
catch (_e) {
|
|
54
|
-
// If we can't write the session, we just move on in life
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
const createSession = (eventCreationDate) => {
|
|
58
|
-
const now = eventCreationDate ?? new Date();
|
|
59
|
-
return {
|
|
60
|
-
id: generateSessionSDRN(uuid()),
|
|
61
|
-
startTime: now,
|
|
62
|
-
lastEventTime: now,
|
|
63
|
-
};
|
|
64
|
-
};
|
|
65
|
-
const updateSession = (session, eventCreationDate) => {
|
|
66
|
-
const now = eventCreationDate ?? new Date();
|
|
67
|
-
const sessionMaxInterval = 30 * 60 * 1000; // 30 minutes in milliseconds
|
|
68
|
-
if (!session || session.lastEventTime.getTime() < now.getTime() - sessionMaxInterval) {
|
|
69
|
-
return createSession(now);
|
|
70
|
-
}
|
|
71
|
-
return { ...session, lastEventTime: now };
|
|
72
|
-
};
|
|
1
|
+
import { generateSessionSDRN, touchSession } from '../session/store';
|
|
2
|
+
import { getXDomainId } from '../utils';
|
|
73
3
|
export const defaultValue = () => undefined;
|
|
74
4
|
// Add session information to an event
|
|
75
5
|
// {
|
|
@@ -79,12 +9,9 @@ export const defaultValue = () => undefined;
|
|
|
79
9
|
// }
|
|
80
10
|
// }
|
|
81
11
|
export default function session(_input, eventCreationDate) {
|
|
82
|
-
const
|
|
83
|
-
const newSession = updateSession(oldSession, eventCreationDate);
|
|
84
|
-
writeSession(newSession);
|
|
85
|
-
const { id, startTime } = newSession;
|
|
12
|
+
const { sessionId, startTime } = touchSession(eventCreationDate ?? new Date());
|
|
86
13
|
return {
|
|
87
|
-
'@id':
|
|
14
|
+
'@id': generateSessionSDRN(sessionId),
|
|
88
15
|
startTime: startTime.toISOString(),
|
|
89
16
|
xDomainId: getXDomainId(),
|
|
90
17
|
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { v4 as uuid } from 'uuid';
|
|
2
|
+
import { cookieConfig, getRootDomain, readCookie, SESSION_SDRN_PREFIX, webApi, writeCookie } from '../utils';
|
|
3
|
+
const { USE_LOCALSTORAGE } = cookieConfig;
|
|
4
|
+
const SESSION_STORAGE_KEY = '_pulsesession';
|
|
5
|
+
const SESSION_MAX_INTERVAL_MS = 30 * 60 * 1000; // 30 minutes
|
|
6
|
+
/**
|
|
7
|
+
* @private
|
|
8
|
+
*/
|
|
9
|
+
export const generateSessionSDRN = (sessionId) => `${SESSION_SDRN_PREFIX}${sessionId}`;
|
|
10
|
+
// Return parsed json from cookie (or localStorage if enabled), or undefined.
|
|
11
|
+
const readAndParse = () => {
|
|
12
|
+
try {
|
|
13
|
+
const storedSession = readCookie(SESSION_STORAGE_KEY);
|
|
14
|
+
if (storedSession) {
|
|
15
|
+
return JSON.parse(storedSession);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
catch (_e) {
|
|
19
|
+
// If localStorage/cookie is not available, we treat it as if
|
|
20
|
+
// the value is undefined.
|
|
21
|
+
// If data is unparseable, we also ignore it.
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Reads the stored session. Any invalid or unreadable data is ignored.
|
|
28
|
+
* @private
|
|
29
|
+
*/
|
|
30
|
+
export const readSession = () => {
|
|
31
|
+
const storedSession = readAndParse();
|
|
32
|
+
if (storedSession) {
|
|
33
|
+
const [sessionSdrn, startTimeMs, lastEventTimeMs] = storedSession;
|
|
34
|
+
const startTime = new Date(startTimeMs);
|
|
35
|
+
const lastEventTime = new Date(lastEventTimeMs);
|
|
36
|
+
if (sessionSdrn && !Number.isNaN(startTime.getTime()) && !Number.isNaN(lastEventTime.getTime())) {
|
|
37
|
+
return {
|
|
38
|
+
// the session is stored as an SDRN, for compatibility with legacy events and older SDK versions
|
|
39
|
+
sessionId: sessionSdrn.replace(SESSION_SDRN_PREFIX, ''),
|
|
40
|
+
startTime,
|
|
41
|
+
lastEventTime,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
};
|
|
47
|
+
const writeSession = ({ sessionId, startTime, lastEventTime }) => {
|
|
48
|
+
try {
|
|
49
|
+
// SDK v8 (supporting TPaaS events) needs to be compatible with legacy events AND older SDK versions
|
|
50
|
+
// as a result, we can't store just the sessionId, we need the full SDRN in there
|
|
51
|
+
const sessionToStore = [
|
|
52
|
+
generateSessionSDRN(sessionId),
|
|
53
|
+
startTime.getTime(),
|
|
54
|
+
lastEventTime.getTime(),
|
|
55
|
+
];
|
|
56
|
+
const halfHourInDays = 1.0 / (24 * 2);
|
|
57
|
+
writeCookie(SESSION_STORAGE_KEY, JSON.stringify(sessionToStore), getRootDomain(), halfHourInDays);
|
|
58
|
+
// Delete old localStorage-based session if needed
|
|
59
|
+
if (!USE_LOCALSTORAGE) {
|
|
60
|
+
// removeItem will do nothing to a non-existing item.
|
|
61
|
+
webApi.localStorage?.removeItem(SESSION_STORAGE_KEY);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch (_e) {
|
|
65
|
+
// If we can't write the session, we just move on in life
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const updateSession = (createdAt, session) => {
|
|
69
|
+
if (!session || session.lastEventTime.getTime() < createdAt.getTime() - SESSION_MAX_INTERVAL_MS) {
|
|
70
|
+
return { sessionId: uuid(), startTime: createdAt, lastEventTime: createdAt, isFirstEvent: true };
|
|
71
|
+
}
|
|
72
|
+
// Never move lastEventTime backwards, or the session would expire early.
|
|
73
|
+
// In case the order of events is not strictly chronological, this ensures the session remains valid.
|
|
74
|
+
const lastEventTime = createdAt.getTime() > session.lastEventTime.getTime() ? createdAt : session.lastEventTime;
|
|
75
|
+
return { ...session, lastEventTime, isFirstEvent: false };
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Registers an event created at `createdAt` in the stored session: extends the current session,
|
|
79
|
+
* or starts a new one when it expired, and stores the result.
|
|
80
|
+
*
|
|
81
|
+
* Call it synchronously, together with capturing `createdAt`, before any asynchronous work,
|
|
82
|
+
* so sessions are updated in the order events are tracked.
|
|
83
|
+
*
|
|
84
|
+
* @private
|
|
85
|
+
*/
|
|
86
|
+
export const touchSession = (createdAt) => {
|
|
87
|
+
const session = updateSession(createdAt, readSession());
|
|
88
|
+
writeSession(session);
|
|
89
|
+
return session;
|
|
90
|
+
};
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import * as logger from 'loglevel';
|
|
2
2
|
import { UAParser } from 'ua-parser-js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
return cachedUAParser;
|
|
3
|
+
let cachedUserAgentResult;
|
|
4
|
+
function getUserAgentResult(userAgent) {
|
|
5
|
+
if (cachedUserAgentResult?.ua !== userAgent) {
|
|
6
|
+
cachedUserAgentResult = UAParser(userAgent);
|
|
7
|
+
}
|
|
8
|
+
return cachedUserAgentResult;
|
|
11
9
|
}
|
|
12
|
-
export const builder = (environmentId, jwe) => {
|
|
13
|
-
if (
|
|
10
|
+
export const builder = (environmentId, jwe, browser) => {
|
|
11
|
+
if (browser.userAgent === undefined) {
|
|
14
12
|
return {
|
|
15
13
|
device: {
|
|
16
14
|
// TODO: server side rendering (or nodejs). do we need to set all the following fields?
|
|
@@ -25,7 +23,7 @@ export const builder = (environmentId, jwe) => {
|
|
|
25
23
|
},
|
|
26
24
|
};
|
|
27
25
|
}
|
|
28
|
-
const uaParser =
|
|
26
|
+
const uaParser = getUserAgentResult(browser.userAgent);
|
|
29
27
|
return {
|
|
30
28
|
device: {
|
|
31
29
|
clientType: getClientType(uaParser),
|
|
@@ -36,12 +34,42 @@ export const builder = (environmentId, jwe) => {
|
|
|
36
34
|
mobileBrand: uaParser.device.vendor,
|
|
37
35
|
mobileModel: uaParser.device.model,
|
|
38
36
|
operatingSystem: getOperatingSystem(uaParser),
|
|
39
|
-
screenOrientation: getScreenOrientation(),
|
|
40
|
-
userAgent:
|
|
41
|
-
viewportSize: getViewportSize(),
|
|
37
|
+
screenOrientation: getScreenOrientation(browser.screenOrientationType),
|
|
38
|
+
userAgent: browser.userAgent,
|
|
39
|
+
viewportSize: getViewportSize(browser.viewportWidth, browser.viewportHeight),
|
|
42
40
|
},
|
|
43
41
|
};
|
|
44
42
|
};
|
|
43
|
+
function getScreenOrientation(orientationType) {
|
|
44
|
+
if (orientationType === 'landscape-primary' || orientationType === 'landscape-secondary') {
|
|
45
|
+
return 'Landscape';
|
|
46
|
+
}
|
|
47
|
+
return 'Portrait';
|
|
48
|
+
}
|
|
49
|
+
function getSizeFromPixels(pixels) {
|
|
50
|
+
if (pixels < 360) {
|
|
51
|
+
return 'XS';
|
|
52
|
+
}
|
|
53
|
+
if (pixels < 480) {
|
|
54
|
+
return 'S';
|
|
55
|
+
}
|
|
56
|
+
if (pixels < 768) {
|
|
57
|
+
return 'M';
|
|
58
|
+
}
|
|
59
|
+
if (pixels < 1024) {
|
|
60
|
+
return 'L';
|
|
61
|
+
}
|
|
62
|
+
if (pixels >= 1024) {
|
|
63
|
+
return 'XL';
|
|
64
|
+
}
|
|
65
|
+
logger.error('Could not determine viewport size, defaulting to M');
|
|
66
|
+
return 'M';
|
|
67
|
+
}
|
|
68
|
+
function getViewportSize(viewportWidth, viewportHeight) {
|
|
69
|
+
const size = getSizeFromPixels(Math.min(viewportWidth, viewportHeight));
|
|
70
|
+
logger.debug(`Viewport size: ${viewportWidth}x${viewportHeight}, mapped to ${size}`);
|
|
71
|
+
return size;
|
|
72
|
+
}
|
|
45
73
|
function getClientType(uaParser) {
|
|
46
74
|
const { browser } = uaParser;
|
|
47
75
|
const browserName = browser.name?.toLocaleLowerCase() || '';
|
|
@@ -122,59 +150,3 @@ function getOperatingSystem(uaParser) {
|
|
|
122
150
|
// default value
|
|
123
151
|
return 'Other';
|
|
124
152
|
}
|
|
125
|
-
function getScreenOrientation() {
|
|
126
|
-
const orientationType = window.screen.orientation?.type;
|
|
127
|
-
const portraitOrientations = ['portrait-primary', 'portrait-secondary'];
|
|
128
|
-
const landscapeOrientations = ['landscape-primary', 'landscape-secondary'];
|
|
129
|
-
// the ifs might be a bit overkill, but it helps with separating the default value from the logic
|
|
130
|
-
if (portraitOrientations.includes(orientationType)) {
|
|
131
|
-
return 'Portrait';
|
|
132
|
-
}
|
|
133
|
-
if (landscapeOrientations.includes(orientationType)) {
|
|
134
|
-
return 'Landscape';
|
|
135
|
-
}
|
|
136
|
-
// default value
|
|
137
|
-
return 'Portrait';
|
|
138
|
-
}
|
|
139
|
-
function getSizeFromPixels(pixels) {
|
|
140
|
-
if (pixels < 360) {
|
|
141
|
-
return 'XS';
|
|
142
|
-
}
|
|
143
|
-
if (pixels < 480) {
|
|
144
|
-
return 'S';
|
|
145
|
-
}
|
|
146
|
-
if (pixels < 768) {
|
|
147
|
-
return 'M';
|
|
148
|
-
}
|
|
149
|
-
if (pixels < 1024) {
|
|
150
|
-
return 'L';
|
|
151
|
-
}
|
|
152
|
-
if (pixels >= 1024) {
|
|
153
|
-
return 'XL';
|
|
154
|
-
}
|
|
155
|
-
// default value
|
|
156
|
-
logger.error('Could not determine viewport size, defaulting to M');
|
|
157
|
-
return 'M';
|
|
158
|
-
}
|
|
159
|
-
function getViewportSize() {
|
|
160
|
-
let viewportWidth;
|
|
161
|
-
let viewportHeight;
|
|
162
|
-
const { document } = window;
|
|
163
|
-
if (typeof window.innerWidth !== 'undefined') {
|
|
164
|
-
viewportWidth = window.innerWidth;
|
|
165
|
-
viewportHeight = window.innerHeight;
|
|
166
|
-
}
|
|
167
|
-
else if (typeof document.documentElement.clientWidth !== 'undefined' &&
|
|
168
|
-
document.documentElement.clientWidth !== 0) {
|
|
169
|
-
viewportWidth = document.documentElement.clientWidth;
|
|
170
|
-
viewportHeight = document.documentElement.clientHeight;
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
viewportWidth = document.getElementsByTagName('body')[0].clientWidth;
|
|
174
|
-
viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
|
|
175
|
-
}
|
|
176
|
-
const shortestDimension = Math.min(viewportWidth, viewportHeight);
|
|
177
|
-
const size = getSizeFromPixels(shortestDimension);
|
|
178
|
-
logger.debug(`Viewport size: ${viewportWidth}x${viewportHeight}, mapped to ${size}`);
|
|
179
|
-
return size;
|
|
180
|
-
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export const builder = (currentPage) => {
|
|
1
|
+
export const builder = (currentPage, url) => {
|
|
3
2
|
// if something happens and there was no View Page type of event sent before, we need a fallback
|
|
4
3
|
if (!currentPage) {
|
|
5
4
|
return {
|
|
@@ -7,7 +6,7 @@ export const builder = (currentPage) => {
|
|
|
7
6
|
info: {
|
|
8
7
|
pageId: 'unknown',
|
|
9
8
|
pageType: 'Page',
|
|
10
|
-
url
|
|
9
|
+
url,
|
|
11
10
|
},
|
|
12
11
|
},
|
|
13
12
|
};
|
|
@@ -1,111 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { cookieConfig, getRootDomain, getXDomainId, readCookie, SESSION_SDRN_PREFIX, webApi, writeCookie, } from '../../utils';
|
|
3
|
-
const { USE_LOCALSTORAGE } = cookieConfig;
|
|
4
|
-
const SESSION_STORAGE_KEY = '_pulsesession';
|
|
5
|
-
const generateSessionSDRN = (sessionId) => `${SESSION_SDRN_PREFIX}${sessionId}`;
|
|
6
|
-
// 1. there are pros and cons to outputting { session: ISession } instead of just ISession
|
|
7
|
-
// this allows us to always have the session builde populate the same property (in this case '.session')
|
|
8
|
-
// if in the future we want the session builder to populate other properties, we should just output ISession and move the mapping logic in index.ts, as part of buildersByEventType
|
|
9
|
-
// 2. we don't need builder-level "isBrowser" checks. If an event is not supposed to be fired from the browser, then the builder shouldn't be assigned to it in buildersByEventType in index.ts
|
|
10
|
-
// however, we should keep minimal "isBrowser" checks before webApi calls because the code might run in SSR environments
|
|
11
|
-
export const builder = (createdAt) => {
|
|
12
|
-
const oldSession = readSession();
|
|
13
|
-
const newSession = updateSession(createdAt, oldSession);
|
|
14
|
-
writeSession(newSession);
|
|
15
|
-
const { sessionId, startTime, isFirstEvent } = newSession;
|
|
16
|
-
return {
|
|
17
|
-
session: {
|
|
18
|
-
isFirstEvent,
|
|
19
|
-
sessionId,
|
|
20
|
-
startTime,
|
|
21
|
-
xDomainId: getXDomainId(),
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
// Return parsed json from cookie (or localStorage if enabled), or undefined.
|
|
26
|
-
// exported for testing purposes.
|
|
27
|
-
export const readAndParse = (storageKey) => {
|
|
28
|
-
try {
|
|
29
|
-
const storedSession = readCookie(storageKey);
|
|
30
|
-
if (storedSession) {
|
|
31
|
-
return JSON.parse(storedSession);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
catch (_e) {
|
|
35
|
-
// If localStorage/cookie is not available, we treat it as if
|
|
36
|
-
// the value is undefined.
|
|
37
|
-
// If data is unparseable, we also ignore it.
|
|
38
|
-
return undefined;
|
|
39
|
-
}
|
|
40
|
-
return undefined;
|
|
41
|
-
};
|
|
42
|
-
// Read and parse session data from localStorage.
|
|
43
|
-
// Any invalid or unreadable data is ignored.
|
|
44
|
-
export const readSession = () => {
|
|
45
|
-
const storedSession = readAndParse(SESSION_STORAGE_KEY);
|
|
46
|
-
if (storedSession) {
|
|
47
|
-
const [sessionId, startTimeMs, lastEventTimeMs] = storedSession;
|
|
48
|
-
const startTime = new Date(startTimeMs);
|
|
49
|
-
const lastEventTime = new Date(lastEventTimeMs);
|
|
50
|
-
if (sessionId &&
|
|
51
|
-
startTime &&
|
|
52
|
-
lastEventTime &&
|
|
53
|
-
!Number.isNaN(startTime.getTime()) &&
|
|
54
|
-
!Number.isNaN(lastEventTime.getTime())) {
|
|
55
|
-
return {
|
|
56
|
-
// to ensure backwards compatibility between the new TPaaS sessionId format and old sessions
|
|
57
|
-
// we need to remove the SDRN prefix from old sessionIds before using it
|
|
58
|
-
sessionId: sessionId.replace(SESSION_SDRN_PREFIX, ''),
|
|
59
|
-
startTime: startTime.toISOString(),
|
|
60
|
-
lastEventTime,
|
|
61
|
-
isFirstEvent: false,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return undefined;
|
|
66
|
-
};
|
|
67
|
-
// Write session data to localStorage
|
|
68
|
-
const writeSession = (session) => {
|
|
69
|
-
const { sessionId, startTime, lastEventTime } = session;
|
|
70
|
-
// SDK v8 (supporting TPaaS events) needs to be compatible with legacy events AND older SDK versions
|
|
71
|
-
// as a result, we can't store just the sessionId, we need the full SDRN in there
|
|
72
|
-
const sessionSdrn = generateSessionSDRN(sessionId);
|
|
73
|
-
try {
|
|
74
|
-
const sessionToStore = [
|
|
75
|
-
sessionSdrn,
|
|
76
|
-
new Date(startTime).getTime(),
|
|
77
|
-
lastEventTime.getTime(),
|
|
78
|
-
];
|
|
79
|
-
const halfHourInDays = 1.0 / (24 * 2);
|
|
80
|
-
writeCookie(SESSION_STORAGE_KEY, JSON.stringify(sessionToStore), getRootDomain(), halfHourInDays);
|
|
81
|
-
// Delete old localStorage-based session if needed
|
|
82
|
-
if (!USE_LOCALSTORAGE) {
|
|
83
|
-
// removeItem will do nothing to a non-existing item.
|
|
84
|
-
// use webApi.localStorage instead of window.localStorage
|
|
85
|
-
webApi.localStorage?.removeItem(SESSION_STORAGE_KEY);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
catch (_e) {
|
|
89
|
-
// If we can't write the session, we just move on in life
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
const createSession = (eventCreationDate) => {
|
|
93
|
-
const now = eventCreationDate ?? new Date();
|
|
94
|
-
return {
|
|
95
|
-
sessionId: uuid(),
|
|
96
|
-
startTime: now.toISOString(),
|
|
97
|
-
lastEventTime: now,
|
|
98
|
-
isFirstEvent: true,
|
|
99
|
-
};
|
|
100
|
-
};
|
|
101
|
-
const updateSession = (eventCreationDate, session) => {
|
|
102
|
-
const sessionMaxInterval = 30 * 60 * 1000; // 30 minutes in milliseconds
|
|
103
|
-
if (!session || session.lastEventTime.getTime() < eventCreationDate.getTime() - sessionMaxInterval) {
|
|
104
|
-
return createSession(eventCreationDate);
|
|
105
|
-
}
|
|
106
|
-
return {
|
|
107
|
-
...session,
|
|
108
|
-
isFirstEvent: false,
|
|
109
|
-
lastEventTime: eventCreationDate,
|
|
110
|
-
};
|
|
111
|
-
};
|
|
1
|
+
export const builder = (session) => ({ session });
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { UTM_MEDIUM_VALUES } from '../../tpaas-schemas';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
if (!isBrowser) {
|
|
5
|
-
return {
|
|
6
|
-
page: {},
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
const urlParams = new URLSearchParams(window?.location?.search ?? '');
|
|
2
|
+
export const builder = (search) => {
|
|
3
|
+
const urlParams = new URLSearchParams(search);
|
|
10
4
|
const utmCampaign = urlParams.get('utm_campaign');
|
|
11
5
|
const utmContent = urlParams.get('utm_content');
|
|
12
6
|
const utmMediumRaw = urlParams.get('utm_medium');
|