@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/index.mjs CHANGED
@@ -91,6 +91,7 @@ var AWSAdapter = class {
91
91
  const eventData = {
92
92
  type: "page",
93
93
  anonymousId: properties.instanceId,
94
+ sessionId: properties.sessionId,
94
95
  userId: properties.userId,
95
96
  properties: {
96
97
  path: properties.path || properties.url,
@@ -133,6 +134,7 @@ var AWSAdapter = class {
133
134
  type: "track",
134
135
  event: eventName,
135
136
  anonymousId: properties.instanceId,
137
+ sessionId: properties.sessionId,
136
138
  userId: properties.userId,
137
139
  properties: {
138
140
  ...properties
@@ -155,6 +157,7 @@ var AWSAdapter = class {
155
157
  const eventData = {
156
158
  type: "identify",
157
159
  anonymousId: traits.instanceId,
160
+ sessionId: traits.sessionId,
158
161
  traits: {
159
162
  ...traits,
160
163
  ...identifiers ? { identifiers } : {}
@@ -173,6 +176,7 @@ var AWSAdapter = class {
173
176
  const eventData = {
174
177
  type: "set_fields",
175
178
  anonymousId: context.instanceId,
179
+ sessionId: context.sessionId,
176
180
  fields,
177
181
  traits: {
178
182
  instanceId: context.instanceId,
@@ -195,7 +199,11 @@ var AWSAdapter = class {
195
199
  // src/storage.ts
196
200
  var INSTANCE_ID_KEY = "amplifyup_instance_id";
197
201
  var USER_ID_KEY = "amplifyup_user_id";
202
+ var SESSION_ID_KEY = "amplifyup_session_id";
203
+ var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
198
204
  var ANON_COOKIE_NAME = "amplifyup_anon_id";
205
+ var SESSION_COOKIE_NAME = "amplifyup_session_id";
206
+ var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
199
207
  function generateGuid() {
200
208
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
201
209
  return crypto.randomUUID();
@@ -222,9 +230,9 @@ function readCookie(name) {
222
230
  }
223
231
  return null;
224
232
  }
225
- function writeCookie(name, value) {
233
+ function writeCookie(name, value, maxAgeSeconds = 31536e3) {
226
234
  if (typeof document === "undefined") return;
227
- document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax`;
235
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
228
236
  }
229
237
  function getInstanceId() {
230
238
  if (typeof window === "undefined") {
@@ -247,6 +255,23 @@ function getUserId() {
247
255
  }
248
256
  return localStorage.getItem(USER_ID_KEY);
249
257
  }
258
+ function getSessionId() {
259
+ if (typeof window === "undefined") {
260
+ return "";
261
+ }
262
+ const now = Date.now();
263
+ const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
264
+ const fromCookie = readCookie(SESSION_COOKIE_NAME);
265
+ const fromLs = localStorage.getItem(SESSION_ID_KEY);
266
+ let sessionId = fromLs || fromCookie || "";
267
+ if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
268
+ sessionId = generateGuid();
269
+ }
270
+ localStorage.setItem(SESSION_ID_KEY, sessionId);
271
+ localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
272
+ writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
273
+ return sessionId;
274
+ }
250
275
  function setUserId(userId) {
251
276
  if (typeof window === "undefined") {
252
277
  return;
@@ -259,7 +284,10 @@ function clearIds() {
259
284
  }
260
285
  localStorage.removeItem(INSTANCE_ID_KEY);
261
286
  localStorage.removeItem(USER_ID_KEY);
287
+ localStorage.removeItem(SESSION_ID_KEY);
288
+ localStorage.removeItem(SESSION_ACTIVITY_KEY);
262
289
  document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
290
+ document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
263
291
  }
264
292
 
265
293
  // src/targets.ts
@@ -422,6 +450,7 @@ async function fetchEdgeResolve(options) {
422
450
  params.set("variants", variantParam);
423
451
  } else if (options.anonId) {
424
452
  params.set("anonId", options.anonId);
453
+ if (options.sessionId) params.set("sessionId", options.sessionId);
425
454
  }
426
455
  const fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
427
456
  const init2 = {
@@ -523,6 +552,7 @@ async function fetchContent(options) {
523
552
  trackingId: options.trackingId,
524
553
  route: options.route,
525
554
  anonId: options.variantKeys ? void 0 : options.anonId,
555
+ sessionId: options.variantKeys ? void 0 : options.sessionId,
526
556
  siteId: options.siteId,
527
557
  fetch: options.fetchFn,
528
558
  revalidate: options.revalidate,
@@ -592,6 +622,7 @@ async function fetchEdgeSelect(options) {
592
622
  ...options.trackingId ? { trackingId: options.trackingId } : {},
593
623
  ...options.site ? { site: options.site } : {},
594
624
  anonId: options.anonId,
625
+ ...options.sessionId ? { sessionId: options.sessionId } : {},
595
626
  ...options.zones?.length ? { zones: options.zones } : {},
596
627
  ...options.context ? { context: options.context } : {}
597
628
  })
@@ -620,6 +651,7 @@ async function selectVariants(options) {
620
651
  site: options.site,
621
652
  view: options.view,
622
653
  anonId: options.anonId,
654
+ sessionId: options.sessionId,
623
655
  zones: options.zones,
624
656
  context: options.context,
625
657
  fetch: options.fetch,
@@ -704,7 +736,6 @@ function searchSpec(pagination, field, term) {
704
736
  }
705
737
 
706
738
  // src/index.ts
707
- var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
708
739
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
709
740
  var DWELL_POLL_MS = 5e3;
710
741
  function readPreviewQueryOverrides() {
@@ -880,6 +911,7 @@ var AmplifyUpSDK = class {
880
911
  if (typeof window === "undefined") return;
881
912
  window.AmplifyUp = {
882
913
  getInstanceId: () => this.getInstanceId(),
914
+ getSessionId: () => this.getSessionId(),
883
915
  getUserId: () => this.getUserId(),
884
916
  getTrackingId: () => this.getTrackingId(),
885
917
  getTarget: () => this.getTarget(),
@@ -1302,12 +1334,14 @@ var AmplifyUpSDK = class {
1302
1334
  return;
1303
1335
  }
1304
1336
  const instanceId = getInstanceId();
1337
+ const sessionId = getSessionId();
1305
1338
  const pageProperties = {
1306
1339
  name: url,
1307
1340
  path: url,
1308
1341
  url: typeof window !== "undefined" ? window.location.href : url,
1309
1342
  referrer: typeof document !== "undefined" ? document.referrer : void 0,
1310
1343
  instanceId,
1344
+ sessionId,
1311
1345
  trackingId: this.config?.trackingId,
1312
1346
  // Timestamp is included in event payload
1313
1347
  ...properties
@@ -1326,8 +1360,10 @@ var AmplifyUpSDK = class {
1326
1360
  return;
1327
1361
  }
1328
1362
  const instanceId = getInstanceId();
1363
+ const sessionId = getSessionId();
1329
1364
  const trackProperties = {
1330
1365
  instanceId,
1366
+ sessionId,
1331
1367
  trackingId: this.config?.trackingId,
1332
1368
  url: typeof window !== "undefined" ? window.location.href : void 0,
1333
1369
  path: properties?.path || (typeof window !== "undefined" ? window.location.pathname : void 0),
@@ -1400,8 +1436,10 @@ var AmplifyUpSDK = class {
1400
1436
  return;
1401
1437
  }
1402
1438
  const instanceId = getInstanceId();
1439
+ const sessionId = getSessionId();
1403
1440
  const identifyTraits = {
1404
1441
  instanceId,
1442
+ sessionId,
1405
1443
  trackingId: this.config?.trackingId,
1406
1444
  ...traits
1407
1445
  };
@@ -1439,6 +1477,7 @@ var AmplifyUpSDK = class {
1439
1477
  if (typeof this.adapter.setProfileFields === "function") {
1440
1478
  this.adapter.setProfileFields(fields, {
1441
1479
  instanceId: instanceId || void 0,
1480
+ sessionId: getSessionId() || void 0,
1442
1481
  trackingId: this.config?.trackingId
1443
1482
  });
1444
1483
  }
@@ -1489,6 +1528,12 @@ var AmplifyUpSDK = class {
1489
1528
  getInstanceId() {
1490
1529
  return getInstanceId();
1491
1530
  }
1531
+ /**
1532
+ * Get session id (30-minute inactivity reset).
1533
+ */
1534
+ getSessionId() {
1535
+ return getSessionId();
1536
+ }
1492
1537
  /**
1493
1538
  * Get user ID
1494
1539
  */
@@ -3010,6 +3055,7 @@ var setProfileFields = (fields) => getSDK().setProfileFields(fields);
3010
3055
  var trackContentEngagement = (contentId, engagementType) => getSDK().trackContentEngagement(contentId, engagementType);
3011
3056
  var reset = () => getSDK().reset();
3012
3057
  var getInstanceId2 = () => getSDK().getInstanceId();
3058
+ var getSessionId2 = () => getSDK().getSessionId();
3013
3059
  var getUserId2 = () => getSDK().getUserId();
3014
3060
  var getTrackingId = () => getSDK().getTrackingId();
3015
3061
  var getTarget = () => getSDK().getTarget();
@@ -3036,6 +3082,6 @@ var verifyAWSConnection = () => {
3036
3082
  };
3037
3083
  var index_default = getSDK();
3038
3084
 
3039
- export { AmplifyUpSDK, DWELL_ACTIVITY_IDLE_MS, EdgeResolveError, QueryContentError, SESSION_IDLE_GAP_MS, SelectVariantsError, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchContent, fetchEdgeQuery, fetchEdgeResolve, fetchEdgeSelect, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, isWorkingContentRequest, mapResolvedToPageConfig, nextPageSpec, normalizeAmplifyUpTarget, normalizeRoute, page, queryContent, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, routeChange, searchSpec, selectVariants, setProfileFields, setupComponentViewTracking, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
3085
+ export { ANON_COOKIE_NAME, AmplifyUpSDK, DWELL_ACTIVITY_IDLE_MS, EdgeResolveError, QueryContentError, SESSION_COOKIE_NAME, SESSION_IDLE_GAP_MS, SelectVariantsError, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchContent, fetchEdgeQuery, fetchEdgeResolve, fetchEdgeSelect, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getSessionId2 as getSessionId, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, isWorkingContentRequest, mapResolvedToPageConfig, nextPageSpec, normalizeAmplifyUpTarget, normalizeRoute, page, queryContent, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, routeChange, searchSpec, selectVariants, setProfileFields, setupComponentViewTracking, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
3040
3086
  //# sourceMappingURL=index.mjs.map
3041
3087
  //# sourceMappingURL=index.mjs.map