@amplifyup/sdk 0.1.74 → 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.
@@ -11,7 +11,121 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
11
11
  throw Error('Dynamic require of "' + x + '" is not supported');
12
12
  });
13
13
 
14
+ // src/storage.ts
15
+ var INSTANCE_ID_KEY = "amplifyup_instance_id";
16
+ var USER_ID_KEY = "amplifyup_user_id";
17
+ var SESSION_ID_KEY = "amplifyup_session_id";
18
+ var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
19
+ var VISITOR_FIRST_SEEN_KEY = "amplifyup_visitor_first_seen";
20
+ var ANON_COOKIE_NAME = "amplifyup_anon_id";
21
+ var SESSION_COOKIE_NAME = "amplifyup_session_id";
22
+ var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
23
+ function generateGuid() {
24
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
25
+ return crypto.randomUUID();
26
+ }
27
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
28
+ const r = Math.random() * 16 | 0;
29
+ const v = c === "x" ? r : r & 3 | 8;
30
+ return v.toString(16);
31
+ });
32
+ }
33
+ function readCookie(name) {
34
+ if (typeof document === "undefined") return null;
35
+ const parts = document.cookie.split(";");
36
+ for (const part of parts) {
37
+ const idx = part.indexOf("=");
38
+ if (idx === -1) continue;
39
+ if (part.slice(0, idx).trim() !== name) continue;
40
+ const value = part.slice(idx + 1).trim();
41
+ try {
42
+ return value ? decodeURIComponent(value) : null;
43
+ } catch {
44
+ return value || null;
45
+ }
46
+ }
47
+ return null;
48
+ }
49
+ function writeCookie(name, value, maxAgeSeconds = 31536e3) {
50
+ if (typeof document === "undefined") return;
51
+ document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
52
+ }
53
+ function getInstanceId() {
54
+ if (typeof window === "undefined") {
55
+ return "";
56
+ }
57
+ const fromCookie = readCookie(ANON_COOKIE_NAME);
58
+ const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
59
+ const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
60
+ if (fromLs !== instanceId) {
61
+ localStorage.setItem(INSTANCE_ID_KEY, instanceId);
62
+ }
63
+ if (fromCookie !== instanceId) {
64
+ writeCookie(ANON_COOKIE_NAME, instanceId);
65
+ }
66
+ return instanceId;
67
+ }
68
+ function getUserId() {
69
+ if (typeof window === "undefined") {
70
+ return null;
71
+ }
72
+ return localStorage.getItem(USER_ID_KEY);
73
+ }
74
+ function getSessionId() {
75
+ if (typeof window === "undefined") {
76
+ return "";
77
+ }
78
+ const now = Date.now();
79
+ const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
80
+ const fromCookie = readCookie(SESSION_COOKIE_NAME);
81
+ const fromLs = localStorage.getItem(SESSION_ID_KEY);
82
+ let sessionId = fromLs || fromCookie || "";
83
+ if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
84
+ sessionId = generateGuid();
85
+ }
86
+ localStorage.setItem(SESSION_ID_KEY, sessionId);
87
+ localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
88
+ writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
89
+ return sessionId;
90
+ }
91
+ function getVisitorFirstSeenAt() {
92
+ if (typeof window === "undefined") {
93
+ return null;
94
+ }
95
+ try {
96
+ const existing = localStorage.getItem(VISITOR_FIRST_SEEN_KEY);
97
+ if (existing) return existing;
98
+ const firstSeenAt = (/* @__PURE__ */ new Date()).toISOString();
99
+ localStorage.setItem(VISITOR_FIRST_SEEN_KEY, firstSeenAt);
100
+ return firstSeenAt;
101
+ } catch {
102
+ return null;
103
+ }
104
+ }
105
+ function setUserId(userId) {
106
+ if (typeof window === "undefined") {
107
+ return;
108
+ }
109
+ localStorage.setItem(USER_ID_KEY, userId);
110
+ }
111
+ function clearIds() {
112
+ if (typeof window === "undefined") {
113
+ return;
114
+ }
115
+ localStorage.removeItem(INSTANCE_ID_KEY);
116
+ localStorage.removeItem(USER_ID_KEY);
117
+ localStorage.removeItem(SESSION_ID_KEY);
118
+ localStorage.removeItem(SESSION_ACTIVITY_KEY);
119
+ localStorage.removeItem(VISITOR_FIRST_SEEN_KEY);
120
+ document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
121
+ document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
122
+ }
123
+
14
124
  // src/adapters/aws.ts
125
+ function visitorContext() {
126
+ const firstSeenAt = getVisitorFirstSeenAt();
127
+ return firstSeenAt ? { firstSeenAt } : void 0;
128
+ }
15
129
  var AWSAdapter = class {
16
130
  constructor() {
17
131
  this.initialized = false;
@@ -51,7 +165,9 @@ var AWSAdapter = class {
51
165
  console.warn("AmplifyUp SDK: AWS configuration missing");
52
166
  return;
53
167
  }
54
- const url = `${this.apiGatewayUrl}${endpoint}`;
168
+ const trackingId = eventData?.trackingId || this.config?.trackingId;
169
+ const query = trackingId ? `?tid=${encodeURIComponent(trackingId)}` : "";
170
+ const url = `${this.apiGatewayUrl}${endpoint}${query}`;
55
171
  try {
56
172
  const response = await fetch(url, {
57
173
  method: "POST",
@@ -113,7 +229,8 @@ var AWSAdapter = class {
113
229
  page: {
114
230
  path: properties.path || properties.url,
115
231
  referrer: properties.referrer
116
- }
232
+ },
233
+ visitor: visitorContext()
117
234
  },
118
235
  timestamp: properties.timestamp || (/* @__PURE__ */ new Date()).toISOString()
119
236
  };
@@ -151,7 +268,8 @@ var AWSAdapter = class {
151
268
  ...properties
152
269
  },
153
270
  context: {
154
- url: properties.url || (typeof window !== "undefined" ? window.location.href : "")
271
+ url: properties.url || (typeof window !== "undefined" ? window.location.href : ""),
272
+ visitor: visitorContext()
155
273
  },
156
274
  timestamp: properties.timestamp || (/* @__PURE__ */ new Date()).toISOString()
157
275
  };
@@ -207,100 +325,6 @@ var AWSAdapter = class {
207
325
  }
208
326
  };
209
327
 
210
- // src/storage.ts
211
- var INSTANCE_ID_KEY = "amplifyup_instance_id";
212
- var USER_ID_KEY = "amplifyup_user_id";
213
- var SESSION_ID_KEY = "amplifyup_session_id";
214
- var SESSION_ACTIVITY_KEY = "amplifyup_session_activity";
215
- var ANON_COOKIE_NAME = "amplifyup_anon_id";
216
- var SESSION_COOKIE_NAME = "amplifyup_session_id";
217
- var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
218
- function generateGuid() {
219
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
220
- return crypto.randomUUID();
221
- }
222
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
223
- const r = Math.random() * 16 | 0;
224
- const v = c === "x" ? r : r & 3 | 8;
225
- return v.toString(16);
226
- });
227
- }
228
- function readCookie(name) {
229
- if (typeof document === "undefined") return null;
230
- const parts = document.cookie.split(";");
231
- for (const part of parts) {
232
- const idx = part.indexOf("=");
233
- if (idx === -1) continue;
234
- if (part.slice(0, idx).trim() !== name) continue;
235
- const value = part.slice(idx + 1).trim();
236
- try {
237
- return value ? decodeURIComponent(value) : null;
238
- } catch {
239
- return value || null;
240
- }
241
- }
242
- return null;
243
- }
244
- function writeCookie(name, value, maxAgeSeconds = 31536e3) {
245
- if (typeof document === "undefined") return;
246
- document.cookie = `${name}=${encodeURIComponent(value)}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
247
- }
248
- function getInstanceId() {
249
- if (typeof window === "undefined") {
250
- return "";
251
- }
252
- const fromCookie = readCookie(ANON_COOKIE_NAME);
253
- const fromLs = localStorage.getItem(INSTANCE_ID_KEY);
254
- const instanceId = fromLs || fromCookie || `browser_${generateGuid()}`;
255
- if (fromLs !== instanceId) {
256
- localStorage.setItem(INSTANCE_ID_KEY, instanceId);
257
- }
258
- if (fromCookie !== instanceId) {
259
- writeCookie(ANON_COOKIE_NAME, instanceId);
260
- }
261
- return instanceId;
262
- }
263
- function getUserId() {
264
- if (typeof window === "undefined") {
265
- return null;
266
- }
267
- return localStorage.getItem(USER_ID_KEY);
268
- }
269
- function getSessionId() {
270
- if (typeof window === "undefined") {
271
- return "";
272
- }
273
- const now = Date.now();
274
- const lastActivity = Number(localStorage.getItem(SESSION_ACTIVITY_KEY) || 0);
275
- const fromCookie = readCookie(SESSION_COOKIE_NAME);
276
- const fromLs = localStorage.getItem(SESSION_ID_KEY);
277
- let sessionId = fromLs || fromCookie || "";
278
- if (!sessionId || lastActivity > 0 && now - lastActivity >= SESSION_IDLE_GAP_MS) {
279
- sessionId = generateGuid();
280
- }
281
- localStorage.setItem(SESSION_ID_KEY, sessionId);
282
- localStorage.setItem(SESSION_ACTIVITY_KEY, String(now));
283
- writeCookie(SESSION_COOKIE_NAME, sessionId, Math.floor(SESSION_IDLE_GAP_MS / 1e3));
284
- return sessionId;
285
- }
286
- function setUserId(userId) {
287
- if (typeof window === "undefined") {
288
- return;
289
- }
290
- localStorage.setItem(USER_ID_KEY, userId);
291
- }
292
- function clearIds() {
293
- if (typeof window === "undefined") {
294
- return;
295
- }
296
- localStorage.removeItem(INSTANCE_ID_KEY);
297
- localStorage.removeItem(USER_ID_KEY);
298
- localStorage.removeItem(SESSION_ID_KEY);
299
- localStorage.removeItem(SESSION_ACTIVITY_KEY);
300
- document.cookie = `${ANON_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
301
- document.cookie = `${SESSION_COOKIE_NAME}=; Path=/; Max-Age=0; SameSite=Lax`;
302
- }
303
-
304
328
  // src/targets.ts
305
329
  var AWS_DEV_API = "https://api-dev.amplifyup.ai";
306
330
  var AWS_PROD_API = "https://api.amplifyup.ai";
@@ -806,6 +830,7 @@ function applyPageContext(input, opts) {
806
830
  }
807
831
  const pageFields = {};
808
832
  for (const pf of opts.pageData?.pageFields || []) {
833
+ if (pf.dataType === "taxonomy") continue;
809
834
  if (pf.source === "static") {
810
835
  if (pf.value !== void 0) pageFields[pf.key] = pf.value;
811
836
  continue;
@@ -828,6 +853,7 @@ function applyPageContext(input, opts) {
828
853
  function staticPageFieldsFromSnapshot(pageData) {
829
854
  const pageFields = {};
830
855
  for (const pf of pageData?.pageFields || []) {
856
+ if (pf.dataType === "taxonomy") continue;
831
857
  if (pf.source === "static" && pf.value !== void 0) {
832
858
  pageFields[pf.key] = pf.value;
833
859
  }
@@ -845,6 +871,11 @@ function attachStaticPageContext(pageData, pageRoute) {
845
871
  pageRoute: pageRoute || ""
846
872
  };
847
873
  }
874
+ function withPageTaxonomies(properties, taxonomies) {
875
+ if (!taxonomies || Object.keys(taxonomies).length === 0) return properties;
876
+ const page = properties.page && typeof properties.page === "object" && !Array.isArray(properties.page) ? properties.page : { fields: {} };
877
+ return { ...properties, page: { ...page, taxonomies } };
878
+ }
848
879
  function mergeEventContext(properties, attached) {
849
880
  if (!attached) return properties;
850
881
  return {
@@ -914,6 +945,27 @@ function hydratePageResourceProps(props, fields) {
914
945
  return out;
915
946
  }
916
947
 
948
+ // src/edge/normalizeRoute.ts
949
+ function normalizeRoute(route) {
950
+ let path = String(route || "").trim();
951
+ if (!path) return "/";
952
+ try {
953
+ if (/^https?:\/\//i.test(path)) {
954
+ path = new URL(path).pathname;
955
+ }
956
+ } catch {
957
+ }
958
+ const q = path.indexOf("?");
959
+ if (q >= 0) path = path.slice(0, q);
960
+ const h = path.indexOf("#");
961
+ if (h >= 0) path = path.slice(0, h);
962
+ if (!path.startsWith("/")) path = `/${path}`;
963
+ if (path.length > 1 && path.endsWith("/")) {
964
+ path = path.replace(/\/+$/, "");
965
+ }
966
+ return path || "/";
967
+ }
968
+
917
969
  // src/page-resource-runtime.ts
918
970
  var pageResourceFields = null;
919
971
  function setPageResourceFields(fields) {
@@ -1007,6 +1059,7 @@ function dedupeLayoutProviderFetch(key, fetchFn) {
1007
1059
  }
1008
1060
 
1009
1061
  // src/index.ts
1062
+ var PAGE_VIEW_HOLD_MS = 1500;
1010
1063
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
1011
1064
  var DWELL_POLL_MS = 5e3;
1012
1065
  function readPreviewQueryOverrides() {
@@ -1080,6 +1133,12 @@ var AmplifyUpSDK = class {
1080
1133
  this.resolvedPageResource = null;
1081
1134
  this.resolvedPageRoute = "";
1082
1135
  this.resolvedRouteParams = {};
1136
+ /** Path whose delivery response is in flight; page_view for it is held. */
1137
+ this.pendingResolvePath = null;
1138
+ this.heldPageView = null;
1139
+ this.resolvedPageTaxonomies = null;
1140
+ /** Path the taxonomies above were resolved for. */
1141
+ this.resolvedTaxonomiesPath = null;
1083
1142
  this.historyHooksInstalled = false;
1084
1143
  this.pageEngagementState = {
1085
1144
  reached: /* @__PURE__ */ new Set(),
@@ -1610,8 +1669,10 @@ var AmplifyUpSDK = class {
1610
1669
  * Apply published pageData (from /v1/resolve) so field validation and
1611
1670
  * mapped page fields can run.
1612
1671
  */
1613
- applyResolvedPageData(pageData, pageRoute, routeParams, pageResources) {
1672
+ applyResolvedPageData(pageData, pageRoute, routeParams, pageResources, pageTaxonomies) {
1614
1673
  this.resolvedPageData = pageData || null;
1674
+ this.resolvedPageTaxonomies = pageTaxonomies || null;
1675
+ this.resolvedTaxonomiesPath = this.pendingResolvePath;
1615
1676
  if (pageRoute) this.resolvedPageRoute = pageRoute;
1616
1677
  if (routeParams) this.resolvedRouteParams = routeParams;
1617
1678
  const firstResourceName = pageData?.resources?.[0]?.name;
@@ -1645,6 +1706,28 @@ var AmplifyUpSDK = class {
1645
1706
  getAttachedPageContext() {
1646
1707
  return this.attachedPageContext;
1647
1708
  }
1709
+ /**
1710
+ * The page's delivery response for `path` is on its way. A page_view for the
1711
+ * same path waits for it (up to PAGE_VIEW_HOLD_MS) so it carries the page's
1712
+ * taxonomies.
1713
+ */
1714
+ beginPageResolve(path) {
1715
+ this.pendingResolvePath = normalizeRoute(path);
1716
+ this.resolvedPageTaxonomies = null;
1717
+ this.resolvedTaxonomiesPath = null;
1718
+ }
1719
+ endPageResolve(path) {
1720
+ if (this.pendingResolvePath !== normalizeRoute(path)) return;
1721
+ this.pendingResolvePath = null;
1722
+ this.flushHeldPageView();
1723
+ }
1724
+ flushHeldPageView() {
1725
+ const held = this.heldPageView;
1726
+ if (!held) return;
1727
+ clearTimeout(held.timer);
1728
+ this.heldPageView = null;
1729
+ this.sendPageView(held.url, held.properties);
1730
+ }
1648
1731
  /**
1649
1732
  * Track a page view
1650
1733
  */
@@ -1655,6 +1738,21 @@ var AmplifyUpSDK = class {
1655
1738
  if (!this.isInitialized()) {
1656
1739
  return;
1657
1740
  }
1741
+ this.lastTrackedPath = url;
1742
+ if (this.pendingResolvePath && this.pendingResolvePath === normalizeRoute(url)) {
1743
+ this.flushHeldPageView();
1744
+ this.heldPageView = {
1745
+ url,
1746
+ properties,
1747
+ timer: setTimeout(() => this.flushHeldPageView(), PAGE_VIEW_HOLD_MS)
1748
+ };
1749
+ return;
1750
+ }
1751
+ this.flushHeldPageView();
1752
+ this.sendPageView(url, properties);
1753
+ }
1754
+ sendPageView(url, properties) {
1755
+ const taxonomies = this.resolvedTaxonomiesPath && this.resolvedTaxonomiesPath === normalizeRoute(url) ? this.resolvedPageTaxonomies : null;
1658
1756
  const instanceId = getInstanceId();
1659
1757
  const sessionId = getSessionId();
1660
1758
  const pageProperties = {
@@ -1666,10 +1764,9 @@ var AmplifyUpSDK = class {
1666
1764
  sessionId,
1667
1765
  trackingId: this.config?.trackingId,
1668
1766
  // Timestamp is included in event payload
1669
- ...mergeEventContext(properties || {}, this.attachedPageContext)
1767
+ ...withPageTaxonomies(mergeEventContext(properties || {}, this.attachedPageContext), taxonomies)
1670
1768
  };
1671
1769
  this.adapter.page(pageProperties);
1672
- this.lastTrackedPath = url;
1673
1770
  }
1674
1771
  /**
1675
1772
  * Track an event