@amplifyup/sdk 0.1.40 → 0.1.41

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/react.mjs CHANGED
@@ -102,6 +102,7 @@ var AWSAdapter = class {
102
102
  const eventData = {
103
103
  type: "page",
104
104
  anonymousId: properties.instanceId,
105
+ sessionId: properties.sessionId,
105
106
  userId: properties.userId,
106
107
  properties: {
107
108
  path: properties.path || properties.url,
@@ -144,6 +145,7 @@ var AWSAdapter = class {
144
145
  type: "track",
145
146
  event: eventName,
146
147
  anonymousId: properties.instanceId,
148
+ sessionId: properties.sessionId,
147
149
  userId: properties.userId,
148
150
  properties: {
149
151
  ...properties
@@ -166,6 +168,7 @@ var AWSAdapter = class {
166
168
  const eventData = {
167
169
  type: "identify",
168
170
  anonymousId: traits.instanceId,
171
+ sessionId: traits.sessionId,
169
172
  traits: {
170
173
  ...traits,
171
174
  ...identifiers ? { identifiers } : {}
@@ -184,6 +187,7 @@ var AWSAdapter = class {
184
187
  const eventData = {
185
188
  type: "set_fields",
186
189
  anonymousId: context.instanceId,
190
+ sessionId: context.sessionId,
187
191
  fields,
188
192
  traits: {
189
193
  instanceId: context.instanceId,
@@ -206,7 +210,11 @@ var AWSAdapter = class {
206
210
  // src/storage.ts
207
211
  var INSTANCE_ID_KEY = "amplifyup_instance_id";
208
212
  var USER_ID_KEY = "amplifyup_user_id";
213
+ var SESSION_ID_KEY = "amplifyup_session_id";
214
+ var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
209
215
  var ANON_COOKIE_NAME = "amplifyup_anon_id";
216
+ var SESSION_COOKIE_NAME = "amplifyup_session_id";
217
+ var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
210
218
  function generateGuid() {
211
219
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
212
220
  return crypto.randomUUID();
@@ -233,9 +241,9 @@ function readCookie(name) {
233
241
  }
234
242
  return null;
235
243
  }
236
- function writeCookie(name, value) {
244
+ function writeCookie(name, value, maxAgeSeconds = 31536e3) {
237
245
  if (typeof document === "undefined") return;
238
- document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax`;
246
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
239
247
  }
240
248
  function getInstanceId() {
241
249
  if (typeof window === "undefined") {
@@ -258,6 +266,23 @@ function getUserId() {
258
266
  }
259
267
  return localStorage.getItem(USER_ID_KEY);
260
268
  }
269
+ function getSessionId() {
270
+ if (typeof window === "undefined") {
271
+ return "";
272
+ }
273
+ const now = Date.now();
274
+ const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
275
+ const fromCookie = readCookie(SESSION_COOKIE_NAME);
276
+ const fromLs = localStorage.getItem(SESSION_ID_KEY);
277
+ let sessionId = fromLs || fromCookie || "";
278
+ if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
279
+ sessionId = generateGuid();
280
+ }
281
+ localStorage.setItem(SESSION_ID_KEY, sessionId);
282
+ localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
283
+ writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
284
+ return sessionId;
285
+ }
261
286
  function setUserId(userId) {
262
287
  if (typeof window === "undefined") {
263
288
  return;
@@ -270,7 +295,10 @@ function clearIds() {
270
295
  }
271
296
  localStorage.removeItem(INSTANCE_ID_KEY);
272
297
  localStorage.removeItem(USER_ID_KEY);
298
+ localStorage.removeItem(SESSION_ID_KEY);
299
+ localStorage.removeItem(SESSION_ACTIVITY_KEY);
273
300
  document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
301
+ document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
274
302
  }
275
303
 
276
304
  // src/targets.ts
@@ -433,6 +461,7 @@ async function fetchEdgeResolve(options) {
433
461
  params.set("variants", variantParam);
434
462
  } else if (options.anonId) {
435
463
  params.set("anonId", options.anonId);
464
+ if (options.sessionId) params.set("sessionId", options.sessionId);
436
465
  }
437
466
  const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
438
467
  const init = {
@@ -534,6 +563,7 @@ async function fetchContent(options) {
534
563
  trackingId: options.trackingId,
535
564
  route: options.route,
536
565
  anonId: options.variantKeys ? void 0 : options.anonId,
566
+ sessionId: options.variantKeys ? void 0 : options.sessionId,
537
567
  siteId: options.siteId,
538
568
  fetch: options.fetchFn,
539
569
  revalidate: options.revalidate,
@@ -603,6 +633,7 @@ async function fetchEdgeSelect(options) {
603
633
  ...options.trackingId ? { trackingId: options.trackingId } : {},
604
634
  ...options.site ? { site: options.site } : {},
605
635
  anonId: options.anonId,
636
+ ...options.sessionId ? { sessionId: options.sessionId } : {},
606
637
  ...options.zones?.length ? { zones: options.zones } : {},
607
638
  ...options.context ? { context: options.context } : {}
608
639
  })
@@ -631,6 +662,7 @@ async function selectVariants(options) {
631
662
  site: options.site,
632
663
  view: options.view,
633
664
  anonId: options.anonId,
665
+ sessionId: options.sessionId,
634
666
  zones: options.zones,
635
667
  context: options.context,
636
668
  fetch: options.fetch,
@@ -715,7 +747,6 @@ function searchSpec(pagination, field, term) {
715
747
  }
716
748
 
717
749
  // src/index.ts
718
- var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
719
750
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
720
751
  var DWELL_POLL_MS = 5e3;
721
752
  function readPreviewQueryOverrides() {
@@ -891,6 +922,7 @@ var AmplifyUpSDK = class {
891
922
  if (typeof window === "undefined") return;
892
923
  window.AmplifyUp = {
893
924
  getInstanceId: () => this.getInstanceId(),
925
+ getSessionId: () => this.getSessionId(),
894
926
  getUserId: () => this.getUserId(),
895
927
  getTrackingId: () => this.getTrackingId(),
896
928
  getTarget: () => this.getTarget(),
@@ -1313,12 +1345,14 @@ var AmplifyUpSDK = class {
1313
1345
  return;
1314
1346
  }
1315
1347
  const instanceId = getInstanceId();
1348
+ const sessionId = getSessionId();
1316
1349
  const pageProperties = {
1317
1350
  name: url,
1318
1351
  path: url,
1319
1352
  url: typeof window !== "undefined" ? window.location.href : url,
1320
1353
  referrer: typeof document !== "undefined" ? document.referrer : void 0,
1321
1354
  instanceId,
1355
+ sessionId,
1322
1356
  trackingId: this.config?.trackingId,
1323
1357
  // Timestamp is included in event payload
1324
1358
  ...properties
@@ -1337,8 +1371,10 @@ var AmplifyUpSDK = class {
1337
1371
  return;
1338
1372
  }
1339
1373
  const instanceId = getInstanceId();
1374
+ const sessionId = getSessionId();
1340
1375
  const trackProperties = {
1341
1376
  instanceId,
1377
+ sessionId,
1342
1378
  trackingId: this.config?.trackingId,
1343
1379
  url: typeof window !== "undefined" ? window.location.href : void 0,
1344
1380
  path: properties?.path || (typeof window !== "undefined" ? window.location.pathname : void 0),
@@ -1411,8 +1447,10 @@ var AmplifyUpSDK = class {
1411
1447
  return;
1412
1448
  }
1413
1449
  const instanceId = getInstanceId();
1450
+ const sessionId = getSessionId();
1414
1451
  const identifyTraits = {
1415
1452
  instanceId,
1453
+ sessionId,
1416
1454
  trackingId: this.config?.trackingId,
1417
1455
  ...traits
1418
1456
  };
@@ -1450,6 +1488,7 @@ var AmplifyUpSDK = class {
1450
1488
  if (typeof this.adapter.setProfileFields === "function") {
1451
1489
  this.adapter.setProfileFields(fields, {
1452
1490
  instanceId: instanceId || void 0,
1491
+ sessionId: getSessionId() || void 0,
1453
1492
  trackingId: this.config?.trackingId
1454
1493
  });
1455
1494
  }
@@ -1500,6 +1539,12 @@ var AmplifyUpSDK = class {
1500
1539
  getInstanceId() {
1501
1540
  return getInstanceId();
1502
1541
  }
1542
+ /**
1543
+ * Get session id (30-minute inactivity reset).
1544
+ */
1545
+ getSessionId() {
1546
+ return getSessionId();
1547
+ }
1503
1548
  /**
1504
1549
  * Get user ID
1505
1550
  */
@@ -4372,12 +4417,14 @@ function AmplifyUpProvider({ children, config }) {
4372
4417
  }
4373
4418
  let variantKeys;
4374
4419
  const anonId = sdk.getInstanceId();
4420
+ const sessionId = sdk.getSessionId();
4375
4421
  if (!preview && anonId) {
4376
4422
  try {
4377
4423
  const selected = await selectVariants({
4378
4424
  trackingId,
4379
4425
  view: actualRoute,
4380
4426
  anonId,
4427
+ sessionId: sessionId || void 0,
4381
4428
  site: sdk.getSiteId() || void 0,
4382
4429
  edgeUrl
4383
4430
  });
@@ -4390,6 +4437,7 @@ function AmplifyUpProvider({ children, config }) {
4390
4437
  route: actualRoute,
4391
4438
  trackingId,
4392
4439
  anonId: variantKeys ? void 0 : anonId,
4440
+ sessionId: variantKeys ? void 0 : sessionId || void 0,
4393
4441
  siteId: sdk.getSiteId() || null,
4394
4442
  preview,
4395
4443
  orchestratorUrl,