@mixpanel/react-native-session-replay 1.2.0 → 1.4.0

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 (37) hide show
  1. package/MixpanelReactNativeSessionReplay.podspec +1 -1
  2. package/README.md +2 -0
  3. package/android/build.gradle +10 -3
  4. package/android/src/main/java/com/mixpanelreactnativesessionreplay/MixpanelReactNativeSessionReplayModule.kt +54 -0
  5. package/android/src/main/java/com/mixpanelreactnativesessionreplay/MixpanelSessionReplayView.kt +56 -0
  6. package/android/src/main/java/com/mixpanelreactnativesessionreplay/MixpanelSessionReplayViewManager.kt +5 -0
  7. package/android/src/test/java/com/mixpanelreactnativesessionreplay/WireframeTextDeclarationTest.kt +41 -0
  8. package/ios/MixpanelReactNativeSessionReplay.h +7 -2
  9. package/ios/MixpanelReactNativeSessionReplay.mm +35 -2
  10. package/ios/MixpanelSessionReplayViewManager.mm +9 -0
  11. package/ios/MixpanelSwiftSessionReplay.swift +43 -1
  12. package/lib/module/MixpanelSessionReplayView.js.map +1 -1
  13. package/lib/module/NativeMixpanelReactNativeSessionReplay.js.map +1 -1
  14. package/lib/module/index.js +623 -5
  15. package/lib/module/index.js.map +1 -1
  16. package/lib/typescript/src/MixpanelSessionReplayView.d.ts +32 -0
  17. package/lib/typescript/src/MixpanelSessionReplayView.d.ts.map +1 -1
  18. package/lib/typescript/src/NativeMixpanelReactNativeSessionReplay.d.ts +2 -0
  19. package/lib/typescript/src/NativeMixpanelReactNativeSessionReplay.d.ts.map +1 -1
  20. package/lib/typescript/src/index.d.ts +351 -1
  21. package/lib/typescript/src/index.d.ts.map +1 -1
  22. package/package.json +2 -24
  23. package/src/MixpanelSessionReplayView.tsx +33 -0
  24. package/src/NativeMixpanelReactNativeSessionReplay.ts +7 -0
  25. package/src/index.tsx +688 -4
  26. package/android/app/build/generated/source/codegen/java/com/facebook/fbreact/specs/NativeMixpanelReactNativeSessionReplaySpec.java +0 -62
  27. package/android/app/build/generated/source/codegen/jni/CMakeLists.txt +0 -36
  28. package/android/app/build/generated/source/codegen/jni/MixpanelReactNativeSessionReplaySpec-generated.cpp +0 -68
  29. package/android/app/build/generated/source/codegen/jni/MixpanelReactNativeSessionReplaySpec.h +0 -31
  30. package/android/app/build/generated/source/codegen/jni/react/renderer/components/MixpanelReactNativeSessionReplaySpec/MixpanelReactNativeSessionReplaySpecJSI-generated.cpp +0 -67
  31. package/android/app/build/generated/source/codegen/jni/react/renderer/components/MixpanelReactNativeSessionReplaySpec/MixpanelReactNativeSessionReplaySpecJSI.h +0 -125
  32. package/android/generated/java/com/mpsessionreplay/NativeMpSessionReplaySpec.java +0 -49
  33. package/android/generated/jni/CMakeLists.txt +0 -36
  34. package/android/generated/jni/RNMpSessionReplaySpec-generated.cpp +0 -50
  35. package/android/generated/jni/RNMpSessionReplaySpec.h +0 -31
  36. package/android/generated/jni/react/renderer/components/RNMpSessionReplaySpec/RNMpSessionReplaySpecJSI-generated.cpp +0 -51
  37. package/android/generated/jni/react/renderer/components/RNMpSessionReplaySpec/RNMpSessionReplaySpecJSI.h +0 -98
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- import { Platform } from 'react-native';
3
+ import { NativeEventEmitter, Platform, processColor } from 'react-native';
4
4
  import MixpanelReactNativeSessionReplay from "./NativeMixpanelReactNativeSessionReplay.js";
5
5
  export let MPSessionReplayMask = /*#__PURE__*/function (MPSessionReplayMask) {
6
6
  MPSessionReplayMask["Text"] = "text";
@@ -15,6 +15,471 @@ export let MPSessionReplayRemoteSettingsMode = /*#__PURE__*/function (MPSessionR
15
15
  MPSessionReplayRemoteSettingsMode["Fallback"] = "fallback";
16
16
  return MPSessionReplayRemoteSettingsMode;
17
17
  }({});
18
+
19
+ /**
20
+ * Base URLs for Mixpanel's managed data residency regions.
21
+ *
22
+ * Pass one of these constants to {@link MPSessionReplayConfig.serverURL} to route
23
+ * session replay traffic to the matching region, or pass any other `https://` URL
24
+ * to send traffic through a custom endpoint (for example, a corporate proxy).
25
+ */
26
+ export const MPDataResidency = {
27
+ /** US data residency (default): `https://api.mixpanel.com`. */
28
+ US: 'https://api.mixpanel.com',
29
+ /** EU data residency: `https://api-eu.mixpanel.com`. */
30
+ EU: 'https://api-eu.mixpanel.com',
31
+ /** India data residency: `https://api-in.mixpanel.com`. */
32
+ IN: 'https://api-in.mixpanel.com'
33
+ };
34
+
35
+ /**
36
+ * Why a wireframe element's text was kept, rewritten, or dropped.
37
+ *
38
+ * Reported through {@link MPDebugOptions.wireframeEmitter} for local inspection only.
39
+ * These are the same `SCREAMING_SNAKE_CASE` tokens Android, iOS and Flutter report, so
40
+ * the same decision reads identically on every platform.
41
+ *
42
+ * ⚠️ **Not a stable contract.** Values may be added or renamed; treat an unrecognized
43
+ * one as "some kind of masking" rather than matching exhaustively. That is also why
44
+ * {@link MPWireframeElement.maskDecision} is typed `string` rather than a closed union —
45
+ * a new native value should not become a type error.
46
+ */
47
+ export const MPMaskDecision = {
48
+ /** Text emitted as-is. */
49
+ None: 'NONE',
50
+ /** Text you declared with the `wireframeText` prop. Sent even when masked. */
51
+ Declared: 'DECLARED',
52
+ /** Explicitly masked — `<MPSessionReplayView sensitive>` or a registered class. */
53
+ Explicit: 'EXPLICIT',
54
+ /** Auto-masked by an `autoMaskedViews` category. */
55
+ Auto: 'AUTO',
56
+ /** A text-entry field. Always masked, cannot be overridden. */
57
+ TextEntry: 'TEXT_ENTRY',
58
+ /** Bounds intersected a mask rect the screenshot painted over. */
59
+ Geometric: 'GEOMETRIC',
60
+ /** Matched a strip rule; text was dropped. */
61
+ RuleStrip: 'RULE_STRIP',
62
+ /** Matched a redact rule; text was rewritten. */
63
+ RuleRedact: 'RULE_REDACT'
64
+ };
65
+
66
+ /** One element of a captured wireframe. See {@link MPWireframeSnapshot}. */
67
+
68
+ /**
69
+ * One captured wireframe, exactly as the SDK built it.
70
+ *
71
+ * ⚠️ **Not a stable contract** — a debugging aid, not a schema to build tooling on.
72
+ */
73
+
74
+ /**
75
+ * Native event carrying one wireframe snapshot as JSON.
76
+ *
77
+ * The payload crosses as a *string* rather than a structured map on purpose: both SDKs
78
+ * already own a `toJson()` for their debug snapshot, so forwarding it verbatim keeps the
79
+ * bridge from becoming a third place where the shape can drift.
80
+ */
81
+ const WIREFRAME_EVENT = 'MixpanelSessionReplayWireframe';
82
+ let wireframeEventEmitter = null;
83
+
84
+ // The one live subscription, owned by `initialize`. Replaced on every initialize so a
85
+ // re-init cannot leave the previous config's callback attached.
86
+ let wireframeSubscription = null;
87
+
88
+ // Which `initialize` call the live subscription belongs to. Bumped on every attach, so a
89
+ // call that fails can tell whether the subscription still in place is its own — see
90
+ // `attachWireframeEmitter`.
91
+ let wireframeGeneration = 0;
92
+ function getWireframeEventEmitter() {
93
+ if (wireframeEventEmitter === null) {
94
+ // Constructed lazily: importing this module must not create an emitter for an app
95
+ // that never listens. The module is passed on both platforms because a TurboModule
96
+ // has to supply `addListener`/`removeListeners` itself.
97
+ wireframeEventEmitter = new NativeEventEmitter(MixpanelReactNativeSessionReplay);
98
+ }
99
+ return wireframeEventEmitter;
100
+ }
101
+
102
+ /**
103
+ * Color configuration for the on-device debug mask overlay.
104
+ *
105
+ * Each color accepts any React Native [`ColorValue`](https://reactnative.dev/docs/colors)
106
+ * — for example `'red'`, `'#FF0000'`, `'rgba(255, 0, 0, 0.5)'`, or `0xFFFF0000`.
107
+ * Setting any of `maskColor`, `autoMaskColor`, or `unmaskColor` to `null` hides that
108
+ * category from the overlay.
109
+ *
110
+ * The overlay only renders in a debuggable build. That is enforced by the native SDKs
111
+ * themselves, so it is safe to leave `overlayColors` set unconditionally.
112
+ */
113
+ export class MPDebugOverlayColors {
114
+ /**
115
+ * Color used to draw regions that are explicitly masked (sensitive views / text input).
116
+ * Set to `null` to hide masked regions from the overlay.
117
+ *
118
+ * - **Default:** `'red'`
119
+ */
120
+
121
+ /**
122
+ * Color used to draw regions that are auto-masked (text, images, web views, maps).
123
+ * Set to `null` to hide auto-masked regions from the overlay.
124
+ *
125
+ * - **Default:** `'orange'`
126
+ */
127
+
128
+ /**
129
+ * Color used to draw regions explicitly excluded from masking (safe views).
130
+ * Set to `null` to hide unmask regions from the overlay.
131
+ *
132
+ * - **Default:** `'green'`
133
+ */
134
+
135
+ /**
136
+ * Opacity of the overlay, from `0.0` (fully transparent) to `1.0` (fully opaque).
137
+ *
138
+ * - **Default:** `0.5`
139
+ */
140
+
141
+ constructor({
142
+ maskColor = 'red',
143
+ autoMaskColor = 'orange',
144
+ unmaskColor = 'green',
145
+ alpha = 0.5
146
+ } = {}) {
147
+ this.maskColor = maskColor;
148
+ this.autoMaskColor = autoMaskColor;
149
+ this.unmaskColor = unmaskColor;
150
+ this.alpha = alpha;
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Configuration for Session Replay debug features.
156
+ *
157
+ * When `overlayColors` is non-null, the SDK renders a visual overlay on top of the app
158
+ * showing which regions are masked, auto-masked, or explicitly unmasked. Set
159
+ * `overlayColors` to `null` to disable the overlay while keeping `MPDebugOptions`
160
+ * available for future debug features.
161
+ *
162
+ * `debugOptions` reaches the native SDK in every bundle, exactly as it would if you
163
+ * configured it natively. Each feature decides for itself what a release build may do:
164
+ * the overlay is restricted to debuggable builds by the native SDKs, while
165
+ * {@link MPDebugOptions.wireframeEmitter} runs wherever you configure it.
166
+ */
167
+ export class MPDebugOptions {
168
+ /**
169
+ * Color configuration for the debug mask overlay. When `null`, the overlay is disabled.
170
+ *
171
+ * - **Default:** `new MPDebugOverlayColors()` (overlay enabled with default colors)
172
+ */
173
+
174
+ /**
175
+ * Hands each captured wireframe back to JavaScript as it is captured, so you can check
176
+ * what you are sending while you develop.
177
+ *
178
+ * You get exactly the elements that go to Mixpanel, plus the reason each one's text was
179
+ * kept or removed. Nothing delivered here leaves the device.
180
+ *
181
+ * This *observes* wireframe capture; it does not enable it. Wireframes are only captured
182
+ * when {@link MPSessionReplayConfig.wireframesOptions} is set, so a callback on its own is
183
+ * harmless but never fires.
184
+ *
185
+ * It runs in whichever bundle you configure it in, matching the native SDKs. Nothing it
186
+ * delivers leaves the device, but building each snapshot is work the SDK otherwise skips
187
+ * and every frame crosses the bridge — so leave it unset once you are done checking.
188
+ *
189
+ * Configuration only, and deliberately so: declaring the callback is what turns snapshot
190
+ * building on, so there is no way to pay for the work with nothing listening. Set it before
191
+ * {@link MPSessionReplay.initialize}, which is where the destination is attached — a later
192
+ * `initialize` replaces it, and there is no runtime subscribe.
193
+ *
194
+ * Snapshots arrive on a background thread natively and are delivered one per frame, so a
195
+ * slow callback queues work on the bridge. Keep it cheap.
196
+ *
197
+ * ```ts
198
+ * config.debugOptions = new MPDebugOptions({
199
+ * wireframeEmitter: (frame) => {
200
+ * const withText = frame.elements.filter((e) => e.text !== null).length;
201
+ * console.log(`[wireframe] ${frame.elements.length} elements, ${withText} with text`);
202
+ * },
203
+ * });
204
+ * ```
205
+ *
206
+ * - **Default:** `null` (no snapshots are built)
207
+ */
208
+
209
+ constructor({
210
+ overlayColors = new MPDebugOverlayColors(),
211
+ wireframeEmitter = null
212
+ } = {}) {
213
+ this.overlayColors = overlayColors;
214
+ this.wireframeEmitter = wireframeEmitter;
215
+ }
216
+ }
217
+ function processOverlayColor(color) {
218
+ if (color === null || color === undefined) {
219
+ return null;
220
+ }
221
+ // `processColor` returns the ARGB integer in the form expected by each native platform:
222
+ // a signed 32-bit Int on Android (kotlinx.serialization Int) and an unsigned 32-bit
223
+ // value on iOS (Swift Int is 64-bit, so it decodes without overflow). Invalid color
224
+ // strings yield `undefined` — treat those the same as `null` (hide the category).
225
+ const processed = processColor(color);
226
+ if (typeof processed !== 'number') {
227
+ console.warn(`MixpanelSessionReplay: invalid debug overlay color ${JSON.stringify(color)} — that category will be hidden from the overlay.`);
228
+ return null;
229
+ }
230
+ return processed;
231
+ }
232
+ function serializeDebugOptions(debugOptions) {
233
+ if (debugOptions === null) {
234
+ return null;
235
+ }
236
+ // No `__DEV__` gate: `debugOptions` is passed through in every bundle, the same as
237
+ // configuring it natively. Each SDK decides for itself what a release build may do —
238
+ // the overlay is restricted to debuggable builds by the native SDKs (`FLAG_DEBUGGABLE`
239
+ // on Android, `#if DEBUG` on iOS, which holds because the pod ships source, not a
240
+ // prebuilt binary) — so JavaScript does not need a second, differently-shaped opinion.
241
+ const overlayColors = debugOptions.overlayColors;
242
+ return {
243
+ overlayColors: overlayColors === null ? null : {
244
+ maskColor: processOverlayColor(overlayColors.maskColor),
245
+ autoMaskColor: processOverlayColor(overlayColors.autoMaskColor),
246
+ unmaskColor: processOverlayColor(overlayColors.unmaskColor),
247
+ alpha: overlayColors.alpha
248
+ },
249
+ // This package's own debug switch, not SDK configuration: neither SDK models the key, and
250
+ // both ignore it when they decode this payload. Their `wireframeEmitter` is a callback and
251
+ // cannot travel here, so each bridge reads the flag and attaches the destination itself.
252
+ // Keeping it out of the SDKs' public `DebugOptions` keeps an RN-only knob off their native
253
+ // API surface, where it would do nothing.
254
+ //
255
+ // Derived rather than declared: a caller who wants snapshots supplies a callback, and the
256
+ // flag follows from that. It is never possible to ask the SDKs to build snapshots that
257
+ // nothing in JavaScript is waiting for.
258
+ emitWireframes: debugOptions.wireframeEmitter !== null
259
+ };
260
+ }
261
+
262
+ /**
263
+ * A rule that rewrites or drops the text of a wireframe element when it matches.
264
+ *
265
+ * Build rules with the {@link MPSensitiveRule} factories rather than by hand — the object
266
+ * shape below is the wire format the native SDKs decode, not an API to write literals
267
+ * against.
268
+ *
269
+ * Rules run in the order you list them in {@link MPWireframesOptions.sensitiveRules},
270
+ * after the SDK's own masking, over whatever text is left — including text you declared
271
+ * with the `wireframeText` prop on {@link MPSessionReplayView}. A matching strip rule
272
+ * drops the text and stops immediately; a matching redact rule rewrites the text, and any
273
+ * rule after it sees the rewritten value.
274
+ */
275
+
276
+ /** Replacement used by the redact rules when you do not supply one. */
277
+ export const MP_DEFAULT_REPLACEMENT = '[REDACTED]';
278
+
279
+ /**
280
+ * Factories for the four {@link MPSensitiveRule} variants.
281
+ *
282
+ * ```ts
283
+ * new MPWireframesOptions({
284
+ * sensitiveRules: [
285
+ * MPSensitiveRule.strip('password'),
286
+ * MPSensitiveRule.redactRegex(/[\w.+-]+@[\w-]+\.[\w.]+/g, '[EMAIL]'),
287
+ * ],
288
+ * });
289
+ * ```
290
+ */
291
+ export const MPSensitiveRule = {
292
+ /**
293
+ * Replaces case-insensitive substring matches of `text` with `replacement`, leaving the
294
+ * surrounding text intact.
295
+ */
296
+ redact(text, replacement = MP_DEFAULT_REPLACEMENT) {
297
+ return {
298
+ type: 'redact',
299
+ text,
300
+ replacement
301
+ };
302
+ },
303
+ /** Drops the element's text entirely if it contains `text` (case-insensitive). */
304
+ strip(text) {
305
+ return {
306
+ type: 'strip',
307
+ text
308
+ };
309
+ },
310
+ /**
311
+ * Replaces matches of `regex` with `replacement`, leaving surrounding text intact.
312
+ *
313
+ * Only the `i`, `m` and `s` flags carry over to native; see
314
+ * {@link MPWireframesOptions} for what the regex itself is subject to.
315
+ */
316
+ redactRegex(regex, replacement = MP_DEFAULT_REPLACEMENT) {
317
+ return {
318
+ type: 'redactRegex',
319
+ regex,
320
+ replacement
321
+ };
322
+ },
323
+ /** Drops the element's text entirely if `regex` finds a match anywhere in the text. */
324
+ stripRegex(regex) {
325
+ return {
326
+ type: 'stripRegex',
327
+ regex
328
+ };
329
+ }
330
+ };
331
+
332
+ /**
333
+ * Enables wireframe capture for Session Replay.
334
+ *
335
+ * **Beta.** Wireframes are in beta. Before shipping to production, inspect the wireframes
336
+ * your app produces with {@link MPDebugOptions.wireframeEmitter} and confirm that no
337
+ * sensitive information is captured.
338
+ *
339
+ * A wireframe is a lightweight text outline of a screen: the visible elements, what kind
340
+ * each one is (text, button, input, image), what it says, and where it sits. One is
341
+ * captured alongside each screenshot, which lets Mixpanel summarize what your users saw
342
+ * and did without anyone having to watch the replay.
343
+ *
344
+ * Pass one to {@link MPSessionReplayConfig.wireframesOptions} to turn wireframes on.
345
+ * Leave it `null` — the default — and none are captured.
346
+ *
347
+ * Mixpanel can also turn wireframe capture off for a project from the server. The native
348
+ * SDKs honor that switch: the rest of session replay keeps recording, only the wireframe
349
+ * payload is dropped, and the reason is logged at init.
350
+ *
351
+ * ### Your masking settings are respected
352
+ *
353
+ * Anything hidden in the replay video is also removed from the wireframe. The element is
354
+ * still listed so the shape of the screen is preserved, but its text is dropped. That
355
+ * includes anything inside a `<MPSessionReplayView sensitive>`, anything covered by
356
+ * {@link MPSessionReplayConfig.autoMaskedViews}, every `TextInput`, and anything sitting
357
+ * underneath a masked area even if it was never marked sensitive itself.
358
+ *
359
+ * Note what this means for the defaults: `autoMaskedViews` masks `Text` out of the box, so
360
+ * with the default configuration every element ships as a textless shell. Narrow
361
+ * `autoMaskedViews` (and mask what matters with `<MPSessionReplayView sensitive>`) for
362
+ * wireframes to carry readable copy.
363
+ *
364
+ * ### Describing an element yourself
365
+ *
366
+ * The `wireframeText` prop on {@link MPSessionReplayView} supplies the text for an element
367
+ * — useful for custom-drawn content, or anywhere the text picked up automatically isn't
368
+ * meaningful. Because you authored it rather than the SDK scraping it off the screen, it is
369
+ * sent even for an element you've masked in the video.
370
+ *
371
+ * ### Catching sensitive content by pattern
372
+ *
373
+ * {@link sensitiveRules} match on the text itself and run last, over everything above,
374
+ * including text you supplied with `wireframeText`.
375
+ */
376
+ export class MPWireframesOptions {
377
+ /**
378
+ * Rules applied to each element's text before it is sent, in the order you declare them.
379
+ * Elements are always kept; only their text is affected.
380
+ *
381
+ * - A matching strip rule drops the text and stops — no later rule runs.
382
+ * - Redact rules build on one another: a rule declared later sees the result of the ones
383
+ * before it, not the original text.
384
+ *
385
+ * A `RegExp` is compiled by the platform's own engine — `java.util.regex` on Android,
386
+ * ICU on iOS — not by JavaScript. A pattern that is valid JS may behave differently, or
387
+ * be rejected outright, on one of them; a rejected pattern fails `initialize` rather
388
+ * than silently matching nothing. Only the `i`, `m` and `s` flags cross the bridge (they
389
+ * are the three that mean the same thing in all three engines); `g` is implied, since
390
+ * every match is replaced, and any other flag is ignored with a warning. Replacement
391
+ * strings are literal — `$1` is emitted verbatim, not as a back-reference.
392
+ *
393
+ * - **Default:** `[]`
394
+ */
395
+
396
+ /**
397
+ * Whether an element with no text of its own may fall back to its accessibility label
398
+ * (React Native's `accessibilityLabel` prop). Off by default: a label is not drawn on
399
+ * screen, so unlike visible text you cannot confirm what it contains by watching the
400
+ * replay, and labels sometimes hold more than what is shown.
401
+ *
402
+ * Turn it on if you want icons and image buttons named. For an icon-only control the
403
+ * label is usually the only description of what the element is for, and with the fallback
404
+ * off it is sent as a bare shell instead — `wireframeText` is then the only way to
405
+ * describe it.
406
+ *
407
+ * The label is only ever a fallback. Text you set with `wireframeText` wins over it, an
408
+ * element's own visible text wins over it, and a masked element stays textless either way.
409
+ *
410
+ * - **Default:** `false`
411
+ */
412
+
413
+ constructor({
414
+ sensitiveRules = [],
415
+ useAccessibilityLabelFallback = false
416
+ } = {}) {
417
+ this.sensitiveRules = sensitiveRules;
418
+ this.useAccessibilityLabelFallback = useAccessibilityLabelFallback;
419
+ }
420
+ }
421
+
422
+ /**
423
+ * Flags a `RegExp` carries that mean the same thing in JavaScript, `java.util.regex` and
424
+ * ICU. `g` is deliberately accepted and dropped: the native rules always replace every
425
+ * match, so a global regex is what a caller means either way.
426
+ */
427
+ const SUPPORTED_REGEX_FLAGS = 'imsg';
428
+ function serializeRegex(regex, ruleType) {
429
+ const unsupported = [...regex.flags].filter(flag => !SUPPORTED_REGEX_FLAGS.includes(flag));
430
+ if (unsupported.length > 0) {
431
+ console.warn(`MixpanelSessionReplay: the ${unsupported.join('')} flag(s) on ${regex} (${ruleType}) are not supported by the native regex engines and will be ignored.`);
432
+ }
433
+ return {
434
+ pattern: regex.source,
435
+ caseInsensitive: regex.flags.includes('i'),
436
+ multiline: regex.flags.includes('m'),
437
+ dotMatchesAll: regex.flags.includes('s')
438
+ };
439
+ }
440
+
441
+ /**
442
+ * The `wireframesOptions` payload, in the one shape both native SDKs decode.
443
+ *
444
+ * Unlike `autoMaskedViews` and `remoteSettingsMode`, this needs no per-platform
445
+ * transformation: Android's `SensitiveRuleSerializer` and iOS's `MPSensitiveRule`
446
+ * decoder were written against the same field names and `type` tokens, and each
447
+ * platform's suite pins them as literal text.
448
+ */
449
+ function serializeWireframesOptions(options) {
450
+ if (options === null || options === undefined) {
451
+ return null;
452
+ }
453
+ return {
454
+ sensitiveRules: options.sensitiveRules.map(rule => {
455
+ switch (rule.type) {
456
+ case 'redact':
457
+ return {
458
+ type: rule.type,
459
+ text: rule.text,
460
+ replacement: rule.replacement
461
+ };
462
+ case 'strip':
463
+ return {
464
+ type: rule.type,
465
+ text: rule.text
466
+ };
467
+ case 'redactRegex':
468
+ return {
469
+ type: rule.type,
470
+ ...serializeRegex(rule.regex, rule.type),
471
+ replacement: rule.replacement
472
+ };
473
+ case 'stripRegex':
474
+ return {
475
+ type: rule.type,
476
+ ...serializeRegex(rule.regex, rule.type)
477
+ };
478
+ }
479
+ }),
480
+ useAccessibilityLabelFallback: options.useAccessibilityLabelFallback
481
+ };
482
+ }
18
483
  export class MPSessionReplayConfig {
19
484
  /**
20
485
  * Determines whether replay events will only be flushed to the server when the device has a WiFi connection.
@@ -88,6 +553,53 @@ export class MPSessionReplayConfig {
88
553
  * - **Default:** `disabled`
89
554
  */
90
555
 
556
+ /**
557
+ * Enables on-device debug features such as the visual mask overlay.
558
+ *
559
+ * When non-null, the SDK renders a colored overlay on top of the app showing which
560
+ * regions are masked, auto-masked, or explicitly unmasked — useful for verifying
561
+ * masking coverage during development. Pass `new MPDebugOptions()` to enable the
562
+ * overlay with default colors, or customize via `MPDebugOverlayColors`.
563
+ *
564
+ * The overlay only renders in a debuggable build — the native SDKs enforce that, so it
565
+ * is safe to leave the option set unconditionally and the overlay will not reach end
566
+ * users. {@link MPDebugOptions.wireframeEmitter}, the other member, has no such
567
+ * restriction and runs wherever you configure it.
568
+ *
569
+ * - **Default:** `null` (disabled)
570
+ */
571
+
572
+ /**
573
+ * Base URL used to send session replay data to Mixpanel.
574
+ *
575
+ * Use one of the {@link MPDataResidency} constants to target a managed region
576
+ * (`MPDataResidency.US`, `MPDataResidency.EU`, `MPDataResidency.IN`), or pass any
577
+ * fully-qualified `https://` URL to route traffic through a custom endpoint such
578
+ * as a corporate proxy.
579
+ *
580
+ * The URL must use `https://`. The underlying native SDKs validate the URL
581
+ * during `initialize` — invalid URLs cause `initialize` to reject with an error
582
+ * on both platforms.
583
+ *
584
+ * - **Default:** `MPDataResidency.US` (`https://api.mixpanel.com`)
585
+ */
586
+
587
+ /**
588
+ * Enables wireframe capture: a text outline of each captured screen — the visible
589
+ * elements, what they say, and where they sit — recorded alongside the replay so
590
+ * sessions can be summarized without watching them.
591
+ *
592
+ * Pass `new MPWireframesOptions()` to turn it on with the defaults, or customize the
593
+ * content rules and the accessibility-label fallback. Your masking settings apply to
594
+ * it; see {@link MPWireframesOptions}.
595
+ *
596
+ * **Beta.** Wireframes are in beta. Before shipping to production, inspect the wireframes
597
+ * your app produces and confirm that no sensitive information is captured; see
598
+ * {@link MPWireframesOptions}.
599
+ *
600
+ * - **Default:** `null` (wireframe capture disabled)
601
+ */
602
+
91
603
  constructor({
92
604
  wifiOnly = true,
93
605
  autoStartRecording = true,
@@ -95,7 +607,10 @@ export class MPSessionReplayConfig {
95
607
  autoMaskedViews = [MPSessionReplayMask.Image, MPSessionReplayMask.Text, MPSessionReplayMask.Web, MPSessionReplayMask.Map],
96
608
  flushInterval = 10,
97
609
  enableLogging = false,
98
- remoteSettingsMode = MPSessionReplayRemoteSettingsMode.Disabled
610
+ remoteSettingsMode = MPSessionReplayRemoteSettingsMode.Disabled,
611
+ debugOptions = null,
612
+ serverURL = MPDataResidency.US,
613
+ wireframesOptions = null
99
614
  } = {}) {
100
615
  this.wifiOnly = wifiOnly;
101
616
  this.autoStartRecording = autoStartRecording;
@@ -104,6 +619,9 @@ export class MPSessionReplayConfig {
104
619
  this.flushInterval = flushInterval;
105
620
  this.enableLogging = enableLogging;
106
621
  this.remoteSettingsMode = remoteSettingsMode;
622
+ this.debugOptions = debugOptions;
623
+ this.serverURL = serverURL;
624
+ this.wireframesOptions = wireframesOptions;
107
625
  }
108
626
  transformMaskValueForPlatform(value) {
109
627
  if (Platform.OS === 'android') {
@@ -125,10 +643,17 @@ export class MPSessionReplayConfig {
125
643
  flushInterval: this.flushInterval,
126
644
  enableLogging: this.enableLogging,
127
645
  remoteSettingsMode: transformedRemoteSettingsMode,
128
- // iOS-specific config to enable session replay on iOS 26 and later
646
+ debugOptions: serializeDebugOptions(this.debugOptions),
647
+ wireframesOptions: serializeWireframesOptions(this.wireframesOptions),
648
+ // The native SDKs spell the server URL field differently — Android's
649
+ // kotlinx.serialization expects `serverUrl`, iOS's Codable expects `serverURL`.
129
650
  ...Platform.select({
130
651
  ios: {
131
- enableSessionReplayOniOS26AndLater: true
652
+ enableSessionReplayOniOS26AndLater: true,
653
+ serverURL: this.serverURL
654
+ },
655
+ android: {
656
+ serverUrl: this.serverURL
132
657
  },
133
658
  default: {}
134
659
  })
@@ -165,7 +690,28 @@ async function initialize(token, distinctId, config) {
165
690
  throw new Error('recordingSessionsPercent must be between 0 and 100');
166
691
  }
167
692
  const json = config.toJSON();
168
- return MixpanelReactNativeSessionReplay.initialize(token, distinctId, json);
693
+ // Attached before the native call so no frame can be emitted before the destination
694
+ // exists, then reconciled once the call settles.
695
+ //
696
+ // `initialize` is async and nothing stops an app calling it twice, so the two paths
697
+ // below hold one invariant between them: a call that succeeds ends up owning the
698
+ // subscription, and a call that fails never leaves one behind that outlives it.
699
+ // Without both halves an overlap silently ends with no destination attached at all —
700
+ // the newer call tears its own down on failure, while the older one has already had
701
+ // its callback replaced and would never restore it.
702
+ const generation = attachWireframeEmitter(config.debugOptions?.wireframeEmitter);
703
+ try {
704
+ const result = await MixpanelReactNativeSessionReplay.initialize(token, distinctId, json);
705
+ if (generation !== wireframeGeneration) {
706
+ // Another initialize attached after this one started. This call is the one that
707
+ // succeeded, so its configuration is the one that should be delivering.
708
+ attachWireframeEmitter(config.debugOptions?.wireframeEmitter);
709
+ }
710
+ return result;
711
+ } catch (error) {
712
+ detachWireframeEmitterIfCurrent(generation);
713
+ throw error;
714
+ }
169
715
  }
170
716
 
171
717
  /**
@@ -240,6 +786,78 @@ async function getReplayId() {
240
786
  async function flush() {
241
787
  return MixpanelReactNativeSessionReplay.flush();
242
788
  }
789
+
790
+ /**
791
+ * Presents one shape to JavaScript regardless of which SDK produced the payload.
792
+ *
793
+ * A textless element is `"text": null` on both platforms now, but iOS used to *omit* the
794
+ * key — Swift's synthesized `Encodable` uses `encodeIfPresent` for optionals — which
795
+ * reached JS as `undefined` and printed as the string `"undefined"` in anything that only
796
+ * checked for `null`. The SDK is fixed; this keeps the declared
797
+ * `text: string | null` contract true against an older one too.
798
+ */
799
+ function normalizeSnapshot(raw) {
800
+ return {
801
+ ...raw,
802
+ elements: (raw.elements ?? []).map(element => ({
803
+ ...element,
804
+ text: element.text ?? null
805
+ }))
806
+ };
807
+ }
808
+
809
+ /**
810
+ * Points the native wireframe event at {@link MPDebugOptions.wireframeEmitter}, replacing
811
+ * whatever a previous `initialize` attached.
812
+ *
813
+ * There is no public subscribe. The callback is declared on the config beside the switch that
814
+ * turns wireframes on, which is how Android, iOS and Flutter spell it too, and it means the
815
+ * one-callback lifetime is the instance's rather than something a caller has to remember to
816
+ * tear down.
817
+ */
818
+ function attachWireframeEmitter(listener) {
819
+ wireframeSubscription?.remove();
820
+ wireframeSubscription = null;
821
+ const generation = ++wireframeGeneration;
822
+ if (!listener) {
823
+ return generation;
824
+ }
825
+ wireframeSubscription = getWireframeEventEmitter().addListener(WIREFRAME_EVENT, payload => {
826
+ let snapshot;
827
+ try {
828
+ snapshot = normalizeSnapshot(JSON.parse(payload));
829
+ } catch (error) {
830
+ // The payload is whichever SDK's `toJson()` produced it. Report and drop the
831
+ // frame rather than throwing out of the emitter callback, which would take the
832
+ // subscription down with it.
833
+ console.warn(`MixpanelSessionReplay: could not parse a wireframe snapshot: ${error}`);
834
+ return;
835
+ }
836
+ try {
837
+ listener(snapshot);
838
+ } catch (error) {
839
+ // A consumer callback is application code. Keep it from escaping through React
840
+ // Native's synchronous EventEmitter and destabilizing the host app or later frames.
841
+ console.warn(`MixpanelSessionReplay: wireframe callback threw: ${error}`);
842
+ }
843
+ });
844
+ return generation;
845
+ }
846
+
847
+ /**
848
+ * Detaches only if the live subscription is still the one `generation` installed.
849
+ *
850
+ * `initialize` is async and nothing stops an app calling it twice. Without this check, an
851
+ * earlier call that rejects would tear down the subscription a *later*, successful call had
852
+ * already installed, and that configuration would silently stop receiving snapshots.
853
+ */
854
+ function detachWireframeEmitterIfCurrent(generation) {
855
+ if (generation !== wireframeGeneration) {
856
+ return;
857
+ }
858
+ wireframeSubscription?.remove();
859
+ wireframeSubscription = null;
860
+ }
243
861
  export { MPSessionReplayView } from "./MixpanelSessionReplayView.js";
244
862
  export const MPSessionReplay = {
245
863
  initialize,