@magicfeedback/popup-sdk 1.20.0 → 1.21.1

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.
Files changed (31) hide show
  1. package/dist/{chunk-ZJ5HG3QF.mjs → chunk-4C3DSMUT.mjs} +2 -2
  2. package/dist/{chunk-OPR5I7WO.mjs → chunk-7KMBH25G.mjs} +2 -2
  3. package/dist/{chunk-FROREHRG.mjs → chunk-NNWYOG43.mjs} +2 -2
  4. package/dist/index.d.mts +38 -8
  5. package/dist/index.d.ts +38 -8
  6. package/dist/index.js +100 -29
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.mjs +102 -31
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/index.native.d.mts +38 -8
  11. package/dist/index.native.d.ts +38 -8
  12. package/dist/index.native.js +117 -39
  13. package/dist/index.native.js.map +1 -1
  14. package/dist/index.native.mjs +119 -41
  15. package/dist/index.native.mjs.map +1 -1
  16. package/dist/react-native.d.mts +32 -8
  17. package/dist/react-native.d.ts +32 -8
  18. package/dist/react-native.js +117 -39
  19. package/dist/react-native.js.map +1 -1
  20. package/dist/react-native.mjs +119 -41
  21. package/dist/react-native.mjs.map +1 -1
  22. package/dist/{renderPopup-I6CPIR75.mjs → renderPopup-IKPXPDB7.mjs} +2 -2
  23. package/dist/{renderPopup-RUY3OBYV.mjs → renderPopup-M5ZIXNV6.mjs} +2 -2
  24. package/dist/{renderPopup-XNVNP2ZU.mjs → renderPopup-QVMBE4L4.mjs} +2 -2
  25. package/package.json +1 -1
  26. /package/dist/{chunk-ZJ5HG3QF.mjs.map → chunk-4C3DSMUT.mjs.map} +0 -0
  27. /package/dist/{chunk-OPR5I7WO.mjs.map → chunk-7KMBH25G.mjs.map} +0 -0
  28. /package/dist/{chunk-FROREHRG.mjs.map → chunk-NNWYOG43.mjs.map} +0 -0
  29. /package/dist/{renderPopup-I6CPIR75.mjs.map → renderPopup-IKPXPDB7.mjs.map} +0 -0
  30. /package/dist/{renderPopup-RUY3OBYV.mjs.map → renderPopup-M5ZIXNV6.mjs.map} +0 -0
  31. /package/dist/{renderPopup-XNVNP2ZU.mjs.map → renderPopup-QVMBE4L4.mjs.map} +0 -0
@@ -29,7 +29,7 @@ import {
29
29
  style_default,
30
30
  surveySdkCssUrls,
31
31
  surveySdkUrls
32
- } from "./chunk-ZJ5HG3QF.mjs";
32
+ } from "./chunk-4C3DSMUT.mjs";
33
33
  import "./chunk-3QS3WKRC.mjs";
34
34
 
35
35
  // src/index.native.ts
@@ -66,13 +66,26 @@ function resolveBackgroundTask(explicit, platform) {
66
66
  }
67
67
  var DEFAULT_REJECTION_WHITELIST = [ReferenceError, TypeError, RangeError];
68
68
  function createHermesRejectionHub(hermes, options) {
69
- if (!hermes || typeof hermes.enablePromiseRejectionTracker !== "function") return null;
70
- const original = hermes.enablePromiseRejectionTracker.bind(hermes);
69
+ let original;
70
+ try {
71
+ if (!hermes || typeof hermes.enablePromiseRejectionTracker !== "function") return null;
72
+ original = hermes.enablePromiseRejectionTracker.bind(hermes);
73
+ } catch {
74
+ return null;
75
+ }
71
76
  const listeners = /* @__PURE__ */ new Set();
72
77
  let hostOptions = null;
73
78
  let active = false;
74
79
  const forwardsToHost = (error) => !!hostOptions?.onUnhandled && (hostOptions.allRejections === true || DEFAULT_REJECTION_WHITELIST.some((type) => error instanceof type));
75
80
  const apply = () => {
81
+ try {
82
+ install();
83
+ return true;
84
+ } catch {
85
+ return false;
86
+ }
87
+ };
88
+ const install = () => {
76
89
  original({
77
90
  allRejections: true,
78
91
  onUnhandled: (id, error) => {
@@ -91,17 +104,17 @@ function createHermesRejectionHub(hermes, options) {
91
104
  onHandled: (id, error) => hostOptions?.onHandled?.(id, error)
92
105
  });
93
106
  };
94
- hermes.enablePromiseRejectionTracker = (next) => {
107
+ const wrapper = (next) => {
95
108
  hostOptions = next ?? null;
96
- if (active) apply();
109
+ if (active) install();
97
110
  else original(next);
98
111
  };
112
+ if (!replaceTracker(hermes, wrapper)) return null;
99
113
  return {
100
114
  addUnhandledListener(listener) {
101
115
  listeners.add(listener);
102
116
  if (!active) {
103
- active = true;
104
- apply();
117
+ active = apply();
105
118
  }
106
119
  return () => {
107
120
  listeners.delete(listener);
@@ -109,22 +122,44 @@ function createHermesRejectionHub(hermes, options) {
109
122
  }
110
123
  };
111
124
  }
125
+ var TRACKER_KEY = "enablePromiseRejectionTracker";
126
+ function replaceTracker(hermes, wrapper) {
127
+ try {
128
+ const own = Object.getOwnPropertyDescriptor(hermes, TRACKER_KEY);
129
+ if (own && own.writable === false && !own.set) {
130
+ if (!own.configurable) return false;
131
+ Object.defineProperty(hermes, TRACKER_KEY, { ...own, value: wrapper });
132
+ } else {
133
+ hermes.enablePromiseRejectionTracker = wrapper;
134
+ }
135
+ return hermes.enablePromiseRejectionTracker === wrapper;
136
+ } catch {
137
+ return false;
138
+ }
139
+ }
112
140
 
113
141
  // src/platform/react-native-defaults.ts
114
142
  var hub;
115
143
  function registerReactNativeDefaults(rn) {
116
144
  if (hub === void 0) {
117
- hub = getReactNativePlatform()?.promiseRejections ?? createHermesRejectionHub(
118
- globalThis.HermesInternal,
119
- { dev: typeof __DEV__ !== "undefined" && !!__DEV__ }
120
- );
145
+ try {
146
+ hub = getReactNativePlatform()?.promiseRejections ?? createHermesRejectionHub(
147
+ globalThis.HermesInternal,
148
+ { dev: typeof __DEV__ !== "undefined" && !!__DEV__ }
149
+ );
150
+ } catch {
151
+ hub = null;
152
+ }
153
+ }
154
+ try {
155
+ registerReactNativePlatform({
156
+ os: rn.Platform.OS,
157
+ appState: rn.AppState,
158
+ backgroundTaskModule: rn.NativeModules?.DeepdotsBackgroundTask ?? null,
159
+ promiseRejections: hub
160
+ });
161
+ } catch {
121
162
  }
122
- registerReactNativePlatform({
123
- os: rn.Platform.OS,
124
- appState: rn.AppState,
125
- backgroundTaskModule: rn.NativeModules?.DeepdotsBackgroundTask ?? null,
126
- promiseRejections: hub
127
- });
128
163
  }
129
164
 
130
165
  // src/types/index.ts
@@ -1092,7 +1127,7 @@ function logRnSurveySdkChoice(choice, debug) {
1092
1127
  }
1093
1128
 
1094
1129
  // src/ui/preload.ts
1095
- var defaultLoader = () => import("./renderPopup-RUY3OBYV.mjs");
1130
+ var defaultLoader = () => import("./renderPopup-M5ZIXNV6.mjs");
1096
1131
  var pending = null;
1097
1132
  function preloadRenderPopup(loader = defaultLoader) {
1098
1133
  if (pending) return pending;
@@ -1165,7 +1200,7 @@ var BrowserPopupRenderer = class {
1165
1200
  if (!this.container) return;
1166
1201
  this.visible = true;
1167
1202
  const container = this.container;
1168
- void import("./renderPopup-RUY3OBYV.mjs").then(
1203
+ void import("./renderPopup-M5ZIXNV6.mjs").then(
1169
1204
  ({ renderPopup }) => {
1170
1205
  renderPopup(container, surveyId, productId, actions, emit, onClose, env, userId, style, sessionId, miniService, analyticsFeedbackSessionId, options);
1171
1206
  },
@@ -1645,9 +1680,11 @@ function createFeedbackSink(options) {
1645
1680
  const body = buildAnalyticsFeedbackBody(envelope, options.keys, feedbackSessionId, { sessionEnd });
1646
1681
  options.log?.("[DeepdotsAnalytics] POST /sdk/feedback \u2192", body);
1647
1682
  const batch = { body, at: now() };
1648
- if (sessionEnd) {
1649
- feedbackSessionId = void 0;
1650
- options.onSessionReset?.();
1683
+ if (sessionEnd || meta?.persist) {
1684
+ if (sessionEnd) {
1685
+ feedbackSessionId = void 0;
1686
+ options.onSessionReset?.();
1687
+ }
1651
1688
  addPending(batch);
1652
1689
  await drain();
1653
1690
  return;
@@ -2340,6 +2377,7 @@ var ANALYTICS_SESSION_TIMEOUT_MS = 30 * 6e4;
2340
2377
  var ANALYTICS_SERVER_IDLE_MS = 60 * 6e4;
2341
2378
  var ANALYTICS_SESSION_DISCARD_MS = ANALYTICS_SERVER_IDLE_MS - 10 * 6e4;
2342
2379
  var ANALYTICS_RESUME_WINDOW_MS = 6e4;
2380
+ var ANALYTICS_BACKGROUND_RESUME_WINDOW_MS = 5 * 6e4;
2343
2381
  var RESERVED_EVENT_PREFIX = "deepdots_";
2344
2382
  var CUSTOM_EVENT_PREFIX = "deepdots_event_";
2345
2383
  var MAX_PENDING_TRIGGERS = 20;
@@ -2507,7 +2545,7 @@ var DeepdotsPopups = class {
2507
2545
  const resumable = this.readResumableSession();
2508
2546
  if (resumable) {
2509
2547
  this.analyticsFeedbackSessionId = resumable.sessionId;
2510
- this.suspended = { at: resumable.at, userId: resumable.userId };
2548
+ this.suspended = { at: resumable.at, userId: resumable.userId, windowMs: ANALYTICS_RESUME_WINDOW_MS };
2511
2549
  }
2512
2550
  const analyticsSink = config.analytics ? createFeedbackSink({
2513
2551
  baseUrl: this.baseUrl,
@@ -2766,21 +2804,28 @@ var DeepdotsPopups = class {
2766
2804
  this.navObserver.visit(name);
2767
2805
  }
2768
2806
  }
2769
- /** App a foreground (RN: AppState 'active'): reanuda el engagement time y abre sesión nueva. */
2807
+ /**
2808
+ * App a foreground (RN: AppState 'active'): reanuda el engagement time. Si la app estuvo en
2809
+ * background menos de ANALYTICS_BACKGROUND_RESUME_WINDOW_MS sigue en la misma sesión
2810
+ * (`deepdots_session_resume`); si no, abre una nueva (`session_start` `foreground`).
2811
+ */
2770
2812
  onForeground() {
2771
2813
  this.engagement?.resume();
2772
2814
  this.openSession("foreground");
2773
2815
  }
2774
2816
  /**
2775
- * App a background (RN: AppState 'background'): FIN DE SESIÓN. Cierra pantalla actual
2776
- * (page_view), mini-services (mini_service_exit), emite engagement + `session_end` y
2777
- * envía el último lote con `completed:true`.
2778
- *
2779
- * Es la única señal disponible en móvil: el kill de la app (swipe) NO da callback.
2817
+ * App a background (RN: AppState 'background'): SUSPENDE la sesión. Cierra la pantalla
2818
+ * actual (page_view), los mini-services (mini_service_exit) y envía lo acumulado con un
2819
+ * `deepdots_session_suspend` (`reason: background`) y SIN `completed`:
2820
+ * - si la app vuelve dentro de ANALYTICS_BACKGROUND_RESUME_WINDOW_MS, `onForeground` sigue
2821
+ * en el mismo registro;
2822
+ * - si no vuelve (o la matan: el swipe NO da callback), el backend la cierra pasados unos
2823
+ * minutos con un `session_end` `background` a la hora de la suspensión.
2824
+ * Si aún no se conoce el registro (el primer POST sigue en vuelo), FIN DE SESIÓN como antes.
2780
2825
  */
2781
2826
  onBackground() {
2782
2827
  this.cancelDelayedPopups("app to background");
2783
- this.closeSession("background");
2828
+ if (!this.suspendSession("background")) this.closeSession("background");
2784
2829
  this.engagement?.pause();
2785
2830
  }
2786
2831
  /**
@@ -3005,27 +3050,37 @@ var DeepdotsPopups = class {
3005
3050
  * - si la página vuelve a cargar en esta pestaña, `init()` sigue en el mismo registro;
3006
3051
  * - si la pestaña se cerró, el backend lo cierra a los pocos minutos sin otro lote.
3007
3052
  *
3053
+ * En React Native (`background`) la suspensión vive en memoria: la app sigue viva en
3054
+ * background y, si el SO la mata, el arranque siguiente es otra sesión (`cold_start`).
3055
+ *
3008
3056
  * Devuelve false cuando no se puede suspender — aún no se conoce el registro (el primer
3009
- * POST sigue en vuelo) o no hay `sessionStorage` — y entonces el caller cierra como antes.
3057
+ * POST sigue en vuelo) o, en web, no hay `sessionStorage` — y entonces el caller cierra
3058
+ * como antes.
3010
3059
  */
3011
- suspendSession() {
3060
+ suspendSession(reason) {
3012
3061
  if (!this.sessionOpen || !this.tracking?.isTrackingEnabled()) return false;
3013
3062
  const sessionId = this.analyticsFeedbackSessionId;
3014
3063
  const integration = this.analyticsIntegration;
3015
- const storage = getSessionStorage();
3016
- if (!sessionId || !integration || !storage) return false;
3064
+ if (!sessionId || !integration) return false;
3017
3065
  const at = Date.now();
3018
3066
  const userId = this.tracking.getUserId();
3019
- if (!writeSuspendedSession(storage, { sessionId, integration, userId, at })) return false;
3067
+ if (reason === "page_hide") {
3068
+ const storage = getSessionStorage();
3069
+ if (!storage || !writeSuspendedSession(storage, { sessionId, integration, userId, at })) return false;
3070
+ }
3020
3071
  this.sessionOpen = false;
3021
- this.suspended = { at, userId };
3072
+ this.suspended = {
3073
+ at,
3074
+ userId,
3075
+ windowMs: reason === "page_hide" ? ANALYTICS_RESUME_WINDOW_MS : ANALYTICS_BACKGROUND_RESUME_WINDOW_MS
3076
+ };
3022
3077
  this.navObserver?.stop();
3023
3078
  this.navStarted = false;
3024
3079
  this.analytics?.exitAllMiniServices();
3025
3080
  this.flushEngagement();
3026
3081
  this.engagement?.pause();
3027
- this.track("deepdots_session_suspend", { reason: "page_hide" });
3028
- this.flushAnalytics({ final: true });
3082
+ this.track("deepdots_session_suspend", { reason });
3083
+ this.flushAnalytics(reason === "page_hide" ? { final: true } : { persist: true });
3029
3084
  this.log("tracking \xB7 session suspended:", sessionId);
3030
3085
  return true;
3031
3086
  }
@@ -3047,7 +3102,8 @@ var DeepdotsPopups = class {
3047
3102
  }
3048
3103
  /**
3049
3104
  * Consume la sesión suspendida al abrir: los ms que estuvo suspendida si se puede seguir en
3050
- * ella (mismo usuario, dentro de ANALYTICS_RESUME_WINDOW_MS); null si no había o ya no toca.
3105
+ * ella (mismo usuario, dentro de su ventana: ANALYTICS_RESUME_WINDOW_MS tras un `pagehide`,
3106
+ * ANALYTICS_BACKGROUND_RESUME_WINDOW_MS tras un background); null si no había o ya no toca.
3051
3107
  * En ese caso se olvida su registro sin enviar nada: lo cierra el backend, y la sesión que
3052
3108
  * se abre ahora va a uno nuevo.
3053
3109
  */
@@ -3058,7 +3114,7 @@ var DeepdotsPopups = class {
3058
3114
  const storage = getSessionStorage();
3059
3115
  if (storage) clearSuspendedSession(storage);
3060
3116
  const suspendedMs = Math.max(0, Date.now() - suspended.at);
3061
- if (this.analyticsFeedbackSessionId && suspendedMs <= ANALYTICS_RESUME_WINDOW_MS && suspended.userId === (this.tracking?.getUserId() ?? null)) {
3117
+ if (this.analyticsFeedbackSessionId && suspendedMs <= suspended.windowMs && suspended.userId === (this.tracking?.getUserId() ?? null)) {
3062
3118
  return suspendedMs;
3063
3119
  }
3064
3120
  this.analytics?.discardSession(this.analyticsIdentity());
@@ -3066,6 +3122,27 @@ var DeepdotsPopups = class {
3066
3122
  this.tracking?.setSessionId(null);
3067
3123
  return null;
3068
3124
  }
3125
+ /**
3126
+ * `pagehide` de una pestaña que llevaba oculta más de ANALYTICS_SESSION_TIMEOUT_MS: la
3127
+ * misma regla que `onDocumentVisible`. Suspenderla mandaba un lote más al registro viejo
3128
+ * (page_view + session_suspend) aunque el backend ya lo hubiera cerrado por inactividad:
3129
+ * el backend lo volvía a montar y las acciones de FEEDBACK_DONE (el aviso de Slack de
3130
+ * #platform-visitors) salían otra vez, horas después. Pasó con una pestaña oculta 7 h.
3131
+ * - Oculta menos de ANALYTICS_SESSION_DISCARD_MS: se cierra con `idle_timeout`.
3132
+ * - Más: el backend ya la cerró → se descarta sin enviar nada.
3133
+ * Devuelve false si la pestaña no llevaba tanto oculta (el caller suspende como siempre).
3134
+ */
3135
+ closeStaleHiddenSession() {
3136
+ if (!this.sessionOpen || this.hiddenAt === null) return false;
3137
+ const hiddenFor = Date.now() - this.hiddenAt;
3138
+ if (hiddenFor < ANALYTICS_SESSION_TIMEOUT_MS) return false;
3139
+ if (hiddenFor < ANALYTICS_SESSION_DISCARD_MS) {
3140
+ this.closeSession("idle_timeout");
3141
+ } else {
3142
+ this.discardSession();
3143
+ }
3144
+ return true;
3145
+ }
3069
3146
  /** Olvida la sesión abierta sin enviar nada (ver onDocumentVisible). */
3070
3147
  discardSession() {
3071
3148
  this.sessionOpen = false;
@@ -3114,7 +3191,8 @@ var DeepdotsPopups = class {
3114
3191
  if (typeof document === "undefined" || typeof window === "undefined") return;
3115
3192
  window.addEventListener("pagehide", () => {
3116
3193
  clearInterval(this.analyticsFlushTimer);
3117
- if (!this.suspendSession()) this.closeSession("page_hide");
3194
+ if (this.closeStaleHiddenSession()) return;
3195
+ if (!this.suspendSession("page_hide")) this.closeSession("page_hide");
3118
3196
  });
3119
3197
  window.addEventListener("pageshow", (event) => {
3120
3198
  if (!event.persisted) return;