@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
package/dist/index.mjs CHANGED
@@ -29,7 +29,7 @@ import {
29
29
  style_default,
30
30
  surveySdkCssUrls,
31
31
  surveySdkUrls
32
- } from "./chunk-OPR5I7WO.mjs";
32
+ } from "./chunk-7KMBH25G.mjs";
33
33
  import "./chunk-3QS3WKRC.mjs";
34
34
 
35
35
  // src/types/index.ts
@@ -997,7 +997,7 @@ function logRnSurveySdkChoice(choice, debug) {
997
997
  }
998
998
 
999
999
  // src/ui/preload.ts
1000
- var defaultLoader = () => import("./renderPopup-XNVNP2ZU.mjs");
1000
+ var defaultLoader = () => import("./renderPopup-QVMBE4L4.mjs");
1001
1001
  var pending = null;
1002
1002
  function preloadRenderPopup(loader = defaultLoader) {
1003
1003
  if (pending) return pending;
@@ -1070,7 +1070,7 @@ var BrowserPopupRenderer = class {
1070
1070
  if (!this.container) return;
1071
1071
  this.visible = true;
1072
1072
  const container = this.container;
1073
- void import("./renderPopup-XNVNP2ZU.mjs").then(
1073
+ void import("./renderPopup-QVMBE4L4.mjs").then(
1074
1074
  ({ renderPopup }) => {
1075
1075
  renderPopup(container, surveyId, productId, actions, emit, onClose, env, userId, style, sessionId, miniService, analyticsFeedbackSessionId, options);
1076
1076
  },
@@ -1550,9 +1550,11 @@ function createFeedbackSink(options) {
1550
1550
  const body = buildAnalyticsFeedbackBody(envelope, options.keys, feedbackSessionId, { sessionEnd });
1551
1551
  options.log?.("[DeepdotsAnalytics] POST /sdk/feedback \u2192", body);
1552
1552
  const batch = { body, at: now() };
1553
- if (sessionEnd) {
1554
- feedbackSessionId = void 0;
1555
- options.onSessionReset?.();
1553
+ if (sessionEnd || meta?.persist) {
1554
+ if (sessionEnd) {
1555
+ feedbackSessionId = void 0;
1556
+ options.onSessionReset?.();
1557
+ }
1556
1558
  addPending(batch);
1557
1559
  await drain();
1558
1560
  return;
@@ -2197,13 +2199,26 @@ function resolveBackgroundTask(explicit, platform) {
2197
2199
  }
2198
2200
  var DEFAULT_REJECTION_WHITELIST = [ReferenceError, TypeError, RangeError];
2199
2201
  function createHermesRejectionHub(hermes, options) {
2200
- if (!hermes || typeof hermes.enablePromiseRejectionTracker !== "function") return null;
2201
- const original = hermes.enablePromiseRejectionTracker.bind(hermes);
2202
+ let original;
2203
+ try {
2204
+ if (!hermes || typeof hermes.enablePromiseRejectionTracker !== "function") return null;
2205
+ original = hermes.enablePromiseRejectionTracker.bind(hermes);
2206
+ } catch {
2207
+ return null;
2208
+ }
2202
2209
  const listeners = /* @__PURE__ */ new Set();
2203
2210
  let hostOptions = null;
2204
2211
  let active = false;
2205
2212
  const forwardsToHost = (error) => !!hostOptions?.onUnhandled && (hostOptions.allRejections === true || DEFAULT_REJECTION_WHITELIST.some((type) => error instanceof type));
2206
2213
  const apply = () => {
2214
+ try {
2215
+ install();
2216
+ return true;
2217
+ } catch {
2218
+ return false;
2219
+ }
2220
+ };
2221
+ const install = () => {
2207
2222
  original({
2208
2223
  allRejections: true,
2209
2224
  onUnhandled: (id, error) => {
@@ -2222,17 +2237,17 @@ function createHermesRejectionHub(hermes, options) {
2222
2237
  onHandled: (id, error) => hostOptions?.onHandled?.(id, error)
2223
2238
  });
2224
2239
  };
2225
- hermes.enablePromiseRejectionTracker = (next) => {
2240
+ const wrapper = (next) => {
2226
2241
  hostOptions = next ?? null;
2227
- if (active) apply();
2242
+ if (active) install();
2228
2243
  else original(next);
2229
2244
  };
2245
+ if (!replaceTracker(hermes, wrapper)) return null;
2230
2246
  return {
2231
2247
  addUnhandledListener(listener) {
2232
2248
  listeners.add(listener);
2233
2249
  if (!active) {
2234
- active = true;
2235
- apply();
2250
+ active = apply();
2236
2251
  }
2237
2252
  return () => {
2238
2253
  listeners.delete(listener);
@@ -2240,6 +2255,21 @@ function createHermesRejectionHub(hermes, options) {
2240
2255
  }
2241
2256
  };
2242
2257
  }
2258
+ var TRACKER_KEY = "enablePromiseRejectionTracker";
2259
+ function replaceTracker(hermes, wrapper) {
2260
+ try {
2261
+ const own = Object.getOwnPropertyDescriptor(hermes, TRACKER_KEY);
2262
+ if (own && own.writable === false && !own.set) {
2263
+ if (!own.configurable) return false;
2264
+ Object.defineProperty(hermes, TRACKER_KEY, { ...own, value: wrapper });
2265
+ } else {
2266
+ hermes.enablePromiseRejectionTracker = wrapper;
2267
+ }
2268
+ return hermes.enablePromiseRejectionTracker === wrapper;
2269
+ } catch {
2270
+ return false;
2271
+ }
2272
+ }
2243
2273
 
2244
2274
  // src/analytics/messaging.ts
2245
2275
  var MESSAGE_CHANNELS = ["push", "in_app"];
@@ -2317,6 +2347,7 @@ var ANALYTICS_SESSION_TIMEOUT_MS = 30 * 6e4;
2317
2347
  var ANALYTICS_SERVER_IDLE_MS = 60 * 6e4;
2318
2348
  var ANALYTICS_SESSION_DISCARD_MS = ANALYTICS_SERVER_IDLE_MS - 10 * 6e4;
2319
2349
  var ANALYTICS_RESUME_WINDOW_MS = 6e4;
2350
+ var ANALYTICS_BACKGROUND_RESUME_WINDOW_MS = 5 * 6e4;
2320
2351
  var RESERVED_EVENT_PREFIX = "deepdots_";
2321
2352
  var CUSTOM_EVENT_PREFIX = "deepdots_event_";
2322
2353
  var MAX_PENDING_TRIGGERS = 20;
@@ -2484,7 +2515,7 @@ var DeepdotsPopups = class {
2484
2515
  const resumable = this.readResumableSession();
2485
2516
  if (resumable) {
2486
2517
  this.analyticsFeedbackSessionId = resumable.sessionId;
2487
- this.suspended = { at: resumable.at, userId: resumable.userId };
2518
+ this.suspended = { at: resumable.at, userId: resumable.userId, windowMs: ANALYTICS_RESUME_WINDOW_MS };
2488
2519
  }
2489
2520
  const analyticsSink = config.analytics ? createFeedbackSink({
2490
2521
  baseUrl: this.baseUrl,
@@ -2743,21 +2774,28 @@ var DeepdotsPopups = class {
2743
2774
  this.navObserver.visit(name);
2744
2775
  }
2745
2776
  }
2746
- /** App a foreground (RN: AppState 'active'): reanuda el engagement time y abre sesión nueva. */
2777
+ /**
2778
+ * App a foreground (RN: AppState 'active'): reanuda el engagement time. Si la app estuvo en
2779
+ * background menos de ANALYTICS_BACKGROUND_RESUME_WINDOW_MS sigue en la misma sesión
2780
+ * (`deepdots_session_resume`); si no, abre una nueva (`session_start` `foreground`).
2781
+ */
2747
2782
  onForeground() {
2748
2783
  this.engagement?.resume();
2749
2784
  this.openSession("foreground");
2750
2785
  }
2751
2786
  /**
2752
- * App a background (RN: AppState 'background'): FIN DE SESIÓN. Cierra pantalla actual
2753
- * (page_view), mini-services (mini_service_exit), emite engagement + `session_end` y
2754
- * envía el último lote con `completed:true`.
2755
- *
2756
- * Es la única señal disponible en móvil: el kill de la app (swipe) NO da callback.
2787
+ * App a background (RN: AppState 'background'): SUSPENDE la sesión. Cierra la pantalla
2788
+ * actual (page_view), los mini-services (mini_service_exit) y envía lo acumulado con un
2789
+ * `deepdots_session_suspend` (`reason: background`) y SIN `completed`:
2790
+ * - si la app vuelve dentro de ANALYTICS_BACKGROUND_RESUME_WINDOW_MS, `onForeground` sigue
2791
+ * en el mismo registro;
2792
+ * - si no vuelve (o la matan: el swipe NO da callback), el backend la cierra pasados unos
2793
+ * minutos con un `session_end` `background` a la hora de la suspensión.
2794
+ * Si aún no se conoce el registro (el primer POST sigue en vuelo), FIN DE SESIÓN como antes.
2757
2795
  */
2758
2796
  onBackground() {
2759
2797
  this.cancelDelayedPopups("app to background");
2760
- this.closeSession("background");
2798
+ if (!this.suspendSession("background")) this.closeSession("background");
2761
2799
  this.engagement?.pause();
2762
2800
  }
2763
2801
  /**
@@ -2982,27 +3020,37 @@ var DeepdotsPopups = class {
2982
3020
  * - si la página vuelve a cargar en esta pestaña, `init()` sigue en el mismo registro;
2983
3021
  * - si la pestaña se cerró, el backend lo cierra a los pocos minutos sin otro lote.
2984
3022
  *
3023
+ * En React Native (`background`) la suspensión vive en memoria: la app sigue viva en
3024
+ * background y, si el SO la mata, el arranque siguiente es otra sesión (`cold_start`).
3025
+ *
2985
3026
  * Devuelve false cuando no se puede suspender — aún no se conoce el registro (el primer
2986
- * POST sigue en vuelo) o no hay `sessionStorage` — y entonces el caller cierra como antes.
3027
+ * POST sigue en vuelo) o, en web, no hay `sessionStorage` — y entonces el caller cierra
3028
+ * como antes.
2987
3029
  */
2988
- suspendSession() {
3030
+ suspendSession(reason) {
2989
3031
  if (!this.sessionOpen || !this.tracking?.isTrackingEnabled()) return false;
2990
3032
  const sessionId = this.analyticsFeedbackSessionId;
2991
3033
  const integration = this.analyticsIntegration;
2992
- const storage = getSessionStorage();
2993
- if (!sessionId || !integration || !storage) return false;
3034
+ if (!sessionId || !integration) return false;
2994
3035
  const at = Date.now();
2995
3036
  const userId = this.tracking.getUserId();
2996
- if (!writeSuspendedSession(storage, { sessionId, integration, userId, at })) return false;
3037
+ if (reason === "page_hide") {
3038
+ const storage = getSessionStorage();
3039
+ if (!storage || !writeSuspendedSession(storage, { sessionId, integration, userId, at })) return false;
3040
+ }
2997
3041
  this.sessionOpen = false;
2998
- this.suspended = { at, userId };
3042
+ this.suspended = {
3043
+ at,
3044
+ userId,
3045
+ windowMs: reason === "page_hide" ? ANALYTICS_RESUME_WINDOW_MS : ANALYTICS_BACKGROUND_RESUME_WINDOW_MS
3046
+ };
2999
3047
  this.navObserver?.stop();
3000
3048
  this.navStarted = false;
3001
3049
  this.analytics?.exitAllMiniServices();
3002
3050
  this.flushEngagement();
3003
3051
  this.engagement?.pause();
3004
- this.track("deepdots_session_suspend", { reason: "page_hide" });
3005
- this.flushAnalytics({ final: true });
3052
+ this.track("deepdots_session_suspend", { reason });
3053
+ this.flushAnalytics(reason === "page_hide" ? { final: true } : { persist: true });
3006
3054
  this.log("tracking \xB7 session suspended:", sessionId);
3007
3055
  return true;
3008
3056
  }
@@ -3024,7 +3072,8 @@ var DeepdotsPopups = class {
3024
3072
  }
3025
3073
  /**
3026
3074
  * Consume la sesión suspendida al abrir: los ms que estuvo suspendida si se puede seguir en
3027
- * ella (mismo usuario, dentro de ANALYTICS_RESUME_WINDOW_MS); null si no había o ya no toca.
3075
+ * ella (mismo usuario, dentro de su ventana: ANALYTICS_RESUME_WINDOW_MS tras un `pagehide`,
3076
+ * ANALYTICS_BACKGROUND_RESUME_WINDOW_MS tras un background); null si no había o ya no toca.
3028
3077
  * En ese caso se olvida su registro sin enviar nada: lo cierra el backend, y la sesión que
3029
3078
  * se abre ahora va a uno nuevo.
3030
3079
  */
@@ -3035,7 +3084,7 @@ var DeepdotsPopups = class {
3035
3084
  const storage = getSessionStorage();
3036
3085
  if (storage) clearSuspendedSession(storage);
3037
3086
  const suspendedMs = Math.max(0, Date.now() - suspended.at);
3038
- if (this.analyticsFeedbackSessionId && suspendedMs <= ANALYTICS_RESUME_WINDOW_MS && suspended.userId === (this.tracking?.getUserId() ?? null)) {
3087
+ if (this.analyticsFeedbackSessionId && suspendedMs <= suspended.windowMs && suspended.userId === (this.tracking?.getUserId() ?? null)) {
3039
3088
  return suspendedMs;
3040
3089
  }
3041
3090
  this.analytics?.discardSession(this.analyticsIdentity());
@@ -3043,6 +3092,27 @@ var DeepdotsPopups = class {
3043
3092
  this.tracking?.setSessionId(null);
3044
3093
  return null;
3045
3094
  }
3095
+ /**
3096
+ * `pagehide` de una pestaña que llevaba oculta más de ANALYTICS_SESSION_TIMEOUT_MS: la
3097
+ * misma regla que `onDocumentVisible`. Suspenderla mandaba un lote más al registro viejo
3098
+ * (page_view + session_suspend) aunque el backend ya lo hubiera cerrado por inactividad:
3099
+ * el backend lo volvía a montar y las acciones de FEEDBACK_DONE (el aviso de Slack de
3100
+ * #platform-visitors) salían otra vez, horas después. Pasó con una pestaña oculta 7 h.
3101
+ * - Oculta menos de ANALYTICS_SESSION_DISCARD_MS: se cierra con `idle_timeout`.
3102
+ * - Más: el backend ya la cerró → se descarta sin enviar nada.
3103
+ * Devuelve false si la pestaña no llevaba tanto oculta (el caller suspende como siempre).
3104
+ */
3105
+ closeStaleHiddenSession() {
3106
+ if (!this.sessionOpen || this.hiddenAt === null) return false;
3107
+ const hiddenFor = Date.now() - this.hiddenAt;
3108
+ if (hiddenFor < ANALYTICS_SESSION_TIMEOUT_MS) return false;
3109
+ if (hiddenFor < ANALYTICS_SESSION_DISCARD_MS) {
3110
+ this.closeSession("idle_timeout");
3111
+ } else {
3112
+ this.discardSession();
3113
+ }
3114
+ return true;
3115
+ }
3046
3116
  /** Olvida la sesión abierta sin enviar nada (ver onDocumentVisible). */
3047
3117
  discardSession() {
3048
3118
  this.sessionOpen = false;
@@ -3091,7 +3161,8 @@ var DeepdotsPopups = class {
3091
3161
  if (typeof document === "undefined" || typeof window === "undefined") return;
3092
3162
  window.addEventListener("pagehide", () => {
3093
3163
  clearInterval(this.analyticsFlushTimer);
3094
- if (!this.suspendSession()) this.closeSession("page_hide");
3164
+ if (this.closeStaleHiddenSession()) return;
3165
+ if (!this.suspendSession("page_hide")) this.closeSession("page_hide");
3095
3166
  });
3096
3167
  window.addEventListener("pageshow", (event) => {
3097
3168
  if (!event.persisted) return;