@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.
@@ -60,7 +60,10 @@ var AWSAdapter = class {
60
60
  headers: {
61
61
  "Content-Type": "application/json"
62
62
  },
63
- body: JSON.stringify(eventData)
63
+ body: JSON.stringify(eventData),
64
+ // Keep the request alive through tab close so the single Time on Page
65
+ // flush on pagehide is not cancelled with the document.
66
+ keepalive: true
64
67
  });
65
68
  if (!response.ok) {
66
69
  if (this.config.debug) {
@@ -348,8 +351,6 @@ function isComposerPreview() {
348
351
 
349
352
  // src/index.ts
350
353
  var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
351
- var DWELL_MILESTONES_SEC = [15, 30, 60, 120, 300];
352
- var DWELL_CHECKPOINT_SEC = 15 * 60;
353
354
  var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
354
355
  var DWELL_POLL_MS = 5e3;
355
356
  function readPreviewQueryOverrides() {
@@ -420,7 +421,8 @@ var AmplifyUpSDK = class {
420
421
  reached: /* @__PURE__ */ new Set(),
421
422
  visibleMs: 0,
422
423
  visibleSince: null,
423
- lastDwellSecondsSent: 0,
424
+ /** True after Time on Page has been sent for the current visit. */
425
+ dwellEmitted: false,
424
426
  /** When the tab last became hidden — used for idle-resume re-page. */
425
427
  hiddenAt: null,
426
428
  /** Last user interaction — freezes dwell when stale while visible. */
@@ -649,9 +651,9 @@ var AmplifyUpSDK = class {
649
651
  }
650
652
  /**
651
653
  * Automatic page-level Scroll Depth + Time on Page.
652
- * Dwell uses milestones / sparse checkpoints (not a fixed 15s heartbeat),
653
- * freezes while the tab is visible but the user is idle, and always flushes
654
- * on hide / unload / SPA route change.
654
+ * Time on Page fires once per visit when the visit ends (route change,
655
+ * tab close, or 30-minute idle resume). Hidden tabs pause the clock;
656
+ * visible-but-idle tabs freeze after DWELL_ACTIVITY_IDLE_MS.
655
657
  */
656
658
  setupPageEngagement() {
657
659
  if (typeof window === "undefined" || typeof document === "undefined") return;
@@ -662,7 +664,7 @@ var AmplifyUpSDK = class {
662
664
  state.reached = /* @__PURE__ */ new Set();
663
665
  state.visibleMs = 0;
664
666
  state.visibleSince = document.visibilityState === "visible" ? Date.now() : null;
665
- state.lastDwellSecondsSent = 0;
667
+ state.dwellEmitted = false;
666
668
  state.hiddenAt = document.visibilityState === "hidden" ? Date.now() : null;
667
669
  state.lastActivityAt = Date.now();
668
670
  state.activityIdle = false;
@@ -724,48 +726,34 @@ var AmplifyUpSDK = class {
724
726
  }
725
727
  }
726
728
  };
727
- const nextEmitTarget = (lastSent) => {
728
- for (const m of DWELL_MILESTONES_SEC) {
729
- if (m > lastSent) return { seconds: m, reason: "milestone" };
730
- }
731
- const nextCheckpoint = Math.floor(lastSent / DWELL_CHECKPOINT_SEC) * DWELL_CHECKPOINT_SEC + DWELL_CHECKPOINT_SEC;
732
- const target = nextCheckpoint > lastSent ? nextCheckpoint : lastSent + DWELL_CHECKPOINT_SEC;
733
- return { seconds: target, reason: "checkpoint" };
734
- };
735
- const emitDwell = (reason, force = false) => {
729
+ const emitDwell = (reason) => {
736
730
  if (this.isPreviewMode()) return;
731
+ if (state.dwellEmitted) return;
737
732
  if (!state.activityIdle) {
738
733
  accumulateVisible();
739
734
  }
740
735
  const seconds = Math.floor(state.visibleMs / 1e3);
741
736
  if (seconds <= 0) return;
742
- if (seconds <= state.lastDwellSecondsSent) return;
743
- if (!force) {
744
- const target = nextEmitTarget(state.lastDwellSecondsSent);
745
- if (!target || seconds < target.seconds) return;
746
- reason = target.reason;
747
- }
748
- state.lastDwellSecondsSent = seconds;
737
+ state.dwellEmitted = true;
749
738
  this.track("Time on Page", {
750
739
  seconds,
751
740
  path: this.lastTrackedPath || path(),
752
741
  reason
753
742
  });
754
743
  };
755
- const evaluateDwell = () => {
744
+ const evaluateIdle = () => {
756
745
  if (document.visibilityState !== "visible") return;
757
746
  if (Date.now() - state.lastActivityAt >= DWELL_ACTIVITY_IDLE_MS) {
758
747
  freezeActivityIdle();
759
- return;
760
748
  }
761
- emitDwell("milestone", false);
762
749
  };
763
- this._emitPageDwell = (force) => emitDwell("route", force !== false);
750
+ this._emitPageDwell = () => emitDwell("route");
764
751
  const onVisibility = () => {
765
752
  if (document.visibilityState === "visible") {
766
753
  const idleMs = state.hiddenAt != null ? Date.now() - state.hiddenAt : 0;
767
754
  state.hiddenAt = null;
768
755
  if (idleMs >= SESSION_IDLE_GAP_MS) {
756
+ emitDwell("idle-resume");
769
757
  this.resetEngagementQuiet();
770
758
  const resumePath = path();
771
759
  this.page(resumePath, { resumed: true });
@@ -782,14 +770,14 @@ var AmplifyUpSDK = class {
782
770
  state.visibleSince = Date.now();
783
771
  state.activityIdle = false;
784
772
  } else {
785
- emitDwell("hidden", true);
773
+ accumulateVisible();
786
774
  state.hiddenAt = Date.now();
787
775
  state.visibleSince = null;
788
776
  state.activityIdle = false;
789
777
  }
790
778
  };
791
779
  const onPageHide = () => {
792
- emitDwell("unload", true);
780
+ emitDwell("unload");
793
781
  if (state.hiddenAt == null) {
794
782
  state.hiddenAt = Date.now();
795
783
  }
@@ -810,10 +798,10 @@ var AmplifyUpSDK = class {
810
798
  window.addEventListener(evt, onActivity, { passive: true });
811
799
  }
812
800
  checkScroll();
813
- const heartbeat = setInterval(() => {
814
- evaluateDwell();
801
+ const idlePoll = setInterval(() => {
802
+ evaluateIdle();
815
803
  }, DWELL_POLL_MS);
816
- this.trackingIntervals.push(heartbeat);
804
+ this.trackingIntervals.push(idlePoll);
817
805
  const previousCleanup = this.trackingCleanup;
818
806
  this.trackingCleanup = () => {
819
807
  window.removeEventListener("scroll", onScroll);
@@ -841,12 +829,12 @@ var AmplifyUpSDK = class {
841
829
  emit?.(true);
842
830
  this.resetEngagementQuiet();
843
831
  }
844
- /** Reset dwell/scroll state without emitting (idle resume already flushed on hide). */
832
+ /** Reset dwell/scroll state without emitting (idle resume already flushed). */
845
833
  resetEngagementQuiet() {
846
834
  const state = this.pageEngagementState;
847
835
  state.reached = /* @__PURE__ */ new Set();
848
836
  state.visibleMs = 0;
849
- state.lastDwellSecondsSent = 0;
837
+ state.dwellEmitted = false;
850
838
  state.hiddenAt = null;
851
839
  state.lastActivityAt = Date.now();
852
840
  state.activityIdle = false;
@@ -1624,6 +1612,12 @@ Available Console Commands:
1624
1612
  return;
1625
1613
  }
1626
1614
  try {
1615
+ if (event.data?.type === "AMPLIFYUP_FIELD_PATCH") {
1616
+ window.dispatchEvent(
1617
+ new CustomEvent("amplifyup:field-patch", { detail: event.data })
1618
+ );
1619
+ return;
1620
+ }
1627
1621
  if (event.data?.type === "COMPONENT_DELETABLE_STATUS") {
1628
1622
  const { componentId, deletable, nodeId } = event.data;
1629
1623
  const element = document.querySelector(`[data-component-id="${componentId}"]`);
@@ -1810,6 +1804,10 @@ Available Console Commands:
1810
1804
  setEventSelectionMode(enabled) {
1811
1805
  if (typeof document === "undefined") return;
1812
1806
  this.eventSelectionMode = enabled;
1807
+ if (typeof window !== "undefined") {
1808
+ window.__AMPLIFYUP_EVENT_SELECTION__ = enabled;
1809
+ window.dispatchEvent(new CustomEvent("amplifyup:composer-edit"));
1810
+ }
1813
1811
  if (this.eventSelectionClickHandler) {
1814
1812
  document.removeEventListener("click", this.eventSelectionClickHandler, true);
1815
1813
  this.eventSelectionClickHandler = null;
@@ -2047,6 +2045,14 @@ Available Console Commands:
2047
2045
  el.classList.add("amplifyup-selected");
2048
2046
  }
2049
2047
  });
2048
+ if (typeof window !== "undefined") {
2049
+ window.__AMPLIFYUP_COMPOSER_EDIT__ = highlightComponents;
2050
+ window.dispatchEvent(
2051
+ new CustomEvent("amplifyup:composer-edit", {
2052
+ detail: { enabled: highlightComponents }
2053
+ })
2054
+ );
2055
+ }
2050
2056
  }
2051
2057
  /** Document-level drag/drop for cross-iframe drags from Composer sidebar. */
2052
2058
  setupComposerDragDrop() {
@@ -2355,6 +2361,12 @@ Available Console Commands:
2355
2361
  const clearSelection = opts?.clearSelection !== false;
2356
2362
  this.highlightingEnabled = false;
2357
2363
  this.teardownComposerDragDrop();
2364
+ if (opts?.clearSelection !== false && typeof window !== "undefined") {
2365
+ window.__AMPLIFYUP_COMPOSER_EDIT__ = false;
2366
+ window.dispatchEvent(
2367
+ new CustomEvent("amplifyup:composer-edit", { detail: { enabled: false } })
2368
+ );
2369
+ }
2358
2370
  if (this.mutationObserver) {
2359
2371
  this.mutationObserver.disconnect();
2360
2372
  this.mutationObserver = null;