@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.
Files changed (74) hide show
  1. package/dist/cjs/Tracker.js +170 -334
  2. package/dist/cjs/builders/session-browser.js +4 -79
  3. package/dist/cjs/session/store.js +96 -0
  4. package/dist/cjs/tpaas-builders/builders/device.js +42 -70
  5. package/dist/cjs/tpaas-builders/builders/pageInfo.js +2 -3
  6. package/dist/cjs/tpaas-builders/builders/session.js +2 -114
  7. package/dist/cjs/tpaas-builders/builders/utm.js +2 -8
  8. package/dist/cjs/tpaas-builders/builders.js +135 -148
  9. package/dist/cjs/tpaas-context/adapters.js +85 -0
  10. package/dist/cjs/tpaas-context/browser.js +51 -0
  11. package/dist/cjs/tpaas-context/index.js +6 -0
  12. package/dist/cjs/tpaas-context/prepare.js +177 -0
  13. package/dist/cjs/tpaas-context/session.js +23 -0
  14. package/dist/cjs/tpaas-schemas/constants.js +68 -75
  15. package/dist/cjs/tracker-proxy/consts.js +1 -2
  16. package/dist/cjs/version.js +1 -1
  17. package/dist/ejs/Tracker.js +171 -335
  18. package/dist/ejs/builders/session-browser.js +4 -77
  19. package/dist/ejs/session/store.js +90 -0
  20. package/dist/ejs/tpaas-builders/builders/device.js +42 -70
  21. package/dist/ejs/tpaas-builders/builders/pageInfo.js +2 -3
  22. package/dist/ejs/tpaas-builders/builders/session.js +1 -111
  23. package/dist/ejs/tpaas-builders/builders/utm.js +2 -8
  24. package/dist/ejs/tpaas-builders/builders.js +135 -147
  25. package/dist/ejs/tpaas-context/adapters.js +80 -0
  26. package/dist/ejs/tpaas-context/browser.js +47 -0
  27. package/dist/ejs/tpaas-context/index.js +1 -0
  28. package/dist/ejs/tpaas-context/prepare.js +137 -0
  29. package/dist/ejs/tpaas-context/session.js +19 -0
  30. package/dist/ejs/tpaas-schemas/constants.js +66 -73
  31. package/dist/ejs/tracker-proxy/consts.js +1 -2
  32. package/dist/ejs/version.js +1 -1
  33. package/dist/types/Tracker.d.ts +27 -23
  34. package/dist/types/builders/session-browser.d.ts +0 -13
  35. package/dist/types/session/store.d.ts +34 -0
  36. package/dist/types/tpaas-builders/builders/consents.d.ts +2 -2
  37. package/dist/types/tpaas-builders/builders/device.d.ts +2 -2
  38. package/dist/types/tpaas-builders/builders/experiments.d.ts +2 -2
  39. package/dist/types/tpaas-builders/builders/pageInfo.d.ts +2 -1
  40. package/dist/types/tpaas-builders/builders/pageviewId.d.ts +2 -2
  41. package/dist/types/tpaas-builders/builders/provider.d.ts +2 -2
  42. package/dist/types/tpaas-builders/builders/session.d.ts +1 -12
  43. package/dist/types/tpaas-builders/builders/trackerBuilder.d.ts +2 -2
  44. package/dist/types/tpaas-builders/builders/user.d.ts +2 -2
  45. package/dist/types/tpaas-builders/builders/utm.d.ts +2 -1
  46. package/dist/types/tpaas-builders/builders.d.ts +70 -70
  47. package/dist/types/tpaas-builders/index.d.ts +0 -1
  48. package/dist/types/tpaas-context/adapters.d.ts +26 -0
  49. package/dist/types/tpaas-context/browser.d.ts +8 -0
  50. package/dist/types/tpaas-context/index.d.ts +2 -0
  51. package/dist/types/tpaas-context/prepare.d.ts +38 -0
  52. package/dist/types/tpaas-context/session.d.ts +10 -0
  53. package/dist/types/tpaas-context/types.d.ts +66 -0
  54. package/dist/types/tpaas-schemas/constants.d.ts +66 -73
  55. package/dist/types/tpaas-schemas/types/ITpaasEvent.d.ts +2 -4
  56. package/dist/types/tpaas-schemas/types/schemas/{IAnonymousViewUIElement.d.ts → IAnonymousImpressionUIElement.d.ts} +2 -2
  57. package/dist/types/tpaas-schemas/types/schemas/IEngagementAudio.d.ts +6 -2
  58. package/dist/types/tpaas-schemas/types/schemas/IEngagementTeaser.d.ts +2 -2
  59. package/dist/types/tpaas-schemas/types/schemas/IEngagementTvModule.d.ts +4 -1
  60. package/dist/types/tpaas-schemas/types/schemas/IEngagementTvSearch.d.ts +4 -4
  61. package/dist/types/tpaas-schemas/types/schemas/IEngagementVideo.d.ts +6 -2
  62. package/dist/types/tpaas-schemas/types/schemas/IEngagementVideoAd.d.ts +10 -8
  63. package/dist/types/tpaas-schemas/types/schemas/IImpressionTeaser.d.ts +2 -2
  64. package/dist/types/tpaas-schemas/types/schemas/ILeaveWeather.d.ts +2 -0
  65. package/dist/types/tpaas-schemas/types/schemas/index.d.ts +1 -2
  66. package/dist/types/tpaas-schemas/types.d.ts +2 -2
  67. package/dist/types/tracker-proxy/consts.d.ts +1 -1
  68. package/dist/types/types.d.ts +2 -4
  69. package/dist/types/version.d.ts +1 -1
  70. package/package.json +1 -1
  71. package/dist/types/tpaas-builders/types.d.ts +0 -36
  72. package/dist/types/tpaas-schemas/types/schemas/IRequestAd.d.ts +0 -95
  73. /package/dist/cjs/{tpaas-builders → tpaas-context}/types.js +0 -0
  74. /package/dist/ejs/{tpaas-builders → tpaas-context}/types.js +0 -0
@@ -1,81 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.defaultValue = exports.readSession = exports.readAndParse = void 0;
3
+ exports.defaultValue = void 0;
4
4
  exports.default = session;
5
- const uuid_1 = require("uuid");
5
+ const store_1 = require("../session/store");
6
6
  const utils_1 = require("../utils");
7
- const { USE_LOCALSTORAGE } = utils_1.cookieConfig;
8
- const SESSION_STORAGE_KEY = '_pulsesession';
9
- const generateSessionSDRN = (sessionId) => `sdrn:schibsted:session:${sessionId}`;
10
- // Return parsed json from cookie (or localStorage if enabled), or undefined.
11
- const readAndParse = (storageKey) => {
12
- try {
13
- const storedSession = (0, utils_1.readCookie)(storageKey);
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
- exports.readAndParse = readAndParse;
27
- // Read and parse session data from localStorage.
28
- // Any invalid or unreadable data is ignored.
29
- const readSession = () => {
30
- const storedSession = (0, exports.readAndParse)(SESSION_STORAGE_KEY);
31
- if (storedSession) {
32
- const [id, startTimeMs, lastEventTimeMs] = storedSession;
33
- const startTime = new Date(startTimeMs);
34
- const lastEventTime = new Date(lastEventTimeMs);
35
- if (id &&
36
- startTime &&
37
- lastEventTime &&
38
- !Number.isNaN(startTime.getTime()) &&
39
- !Number.isNaN(lastEventTime.getTime())) {
40
- return { id, startTime, lastEventTime };
41
- }
42
- }
43
- return undefined;
44
- };
45
- exports.readSession = readSession;
46
- // Write session data to localStorage
47
- const writeSession = (session) => {
48
- const { id, startTime, lastEventTime } = session;
49
- try {
50
- const sessionToStore = [id, startTime.getTime(), lastEventTime.getTime()];
51
- const halfHourInDays = 1.0 / (24 * 2);
52
- (0, utils_1.writeCookie)(SESSION_STORAGE_KEY, JSON.stringify(sessionToStore), (0, utils_1.getRootDomain)(), halfHourInDays);
53
- // Delete old localStorage-based session if needed
54
- if (!USE_LOCALSTORAGE && utils_1.isBrowser && window.localStorage) {
55
- // removeItem will do nothing to a non-existing item.
56
- window.localStorage.removeItem(SESSION_STORAGE_KEY);
57
- }
58
- }
59
- catch (_e) {
60
- // If we can't write the session, we just move on in life
61
- }
62
- };
63
- const createSession = (eventCreationDate) => {
64
- const now = eventCreationDate ?? new Date();
65
- return {
66
- id: generateSessionSDRN((0, uuid_1.v4)()),
67
- startTime: now,
68
- lastEventTime: now,
69
- };
70
- };
71
- const updateSession = (session, eventCreationDate) => {
72
- const now = eventCreationDate ?? new Date();
73
- const sessionMaxInterval = 30 * 60 * 1000; // 30 minutes in milliseconds
74
- if (!session || session.lastEventTime.getTime() < now.getTime() - sessionMaxInterval) {
75
- return createSession(now);
76
- }
77
- return { ...session, lastEventTime: now };
78
- };
79
7
  const defaultValue = () => undefined;
80
8
  exports.defaultValue = defaultValue;
81
9
  // Add session information to an event
@@ -86,12 +14,9 @@ exports.defaultValue = defaultValue;
86
14
  // }
87
15
  // }
88
16
  function session(_input, eventCreationDate) {
89
- const oldSession = (0, exports.readSession)();
90
- const newSession = updateSession(oldSession, eventCreationDate);
91
- writeSession(newSession);
92
- const { id, startTime } = newSession;
17
+ const { sessionId, startTime } = (0, store_1.touchSession)(eventCreationDate ?? new Date());
93
18
  return {
94
- '@id': id,
19
+ '@id': (0, store_1.generateSessionSDRN)(sessionId),
95
20
  startTime: startTime.toISOString(),
96
21
  xDomainId: (0, utils_1.getXDomainId)(),
97
22
  };
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.touchSession = exports.readSession = exports.generateSessionSDRN = void 0;
4
+ const uuid_1 = require("uuid");
5
+ const utils_1 = require("../utils");
6
+ const { USE_LOCALSTORAGE } = utils_1.cookieConfig;
7
+ const SESSION_STORAGE_KEY = '_pulsesession';
8
+ const SESSION_MAX_INTERVAL_MS = 30 * 60 * 1000; // 30 minutes
9
+ /**
10
+ * @private
11
+ */
12
+ const generateSessionSDRN = (sessionId) => `${utils_1.SESSION_SDRN_PREFIX}${sessionId}`;
13
+ exports.generateSessionSDRN = generateSessionSDRN;
14
+ // Return parsed json from cookie (or localStorage if enabled), or undefined.
15
+ const readAndParse = () => {
16
+ try {
17
+ const storedSession = (0, utils_1.readCookie)(SESSION_STORAGE_KEY);
18
+ if (storedSession) {
19
+ return JSON.parse(storedSession);
20
+ }
21
+ }
22
+ catch (_e) {
23
+ // If localStorage/cookie is not available, we treat it as if
24
+ // the value is undefined.
25
+ // If data is unparseable, we also ignore it.
26
+ return undefined;
27
+ }
28
+ return undefined;
29
+ };
30
+ /**
31
+ * Reads the stored session. Any invalid or unreadable data is ignored.
32
+ * @private
33
+ */
34
+ const readSession = () => {
35
+ const storedSession = readAndParse();
36
+ if (storedSession) {
37
+ const [sessionSdrn, startTimeMs, lastEventTimeMs] = storedSession;
38
+ const startTime = new Date(startTimeMs);
39
+ const lastEventTime = new Date(lastEventTimeMs);
40
+ if (sessionSdrn && !Number.isNaN(startTime.getTime()) && !Number.isNaN(lastEventTime.getTime())) {
41
+ return {
42
+ // the session is stored as an SDRN, for compatibility with legacy events and older SDK versions
43
+ sessionId: sessionSdrn.replace(utils_1.SESSION_SDRN_PREFIX, ''),
44
+ startTime,
45
+ lastEventTime,
46
+ };
47
+ }
48
+ }
49
+ return undefined;
50
+ };
51
+ exports.readSession = readSession;
52
+ const writeSession = ({ sessionId, startTime, lastEventTime }) => {
53
+ try {
54
+ // SDK v8 (supporting TPaaS events) needs to be compatible with legacy events AND older SDK versions
55
+ // as a result, we can't store just the sessionId, we need the full SDRN in there
56
+ const sessionToStore = [
57
+ (0, exports.generateSessionSDRN)(sessionId),
58
+ startTime.getTime(),
59
+ lastEventTime.getTime(),
60
+ ];
61
+ const halfHourInDays = 1.0 / (24 * 2);
62
+ (0, utils_1.writeCookie)(SESSION_STORAGE_KEY, JSON.stringify(sessionToStore), (0, utils_1.getRootDomain)(), halfHourInDays);
63
+ // Delete old localStorage-based session if needed
64
+ if (!USE_LOCALSTORAGE) {
65
+ // removeItem will do nothing to a non-existing item.
66
+ utils_1.webApi.localStorage?.removeItem(SESSION_STORAGE_KEY);
67
+ }
68
+ }
69
+ catch (_e) {
70
+ // If we can't write the session, we just move on in life
71
+ }
72
+ };
73
+ const updateSession = (createdAt, session) => {
74
+ if (!session || session.lastEventTime.getTime() < createdAt.getTime() - SESSION_MAX_INTERVAL_MS) {
75
+ return { sessionId: (0, uuid_1.v4)(), startTime: createdAt, lastEventTime: createdAt, isFirstEvent: true };
76
+ }
77
+ // Never move lastEventTime backwards, or the session would expire early.
78
+ // In case the order of events is not strictly chronological, this ensures the session remains valid.
79
+ const lastEventTime = createdAt.getTime() > session.lastEventTime.getTime() ? createdAt : session.lastEventTime;
80
+ return { ...session, lastEventTime, isFirstEvent: false };
81
+ };
82
+ /**
83
+ * Registers an event created at `createdAt` in the stored session: extends the current session,
84
+ * or starts a new one when it expired, and stores the result.
85
+ *
86
+ * Call it synchronously, together with capturing `createdAt`, before any asynchronous work,
87
+ * so sessions are updated in the order events are tracked.
88
+ *
89
+ * @private
90
+ */
91
+ const touchSession = (createdAt) => {
92
+ const session = updateSession(createdAt, (0, exports.readSession)());
93
+ writeSession(session);
94
+ return session;
95
+ };
96
+ exports.touchSession = touchSession;
@@ -36,17 +36,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.builder = void 0;
37
37
  const logger = __importStar(require("loglevel"));
38
38
  const ua_parser_js_1 = require("ua-parser-js");
39
- const utils_1 = require("../../utils");
40
- // Cache the UAParser result to avoid re-parsing on subsequent calls
41
- let cachedUAParser;
42
- function getUAParser() {
43
- if (!cachedUAParser) {
44
- cachedUAParser = (0, ua_parser_js_1.UAParser)();
45
- }
46
- return cachedUAParser;
39
+ let cachedUserAgentResult;
40
+ function getUserAgentResult(userAgent) {
41
+ if (cachedUserAgentResult?.ua !== userAgent) {
42
+ cachedUserAgentResult = (0, ua_parser_js_1.UAParser)(userAgent);
43
+ }
44
+ return cachedUserAgentResult;
47
45
  }
48
- const builder = (environmentId, jwe) => {
49
- if (!utils_1.isBrowser) {
46
+ const builder = (environmentId, jwe, browser) => {
47
+ if (browser.userAgent === undefined) {
50
48
  return {
51
49
  device: {
52
50
  // TODO: server side rendering (or nodejs). do we need to set all the following fields?
@@ -61,7 +59,7 @@ const builder = (environmentId, jwe) => {
61
59
  },
62
60
  };
63
61
  }
64
- const uaParser = getUAParser();
62
+ const uaParser = getUserAgentResult(browser.userAgent);
65
63
  return {
66
64
  device: {
67
65
  clientType: getClientType(uaParser),
@@ -72,13 +70,43 @@ const builder = (environmentId, jwe) => {
72
70
  mobileBrand: uaParser.device.vendor,
73
71
  mobileModel: uaParser.device.model,
74
72
  operatingSystem: getOperatingSystem(uaParser),
75
- screenOrientation: getScreenOrientation(),
76
- userAgent: window.navigator.userAgent,
77
- viewportSize: getViewportSize(),
73
+ screenOrientation: getScreenOrientation(browser.screenOrientationType),
74
+ userAgent: browser.userAgent,
75
+ viewportSize: getViewportSize(browser.viewportWidth, browser.viewportHeight),
78
76
  },
79
77
  };
80
78
  };
81
79
  exports.builder = builder;
80
+ function getScreenOrientation(orientationType) {
81
+ if (orientationType === 'landscape-primary' || orientationType === 'landscape-secondary') {
82
+ return 'Landscape';
83
+ }
84
+ return 'Portrait';
85
+ }
86
+ function getSizeFromPixels(pixels) {
87
+ if (pixels < 360) {
88
+ return 'XS';
89
+ }
90
+ if (pixels < 480) {
91
+ return 'S';
92
+ }
93
+ if (pixels < 768) {
94
+ return 'M';
95
+ }
96
+ if (pixels < 1024) {
97
+ return 'L';
98
+ }
99
+ if (pixels >= 1024) {
100
+ return 'XL';
101
+ }
102
+ logger.error('Could not determine viewport size, defaulting to M');
103
+ return 'M';
104
+ }
105
+ function getViewportSize(viewportWidth, viewportHeight) {
106
+ const size = getSizeFromPixels(Math.min(viewportWidth, viewportHeight));
107
+ logger.debug(`Viewport size: ${viewportWidth}x${viewportHeight}, mapped to ${size}`);
108
+ return size;
109
+ }
82
110
  function getClientType(uaParser) {
83
111
  const { browser } = uaParser;
84
112
  const browserName = browser.name?.toLocaleLowerCase() || '';
@@ -159,59 +187,3 @@ function getOperatingSystem(uaParser) {
159
187
  // default value
160
188
  return 'Other';
161
189
  }
162
- function getScreenOrientation() {
163
- const orientationType = window.screen.orientation?.type;
164
- const portraitOrientations = ['portrait-primary', 'portrait-secondary'];
165
- const landscapeOrientations = ['landscape-primary', 'landscape-secondary'];
166
- // the ifs might be a bit overkill, but it helps with separating the default value from the logic
167
- if (portraitOrientations.includes(orientationType)) {
168
- return 'Portrait';
169
- }
170
- if (landscapeOrientations.includes(orientationType)) {
171
- return 'Landscape';
172
- }
173
- // default value
174
- return 'Portrait';
175
- }
176
- function getSizeFromPixels(pixels) {
177
- if (pixels < 360) {
178
- return 'XS';
179
- }
180
- if (pixels < 480) {
181
- return 'S';
182
- }
183
- if (pixels < 768) {
184
- return 'M';
185
- }
186
- if (pixels < 1024) {
187
- return 'L';
188
- }
189
- if (pixels >= 1024) {
190
- return 'XL';
191
- }
192
- // default value
193
- logger.error('Could not determine viewport size, defaulting to M');
194
- return 'M';
195
- }
196
- function getViewportSize() {
197
- let viewportWidth;
198
- let viewportHeight;
199
- const { document } = window;
200
- if (typeof window.innerWidth !== 'undefined') {
201
- viewportWidth = window.innerWidth;
202
- viewportHeight = window.innerHeight;
203
- }
204
- else if (typeof document.documentElement.clientWidth !== 'undefined' &&
205
- document.documentElement.clientWidth !== 0) {
206
- viewportWidth = document.documentElement.clientWidth;
207
- viewportHeight = document.documentElement.clientHeight;
208
- }
209
- else {
210
- viewportWidth = document.getElementsByTagName('body')[0].clientWidth;
211
- viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
212
- }
213
- const shortestDimension = Math.min(viewportWidth, viewportHeight);
214
- const size = getSizeFromPixels(shortestDimension);
215
- logger.debug(`Viewport size: ${viewportWidth}x${viewportHeight}, mapped to ${size}`);
216
- return size;
217
- }
@@ -1,8 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.builder = void 0;
4
- const utils_1 = require("../../utils");
5
- const builder = (currentPage) => {
4
+ const builder = (currentPage, url) => {
6
5
  // if something happens and there was no View Page type of event sent before, we need a fallback
7
6
  if (!currentPage) {
8
7
  return {
@@ -10,7 +9,7 @@ const builder = (currentPage) => {
10
9
  info: {
11
10
  pageId: 'unknown',
12
11
  pageType: 'Page',
13
- url: utils_1.isBrowser ? window.location.href : undefined,
12
+ url,
14
13
  },
15
14
  },
16
15
  };
@@ -1,117 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readSession = exports.readAndParse = exports.builder = void 0;
4
- const uuid_1 = require("uuid");
5
- const utils_1 = require("../../utils");
6
- const { USE_LOCALSTORAGE } = utils_1.cookieConfig;
7
- const SESSION_STORAGE_KEY = '_pulsesession';
8
- const generateSessionSDRN = (sessionId) => `${utils_1.SESSION_SDRN_PREFIX}${sessionId}`;
9
- // 1. there are pros and cons to outputting { session: ISession } instead of just ISession
10
- // this allows us to always have the session builde populate the same property (in this case '.session')
11
- // 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
12
- // 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
13
- // however, we should keep minimal "isBrowser" checks before webApi calls because the code might run in SSR environments
14
- const builder = (createdAt) => {
15
- const oldSession = (0, exports.readSession)();
16
- const newSession = updateSession(createdAt, oldSession);
17
- writeSession(newSession);
18
- const { sessionId, startTime, isFirstEvent } = newSession;
19
- return {
20
- session: {
21
- isFirstEvent,
22
- sessionId,
23
- startTime,
24
- xDomainId: (0, utils_1.getXDomainId)(),
25
- },
26
- };
27
- };
3
+ exports.builder = void 0;
4
+ const builder = (session) => ({ session });
28
5
  exports.builder = builder;
29
- // Return parsed json from cookie (or localStorage if enabled), or undefined.
30
- // exported for testing purposes.
31
- const readAndParse = (storageKey) => {
32
- try {
33
- const storedSession = (0, utils_1.readCookie)(storageKey);
34
- if (storedSession) {
35
- return JSON.parse(storedSession);
36
- }
37
- }
38
- catch (_e) {
39
- // If localStorage/cookie is not available, we treat it as if
40
- // the value is undefined.
41
- // If data is unparseable, we also ignore it.
42
- return undefined;
43
- }
44
- return undefined;
45
- };
46
- exports.readAndParse = readAndParse;
47
- // Read and parse session data from localStorage.
48
- // Any invalid or unreadable data is ignored.
49
- const readSession = () => {
50
- const storedSession = (0, exports.readAndParse)(SESSION_STORAGE_KEY);
51
- if (storedSession) {
52
- const [sessionId, startTimeMs, lastEventTimeMs] = storedSession;
53
- const startTime = new Date(startTimeMs);
54
- const lastEventTime = new Date(lastEventTimeMs);
55
- if (sessionId &&
56
- startTime &&
57
- lastEventTime &&
58
- !Number.isNaN(startTime.getTime()) &&
59
- !Number.isNaN(lastEventTime.getTime())) {
60
- return {
61
- // to ensure backwards compatibility between the new TPaaS sessionId format and old sessions
62
- // we need to remove the SDRN prefix from old sessionIds before using it
63
- sessionId: sessionId.replace(utils_1.SESSION_SDRN_PREFIX, ''),
64
- startTime: startTime.toISOString(),
65
- lastEventTime,
66
- isFirstEvent: false,
67
- };
68
- }
69
- }
70
- return undefined;
71
- };
72
- exports.readSession = readSession;
73
- // Write session data to localStorage
74
- const writeSession = (session) => {
75
- const { sessionId, startTime, lastEventTime } = session;
76
- // SDK v8 (supporting TPaaS events) needs to be compatible with legacy events AND older SDK versions
77
- // as a result, we can't store just the sessionId, we need the full SDRN in there
78
- const sessionSdrn = generateSessionSDRN(sessionId);
79
- try {
80
- const sessionToStore = [
81
- sessionSdrn,
82
- new Date(startTime).getTime(),
83
- lastEventTime.getTime(),
84
- ];
85
- const halfHourInDays = 1.0 / (24 * 2);
86
- (0, utils_1.writeCookie)(SESSION_STORAGE_KEY, JSON.stringify(sessionToStore), (0, utils_1.getRootDomain)(), halfHourInDays);
87
- // Delete old localStorage-based session if needed
88
- if (!USE_LOCALSTORAGE) {
89
- // removeItem will do nothing to a non-existing item.
90
- // use webApi.localStorage instead of window.localStorage
91
- utils_1.webApi.localStorage?.removeItem(SESSION_STORAGE_KEY);
92
- }
93
- }
94
- catch (_e) {
95
- // If we can't write the session, we just move on in life
96
- }
97
- };
98
- const createSession = (eventCreationDate) => {
99
- const now = eventCreationDate ?? new Date();
100
- return {
101
- sessionId: (0, uuid_1.v4)(),
102
- startTime: now.toISOString(),
103
- lastEventTime: now,
104
- isFirstEvent: true,
105
- };
106
- };
107
- const updateSession = (eventCreationDate, session) => {
108
- const sessionMaxInterval = 30 * 60 * 1000; // 30 minutes in milliseconds
109
- if (!session || session.lastEventTime.getTime() < eventCreationDate.getTime() - sessionMaxInterval) {
110
- return createSession(eventCreationDate);
111
- }
112
- return {
113
- ...session,
114
- isFirstEvent: false,
115
- lastEventTime: eventCreationDate,
116
- };
117
- };
@@ -2,14 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.builder = void 0;
4
4
  const tpaas_schemas_1 = require("../../tpaas-schemas");
5
- const utils_1 = require("../../utils");
6
- const builder = () => {
7
- if (!utils_1.isBrowser) {
8
- return {
9
- page: {},
10
- };
11
- }
12
- const urlParams = new URLSearchParams(window?.location?.search ?? '');
5
+ const builder = (search) => {
6
+ const urlParams = new URLSearchParams(search);
13
7
  const utmCampaign = urlParams.get('utm_campaign');
14
8
  const utmContent = urlParams.get('utm_content');
15
9
  const utmMediumRaw = urlParams.get('utm_medium');