@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
@@ -25,7 +25,7 @@ import {
25
25
  style_default,
26
26
  surveySdkCssUrls,
27
27
  surveySdkUrls
28
- } from "./chunk-FROREHRG.mjs";
28
+ } from "./chunk-NNWYOG43.mjs";
29
29
  import {
30
30
  __require
31
31
  } from "./chunk-BJTO5JO5.mjs";
@@ -1000,7 +1000,7 @@ function logRnSurveySdkChoice(choice, debug) {
1000
1000
  }
1001
1001
 
1002
1002
  // src/ui/preload.ts
1003
- var defaultLoader = () => import("./renderPopup-I6CPIR75.mjs");
1003
+ var defaultLoader = () => import("./renderPopup-IKPXPDB7.mjs");
1004
1004
  var pending = null;
1005
1005
  function preloadRenderPopup(loader = defaultLoader) {
1006
1006
  if (pending) return pending;
@@ -1073,7 +1073,7 @@ var BrowserPopupRenderer = class {
1073
1073
  if (!this.container) return;
1074
1074
  this.visible = true;
1075
1075
  const container = this.container;
1076
- void import("./renderPopup-I6CPIR75.mjs").then(
1076
+ void import("./renderPopup-IKPXPDB7.mjs").then(
1077
1077
  ({ renderPopup }) => {
1078
1078
  renderPopup(container, surveyId, productId, actions, emit, onClose, env, userId, style, sessionId, miniService, analyticsFeedbackSessionId, options);
1079
1079
  },
@@ -1539,9 +1539,11 @@ function createFeedbackSink(options) {
1539
1539
  const body = buildAnalyticsFeedbackBody(envelope, options.keys, feedbackSessionId, { sessionEnd });
1540
1540
  options.log?.("[DeepdotsAnalytics] POST /sdk/feedback \u2192", body);
1541
1541
  const batch = { body, at: now() };
1542
- if (sessionEnd) {
1543
- feedbackSessionId = void 0;
1544
- options.onSessionReset?.();
1542
+ if (sessionEnd || meta?.persist) {
1543
+ if (sessionEnd) {
1544
+ feedbackSessionId = void 0;
1545
+ options.onSessionReset?.();
1546
+ }
1545
1547
  addPending(batch);
1546
1548
  await drain();
1547
1549
  return;
@@ -2189,13 +2191,26 @@ function resolveBackgroundTask(explicit, platform) {
2189
2191
  }
2190
2192
  var DEFAULT_REJECTION_WHITELIST = [ReferenceError, TypeError, RangeError];
2191
2193
  function createHermesRejectionHub(hermes, options) {
2192
- if (!hermes || typeof hermes.enablePromiseRejectionTracker !== "function") return null;
2193
- const original = hermes.enablePromiseRejectionTracker.bind(hermes);
2194
+ let original;
2195
+ try {
2196
+ if (!hermes || typeof hermes.enablePromiseRejectionTracker !== "function") return null;
2197
+ original = hermes.enablePromiseRejectionTracker.bind(hermes);
2198
+ } catch {
2199
+ return null;
2200
+ }
2194
2201
  const listeners = /* @__PURE__ */ new Set();
2195
2202
  let hostOptions = null;
2196
2203
  let active = false;
2197
2204
  const forwardsToHost = (error) => !!hostOptions?.onUnhandled && (hostOptions.allRejections === true || DEFAULT_REJECTION_WHITELIST.some((type) => error instanceof type));
2198
2205
  const apply = () => {
2206
+ try {
2207
+ install();
2208
+ return true;
2209
+ } catch {
2210
+ return false;
2211
+ }
2212
+ };
2213
+ const install = () => {
2199
2214
  original({
2200
2215
  allRejections: true,
2201
2216
  onUnhandled: (id, error) => {
@@ -2214,17 +2229,17 @@ function createHermesRejectionHub(hermes, options) {
2214
2229
  onHandled: (id, error) => hostOptions?.onHandled?.(id, error)
2215
2230
  });
2216
2231
  };
2217
- hermes.enablePromiseRejectionTracker = (next) => {
2232
+ const wrapper = (next) => {
2218
2233
  hostOptions = next ?? null;
2219
- if (active) apply();
2234
+ if (active) install();
2220
2235
  else original(next);
2221
2236
  };
2237
+ if (!replaceTracker(hermes, wrapper)) return null;
2222
2238
  return {
2223
2239
  addUnhandledListener(listener) {
2224
2240
  listeners.add(listener);
2225
2241
  if (!active) {
2226
- active = true;
2227
- apply();
2242
+ active = apply();
2228
2243
  }
2229
2244
  return () => {
2230
2245
  listeners.delete(listener);
@@ -2232,6 +2247,21 @@ function createHermesRejectionHub(hermes, options) {
2232
2247
  }
2233
2248
  };
2234
2249
  }
2250
+ var TRACKER_KEY = "enablePromiseRejectionTracker";
2251
+ function replaceTracker(hermes, wrapper) {
2252
+ try {
2253
+ const own = Object.getOwnPropertyDescriptor(hermes, TRACKER_KEY);
2254
+ if (own && own.writable === false && !own.set) {
2255
+ if (!own.configurable) return false;
2256
+ Object.defineProperty(hermes, TRACKER_KEY, { ...own, value: wrapper });
2257
+ } else {
2258
+ hermes.enablePromiseRejectionTracker = wrapper;
2259
+ }
2260
+ return hermes.enablePromiseRejectionTracker === wrapper;
2261
+ } catch {
2262
+ return false;
2263
+ }
2264
+ }
2235
2265
 
2236
2266
  // src/analytics/messaging.ts
2237
2267
  var MESSAGE_CHANNELS = ["push", "in_app"];
@@ -2309,6 +2339,7 @@ var ANALYTICS_SESSION_TIMEOUT_MS = 30 * 6e4;
2309
2339
  var ANALYTICS_SERVER_IDLE_MS = 60 * 6e4;
2310
2340
  var ANALYTICS_SESSION_DISCARD_MS = ANALYTICS_SERVER_IDLE_MS - 10 * 6e4;
2311
2341
  var ANALYTICS_RESUME_WINDOW_MS = 6e4;
2342
+ var ANALYTICS_BACKGROUND_RESUME_WINDOW_MS = 5 * 6e4;
2312
2343
  var RESERVED_EVENT_PREFIX = "deepdots_";
2313
2344
  var CUSTOM_EVENT_PREFIX = "deepdots_event_";
2314
2345
  var MAX_PENDING_TRIGGERS = 20;
@@ -2476,7 +2507,7 @@ var DeepdotsPopups = class {
2476
2507
  const resumable = this.readResumableSession();
2477
2508
  if (resumable) {
2478
2509
  this.analyticsFeedbackSessionId = resumable.sessionId;
2479
- this.suspended = { at: resumable.at, userId: resumable.userId };
2510
+ this.suspended = { at: resumable.at, userId: resumable.userId, windowMs: ANALYTICS_RESUME_WINDOW_MS };
2480
2511
  }
2481
2512
  const analyticsSink = config.analytics ? createFeedbackSink({
2482
2513
  baseUrl: this.baseUrl,
@@ -2735,21 +2766,28 @@ var DeepdotsPopups = class {
2735
2766
  this.navObserver.visit(name);
2736
2767
  }
2737
2768
  }
2738
- /** App a foreground (RN: AppState 'active'): reanuda el engagement time y abre sesión nueva. */
2769
+ /**
2770
+ * App a foreground (RN: AppState 'active'): reanuda el engagement time. Si la app estuvo en
2771
+ * background menos de ANALYTICS_BACKGROUND_RESUME_WINDOW_MS sigue en la misma sesión
2772
+ * (`deepdots_session_resume`); si no, abre una nueva (`session_start` `foreground`).
2773
+ */
2739
2774
  onForeground() {
2740
2775
  this.engagement?.resume();
2741
2776
  this.openSession("foreground");
2742
2777
  }
2743
2778
  /**
2744
- * App a background (RN: AppState 'background'): FIN DE SESIÓN. Cierra pantalla actual
2745
- * (page_view), mini-services (mini_service_exit), emite engagement + `session_end` y
2746
- * envía el último lote con `completed:true`.
2747
- *
2748
- * Es la única señal disponible en móvil: el kill de la app (swipe) NO da callback.
2779
+ * App a background (RN: AppState 'background'): SUSPENDE la sesión. Cierra la pantalla
2780
+ * actual (page_view), los mini-services (mini_service_exit) y envía lo acumulado con un
2781
+ * `deepdots_session_suspend` (`reason: background`) y SIN `completed`:
2782
+ * - si la app vuelve dentro de ANALYTICS_BACKGROUND_RESUME_WINDOW_MS, `onForeground` sigue
2783
+ * en el mismo registro;
2784
+ * - si no vuelve (o la matan: el swipe NO da callback), el backend la cierra pasados unos
2785
+ * minutos con un `session_end` `background` a la hora de la suspensión.
2786
+ * Si aún no se conoce el registro (el primer POST sigue en vuelo), FIN DE SESIÓN como antes.
2749
2787
  */
2750
2788
  onBackground() {
2751
2789
  this.cancelDelayedPopups("app to background");
2752
- this.closeSession("background");
2790
+ if (!this.suspendSession("background")) this.closeSession("background");
2753
2791
  this.engagement?.pause();
2754
2792
  }
2755
2793
  /**
@@ -2974,27 +3012,37 @@ var DeepdotsPopups = class {
2974
3012
  * - si la página vuelve a cargar en esta pestaña, `init()` sigue en el mismo registro;
2975
3013
  * - si la pestaña se cerró, el backend lo cierra a los pocos minutos sin otro lote.
2976
3014
  *
3015
+ * En React Native (`background`) la suspensión vive en memoria: la app sigue viva en
3016
+ * background y, si el SO la mata, el arranque siguiente es otra sesión (`cold_start`).
3017
+ *
2977
3018
  * Devuelve false cuando no se puede suspender — aún no se conoce el registro (el primer
2978
- * POST sigue en vuelo) o no hay `sessionStorage` — y entonces el caller cierra como antes.
3019
+ * POST sigue en vuelo) o, en web, no hay `sessionStorage` — y entonces el caller cierra
3020
+ * como antes.
2979
3021
  */
2980
- suspendSession() {
3022
+ suspendSession(reason) {
2981
3023
  if (!this.sessionOpen || !this.tracking?.isTrackingEnabled()) return false;
2982
3024
  const sessionId = this.analyticsFeedbackSessionId;
2983
3025
  const integration = this.analyticsIntegration;
2984
- const storage = getSessionStorage();
2985
- if (!sessionId || !integration || !storage) return false;
3026
+ if (!sessionId || !integration) return false;
2986
3027
  const at = Date.now();
2987
3028
  const userId = this.tracking.getUserId();
2988
- if (!writeSuspendedSession(storage, { sessionId, integration, userId, at })) return false;
3029
+ if (reason === "page_hide") {
3030
+ const storage = getSessionStorage();
3031
+ if (!storage || !writeSuspendedSession(storage, { sessionId, integration, userId, at })) return false;
3032
+ }
2989
3033
  this.sessionOpen = false;
2990
- this.suspended = { at, userId };
3034
+ this.suspended = {
3035
+ at,
3036
+ userId,
3037
+ windowMs: reason === "page_hide" ? ANALYTICS_RESUME_WINDOW_MS : ANALYTICS_BACKGROUND_RESUME_WINDOW_MS
3038
+ };
2991
3039
  this.navObserver?.stop();
2992
3040
  this.navStarted = false;
2993
3041
  this.analytics?.exitAllMiniServices();
2994
3042
  this.flushEngagement();
2995
3043
  this.engagement?.pause();
2996
- this.track("deepdots_session_suspend", { reason: "page_hide" });
2997
- this.flushAnalytics({ final: true });
3044
+ this.track("deepdots_session_suspend", { reason });
3045
+ this.flushAnalytics(reason === "page_hide" ? { final: true } : { persist: true });
2998
3046
  this.log("tracking \xB7 session suspended:", sessionId);
2999
3047
  return true;
3000
3048
  }
@@ -3016,7 +3064,8 @@ var DeepdotsPopups = class {
3016
3064
  }
3017
3065
  /**
3018
3066
  * Consume la sesión suspendida al abrir: los ms que estuvo suspendida si se puede seguir en
3019
- * ella (mismo usuario, dentro de ANALYTICS_RESUME_WINDOW_MS); null si no había o ya no toca.
3067
+ * ella (mismo usuario, dentro de su ventana: ANALYTICS_RESUME_WINDOW_MS tras un `pagehide`,
3068
+ * ANALYTICS_BACKGROUND_RESUME_WINDOW_MS tras un background); null si no había o ya no toca.
3020
3069
  * En ese caso se olvida su registro sin enviar nada: lo cierra el backend, y la sesión que
3021
3070
  * se abre ahora va a uno nuevo.
3022
3071
  */
@@ -3027,7 +3076,7 @@ var DeepdotsPopups = class {
3027
3076
  const storage = getSessionStorage();
3028
3077
  if (storage) clearSuspendedSession(storage);
3029
3078
  const suspendedMs = Math.max(0, Date.now() - suspended.at);
3030
- if (this.analyticsFeedbackSessionId && suspendedMs <= ANALYTICS_RESUME_WINDOW_MS && suspended.userId === (this.tracking?.getUserId() ?? null)) {
3079
+ if (this.analyticsFeedbackSessionId && suspendedMs <= suspended.windowMs && suspended.userId === (this.tracking?.getUserId() ?? null)) {
3031
3080
  return suspendedMs;
3032
3081
  }
3033
3082
  this.analytics?.discardSession(this.analyticsIdentity());
@@ -3035,6 +3084,27 @@ var DeepdotsPopups = class {
3035
3084
  this.tracking?.setSessionId(null);
3036
3085
  return null;
3037
3086
  }
3087
+ /**
3088
+ * `pagehide` de una pestaña que llevaba oculta más de ANALYTICS_SESSION_TIMEOUT_MS: la
3089
+ * misma regla que `onDocumentVisible`. Suspenderla mandaba un lote más al registro viejo
3090
+ * (page_view + session_suspend) aunque el backend ya lo hubiera cerrado por inactividad:
3091
+ * el backend lo volvía a montar y las acciones de FEEDBACK_DONE (el aviso de Slack de
3092
+ * #platform-visitors) salían otra vez, horas después. Pasó con una pestaña oculta 7 h.
3093
+ * - Oculta menos de ANALYTICS_SESSION_DISCARD_MS: se cierra con `idle_timeout`.
3094
+ * - Más: el backend ya la cerró → se descarta sin enviar nada.
3095
+ * Devuelve false si la pestaña no llevaba tanto oculta (el caller suspende como siempre).
3096
+ */
3097
+ closeStaleHiddenSession() {
3098
+ if (!this.sessionOpen || this.hiddenAt === null) return false;
3099
+ const hiddenFor = Date.now() - this.hiddenAt;
3100
+ if (hiddenFor < ANALYTICS_SESSION_TIMEOUT_MS) return false;
3101
+ if (hiddenFor < ANALYTICS_SESSION_DISCARD_MS) {
3102
+ this.closeSession("idle_timeout");
3103
+ } else {
3104
+ this.discardSession();
3105
+ }
3106
+ return true;
3107
+ }
3038
3108
  /** Olvida la sesión abierta sin enviar nada (ver onDocumentVisible). */
3039
3109
  discardSession() {
3040
3110
  this.sessionOpen = false;
@@ -3083,7 +3153,8 @@ var DeepdotsPopups = class {
3083
3153
  if (typeof document === "undefined" || typeof window === "undefined") return;
3084
3154
  window.addEventListener("pagehide", () => {
3085
3155
  clearInterval(this.analyticsFlushTimer);
3086
- if (!this.suspendSession()) this.closeSession("page_hide");
3156
+ if (this.closeStaleHiddenSession()) return;
3157
+ if (!this.suspendSession("page_hide")) this.closeSession("page_hide");
3087
3158
  });
3088
3159
  window.addEventListener("pageshow", (event) => {
3089
3160
  if (!event.persisted) return;
@@ -3944,17 +4015,24 @@ function setupReactNative(sdk2, config, deps = {}) {
3944
4015
  var hub;
3945
4016
  function registerReactNativeDefaults(rn) {
3946
4017
  if (hub === void 0) {
3947
- hub = getReactNativePlatform()?.promiseRejections ?? createHermesRejectionHub(
3948
- globalThis.HermesInternal,
3949
- { dev: typeof __DEV__ !== "undefined" && !!__DEV__ }
3950
- );
4018
+ try {
4019
+ hub = getReactNativePlatform()?.promiseRejections ?? createHermesRejectionHub(
4020
+ globalThis.HermesInternal,
4021
+ { dev: typeof __DEV__ !== "undefined" && !!__DEV__ }
4022
+ );
4023
+ } catch {
4024
+ hub = null;
4025
+ }
4026
+ }
4027
+ try {
4028
+ registerReactNativePlatform({
4029
+ os: rn.Platform.OS,
4030
+ appState: rn.AppState,
4031
+ backgroundTaskModule: rn.NativeModules?.DeepdotsBackgroundTask ?? null,
4032
+ promiseRejections: hub
4033
+ });
4034
+ } catch {
3951
4035
  }
3952
- registerReactNativePlatform({
3953
- os: rn.Platform.OS,
3954
- appState: rn.AppState,
3955
- backgroundTaskModule: rn.NativeModules?.DeepdotsBackgroundTask ?? null,
3956
- promiseRejections: hub
3957
- });
3958
4036
  }
3959
4037
 
3960
4038
  // src/react-native/index.tsx