@amplifyup/sdk 0.1.75 → 0.1.77

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
@@ -1,6 +1,120 @@
1
1
  /* @amplifyup/sdk - https://amplifyup.ai */
2
2
 
3
+ // src/storage.ts
4
+ var INSTANCE_ID_KEY = "amplifyup_instance_id";
5
+ var USER_ID_KEY = "amplifyup_user_id";
6
+ var SESSION_ID_KEY = "amplifyup_session_id";
7
+ var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
8
+ var VISITOR_FIRST_SEEN_KEY = "amplifyup_visitor_first_seen";
9
+ var ANON_COOKIE_NAME = "amplifyup_anon_id";
10
+ var SESSION_COOKIE_NAME = "amplifyup_session_id";
11
+ var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
12
+ function generateGuid() {
13
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
14
+ return crypto.randomUUID();
15
+ }
16
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
17
+ const r = Math.random() * 16 | 0;
18
+ const v = c === "x" ? r : r & 3 | 8;
19
+ return v.toString(16);
20
+ });
21
+ }
22
+ function readCookie(name) {
23
+ if (typeof document === "undefined") return null;
24
+ const parts = document.cookie.split(";");
25
+ for (const part of parts) {
26
+ const idx = part.indexOf("=");
27
+ if (idx === -1) continue;
28
+ if (part.slice(0, idx).trim() !== name) continue;
29
+ const value = part.slice(idx + 1).trim();
30
+ try {
31
+ return value ? decodeURIComponent(value) : null;
32
+ } catch {
33
+ return value || null;
34
+ }
35
+ }
36
+ return null;
37
+ }
38
+ function writeCookie(name, value, maxAgeSeconds = 31536e3) {
39
+ if (typeof document === "undefined") return;
40
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
41
+ }
42
+ function getInstanceId() {
43
+ if (typeof window === "undefined") {
44
+ return "";
45
+ }
46
+ const fromCookie = readCookie(ANON_COOKIE_NAME);
47
+ const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
48
+ const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
49
+ if (fromLs !== instanceId) {
50
+ localStorage.setItem(INSTANCE_ID_KEY, instanceId);
51
+ }
52
+ if (fromCookie !== instanceId) {
53
+ writeCookie(ANON_COOKIE_NAME, instanceId);
54
+ }
55
+ return instanceId;
56
+ }
57
+ function getUserId() {
58
+ if (typeof window === "undefined") {
59
+ return null;
60
+ }
61
+ return localStorage.getItem(USER_ID_KEY);
62
+ }
63
+ function getSessionId() {
64
+ if (typeof window === "undefined") {
65
+ return "";
66
+ }
67
+ const now = Date.now();
68
+ const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
69
+ const fromCookie = readCookie(SESSION_COOKIE_NAME);
70
+ const fromLs = localStorage.getItem(SESSION_ID_KEY);
71
+ let sessionId = fromLs || fromCookie || "";
72
+ if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
73
+ sessionId = generateGuid();
74
+ }
75
+ localStorage.setItem(SESSION_ID_KEY, sessionId);
76
+ localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
77
+ writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
78
+ return sessionId;
79
+ }
80
+ function getVisitorFirstSeenAt() {
81
+ if (typeof window === "undefined") {
82
+ return null;
83
+ }
84
+ try {
85
+ const existing = localStorage.getItem(VISITOR_FIRST_SEEN_KEY);
86
+ if (existing) return existing;
87
+ const firstSeenAt = (/* @__PURE__ */ new Date()).toISOString();
88
+ localStorage.setItem(VISITOR_FIRST_SEEN_KEY, firstSeenAt);
89
+ return firstSeenAt;
90
+ } catch {
91
+ return null;
92
+ }
93
+ }
94
+ function setUserId(userId) {
95
+ if (typeof window === "undefined") {
96
+ return;
97
+ }
98
+ localStorage.setItem(USER_ID_KEY, userId);
99
+ }
100
+ function clearIds() {
101
+ if (typeof window === "undefined") {
102
+ return;
103
+ }
104
+ localStorage.removeItem(INSTANCE_ID_KEY);
105
+ localStorage.removeItem(USER_ID_KEY);
106
+ localStorage.removeItem(SESSION_ID_KEY);
107
+ localStorage.removeItem(SESSION_ACTIVITY_KEY);
108
+ localStorage.removeItem(VISITOR_FIRST_SEEN_KEY);
109
+ document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
110
+ document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
111
+ }
112
+
3
113
  // src/adapters/aws.ts
114
+ function visitorContext() {
115
+ const firstSeenAt = getVisitorFirstSeenAt();
116
+ return firstSeenAt ? { firstSeenAt } : void 0;
117
+ }
4
118
  var AWSAdapter = class {
5
119
  constructor() {
6
120
  this.initialized = false;
@@ -40,7 +154,9 @@ var AWSAdapter = class {
40
154
  console.warn("AmplifyUp SDK: AWS configuration missing");
41
155
  return;
42
156
  }
43
- const url = `${this.apiGatewayUrl}${endpoint}`;
157
+ const trackingId = eventData?.trackingId || this.config?.trackingId;
158
+ const query = trackingId ? `?tid=${encodeURIComponent(trackingId)}` : "";
159
+ const url = `${this.apiGatewayUrl}${endpoint}${query}`;
44
160
  try {
45
161
  const response = await fetch(url, {
46
162
  method: "POST",
@@ -102,7 +218,8 @@ var AWSAdapter = class {
102
218
  page: {
103
219
  path: properties.path || properties.url,
104
220
  referrer: properties.referrer
105
- }
221
+ },
222
+ visitor: visitorContext()
106
223
  },
107
224
  timestamp: properties.timestamp || (/* @__PURE__ */ new Date()).toISOString()
108
225
  };
@@ -140,7 +257,8 @@ var AWSAdapter = class {
140
257
  ...properties
141
258
  },
142
259
  context: {
143
- url: properties.url || (typeof window !== "undefined" ? window.location.href : "")
260
+ url: properties.url || (typeof window !== "undefined" ? window.location.href : ""),
261
+ visitor: visitorContext()
144
262
  },
145
263
  timestamp: properties.timestamp || (/* @__PURE__ */ new Date()).toISOString()
146
264
  };
@@ -196,100 +314,6 @@ var AWSAdapter = class {
196
314
  }
197
315
  };
198
316
 
199
- // src/storage.ts
200
- var INSTANCE_ID_KEY = "amplifyup_instance_id";
201
- var USER_ID_KEY = "amplifyup_user_id";
202
- var SESSION_ID_KEY = "amplifyup_session_id";
203
- var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
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;
207
- function generateGuid() {
208
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
209
- return crypto.randomUUID();
210
- }
211
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
212
- const r = Math.random() * 16 | 0;
213
- const v = c === "x" ? r : r & 3 | 8;
214
- return v.toString(16);
215
- });
216
- }
217
- function readCookie(name) {
218
- if (typeof document === "undefined") return null;
219
- const parts = document.cookie.split(";");
220
- for (const part of parts) {
221
- const idx = part.indexOf("=");
222
- if (idx === -1) continue;
223
- if (part.slice(0, idx).trim() !== name) continue;
224
- const value = part.slice(idx + 1).trim();
225
- try {
226
- return value ? decodeURIComponent(value) : null;
227
- } catch {
228
- return value || null;
229
- }
230
- }
231
- return null;
232
- }
233
- function writeCookie(name, value, maxAgeSeconds = 31536e3) {
234
- if (typeof document === "undefined") return;
235
- document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
236
- }
237
- function getInstanceId() {
238
- if (typeof window === "undefined") {
239
- return "";
240
- }
241
- const fromCookie = readCookie(ANON_COOKIE_NAME);
242
- const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
243
- const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
244
- if (fromLs !== instanceId) {
245
- localStorage.setItem(INSTANCE_ID_KEY, instanceId);
246
- }
247
- if (fromCookie !== instanceId) {
248
- writeCookie(ANON_COOKIE_NAME, instanceId);
249
- }
250
- return instanceId;
251
- }
252
- function getUserId() {
253
- if (typeof window === "undefined") {
254
- return null;
255
- }
256
- return localStorage.getItem(USER_ID_KEY);
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
- }
275
- function setUserId(userId) {
276
- if (typeof window === "undefined") {
277
- return;
278
- }
279
- localStorage.setItem(USER_ID_KEY, userId);
280
- }
281
- function clearIds() {
282
- if (typeof window === "undefined") {
283
- return;
284
- }
285
- localStorage.removeItem(INSTANCE_ID_KEY);
286
- localStorage.removeItem(USER_ID_KEY);
287
- localStorage.removeItem(SESSION_ID_KEY);
288
- localStorage.removeItem(SESSION_ACTIVITY_KEY);
289
- document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
290
- document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
291
- }
292
-
293
317
  // src/targets.ts
294
318
  var AWS_DEV_API = "https://api-dev.amplifyup.ai";
295
319
  var AWS_PROD_API = "https://api.amplifyup.ai";