@amplifyup/sdk 0.1.24 → 0.1.25

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/react.js CHANGED
@@ -519,6 +519,10 @@ async function fetchContent(options) {
519
519
 
520
520
  // src/index.ts
521
521
  var SESSION_IDLE_GAP_MS = 30 * 60 * 1e3;
522
+ var DWELL_MILESTONES_SEC = [15, 30, 60, 120, 300];
523
+ var DWELL_CHECKPOINT_SEC = 15 * 60;
524
+ var DWELL_ACTIVITY_IDLE_MS = 2 * 60 * 1e3;
525
+ var DWELL_POLL_MS = 5e3;
522
526
  function readPreviewQueryOverrides() {
523
527
  if (typeof window === "undefined") return {};
524
528
  const params = new URLSearchParams(window.location.search);
@@ -589,7 +593,10 @@ var AmplifyUpSDK = class {
589
593
  visibleSince: null,
590
594
  lastDwellSecondsSent: 0,
591
595
  /** When the tab last became hidden — used for idle-resume re-page. */
592
- hiddenAt: null
596
+ hiddenAt: null,
597
+ /** Last user interaction — freezes dwell when stale while visible. */
598
+ lastActivityAt: 0,
599
+ activityIdle: false
593
600
  };
594
601
  this.adapter = adapter || new AWSAdapter();
595
602
  if (typeof window !== "undefined") {
@@ -812,8 +819,10 @@ var AmplifyUpSDK = class {
812
819
  return window.location.pathname || "/";
813
820
  }
814
821
  /**
815
- * Automatic page-level Scroll Depth + Time on Page (dwell heartbeat).
816
- * State lives on the instance so SPA route changes can reset milestones.
822
+ * Automatic page-level Scroll Depth + Time on Page.
823
+ * Dwell uses milestones / sparse checkpoints (not a fixed 15s heartbeat),
824
+ * freezes while the tab is visible but the user is idle, and always flushes
825
+ * on hide / unload / SPA route change.
817
826
  */
818
827
  setupPageEngagement() {
819
828
  if (typeof window === "undefined" || typeof document === "undefined") return;
@@ -826,6 +835,8 @@ var AmplifyUpSDK = class {
826
835
  state.visibleSince = document.visibilityState === "visible" ? Date.now() : null;
827
836
  state.lastDwellSecondsSent = 0;
828
837
  state.hiddenAt = document.visibilityState === "hidden" ? Date.now() : null;
838
+ state.lastActivityAt = Date.now();
839
+ state.activityIdle = false;
829
840
  let scrollRaf = null;
830
841
  const path = () => this.currentPath();
831
842
  const measureScrollPercent = () => {
@@ -857,6 +868,7 @@ var AmplifyUpSDK = class {
857
868
  }
858
869
  };
859
870
  const onScroll = () => {
871
+ noteActivity();
860
872
  if (scrollRaf != null) return;
861
873
  scrollRaf = window.requestAnimationFrame(() => {
862
874
  scrollRaf = null;
@@ -868,19 +880,58 @@ var AmplifyUpSDK = class {
868
880
  state.visibleMs += Date.now() - state.visibleSince;
869
881
  state.visibleSince = Date.now();
870
882
  };
871
- const emitDwell = (_force = false) => {
872
- if (this.isPreviewMode()) return;
883
+ const freezeActivityIdle = () => {
884
+ if (state.activityIdle) return;
873
885
  accumulateVisible();
886
+ state.visibleSince = null;
887
+ state.activityIdle = true;
888
+ };
889
+ const noteActivity = () => {
890
+ state.lastActivityAt = Date.now();
891
+ if (state.activityIdle) {
892
+ state.activityIdle = false;
893
+ if (document.visibilityState === "visible" && state.visibleSince == null) {
894
+ state.visibleSince = Date.now();
895
+ }
896
+ }
897
+ };
898
+ const nextEmitTarget = (lastSent) => {
899
+ for (const m of DWELL_MILESTONES_SEC) {
900
+ if (m > lastSent) return { seconds: m, reason: "milestone" };
901
+ }
902
+ const nextCheckpoint = Math.floor(lastSent / DWELL_CHECKPOINT_SEC) * DWELL_CHECKPOINT_SEC + DWELL_CHECKPOINT_SEC;
903
+ const target = nextCheckpoint > lastSent ? nextCheckpoint : lastSent + DWELL_CHECKPOINT_SEC;
904
+ return { seconds: target, reason: "checkpoint" };
905
+ };
906
+ const emitDwell = (reason, force = false) => {
907
+ if (this.isPreviewMode()) return;
908
+ if (!state.activityIdle) {
909
+ accumulateVisible();
910
+ }
874
911
  const seconds = Math.floor(state.visibleMs / 1e3);
875
912
  if (seconds <= 0) return;
876
913
  if (seconds <= state.lastDwellSecondsSent) return;
914
+ if (!force) {
915
+ const target = nextEmitTarget(state.lastDwellSecondsSent);
916
+ if (!target || seconds < target.seconds) return;
917
+ reason = target.reason;
918
+ }
877
919
  state.lastDwellSecondsSent = seconds;
878
920
  this.track("Time on Page", {
879
921
  seconds,
880
- path: this.lastTrackedPath || path()
922
+ path: this.lastTrackedPath || path(),
923
+ reason
881
924
  });
882
925
  };
883
- this._emitPageDwell = emitDwell;
926
+ const evaluateDwell = () => {
927
+ if (document.visibilityState !== "visible") return;
928
+ if (Date.now() - state.lastActivityAt >= DWELL_ACTIVITY_IDLE_MS) {
929
+ freezeActivityIdle();
930
+ return;
931
+ }
932
+ emitDwell("milestone", false);
933
+ };
934
+ this._emitPageDwell = (force) => emitDwell("route", force !== false);
884
935
  const onVisibility = () => {
885
936
  if (document.visibilityState === "visible") {
886
937
  const idleMs = state.hiddenAt != null ? Date.now() - state.hiddenAt : 0;
@@ -898,35 +949,51 @@ var AmplifyUpSDK = class {
898
949
  );
899
950
  }
900
951
  }
952
+ noteActivity();
901
953
  state.visibleSince = Date.now();
954
+ state.activityIdle = false;
902
955
  } else {
903
- emitDwell(true);
956
+ emitDwell("hidden", true);
904
957
  state.hiddenAt = Date.now();
905
958
  state.visibleSince = null;
959
+ state.activityIdle = false;
906
960
  }
907
961
  };
908
962
  const onPageHide = () => {
909
- emitDwell(true);
963
+ emitDwell("unload", true);
910
964
  if (state.hiddenAt == null) {
911
965
  state.hiddenAt = Date.now();
912
966
  }
913
967
  state.visibleSince = null;
914
968
  };
969
+ const activityEvents = [
970
+ "pointerdown",
971
+ "keydown",
972
+ "scroll",
973
+ "touchstart"
974
+ ];
975
+ const onActivity = () => noteActivity();
915
976
  window.addEventListener("scroll", onScroll, { passive: true });
916
977
  document.addEventListener("visibilitychange", onVisibility);
917
978
  window.addEventListener("pagehide", onPageHide);
979
+ for (const evt of activityEvents) {
980
+ if (evt === "scroll") continue;
981
+ window.addEventListener(evt, onActivity, { passive: true });
982
+ }
918
983
  checkScroll();
919
984
  const heartbeat = setInterval(() => {
920
- if (document.visibilityState === "visible") {
921
- emitDwell(false);
922
- }
923
- }, 15e3);
985
+ evaluateDwell();
986
+ }, DWELL_POLL_MS);
924
987
  this.trackingIntervals.push(heartbeat);
925
988
  const previousCleanup = this.trackingCleanup;
926
989
  this.trackingCleanup = () => {
927
990
  window.removeEventListener("scroll", onScroll);
928
991
  document.removeEventListener("visibilitychange", onVisibility);
929
992
  window.removeEventListener("pagehide", onPageHide);
993
+ for (const evt of activityEvents) {
994
+ if (evt === "scroll") continue;
995
+ window.removeEventListener(evt, onActivity);
996
+ }
930
997
  if (scrollRaf != null) {
931
998
  window.cancelAnimationFrame(scrollRaf);
932
999
  scrollRaf = null;
@@ -952,6 +1019,8 @@ var AmplifyUpSDK = class {
952
1019
  state.visibleMs = 0;
953
1020
  state.lastDwellSecondsSent = 0;
954
1021
  state.hiddenAt = null;
1022
+ state.lastActivityAt = Date.now();
1023
+ state.activityIdle = false;
955
1024
  state.visibleSince = typeof document !== "undefined" && document.visibilityState === "visible" ? Date.now() : null;
956
1025
  this.componentViewSeen.clear();
957
1026
  }