@amplifyup/sdk 0.1.75 → 0.1.76

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.js CHANGED
@@ -17,7 +17,121 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
17
17
  throw Error('Dynamic require of "' + x + '" is not supported');
18
18
  });
19
19
 
20
+ // src/storage.ts
21
+ var INSTANCE_ID_KEY = "amplifyup_instance_id";
22
+ var USER_ID_KEY = "amplifyup_user_id";
23
+ var SESSION_ID_KEY = "amplifyup_session_id";
24
+ var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
25
+ var VISITOR_FIRST_SEEN_KEY = "amplifyup_visitor_first_seen";
26
+ var ANON_COOKIE_NAME = "amplifyup_anon_id";
27
+ var SESSION_COOKIE_NAME = "amplifyup_session_id";
28
+ var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
29
+ function generateGuid() {
30
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
31
+ return crypto.randomUUID();
32
+ }
33
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
34
+ const r = Math.random() * 16 | 0;
35
+ const v = c === "x" ? r : r & 3 | 8;
36
+ return v.toString(16);
37
+ });
38
+ }
39
+ function readCookie(name) {
40
+ if (typeof document === "undefined") return null;
41
+ const parts = document.cookie.split(";");
42
+ for (const part of parts) {
43
+ const idx = part.indexOf("=");
44
+ if (idx === -1) continue;
45
+ if (part.slice(0, idx).trim() !== name) continue;
46
+ const value = part.slice(idx + 1).trim();
47
+ try {
48
+ return value ? decodeURIComponent(value) : null;
49
+ } catch {
50
+ return value || null;
51
+ }
52
+ }
53
+ return null;
54
+ }
55
+ function writeCookie(name, value, maxAgeSeconds = 31536e3) {
56
+ if (typeof document === "undefined") return;
57
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
58
+ }
59
+ function getInstanceId() {
60
+ if (typeof window === "undefined") {
61
+ return "";
62
+ }
63
+ const fromCookie = readCookie(ANON_COOKIE_NAME);
64
+ const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
65
+ const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
66
+ if (fromLs !== instanceId) {
67
+ localStorage.setItem(INSTANCE_ID_KEY, instanceId);
68
+ }
69
+ if (fromCookie !== instanceId) {
70
+ writeCookie(ANON_COOKIE_NAME, instanceId);
71
+ }
72
+ return instanceId;
73
+ }
74
+ function getUserId() {
75
+ if (typeof window === "undefined") {
76
+ return null;
77
+ }
78
+ return localStorage.getItem(USER_ID_KEY);
79
+ }
80
+ function getSessionId() {
81
+ if (typeof window === "undefined") {
82
+ return "";
83
+ }
84
+ const now = Date.now();
85
+ const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
86
+ const fromCookie = readCookie(SESSION_COOKIE_NAME);
87
+ const fromLs = localStorage.getItem(SESSION_ID_KEY);
88
+ let sessionId = fromLs || fromCookie || "";
89
+ if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
90
+ sessionId = generateGuid();
91
+ }
92
+ localStorage.setItem(SESSION_ID_KEY, sessionId);
93
+ localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
94
+ writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
95
+ return sessionId;
96
+ }
97
+ function getVisitorFirstSeenAt() {
98
+ if (typeof window === "undefined") {
99
+ return null;
100
+ }
101
+ try {
102
+ const existing = localStorage.getItem(VISITOR_FIRST_SEEN_KEY);
103
+ if (existing) return existing;
104
+ const firstSeenAt = (/* @__PURE__ */ new Date()).toISOString();
105
+ localStorage.setItem(VISITOR_FIRST_SEEN_KEY, firstSeenAt);
106
+ return firstSeenAt;
107
+ } catch {
108
+ return null;
109
+ }
110
+ }
111
+ function setUserId(userId) {
112
+ if (typeof window === "undefined") {
113
+ return;
114
+ }
115
+ localStorage.setItem(USER_ID_KEY, userId);
116
+ }
117
+ function clearIds() {
118
+ if (typeof window === "undefined") {
119
+ return;
120
+ }
121
+ localStorage.removeItem(INSTANCE_ID_KEY);
122
+ localStorage.removeItem(USER_ID_KEY);
123
+ localStorage.removeItem(SESSION_ID_KEY);
124
+ localStorage.removeItem(SESSION_ACTIVITY_KEY);
125
+ localStorage.removeItem(VISITOR_FIRST_SEEN_KEY);
126
+ document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
127
+ document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
128
+ }
129
+
20
130
  // src/adapters/aws.ts
131
+ function visitorContext() {
132
+ const firstSeenAt = getVisitorFirstSeenAt();
133
+ return firstSeenAt ? { firstSeenAt } : void 0;
134
+ }
21
135
  var AWSAdapter = class {
22
136
  constructor() {
23
137
  this.initialized = false;
@@ -57,7 +171,9 @@ var AWSAdapter = class {
57
171
  console.warn("AmplifyUp SDK: AWS configuration missing");
58
172
  return;
59
173
  }
60
- const url = `${this.apiGatewayUrl}${endpoint}`;
174
+ const trackingId = eventData?.trackingId || this.config?.trackingId;
175
+ const query = trackingId ? `?tid=${encodeURIComponent(trackingId)}` : "";
176
+ const url = `${this.apiGatewayUrl}${endpoint}${query}`;
61
177
  try {
62
178
  const response = await fetch(url, {
63
179
  method: "POST",
@@ -119,7 +235,8 @@ var AWSAdapter = class {
119
235
  page: {
120
236
  path: properties.path || properties.url,
121
237
  referrer: properties.referrer
122
- }
238
+ },
239
+ visitor: visitorContext()
123
240
  },
124
241
  timestamp: properties.timestamp || (/* @__PURE__ */ new Date()).toISOString()
125
242
  };
@@ -157,7 +274,8 @@ var AWSAdapter = class {
157
274
  ...properties
158
275
  },
159
276
  context: {
160
- url: properties.url || (typeof window !== "undefined" ? window.location.href : "")
277
+ url: properties.url || (typeof window !== "undefined" ? window.location.href : ""),
278
+ visitor: visitorContext()
161
279
  },
162
280
  timestamp: properties.timestamp || (/* @__PURE__ */ new Date()).toISOString()
163
281
  };
@@ -213,100 +331,6 @@ var AWSAdapter = class {
213
331
  }
214
332
  };
215
333
 
216
- // src/storage.ts
217
- var INSTANCE_ID_KEY = "amplifyup_instance_id";
218
- var USER_ID_KEY = "amplifyup_user_id";
219
- var SESSION_ID_KEY = "amplifyup_session_id";
220
- var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
221
- var ANON_COOKIE_NAME = "amplifyup_anon_id";
222
- var SESSION_COOKIE_NAME = "amplifyup_session_id";
223
- var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
224
- function generateGuid() {
225
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
226
- return crypto.randomUUID();
227
- }
228
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
229
- const r = Math.random() * 16 | 0;
230
- const v = c === "x" ? r : r & 3 | 8;
231
- return v.toString(16);
232
- });
233
- }
234
- function readCookie(name) {
235
- if (typeof document === "undefined") return null;
236
- const parts = document.cookie.split(";");
237
- for (const part of parts) {
238
- const idx = part.indexOf("=");
239
- if (idx === -1) continue;
240
- if (part.slice(0, idx).trim() !== name) continue;
241
- const value = part.slice(idx + 1).trim();
242
- try {
243
- return value ? decodeURIComponent(value) : null;
244
- } catch {
245
- return value || null;
246
- }
247
- }
248
- return null;
249
- }
250
- function writeCookie(name, value, maxAgeSeconds = 31536e3) {
251
- if (typeof document === "undefined") return;
252
- document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
253
- }
254
- function getInstanceId() {
255
- if (typeof window === "undefined") {
256
- return "";
257
- }
258
- const fromCookie = readCookie(ANON_COOKIE_NAME);
259
- const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
260
- const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
261
- if (fromLs !== instanceId) {
262
- localStorage.setItem(INSTANCE_ID_KEY, instanceId);
263
- }
264
- if (fromCookie !== instanceId) {
265
- writeCookie(ANON_COOKIE_NAME, instanceId);
266
- }
267
- return instanceId;
268
- }
269
- function getUserId() {
270
- if (typeof window === "undefined") {
271
- return null;
272
- }
273
- return localStorage.getItem(USER_ID_KEY);
274
- }
275
- function getSessionId() {
276
- if (typeof window === "undefined") {
277
- return "";
278
- }
279
- const now = Date.now();
280
- const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
281
- const fromCookie = readCookie(SESSION_COOKIE_NAME);
282
- const fromLs = localStorage.getItem(SESSION_ID_KEY);
283
- let sessionId = fromLs || fromCookie || "";
284
- if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
285
- sessionId = generateGuid();
286
- }
287
- localStorage.setItem(SESSION_ID_KEY, sessionId);
288
- localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
289
- writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
290
- return sessionId;
291
- }
292
- function setUserId(userId) {
293
- if (typeof window === "undefined") {
294
- return;
295
- }
296
- localStorage.setItem(USER_ID_KEY, userId);
297
- }
298
- function clearIds() {
299
- if (typeof window === "undefined") {
300
- return;
301
- }
302
- localStorage.removeItem(INSTANCE_ID_KEY);
303
- localStorage.removeItem(USER_ID_KEY);
304
- localStorage.removeItem(SESSION_ID_KEY);
305
- localStorage.removeItem(SESSION_ACTIVITY_KEY);
306
- document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
307
- document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
308
- }
309
-
310
334
  // src/targets.ts
311
335
  var AWS_DEV_API = "https://api-dev.amplifyup.ai";
312
336
  var AWS_PROD_API = "https://api.amplifyup.ai";