@oodle-ai/mobile-sdk-reactnative 0.0.0-stage → 0.1.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 (166) hide show
  1. package/LICENSE +201 -0
  2. package/OodleMobileSdkReactNative.podspec +24 -0
  3. package/README.md +191 -2
  4. package/android/build.gradle +55 -0
  5. package/android/proguard-rules.pro +5 -0
  6. package/android/src/main/AndroidManifest.xml +4 -0
  7. package/android/src/main/java/ai/oodle/rum/Connectivity.kt +19 -0
  8. package/android/src/main/java/ai/oodle/rum/CrashReporter.kt +88 -0
  9. package/android/src/main/java/ai/oodle/rum/DeviceInfo.kt +50 -0
  10. package/android/src/main/java/ai/oodle/rum/ExitReasons.kt +104 -0
  11. package/android/src/main/java/ai/oodle/rum/NativeEvents.kt +24 -0
  12. package/android/src/main/java/ai/oodle/rum/OodleRumModule.kt +96 -0
  13. package/android/src/main/java/ai/oodle/rum/OodleRumPackage.kt +26 -0
  14. package/android/src/main/java/ai/oodle/rum/Storage.kt +84 -0
  15. package/android/src/main/java/ai/oodle/rum/Vitals.kt +188 -0
  16. package/android/src/main/java/ai/oodle/rum/Watchdog.kt +81 -0
  17. package/android/src/main/java/ai/oodle/rum/WireframeCapture.kt +398 -0
  18. package/bin/oodle-rum-upload.mjs +104 -0
  19. package/ios/OodleCrashReporter.h +16 -0
  20. package/ios/OodleCrashReporter.m +202 -0
  21. package/ios/OodleRum.h +18 -0
  22. package/ios/OodleRum.mm +129 -0
  23. package/ios/OodleRumImpl.swift +450 -0
  24. package/ios/WireframeWalker.swift +279 -0
  25. package/lib/core/clock.d.ts +19 -0
  26. package/lib/core/clock.js +47 -0
  27. package/lib/core/clock.js.map +1 -0
  28. package/lib/core/config.d.ts +142 -0
  29. package/lib/core/config.js +178 -0
  30. package/lib/core/config.js.map +1 -0
  31. package/lib/core/consent.d.ts +5 -0
  32. package/lib/core/consent.js +33 -0
  33. package/lib/core/consent.js.map +1 -0
  34. package/lib/core/context.d.ts +36 -0
  35. package/lib/core/context.js +172 -0
  36. package/lib/core/context.js.map +1 -0
  37. package/lib/core/flags.d.ts +3 -0
  38. package/lib/core/flags.js +20 -0
  39. package/lib/core/flags.js.map +1 -0
  40. package/lib/core/ids.d.ts +7 -0
  41. package/lib/core/ids.js +40 -0
  42. package/lib/core/ids.js.map +1 -0
  43. package/lib/core/internal.d.ts +6 -0
  44. package/lib/core/internal.js +10 -0
  45. package/lib/core/internal.js.map +1 -0
  46. package/lib/core/logger.d.ts +8 -0
  47. package/lib/core/logger.js +38 -0
  48. package/lib/core/logger.js.map +1 -0
  49. package/lib/core/native.d.ts +71 -0
  50. package/lib/core/native.js +156 -0
  51. package/lib/core/native.js.map +1 -0
  52. package/lib/core/rate-limiter.d.ts +7 -0
  53. package/lib/core/rate-limiter.js +31 -0
  54. package/lib/core/rate-limiter.js.map +1 -0
  55. package/lib/core/session.d.ts +51 -0
  56. package/lib/core/session.js +196 -0
  57. package/lib/core/session.js.map +1 -0
  58. package/lib/core/size.d.ts +15 -0
  59. package/lib/core/size.js +68 -0
  60. package/lib/core/size.js.map +1 -0
  61. package/lib/core/tags.d.ts +10 -0
  62. package/lib/core/tags.js +33 -0
  63. package/lib/core/tags.js.map +1 -0
  64. package/lib/core/telemetry.d.ts +6 -0
  65. package/lib/core/telemetry.js +37 -0
  66. package/lib/core/telemetry.js.map +1 -0
  67. package/lib/core/tracing.d.ts +9 -0
  68. package/lib/core/tracing.js +57 -0
  69. package/lib/core/tracing.js.map +1 -0
  70. package/lib/core/transport.d.ts +76 -0
  71. package/lib/core/transport.js +560 -0
  72. package/lib/core/transport.js.map +1 -0
  73. package/lib/core/user.d.ts +12 -0
  74. package/lib/core/user.js +41 -0
  75. package/lib/core/user.js.map +1 -0
  76. package/lib/events/actions.d.ts +13 -0
  77. package/lib/events/actions.js +147 -0
  78. package/lib/events/actions.js.map +1 -0
  79. package/lib/events/emit.d.ts +21 -0
  80. package/lib/events/emit.js +144 -0
  81. package/lib/events/emit.js.map +1 -0
  82. package/lib/events/errors.d.ts +19 -0
  83. package/lib/events/errors.js +257 -0
  84. package/lib/events/errors.js.map +1 -0
  85. package/lib/events/lifecycle.d.ts +7 -0
  86. package/lib/events/lifecycle.js +101 -0
  87. package/lib/events/lifecycle.js.map +1 -0
  88. package/lib/events/longtasks.d.ts +2 -0
  89. package/lib/events/longtasks.js +42 -0
  90. package/lib/events/longtasks.js.map +1 -0
  91. package/lib/events/resources.d.ts +15 -0
  92. package/lib/events/resources.js +156 -0
  93. package/lib/events/resources.js.map +1 -0
  94. package/lib/events/tap-target.d.ts +42 -0
  95. package/lib/events/tap-target.js +191 -0
  96. package/lib/events/tap-target.js.map +1 -0
  97. package/lib/events/view-state.d.ts +13 -0
  98. package/lib/events/view-state.js +25 -0
  99. package/lib/events/view-state.js.map +1 -0
  100. package/lib/events/views.d.ts +21 -0
  101. package/lib/events/views.js +347 -0
  102. package/lib/events/views.js.map +1 -0
  103. package/lib/index.d.ts +16 -0
  104. package/lib/index.js +24 -0
  105. package/lib/index.js.map +1 -0
  106. package/lib/navigation/react-navigation.d.ts +33 -0
  107. package/lib/navigation/react-navigation.js +61 -0
  108. package/lib/navigation/react-navigation.js.map +1 -0
  109. package/lib/react/OodlePrivacy.d.ts +15 -0
  110. package/lib/react/OodlePrivacy.js +13 -0
  111. package/lib/react/OodlePrivacy.js.map +1 -0
  112. package/lib/react/OodleProvider.d.ts +18 -0
  113. package/lib/react/OodleProvider.js +72 -0
  114. package/lib/react/OodleProvider.js.map +1 -0
  115. package/lib/replay/recorder.d.ts +22 -0
  116. package/lib/replay/recorder.js +317 -0
  117. package/lib/replay/recorder.js.map +1 -0
  118. package/lib/replay/rrweb.d.ts +105 -0
  119. package/lib/replay/rrweb.js +335 -0
  120. package/lib/replay/rrweb.js.map +1 -0
  121. package/lib/rum.d.ts +52 -0
  122. package/lib/rum.js +281 -0
  123. package/lib/rum.js.map +1 -0
  124. package/lib/specs/NativeOodleRum.d.ts +43 -0
  125. package/lib/specs/NativeOodleRum.js +5 -0
  126. package/lib/specs/NativeOodleRum.js.map +1 -0
  127. package/lib/version.d.ts +1 -0
  128. package/lib/version.js +6 -0
  129. package/lib/version.js.map +1 -0
  130. package/package.json +91 -4
  131. package/react-native.config.js +10 -0
  132. package/src/core/clock.ts +43 -0
  133. package/src/core/config.ts +344 -0
  134. package/src/core/consent.ts +32 -0
  135. package/src/core/context.ts +189 -0
  136. package/src/core/flags.ts +15 -0
  137. package/src/core/ids.ts +35 -0
  138. package/src/core/internal.ts +6 -0
  139. package/src/core/logger.ts +37 -0
  140. package/src/core/native.ts +201 -0
  141. package/src/core/rate-limiter.ts +34 -0
  142. package/src/core/session.ts +209 -0
  143. package/src/core/size.ts +79 -0
  144. package/src/core/tags.ts +30 -0
  145. package/src/core/telemetry.ts +35 -0
  146. package/src/core/tracing.ts +60 -0
  147. package/src/core/transport.ts +590 -0
  148. package/src/core/user.ts +45 -0
  149. package/src/events/actions.ts +157 -0
  150. package/src/events/emit.ts +131 -0
  151. package/src/events/errors.ts +283 -0
  152. package/src/events/lifecycle.ts +102 -0
  153. package/src/events/longtasks.ts +40 -0
  154. package/src/events/resources.ts +167 -0
  155. package/src/events/tap-target.ts +210 -0
  156. package/src/events/view-state.ts +30 -0
  157. package/src/events/views.ts +356 -0
  158. package/src/index.ts +34 -0
  159. package/src/navigation/react-navigation.ts +72 -0
  160. package/src/react/OodlePrivacy.tsx +25 -0
  161. package/src/react/OodleProvider.tsx +88 -0
  162. package/src/replay/recorder.ts +310 -0
  163. package/src/replay/rrweb.ts +460 -0
  164. package/src/rum.ts +295 -0
  165. package/src/specs/NativeOodleRum.ts +51 -0
  166. package/src/version.ts +2 -0
@@ -0,0 +1,356 @@
1
+ import { getConfig } from '../core/config';
2
+ import { saveLastContext, viewUrlFor } from '../core/context';
3
+ import { uuid } from '../core/ids';
4
+ import { getDeviceInfo, native, type NativeVitals } from '../core/native';
5
+ import { getSession } from '../core/session';
6
+ import { attributesJSON, canRecord, emitViewUpdate, setViewCounter, type EventData } from './emit';
7
+ import { setCurrentViewInfo, type ViewInfo } from './view-state';
8
+ import { isoTime } from '../core/clock';
9
+
10
+ /** How often an open view is re-sent with its time spent so far. */
11
+ const VIEW_REFRESH_MS = 30_000;
12
+ /** A change in a view's counts is sent at most this often. */
13
+ const UPDATE_THROTTLE_MS = 3000;
14
+ /** A first view later than this after process start is not a cold start. */
15
+ const COLD_START_WINDOW_MS = 60_000;
16
+
17
+ interface VitalsAcc {
18
+ durationMs: number;
19
+ refreshSum: number;
20
+ refreshMin?: number;
21
+ slow: number;
22
+ frozen: number;
23
+ memSum: number;
24
+ memDuration: number;
25
+ memMax: number;
26
+ cpuSum: number;
27
+ cpuDuration: number;
28
+ }
29
+
30
+ interface ActiveView extends ViewInfo {
31
+ sessionId: string;
32
+ startMs: number;
33
+ loadingType: 'initial_load' | 'route_change';
34
+ /** Update number; readers keep the highest per view. */
35
+ seq: number;
36
+ attributes?: Record<string, unknown>;
37
+ loadingTimeMs?: number;
38
+ appStartMs?: number;
39
+ appStartType?: string;
40
+ ttfdMs?: number;
41
+ actionCount: number;
42
+ errorCount: number;
43
+ resourceCount: number;
44
+ longTaskCount: number;
45
+ frustrationCount: number;
46
+ vitals: VitalsAcc;
47
+ }
48
+
49
+ let active: ActiveView | null = null;
50
+ /** The view to restart when the app returns to the foreground. */
51
+ let paused: { key: string; name: string; attributes?: Record<string, unknown> } | null = null;
52
+ let refreshTimer: ReturnType<typeof setInterval> | null = null;
53
+ let throttleTimer: ReturnType<typeof setTimeout> | null = null;
54
+ let firstViewSeen = false;
55
+ let fullyDisplayedReported = false;
56
+ let coldStart: { ms: number; viewId: string } | null = null;
57
+ const startListeners: Array<(v: ViewInfo) => void> = [];
58
+
59
+ function emptyVitals(): VitalsAcc {
60
+ return {
61
+ durationMs: 0,
62
+ refreshSum: 0,
63
+ slow: 0,
64
+ frozen: 0,
65
+ memSum: 0,
66
+ memDuration: 0,
67
+ memMax: 0,
68
+ cpuSum: 0,
69
+ cpuDuration: 0,
70
+ };
71
+ }
72
+
73
+ export function onViewStart(fn: (v: ViewInfo) => void) {
74
+ startListeners.push(fn);
75
+ }
76
+
77
+ function mergeVitals(acc: VitalsAcc, v: NativeVitals & { duration_ms?: number }) {
78
+ const d = typeof v.duration_ms === 'number' && v.duration_ms > 0 ? v.duration_ms : 0;
79
+ if (d === 0) return;
80
+ acc.durationMs += d;
81
+ if (typeof v.refresh_rate_avg === 'number') acc.refreshSum += v.refresh_rate_avg * d;
82
+ if (typeof v.refresh_rate_min === 'number' && v.refresh_rate_min > 0) {
83
+ acc.refreshMin =
84
+ acc.refreshMin === undefined ? v.refresh_rate_min : Math.min(acc.refreshMin, v.refresh_rate_min);
85
+ }
86
+ acc.slow += v.slow_frame_count ?? 0;
87
+ acc.frozen += v.frozen_frame_count ?? 0;
88
+ if (typeof v.memory_avg_bytes === 'number' && v.memory_avg_bytes > 0) {
89
+ acc.memSum += v.memory_avg_bytes * d;
90
+ acc.memDuration += d;
91
+ }
92
+ acc.memMax = Math.max(acc.memMax, v.memory_max_bytes ?? 0);
93
+ if (typeof v.cpu_ms_per_sec === 'number') {
94
+ acc.cpuSum += v.cpu_ms_per_sec * d;
95
+ acc.cpuDuration += d;
96
+ }
97
+ }
98
+
99
+ function collectVitals() {
100
+ if (!active || !getConfig()?.vitals) return;
101
+ mergeVitals(active.vitals, native.collectVitals());
102
+ }
103
+
104
+ function vitalsFields(acc: VitalsAcc): Record<string, number> {
105
+ const out: Record<string, number> = {};
106
+ if (acc.durationMs <= 0) return out;
107
+ if (acc.refreshSum > 0) out.refresh_rate_avg = round(acc.refreshSum / acc.durationMs, 1);
108
+ if (acc.refreshMin !== undefined) out.refresh_rate_min = round(acc.refreshMin, 1);
109
+ out.slow_frame_count = acc.slow;
110
+ out.frozen_frame_count = acc.frozen;
111
+ if (acc.memDuration > 0) out.memory_avg_bytes = Math.round(acc.memSum / acc.memDuration);
112
+ if (acc.memMax > 0) out.memory_max_bytes = acc.memMax;
113
+ if (acc.cpuDuration > 0) out.cpu_ms_per_sec = round(acc.cpuSum / acc.cpuDuration, 1);
114
+ return out;
115
+ }
116
+
117
+ function round(n: number, digits: number): number {
118
+ const f = Math.pow(10, digits);
119
+ return Math.round(n * f) / f;
120
+ }
121
+
122
+ function send(v: ActiveView, final: boolean) {
123
+ collectVitals();
124
+ v.seq++;
125
+ const now = Date.now();
126
+ const d = getDeviceInfo();
127
+ const data: Record<string, unknown> = {
128
+ event_type: 'view',
129
+ timestamp: isoTime(now),
130
+ view_id: v.id,
131
+ app_screen_name: v.name,
132
+ view_loading_type: v.loadingType,
133
+ view_update_seq: v.seq,
134
+ view_is_active: final ? 0 : 1,
135
+ view_start: isoTime(v.startMs),
136
+ view_time_spent_ms: Math.max(0, now - v.startMs),
137
+ view_action_count: v.actionCount,
138
+ view_error_count: v.errorCount,
139
+ view_resource_count: v.resourceCount,
140
+ view_long_task_count: v.longTaskCount,
141
+ view_frustration_count: v.frustrationCount,
142
+ screen_width: Math.round(d.screenWidth),
143
+ screen_height: Math.round(d.screenHeight),
144
+ ...vitalsFields(v.vitals),
145
+ };
146
+ if (v.loadingTimeMs !== undefined) data.view_loading_time_ms = v.loadingTimeMs;
147
+ if (v.appStartMs !== undefined) {
148
+ data.app_start_ms = v.appStartMs;
149
+ data.app_start_type = v.appStartType;
150
+ }
151
+ if (v.ttfdMs !== undefined) data.app_ttfd_ms = v.ttfdMs;
152
+ const attrs = attributesJSON(v.attributes);
153
+ if (attrs) data.event_attributes = attrs;
154
+ // Only the active view is sent, and a view's last update is sent before
155
+ // the next view becomes current, so the event context is this view.
156
+ emitViewUpdate(data);
157
+ }
158
+
159
+ /** Counts an event in the view it happened in, and sends the view soon. */
160
+ function countInView(data: EventData) {
161
+ if (!active) return;
162
+ switch (data.event_type) {
163
+ case 'action':
164
+ if (data.is_frustration === 1) active.frustrationCount++;
165
+ else active.actionCount++;
166
+ break;
167
+ case 'error':
168
+ active.errorCount++;
169
+ break;
170
+ case 'resource':
171
+ active.resourceCount++;
172
+ break;
173
+ case 'long_task':
174
+ active.longTaskCount++;
175
+ break;
176
+ default:
177
+ return;
178
+ }
179
+ if (throttleTimer) return;
180
+ const view = active;
181
+ throttleTimer = setTimeout(() => {
182
+ throttleTimer = null;
183
+ if (active === view) send(view, false);
184
+ }, UPDATE_THROTTLE_MS);
185
+ }
186
+
187
+ setViewCounter(countInView);
188
+
189
+ function ensureRefreshTimer() {
190
+ if (refreshTimer) return;
191
+ refreshTimer = setInterval(() => {
192
+ if (active) send(active, false);
193
+ }, VIEW_REFRESH_MS);
194
+ }
195
+
196
+ function stopRefreshTimer() {
197
+ if (refreshTimer) clearInterval(refreshTimer);
198
+ refreshTimer = null;
199
+ if (throttleTimer) clearTimeout(throttleTimer);
200
+ throttleTimer = null;
201
+ }
202
+
203
+ function measureColdStart(view: ActiveView) {
204
+ const processStart = getDeviceInfo().processStartMs;
205
+ if (!processStart || view.startMs - processStart > COLD_START_WINDOW_MS) return;
206
+ const done = () => {
207
+ const ms = Date.now() - processStart;
208
+ coldStart = { ms, viewId: view.id };
209
+ view.appStartMs = ms;
210
+ view.appStartType = 'cold';
211
+ if (active === view) send(view, false);
212
+ };
213
+ // The frame after the first view's render is the first one the user sees.
214
+ if (typeof requestAnimationFrame === 'function') {
215
+ requestAnimationFrame(() => setTimeout(done, 0));
216
+ } else {
217
+ setTimeout(done, 0);
218
+ }
219
+ }
220
+
221
+ export function startView(
222
+ key: string,
223
+ name?: string,
224
+ attributes?: Record<string, unknown>,
225
+ startMs = Date.now(),
226
+ ) {
227
+ if (!canRecord()) return;
228
+ if (active) {
229
+ if (active.key === key) return;
230
+ stopView(active.key);
231
+ }
232
+ const viewName = name || key;
233
+ const { url, path } = viewUrlFor(viewName);
234
+ // Start or renew the session first, so a renewal cannot restart the
235
+ // view that is being created here.
236
+ const sessionId = getSession(true).id;
237
+ if (getConfig()?.vitals) native.collectVitals(); // reset the interval
238
+ const view: ActiveView = {
239
+ id: uuid(),
240
+ sessionId,
241
+ key,
242
+ name: viewName,
243
+ url,
244
+ path,
245
+ startMs,
246
+ loadingType: firstViewSeen ? 'route_change' : 'initial_load',
247
+ seq: 0,
248
+ attributes,
249
+ actionCount: 0,
250
+ errorCount: 0,
251
+ resourceCount: 0,
252
+ longTaskCount: 0,
253
+ frustrationCount: 0,
254
+ vitals: emptyVitals(),
255
+ };
256
+ active = view;
257
+ paused = null;
258
+ setCurrentViewInfo(view);
259
+ send(view, false);
260
+ saveLastContext();
261
+ ensureRefreshTimer();
262
+ if (!firstViewSeen) {
263
+ firstViewSeen = true;
264
+ measureColdStart(view);
265
+ }
266
+ for (const fn of startListeners) {
267
+ try {
268
+ fn(view);
269
+ } catch {
270
+ // listeners are best effort
271
+ }
272
+ }
273
+ }
274
+
275
+ export function stopView(key?: string) {
276
+ if (!active || (key !== undefined && active.key !== key)) return;
277
+ const v = active;
278
+ active = null;
279
+ send(v, true);
280
+ stopRefreshTimer();
281
+ }
282
+
283
+ export function getActiveView(): ViewInfo | null {
284
+ return active;
285
+ }
286
+
287
+ /** Marks the current view as loaded. Sets `view_loading_time_ms`. */
288
+ export function reportViewLoaded(overwrite = false) {
289
+ if (!active) return;
290
+ if (active.loadingTimeMs !== undefined && !overwrite) return;
291
+ active.loadingTimeMs = Date.now() - active.startMs;
292
+ send(active, false);
293
+ }
294
+
295
+ /** Time to full display: the app start until its first screen has its data. */
296
+ export function reportAppFullyDisplayed() {
297
+ if (fullyDisplayedReported) return;
298
+ const processStart = getDeviceInfo().processStartMs;
299
+ if (!processStart || !coldStart) return;
300
+ fullyDisplayedReported = true;
301
+ const ms = Date.now() - processStart;
302
+ if (active) {
303
+ active.ttfdMs = ms;
304
+ send(active, false);
305
+ }
306
+ }
307
+
308
+ /** Time since the current view started, or -1 when there is none. */
309
+ export function viewElapsedMs(): number {
310
+ return active ? Date.now() - active.startMs : -1;
311
+ }
312
+
313
+ export function addViewAttributes(attrs: Record<string, unknown>) {
314
+ if (!active) return;
315
+ active.attributes = { ...(active.attributes ?? {}), ...attrs };
316
+ }
317
+
318
+ /** Ends the view while the app is in the background, so time away is not counted. */
319
+ export function pauseViews() {
320
+ if (!active) return;
321
+ paused = { key: active.key, name: active.name, attributes: active.attributes };
322
+ stopView(active.key);
323
+ }
324
+
325
+ /** Starts the paused view again as a new view instance. */
326
+ export function resumeViews() {
327
+ if (!paused || active) return;
328
+ const p = paused;
329
+ paused = null;
330
+ startView(p.key, p.name, p.attributes);
331
+ }
332
+
333
+ /** Session ended: the open view belongs to the old session. */
334
+ export function restartViewForNewSession(sessionId: string) {
335
+ if (!active || active.sessionId === sessionId) return;
336
+ const { key, name, attributes } = active;
337
+ active = null;
338
+ stopRefreshTimer();
339
+ startView(key, name, attributes);
340
+ }
341
+
342
+ export function flushActiveView() {
343
+ if (active) send(active, false);
344
+ }
345
+
346
+ /** Test hook. */
347
+ export function resetViews() {
348
+ stopRefreshTimer();
349
+ active = null;
350
+ paused = null;
351
+ firstViewSeen = false;
352
+ fullyDisplayedReported = false;
353
+ coldStart = null;
354
+ startListeners.length = 0;
355
+ setCurrentViewInfo(null);
356
+ }
package/src/index.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { isRecording, startRecording, stopRecording } from './replay/recorder';
2
+
3
+ export { OodleRum } from './rum';
4
+ export { OodleProvider, type OodleProviderProps } from './react/OodleProvider';
5
+ export { OodlePrivacy, type OodlePrivacyProps, type PrivacyLevel } from './react/OodlePrivacy';
6
+ export {
7
+ trackNavigation,
8
+ useOodleNavigationTracking,
9
+ type NavigationRefLike,
10
+ type NavigationTrackingOptions,
11
+ } from './navigation/react-navigation';
12
+ export type {
13
+ OodleRumConfig,
14
+ SessionReplayConfig,
15
+ TrackingConsent,
16
+ TracingUrl,
17
+ Propagator,
18
+ TextAndInputPrivacy,
19
+ TouchPrivacy,
20
+ ImagePrivacy,
21
+ UrlMatcher,
22
+ TagValue,
23
+ } from './core/config';
24
+ export type { UserInfo } from './core/user';
25
+ export { SDK_VERSION } from './version';
26
+
27
+ /** Session replay controls. Recording also needs consent `granted`. */
28
+ export const OodleSessionReplay = {
29
+ /** Starts recording, if the session is sampled for replay. */
30
+ start: startRecording,
31
+ /** Stops recording. Already recorded segments are still sent. */
32
+ stop: stopRecording,
33
+ isRecording,
34
+ };
@@ -0,0 +1,72 @@
1
+ import { useEffect } from 'react';
2
+ import { OodleRum } from '../rum';
3
+
4
+ /** The parts of a react-navigation container ref the SDK uses. */
5
+ export interface NavigationRefLike {
6
+ current?: NavigationContainerLike | null;
7
+ getCurrentRoute?: () => { key: string; name: string } | undefined;
8
+ addListener?: (event: 'state', cb: () => void) => () => void;
9
+ isReady?: () => boolean;
10
+ }
11
+
12
+ interface NavigationContainerLike {
13
+ getCurrentRoute(): { key: string; name: string } | undefined;
14
+ addListener(event: 'state', cb: () => void): () => void;
15
+ isReady?(): boolean;
16
+ }
17
+
18
+ function container(ref: NavigationRefLike): NavigationContainerLike | null {
19
+ // createNavigationContainerRef() returns the container itself; useRef()
20
+ // holds it in `current`.
21
+ if (typeof ref.getCurrentRoute === 'function' && typeof ref.addListener === 'function') {
22
+ return ref as NavigationContainerLike;
23
+ }
24
+ return ref.current ?? null;
25
+ }
26
+
27
+ export interface NavigationTrackingOptions {
28
+ /** Screen name for a route. Return null to skip the route. */
29
+ viewName?: (route: { key: string; name: string }) => string | null;
30
+ }
31
+
32
+ /**
33
+ * Starts a view for each screen the navigator shows. Returns a function
34
+ * that stops tracking.
35
+ */
36
+ export function trackNavigation(ref: NavigationRefLike, options: NavigationTrackingOptions = {}): () => void {
37
+ const nav = container(ref);
38
+ if (!nav) return () => {};
39
+ const update = () => {
40
+ const route = nav.getCurrentRoute();
41
+ if (!route) return;
42
+ const name = options.viewName ? options.viewName(route) : route.name;
43
+ if (name === null) return;
44
+ OodleRum.startView(route.key, name);
45
+ };
46
+ const unsubscribe = nav.addListener('state', update);
47
+ // A container that is not ready yet has no route. Its first state may
48
+ // not fire a 'state' event, so readiness is polled for a short time.
49
+ let tries = 0;
50
+ let poll: ReturnType<typeof setInterval> | null = null;
51
+ if (!nav.isReady || nav.isReady()) {
52
+ update();
53
+ } else {
54
+ poll = setInterval(() => {
55
+ tries++;
56
+ if (nav.isReady?.()) update();
57
+ if ((nav.isReady?.() || tries >= 50) && poll) {
58
+ clearInterval(poll);
59
+ poll = null;
60
+ }
61
+ }, 100);
62
+ }
63
+ return () => {
64
+ if (poll) clearInterval(poll);
65
+ unsubscribe();
66
+ };
67
+ }
68
+
69
+ /** React hook form of `trackNavigation`. */
70
+ export function useOodleNavigationTracking(ref: NavigationRefLike, options?: NavigationTrackingOptions) {
71
+ useEffect(() => trackNavigation(ref, options), [ref]);
72
+ }
@@ -0,0 +1,25 @@
1
+ import { View, type ViewProps } from 'react-native';
2
+
3
+ export type PrivacyLevel = 'hidden' | 'mask' | 'allow';
4
+
5
+ export interface OodlePrivacyProps extends ViewProps {
6
+ /**
7
+ * `hidden`: the subtree is replaced by an empty box in replays.
8
+ * `mask`: all text and input values in the subtree are masked.
9
+ * `allow`: text and input values are shown, except sensitive inputs
10
+ * such as passwords.
11
+ */
12
+ level: PrivacyLevel;
13
+ }
14
+
15
+ /** The native view tree walker reads this marker from the view's nativeID. */
16
+ export const PRIVACY_NATIVE_ID_PREFIX = 'oodle-privacy-';
17
+
18
+ /** Sets the replay privacy of everything inside it. */
19
+ export function OodlePrivacy({ level, children, ...rest }: OodlePrivacyProps) {
20
+ return (
21
+ <View {...rest} nativeID={`${PRIVACY_NATIVE_ID_PREFIX}${level}`} collapsable={false}>
22
+ {children}
23
+ </View>
24
+ );
25
+ }
@@ -0,0 +1,88 @@
1
+ import React, { useRef } from 'react';
2
+ import { StyleSheet, View, type GestureResponderEvent, type StyleProp, type ViewStyle } from 'react-native';
3
+ import { recordTap, type TouchPoint } from '../events/actions';
4
+ import { recordTouchEnd, recordTouchMove, recordTouchStart } from '../replay/recorder';
5
+ import { OodleRum } from '../rum';
6
+ import type { OodleRumConfig } from '../core/config';
7
+
8
+ export interface OodleProviderProps {
9
+ children?: React.ReactNode;
10
+ /**
11
+ * Starts the SDK during the first render, so the first screen is
12
+ * recorded. Leave it out when the app calls `OodleRum.init()` itself.
13
+ */
14
+ config?: OodleRumConfig;
15
+ style?: StyleProp<ViewStyle>;
16
+ }
17
+
18
+ function point(e: GestureResponderEvent): TouchPoint {
19
+ const n = e.nativeEvent;
20
+ return { x: n.pageX, y: n.pageY, t: Date.now() };
21
+ }
22
+
23
+ /**
24
+ * Wraps the app to record taps and replay touches. It listens in the
25
+ * capture phase and never claims the responder, so it does not change
26
+ * how any touch is handled.
27
+ */
28
+ export function OodleProvider({ children, config, style }: OodleProviderProps) {
29
+ const initialized = useRef(false);
30
+ if (config && !initialized.current) {
31
+ initialized.current = true;
32
+ OodleRum.init(config);
33
+ }
34
+ const start = useRef<{ point: TouchPoint; event: GestureResponderEvent } | null>(null);
35
+ const touchCount = useRef(0);
36
+
37
+ return (
38
+ <View
39
+ style={[styles.root, style]}
40
+ collapsable={false}
41
+ onTouchStartCapture={(e) => {
42
+ try {
43
+ const touches = e.nativeEvent.touches?.length ?? 1;
44
+ touchCount.current = touches;
45
+ const p = point(e);
46
+ // A second finger turns the gesture into a pinch, not a tap.
47
+ start.current = touches > 1 ? null : { point: p, event: e };
48
+ recordTouchStart(p.x, p.y);
49
+ } catch {
50
+ // instrumentation must never break a touch
51
+ }
52
+ }}
53
+ onTouchMoveCapture={(e) => {
54
+ try {
55
+ const p = point(e);
56
+ recordTouchMove(p.x, p.y);
57
+ } catch {
58
+ // ignore
59
+ }
60
+ }}
61
+ onTouchEndCapture={(e) => {
62
+ try {
63
+ const p = point(e);
64
+ const s = start.current;
65
+ start.current = null;
66
+ const tap = Boolean(s) && touchCount.current <= 1;
67
+ if (s && tap) recordTap(s.event, s.point, p);
68
+ recordTouchEnd(p.x, p.y, tap);
69
+ } catch {
70
+ // ignore
71
+ }
72
+ }}
73
+ onTouchCancelCapture={(e) => {
74
+ try {
75
+ start.current = null;
76
+ const p = point(e);
77
+ recordTouchEnd(p.x, p.y, false);
78
+ } catch {
79
+ // ignore
80
+ }
81
+ }}
82
+ >
83
+ {children}
84
+ </View>
85
+ );
86
+ }
87
+
88
+ const styles = StyleSheet.create({ root: { flex: 1 } });