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