@amplifyup/sdk 0.1.25 → 0.1.27

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
@@ -47,7 +47,10 @@ var AWSAdapter = class {
47
47
  headers: {
48
48
  "Content-Type": "application/json"
49
49
  },
50
- body: JSON.stringify(eventData)
50
+ body: JSON.stringify(eventData),
51
+ // Keep the request alive through tab close so the single Time on Page
52
+ // flush on pagehide is not cancelled with the document.
53
+ keepalive: true
51
54
  });
52
55
  if (!response.ok) {
53
56
  if (this.config.debug) {
@@ -506,8 +509,6 @@ async function fetchContent(options) {
506
509
 
507
510
  // src/index.ts
508
511
  var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
509
- var DWELL_MILESTONES_SEC = [15, 30, 60, 120, 300];
510
- var DWELL_CHECKPOINT_SEC = 15 * 60;
511
512
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
512
513
  var DWELL_POLL_MS = 5e3;
513
514
  function readPreviewQueryOverrides() {
@@ -578,7 +579,8 @@ var AmplifyUpSDK = class {
578
579
  reached: /* @__PURE__ */ new Set(),
579
580
  visibleMs: 0,
580
581
  visibleSince: null,
581
- lastDwellSecondsSent: 0,
582
+ /** True after Time on Page has been sent for the current visit. */
583
+ dwellEmitted: false,
582
584
  /** When the tab last became hidden — used for idle-resume re-page. */
583
585
  hiddenAt: null,
584
586
  /** Last user interaction — freezes dwell when stale while visible. */
@@ -807,9 +809,9 @@ var AmplifyUpSDK = class {
807
809
  }
808
810
  /**
809
811
  * Automatic page-level Scroll Depth + Time on Page.
810
- * Dwell uses milestones / sparse checkpoints (not a fixed 15s heartbeat),
811
- * freezes while the tab is visible but the user is idle, and always flushes
812
- * on hide / unload / SPA route change.
812
+ * Time on Page fires once per visit when the visit ends (route change,
813
+ * tab close, or 30-minute idle resume). Hidden tabs pause the clock;
814
+ * visible-but-idle tabs freeze after DWELL_ACTIVITY_IDLE_MS.
813
815
  */
814
816
  setupPageEngagement() {
815
817
  if (typeof window === "undefined" || typeof document === "undefined") return;
@@ -820,7 +822,7 @@ var AmplifyUpSDK = class {
820
822
  state.reached = /* @__PURE__ */ new Set();
821
823
  state.visibleMs = 0;
822
824
  state.visibleSince = document.visibilityState === "visible" ? Date.now() : null;
823
- state.lastDwellSecondsSent = 0;
825
+ state.dwellEmitted = false;
824
826
  state.hiddenAt = document.visibilityState === "hidden" ? Date.now() : null;
825
827
  state.lastActivityAt = Date.now();
826
828
  state.activityIdle = false;
@@ -882,48 +884,34 @@ var AmplifyUpSDK = class {
882
884
  }
883
885
  }
884
886
  };
885
- const nextEmitTarget = (lastSent) => {
886
- for (const m of DWELL_MILESTONES_SEC) {
887
- if (m > lastSent) return { seconds: m, reason: "milestone" };
888
- }
889
- const nextCheckpoint = Math.floor(lastSent / DWELL_CHECKPOINT_SEC) * DWELL_CHECKPOINT_SEC + DWELL_CHECKPOINT_SEC;
890
- const target = nextCheckpoint > lastSent ? nextCheckpoint : lastSent + DWELL_CHECKPOINT_SEC;
891
- return { seconds: target, reason: "checkpoint" };
892
- };
893
- const emitDwell = (reason, force = false) => {
887
+ const emitDwell = (reason) => {
894
888
  if (this.isPreviewMode()) return;
889
+ if (state.dwellEmitted) return;
895
890
  if (!state.activityIdle) {
896
891
  accumulateVisible();
897
892
  }
898
893
  const seconds = Math.floor(state.visibleMs / 1e3);
899
894
  if (seconds <= 0) return;
900
- if (seconds <= state.lastDwellSecondsSent) return;
901
- if (!force) {
902
- const target = nextEmitTarget(state.lastDwellSecondsSent);
903
- if (!target || seconds < target.seconds) return;
904
- reason = target.reason;
905
- }
906
- state.lastDwellSecondsSent = seconds;
895
+ state.dwellEmitted = true;
907
896
  this.track("Time on Page", {
908
897
  seconds,
909
898
  path: this.lastTrackedPath || path(),
910
899
  reason
911
900
  });
912
901
  };
913
- const evaluateDwell = () => {
902
+ const evaluateIdle = () => {
914
903
  if (document.visibilityState !== "visible") return;
915
904
  if (Date.now() - state.lastActivityAt >= DWELL_ACTIVITY_IDLE_MS) {
916
905
  freezeActivityIdle();
917
- return;
918
906
  }
919
- emitDwell("milestone", false);
920
907
  };
921
- this._emitPageDwell = (force) => emitDwell("route", force !== false);
908
+ this._emitPageDwell = () => emitDwell("route");
922
909
  const onVisibility = () => {
923
910
  if (document.visibilityState === "visible") {
924
911
  const idleMs = state.hiddenAt != null ? Date.now() - state.hiddenAt : 0;
925
912
  state.hiddenAt = null;
926
913
  if (idleMs >= SESSION_IDLE_GAP_MS) {
914
+ emitDwell("idle-resume");
927
915
  this.resetEngagementQuiet();
928
916
  const resumePath = path();
929
917
  this.page(resumePath, { resumed: true });
@@ -940,14 +928,14 @@ var AmplifyUpSDK = class {
940
928
  state.visibleSince = Date.now();
941
929
  state.activityIdle = false;
942
930
  } else {
943
- emitDwell("hidden", true);
931
+ accumulateVisible();
944
932
  state.hiddenAt = Date.now();
945
933
  state.visibleSince = null;
946
934
  state.activityIdle = false;
947
935
  }
948
936
  };
949
937
  const onPageHide = () => {
950
- emitDwell("unload", true);
938
+ emitDwell("unload");
951
939
  if (state.hiddenAt == null) {
952
940
  state.hiddenAt = Date.now();
953
941
  }
@@ -968,10 +956,10 @@ var AmplifyUpSDK = class {
968
956
  window.addEventListener(evt, onActivity, { passive: true });
969
957
  }
970
958
  checkScroll();
971
- const heartbeat = setInterval(() => {
972
- evaluateDwell();
959
+ const idlePoll = setInterval(() => {
960
+ evaluateIdle();
973
961
  }, DWELL_POLL_MS);
974
- this.trackingIntervals.push(heartbeat);
962
+ this.trackingIntervals.push(idlePoll);
975
963
  const previousCleanup = this.trackingCleanup;
976
964
  this.trackingCleanup = () => {
977
965
  window.removeEventListener("scroll", onScroll);
@@ -999,12 +987,12 @@ var AmplifyUpSDK = class {
999
987
  emit?.(true);
1000
988
  this.resetEngagementQuiet();
1001
989
  }
1002
- /** Reset dwell/scroll state without emitting (idle resume already flushed on hide). */
990
+ /** Reset dwell/scroll state without emitting (idle resume already flushed). */
1003
991
  resetEngagementQuiet() {
1004
992
  const state = this.pageEngagementState;
1005
993
  state.reached = /* @__PURE__ */ new Set();
1006
994
  state.visibleMs = 0;
1007
- state.lastDwellSecondsSent = 0;
995
+ state.dwellEmitted = false;
1008
996
  state.hiddenAt = null;
1009
997
  state.lastActivityAt = Date.now();
1010
998
  state.activityIdle = false;
@@ -1782,6 +1770,12 @@ Available Console Commands:
1782
1770
  return;
1783
1771
  }
1784
1772
  try {
1773
+ if (event.data?.type === "AMPLIFYUP_FIELD_PATCH") {
1774
+ window.dispatchEvent(
1775
+ new CustomEvent("amplifyup:field-patch", { detail: event.data })
1776
+ );
1777
+ return;
1778
+ }
1785
1779
  if (event.data?.type === "COMPONENT_DELETABLE_STATUS") {
1786
1780
  const { componentId, deletable, nodeId } = event.data;
1787
1781
  const element = document.querySelector(`[data-component-id="${componentId}"]`);
@@ -1968,6 +1962,10 @@ Available Console Commands:
1968
1962
  setEventSelectionMode(enabled) {
1969
1963
  if (typeof document === "undefined") return;
1970
1964
  this.eventSelectionMode = enabled;
1965
+ if (typeof window !== "undefined") {
1966
+ window.__AMPLIFYUP_EVENT_SELECTION__ = enabled;
1967
+ window.dispatchEvent(new CustomEvent("amplifyup:composer-edit"));
1968
+ }
1971
1969
  if (this.eventSelectionClickHandler) {
1972
1970
  document.removeEventListener("click", this.eventSelectionClickHandler, true);
1973
1971
  this.eventSelectionClickHandler = null;
@@ -2205,6 +2203,14 @@ Available Console Commands:
2205
2203
  el.classList.add("amplifyup-selected");
2206
2204
  }
2207
2205
  });
2206
+ if (typeof window !== "undefined") {
2207
+ window.__AMPLIFYUP_COMPOSER_EDIT__ = highlightComponents;
2208
+ window.dispatchEvent(
2209
+ new CustomEvent("amplifyup:composer-edit", {
2210
+ detail: { enabled: highlightComponents }
2211
+ })
2212
+ );
2213
+ }
2208
2214
  }
2209
2215
  /** Document-level drag/drop for cross-iframe drags from Composer sidebar. */
2210
2216
  setupComposerDragDrop() {
@@ -2513,6 +2519,12 @@ Available Console Commands:
2513
2519
  const clearSelection = opts?.clearSelection !== false;
2514
2520
  this.highlightingEnabled = false;
2515
2521
  this.teardownComposerDragDrop();
2522
+ if (opts?.clearSelection !== false && typeof window !== "undefined") {
2523
+ window.__AMPLIFYUP_COMPOSER_EDIT__ = false;
2524
+ window.dispatchEvent(
2525
+ new CustomEvent("amplifyup:composer-edit", { detail: { enabled: false } })
2526
+ );
2527
+ }
2516
2528
  if (this.mutationObserver) {
2517
2529
  this.mutationObserver.disconnect();
2518
2530
  this.mutationObserver = null;
@@ -2757,6 +2769,6 @@ var verifyAWSConnection = () => {
2757
2769
  };
2758
2770
  var index_default = getSDK();
2759
2771
 
2760
- export { AmplifyUpSDK, DWELL_ACTIVITY_IDLE_MS, DWELL_CHECKPOINT_SEC, DWELL_MILESTONES_SEC, EdgeResolveError, SESSION_IDLE_GAP_MS, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchContent, fetchEdgeResolve, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, mapResolvedToPageConfig, normalizeAmplifyUpTarget, normalizeRoute, page, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, routeChange, setProfileFields, setupComponentViewTracking, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
2772
+ export { AmplifyUpSDK, DWELL_ACTIVITY_IDLE_MS, EdgeResolveError, SESSION_IDLE_GAP_MS, buildLayoutProviderUrl, buildPageRenderUrl, cleanupEventTracking, index_default as default, fetchContent, fetchEdgeResolve, fetchPageConfig, getDiagnostics, getInstanceId2 as getInstanceId, getOrchestratorUrl, getSDK, getStatus, getTarget, getTrackingId, getUserId2 as getUserId, identify, init, isAmplifyUpTarget, isComposerPreview, isPreviewRequest, mapResolvedToPageConfig, normalizeAmplifyUpTarget, normalizeRoute, page, refreshEventTracking, reset, resolveAmplifyUpTarget, resolveAmplifyUpUrls, routeChange, setProfileFields, setupComponentViewTracking, setupEventTracking, testTracking, track, trackClick, trackContentEngagement, trackContentView, trackFormFieldInteraction, trackFormSubmit, verifyAWSConnection };
2761
2773
  //# sourceMappingURL=index.mjs.map
2762
2774
  //# sourceMappingURL=index.mjs.map