@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.
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { ReactNode } from 'react';
3
- import { P as PageConfig } from './types-CBoVIFI8.mjs';
3
+ import { P as PageConfig } from './types-DUP8Lb7R.mjs';
4
4
 
5
5
  interface AmplifyRendererProps {
6
6
  /**
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { ReactNode } from 'react';
3
- import { P as PageConfig } from './types-CBoVIFI8.js';
3
+ import { P as PageConfig } from './types-DUP8Lb7R.js';
4
4
 
5
5
  interface AmplifyRendererProps {
6
6
  /**
@@ -104,6 +104,7 @@ var AWSAdapter = class {
104
104
  const eventData = {
105
105
  type: "page",
106
106
  anonymousId: properties.instanceId,
107
+ sessionId: properties.sessionId,
107
108
  userId: properties.userId,
108
109
  properties: {
109
110
  path: properties.path || properties.url,
@@ -146,6 +147,7 @@ var AWSAdapter = class {
146
147
  type: "track",
147
148
  event: eventName,
148
149
  anonymousId: properties.instanceId,
150
+ sessionId: properties.sessionId,
149
151
  userId: properties.userId,
150
152
  properties: {
151
153
  ...properties
@@ -168,6 +170,7 @@ var AWSAdapter = class {
168
170
  const eventData = {
169
171
  type: "identify",
170
172
  anonymousId: traits.instanceId,
173
+ sessionId: traits.sessionId,
171
174
  traits: {
172
175
  ...traits,
173
176
  ...identifiers ? { identifiers } : {}
@@ -186,6 +189,7 @@ var AWSAdapter = class {
186
189
  const eventData = {
187
190
  type: "set_fields",
188
191
  anonymousId: context.instanceId,
192
+ sessionId: context.sessionId,
189
193
  fields,
190
194
  traits: {
191
195
  instanceId: context.instanceId,
@@ -208,7 +212,11 @@ var AWSAdapter = class {
208
212
  // src/storage.ts
209
213
  var INSTANCE_ID_KEY = "amplifyup_instance_id";
210
214
  var USER_ID_KEY = "amplifyup_user_id";
215
+ var SESSION_ID_KEY = "amplifyup_session_id";
216
+ var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
211
217
  var ANON_COOKIE_NAME = "amplifyup_anon_id";
218
+ var SESSION_COOKIE_NAME = "amplifyup_session_id";
219
+ var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
212
220
  function generateGuid() {
213
221
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
214
222
  return crypto.randomUUID();
@@ -235,9 +243,9 @@ function readCookie(name) {
235
243
  }
236
244
  return null;
237
245
  }
238
- function writeCookie(name, value) {
246
+ function writeCookie(name, value, maxAgeSeconds = 31536e3) {
239
247
  if (typeof document === "undefined") return;
240
- document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax`;
248
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
241
249
  }
242
250
  function getInstanceId() {
243
251
  if (typeof window === "undefined") {
@@ -260,6 +268,23 @@ function getUserId() {
260
268
  }
261
269
  return localStorage.getItem(USER_ID_KEY);
262
270
  }
271
+ function getSessionId() {
272
+ if (typeof window === "undefined") {
273
+ return "";
274
+ }
275
+ const now = Date.now();
276
+ const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
277
+ const fromCookie = readCookie(SESSION_COOKIE_NAME);
278
+ const fromLs = localStorage.getItem(SESSION_ID_KEY);
279
+ let sessionId = fromLs || fromCookie || "";
280
+ if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
281
+ sessionId = generateGuid();
282
+ }
283
+ localStorage.setItem(SESSION_ID_KEY, sessionId);
284
+ localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
285
+ writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
286
+ return sessionId;
287
+ }
263
288
  function setUserId(userId) {
264
289
  if (typeof window === "undefined") {
265
290
  return;
@@ -272,7 +297,10 @@ function clearIds() {
272
297
  }
273
298
  localStorage.removeItem(INSTANCE_ID_KEY);
274
299
  localStorage.removeItem(USER_ID_KEY);
300
+ localStorage.removeItem(SESSION_ID_KEY);
301
+ localStorage.removeItem(SESSION_ACTIVITY_KEY);
275
302
  document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
303
+ document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
276
304
  }
277
305
 
278
306
  // src/targets.ts
@@ -380,7 +408,6 @@ function isComposerPreview() {
380
408
  }
381
409
 
382
410
  // src/index.ts
383
- var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
384
411
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
385
412
  var DWELL_POLL_MS = 5e3;
386
413
  function readPreviewQueryOverrides() {
@@ -556,6 +583,7 @@ var AmplifyUpSDK = class {
556
583
  if (typeof window === "undefined") return;
557
584
  window.AmplifyUp = {
558
585
  getInstanceId: () => this.getInstanceId(),
586
+ getSessionId: () => this.getSessionId(),
559
587
  getUserId: () => this.getUserId(),
560
588
  getTrackingId: () => this.getTrackingId(),
561
589
  getTarget: () => this.getTarget(),
@@ -978,12 +1006,14 @@ var AmplifyUpSDK = class {
978
1006
  return;
979
1007
  }
980
1008
  const instanceId = getInstanceId();
1009
+ const sessionId = getSessionId();
981
1010
  const pageProperties = {
982
1011
  name: url,
983
1012
  path: url,
984
1013
  url: typeof window !== "undefined" ? window.location.href : url,
985
1014
  referrer: typeof document !== "undefined" ? document.referrer : void 0,
986
1015
  instanceId,
1016
+ sessionId,
987
1017
  trackingId: this.config?.trackingId,
988
1018
  // Timestamp is included in event payload
989
1019
  ...properties
@@ -1002,8 +1032,10 @@ var AmplifyUpSDK = class {
1002
1032
  return;
1003
1033
  }
1004
1034
  const instanceId = getInstanceId();
1035
+ const sessionId = getSessionId();
1005
1036
  const trackProperties = {
1006
1037
  instanceId,
1038
+ sessionId,
1007
1039
  trackingId: this.config?.trackingId,
1008
1040
  url: typeof window !== "undefined" ? window.location.href : void 0,
1009
1041
  path: properties?.path || (typeof window !== "undefined" ? window.location.pathname : void 0),
@@ -1076,8 +1108,10 @@ var AmplifyUpSDK = class {
1076
1108
  return;
1077
1109
  }
1078
1110
  const instanceId = getInstanceId();
1111
+ const sessionId = getSessionId();
1079
1112
  const identifyTraits = {
1080
1113
  instanceId,
1114
+ sessionId,
1081
1115
  trackingId: this.config?.trackingId,
1082
1116
  ...traits
1083
1117
  };
@@ -1115,6 +1149,7 @@ var AmplifyUpSDK = class {
1115
1149
  if (typeof this.adapter.setProfileFields === "function") {
1116
1150
  this.adapter.setProfileFields(fields, {
1117
1151
  instanceId: instanceId || void 0,
1152
+ sessionId: getSessionId() || void 0,
1118
1153
  trackingId: this.config?.trackingId
1119
1154
  });
1120
1155
  }
@@ -1165,6 +1200,12 @@ var AmplifyUpSDK = class {
1165
1200
  getInstanceId() {
1166
1201
  return getInstanceId();
1167
1202
  }
1203
+ /**
1204
+ * Get session id (30-minute inactivity reset).
1205
+ */
1206
+ getSessionId() {
1207
+ return getSessionId();
1208
+ }
1168
1209
  /**
1169
1210
  * Get user ID
1170
1211
  */