@datadog/mobile-react-navigation 2.14.1 → 3.0.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 (189) hide show
  1. package/README.md +40 -5
  2. package/lib/commonjs/index.js.map +1 -1
  3. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js +128 -65
  4. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  5. package/lib/commonjs/rum/instrumentation/utils.js +31 -0
  6. package/lib/commonjs/rum/instrumentation/utils.js.map +1 -0
  7. package/lib/module/index.js.map +1 -1
  8. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js +127 -64
  9. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  10. package/lib/module/rum/instrumentation/utils.js +25 -0
  11. package/lib/module/rum/instrumentation/utils.js.map +1 -0
  12. package/lib/typescript/core/src/DdAttributes.d.ts.map +1 -0
  13. package/lib/typescript/core/src/DdSdkReactNative.d.ts +49 -12
  14. package/lib/typescript/core/src/DdSdkReactNative.d.ts.map +1 -1
  15. package/lib/typescript/core/src/InternalLog.d.ts +1 -1
  16. package/lib/typescript/core/src/InternalLog.d.ts.map +1 -1
  17. package/lib/typescript/core/src/config/DatadogProviderConfiguration.d.ts +12 -0
  18. package/lib/typescript/core/src/config/DatadogProviderConfiguration.d.ts.map +1 -0
  19. package/lib/typescript/core/src/config/FileBasedConfiguration.d.ts +17 -0
  20. package/lib/typescript/core/src/config/FileBasedConfiguration.d.ts.map +1 -0
  21. package/lib/typescript/core/src/config/FileBasedConfiguration.type.d.ts +52 -0
  22. package/lib/typescript/core/src/config/FileBasedConfiguration.type.d.ts.map +1 -0
  23. package/lib/typescript/core/src/config/async/AutoInstrumentationConfiguration.d.ts +60 -0
  24. package/lib/typescript/core/src/config/async/AutoInstrumentationConfiguration.d.ts.map +1 -0
  25. package/lib/typescript/core/src/config/async/PartialInitializationConfiguration.d.ts +47 -0
  26. package/lib/typescript/core/src/config/async/PartialInitializationConfiguration.d.ts.map +1 -0
  27. package/lib/typescript/core/src/config/async/asyncInitializationHelper.d.ts +5 -0
  28. package/lib/typescript/core/src/config/async/asyncInitializationHelper.d.ts.map +1 -0
  29. package/lib/typescript/core/src/config/features/CoreConfiguration.d.ts +52 -0
  30. package/lib/typescript/core/src/config/features/CoreConfiguration.d.ts.map +1 -0
  31. package/lib/typescript/core/src/config/features/CoreConfiguration.type.d.ts +90 -0
  32. package/lib/typescript/core/src/config/features/CoreConfiguration.type.d.ts.map +1 -0
  33. package/lib/typescript/core/src/config/features/CoreConfigurationNative.d.ts +54 -0
  34. package/lib/typescript/core/src/config/features/CoreConfigurationNative.d.ts.map +1 -0
  35. package/lib/typescript/core/src/config/features/LogsConfiguration.d.ts +17 -0
  36. package/lib/typescript/core/src/config/features/LogsConfiguration.d.ts.map +1 -0
  37. package/lib/typescript/core/src/config/features/LogsConfiguration.type.d.ts +24 -0
  38. package/lib/typescript/core/src/config/features/LogsConfiguration.type.d.ts.map +1 -0
  39. package/lib/typescript/core/src/config/features/LogsConfigurationNative.d.ts +6 -0
  40. package/lib/typescript/core/src/config/features/LogsConfigurationNative.d.ts.map +1 -0
  41. package/lib/typescript/core/src/config/features/RumConfiguration.d.ts +66 -0
  42. package/lib/typescript/core/src/config/features/RumConfiguration.d.ts.map +1 -0
  43. package/lib/typescript/core/src/config/features/RumConfiguration.type.d.ts +131 -0
  44. package/lib/typescript/core/src/config/features/RumConfiguration.type.d.ts.map +1 -0
  45. package/lib/typescript/core/src/config/features/RumConfigurationNative.d.ts +25 -0
  46. package/lib/typescript/core/src/config/features/RumConfigurationNative.d.ts.map +1 -0
  47. package/lib/typescript/core/src/config/features/TraceConfiguration.d.ts +10 -0
  48. package/lib/typescript/core/src/config/features/TraceConfiguration.d.ts.map +1 -0
  49. package/lib/typescript/core/src/config/features/TraceConfiguration.type.d.ts +11 -0
  50. package/lib/typescript/core/src/config/features/TraceConfiguration.type.d.ts.map +1 -0
  51. package/lib/typescript/core/src/config/features/TraceConfigurationNative.d.ts +4 -0
  52. package/lib/typescript/core/src/config/features/TraceConfigurationNative.d.ts.map +1 -0
  53. package/lib/typescript/core/src/config/types/BatchProcessingLevel.d.ts +18 -0
  54. package/lib/typescript/core/src/config/types/BatchProcessingLevel.d.ts.map +1 -0
  55. package/lib/typescript/core/src/config/types/BatchSize.d.ts +19 -0
  56. package/lib/typescript/core/src/config/types/BatchSize.d.ts.map +1 -0
  57. package/lib/typescript/core/src/config/types/InitializationMode.d.ts +5 -0
  58. package/lib/typescript/core/src/config/types/InitializationMode.d.ts.map +1 -0
  59. package/lib/typescript/core/src/config/types/InitializationModeForTelemetry.d.ts +2 -0
  60. package/lib/typescript/core/src/config/types/InitializationModeForTelemetry.d.ts.map +1 -0
  61. package/lib/typescript/core/src/config/types/ProxyConfiguration.d.ts.map +1 -0
  62. package/lib/typescript/core/src/config/types/SdkVerbosity.d.ts.map +1 -0
  63. package/lib/typescript/core/src/config/types/TrackingConsent.d.ts.map +1 -0
  64. package/lib/typescript/core/src/config/types/UploadFrequency.d.ts +18 -0
  65. package/lib/typescript/core/src/config/types/UploadFrequency.d.ts.map +1 -0
  66. package/lib/typescript/core/src/config/types/VitalsUpdateFrequency.d.ts +7 -0
  67. package/lib/typescript/core/src/config/types/VitalsUpdateFrequency.d.ts.map +1 -0
  68. package/lib/typescript/core/src/config/types/index.d.ts +10 -0
  69. package/lib/typescript/core/src/config/types/index.d.ts.map +1 -0
  70. package/lib/typescript/core/src/index.d.ts +20 -9
  71. package/lib/typescript/core/src/index.d.ts.map +1 -1
  72. package/lib/typescript/core/src/logs/DdLogs.d.ts +2 -1
  73. package/lib/typescript/core/src/logs/DdLogs.d.ts.map +1 -1
  74. package/lib/typescript/core/src/logs/eventMapper.d.ts +3 -2
  75. package/lib/typescript/core/src/logs/eventMapper.d.ts.map +1 -1
  76. package/lib/typescript/core/src/logs/types.d.ts +1 -16
  77. package/lib/typescript/core/src/logs/types.d.ts.map +1 -1
  78. package/lib/typescript/core/src/nativeModulesTypes.d.ts +2 -15
  79. package/lib/typescript/core/src/nativeModulesTypes.d.ts.map +1 -1
  80. package/lib/typescript/core/src/rum/DdRum.d.ts +10 -1
  81. package/lib/typescript/core/src/rum/DdRum.d.ts.map +1 -1
  82. package/lib/typescript/core/src/rum/eventMappers/errorEventMapper.d.ts +1 -1
  83. package/lib/typescript/core/src/rum/eventMappers/errorEventMapper.d.ts.map +1 -1
  84. package/lib/typescript/core/src/rum/helper.d.ts +10 -0
  85. package/lib/typescript/core/src/rum/helper.d.ts.map +1 -0
  86. package/lib/typescript/core/src/rum/instrumentation/DdRumErrorTracking.d.ts.map +1 -1
  87. package/lib/typescript/core/src/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.d.ts +6 -6
  88. package/lib/typescript/core/src/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.d.ts.map +1 -1
  89. package/lib/typescript/core/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.d.ts +7 -0
  90. package/lib/typescript/core/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.d.ts.map +1 -1
  91. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +12 -6
  92. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts.map +1 -1
  93. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingContext.d.ts +1 -0
  94. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingContext.d.ts.map +1 -1
  95. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.d.ts +8 -0
  96. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.d.ts.map +1 -1
  97. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.d.ts +5 -17
  98. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.d.ts.map +1 -1
  99. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingAttributes.d.ts +20 -0
  100. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingAttributes.d.ts.map +1 -0
  101. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.d.ts +3 -24
  102. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.d.ts.map +1 -1
  103. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingSampling.d.ts +12 -0
  104. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingSampling.d.ts.map +1 -0
  105. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/headers.d.ts +24 -0
  106. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/distributedTracing/headers.d.ts.map +1 -0
  107. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.d.ts +2 -1
  108. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.d.ts.map +1 -1
  109. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/baggageHeaderUtils.d.ts.map +1 -1
  110. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/interfaces/RumResource.d.ts +1 -1
  111. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/requestProxy/interfaces/RumResource.d.ts.map +1 -1
  112. package/lib/typescript/core/src/rum/types.d.ts +47 -7
  113. package/lib/typescript/core/src/rum/types.d.ts.map +1 -1
  114. package/lib/typescript/core/src/sdk/AccountInfoSingleton/AccountInfoSingleton.d.ts +15 -0
  115. package/lib/typescript/core/src/sdk/AccountInfoSingleton/AccountInfoSingleton.d.ts.map +1 -0
  116. package/lib/typescript/core/src/sdk/AccountInfoSingleton/types.d.ts +6 -0
  117. package/lib/typescript/core/src/sdk/AccountInfoSingleton/types.d.ts.map +1 -0
  118. package/lib/typescript/core/src/sdk/AttributesEncoding/attributesEncoding.d.ts +13 -0
  119. package/lib/typescript/core/src/sdk/AttributesEncoding/attributesEncoding.d.ts.map +1 -0
  120. package/lib/typescript/core/src/sdk/AttributesEncoding/defaultEncoders.d.ts +33 -0
  121. package/lib/typescript/core/src/sdk/AttributesEncoding/defaultEncoders.d.ts.map +1 -0
  122. package/lib/typescript/core/src/{utils → sdk/AttributesEncoding}/errorUtils.d.ts +4 -4
  123. package/lib/typescript/core/src/sdk/AttributesEncoding/errorUtils.d.ts.map +1 -0
  124. package/lib/typescript/core/src/sdk/AttributesEncoding/helpers.d.ts +17 -0
  125. package/lib/typescript/core/src/sdk/AttributesEncoding/helpers.d.ts.map +1 -0
  126. package/lib/typescript/core/src/sdk/AttributesEncoding/types.d.ts +21 -0
  127. package/lib/typescript/core/src/sdk/AttributesEncoding/types.d.ts.map +1 -0
  128. package/lib/typescript/core/src/sdk/AttributesEncoding/utils.d.ts +7 -0
  129. package/lib/typescript/core/src/sdk/AttributesEncoding/utils.d.ts.map +1 -0
  130. package/lib/typescript/core/src/sdk/AttributesSingleton/AttributesSingleton.d.ts +5 -1
  131. package/lib/typescript/core/src/sdk/AttributesSingleton/AttributesSingleton.d.ts.map +1 -1
  132. package/lib/typescript/core/src/sdk/DatadogProvider/DatadogProvider.d.ts +4 -3
  133. package/lib/typescript/core/src/sdk/DatadogProvider/DatadogProvider.d.ts.map +1 -1
  134. package/lib/typescript/core/src/sdk/DatadogProvider/DatadogProviderState.d.ts +9 -0
  135. package/lib/typescript/core/src/sdk/DatadogProvider/DatadogProviderState.d.ts.map +1 -0
  136. package/lib/typescript/core/src/sdk/DdSdk.d.ts +2 -3
  137. package/lib/typescript/core/src/sdk/DdSdk.d.ts.map +1 -1
  138. package/lib/typescript/core/src/sdk/DdSdkInternal.d.ts +39 -0
  139. package/lib/typescript/core/src/sdk/DdSdkInternal.d.ts.map +1 -0
  140. package/lib/typescript/core/src/sdk/EventMappers/EventMapper.d.ts +3 -1
  141. package/lib/typescript/core/src/sdk/EventMappers/EventMapper.d.ts.map +1 -1
  142. package/lib/typescript/core/src/sdk/GlobalState/GlobalState.d.ts +3 -3
  143. package/lib/typescript/core/src/sdk/GlobalState/GlobalState.d.ts.map +1 -1
  144. package/lib/typescript/core/src/sdk/UserInfoSingleton/UserInfoSingleton.d.ts +2 -1
  145. package/lib/typescript/core/src/sdk/UserInfoSingleton/UserInfoSingleton.d.ts.map +1 -1
  146. package/lib/typescript/core/src/sdk/UserInfoSingleton/types.d.ts +1 -2
  147. package/lib/typescript/core/src/sdk/UserInfoSingleton/types.d.ts.map +1 -1
  148. package/lib/typescript/core/src/specs/NativeDdRum.d.ts +45 -0
  149. package/lib/typescript/core/src/specs/NativeDdRum.d.ts.map +1 -1
  150. package/lib/typescript/core/src/specs/NativeDdSdk.d.ts +34 -6
  151. package/lib/typescript/core/src/specs/NativeDdSdk.d.ts.map +1 -1
  152. package/lib/typescript/core/src/trace/DdTrace.d.ts +1 -2
  153. package/lib/typescript/core/src/trace/DdTrace.d.ts.map +1 -1
  154. package/lib/typescript/core/src/types.d.ts +74 -92
  155. package/lib/typescript/core/src/types.d.ts.map +1 -1
  156. package/lib/typescript/core/src/utils/longTasksUtils.d.ts +1 -1
  157. package/lib/typescript/core/src/utils/longTasksUtils.d.ts.map +1 -1
  158. package/lib/typescript/core/src/utils/singletonUtils.d.ts +2 -0
  159. package/lib/typescript/core/src/utils/singletonUtils.d.ts.map +1 -0
  160. package/lib/typescript/core/src/version.d.ts +1 -1
  161. package/lib/typescript/core/src/version.d.ts.map +1 -1
  162. package/lib/typescript/react-navigation/src/index.d.ts +2 -2
  163. package/lib/typescript/react-navigation/src/index.d.ts.map +1 -1
  164. package/lib/typescript/react-navigation/src/rum/instrumentation/DdRumReactNavigationTracking.d.ts +41 -24
  165. package/lib/typescript/react-navigation/src/rum/instrumentation/DdRumReactNavigationTracking.d.ts.map +1 -1
  166. package/lib/typescript/react-navigation/src/rum/instrumentation/utils.d.ts +13 -0
  167. package/lib/typescript/react-navigation/src/rum/instrumentation/utils.d.ts.map +1 -0
  168. package/package.json +4 -4
  169. package/src/__tests__/rum/instrumentation/DdRumReactNavigationTracking.test.tsx +147 -14
  170. package/src/index.tsx +12 -2
  171. package/src/rum/instrumentation/DdRumReactNavigationTracking.tsx +168 -101
  172. package/src/rum/instrumentation/utils.ts +28 -0
  173. package/lib/typescript/core/src/DdSdkReactNativeConfiguration.d.ts +0 -376
  174. package/lib/typescript/core/src/DdSdkReactNativeConfiguration.d.ts.map +0 -1
  175. package/lib/typescript/core/src/ProxyConfiguration.d.ts.map +0 -1
  176. package/lib/typescript/core/src/SdkVerbosity.d.ts.map +0 -1
  177. package/lib/typescript/core/src/TrackingConsent.d.ts.map +0 -1
  178. package/lib/typescript/core/src/rum/DdAttributes.d.ts.map +0 -1
  179. package/lib/typescript/core/src/rum/sessionId/sessionIdHelper.d.ts +0 -4
  180. package/lib/typescript/core/src/rum/sessionId/sessionIdHelper.d.ts.map +0 -1
  181. package/lib/typescript/core/src/sdk/FileBasedConfiguration/FileBasedConfiguration.d.ts +0 -34
  182. package/lib/typescript/core/src/sdk/FileBasedConfiguration/FileBasedConfiguration.d.ts.map +0 -1
  183. package/lib/typescript/core/src/utils/argsUtils.d.ts +0 -8
  184. package/lib/typescript/core/src/utils/argsUtils.d.ts.map +0 -1
  185. package/lib/typescript/core/src/utils/errorUtils.d.ts.map +0 -1
  186. /package/lib/typescript/core/src/{rum/DdAttributes.d.ts → DdAttributes.d.ts} +0 -0
  187. /package/lib/typescript/core/src/{ProxyConfiguration.d.ts → config/types/ProxyConfiguration.d.ts} +0 -0
  188. /package/lib/typescript/core/src/{SdkVerbosity.d.ts → config/types/SdkVerbosity.d.ts} +0 -0
  189. /package/lib/typescript/core/src/{TrackingConsent.d.ts → config/types/TrackingConsent.d.ts} +0 -0
@@ -15,6 +15,20 @@ import type {
15
15
  Route,
16
16
  NavigationListener
17
17
  } from './react-navigation';
18
+ import { transformViewKey } from './utils';
19
+
20
+ const REACT_NAVIGATION_TRACKING_MODULE =
21
+ 'com.datadog.reactnative.rum.react_navigation_tracking';
22
+
23
+ function getGlobalInstance<T>(key: string, objectConstructor: () => T): T {
24
+ const symbol = Symbol.for(key);
25
+ const g = (globalThis as unknown) as Record<PropertyKey, unknown>;
26
+
27
+ if (!(symbol in g)) {
28
+ g[symbol] = objectConstructor();
29
+ }
30
+ return g[symbol] as T;
31
+ }
18
32
 
19
33
  // AppStateStatus can have values:
20
34
  // 'active' - The app is running in the foreground
@@ -24,17 +38,53 @@ import type {
24
38
  // 'extension' [iOS] - The app is running as an app extension
25
39
  declare type AppStateListener = (appStateStatus: AppStateStatus) => void | null;
26
40
 
41
+ export type NavigationTrackingOptions = {
42
+ viewNamePredicate?: ViewNamePredicate;
43
+ viewTrackingPredicate?: ViewTrackingPredicate;
44
+ paramsTrackingPredicate?: ParamsTrackingPredicate;
45
+ };
46
+
27
47
  export type ViewNamePredicate = (
28
48
  route: Route<string, any | undefined>,
29
49
  trackedName: string
30
50
  ) => string | null;
31
51
 
52
+ export type ViewTrackingPredicate = (
53
+ route: Route<string, any | undefined>
54
+ ) => boolean;
55
+
56
+ export type ParamsTrackingPredicate = (
57
+ route: Route<string, any | undefined>
58
+ ) => object | undefined;
59
+
60
+ function defaultViewNamePredicate(
61
+ _route: Route<string, any | undefined>,
62
+ trackedName: string
63
+ ) {
64
+ return trackedName;
65
+ }
66
+
67
+ function defaultParamsPredicate(_route: Route<string, any | undefined>) {
68
+ return undefined;
69
+ }
70
+
71
+ function defaultViewTrackingPredicate(_route: Route<string, any | undefined>) {
72
+ return true;
73
+ }
74
+
32
75
  /**
33
76
  * Provides RUM integration for the [ReactNavigation](https://reactnavigation.org/) API.
34
77
  */
35
- export class DdRumReactNavigationTracking {
36
- private static _navigationTimeline?: NavigationTimeline;
37
- private static get navigationTimeline(): NavigationTimeline | undefined {
78
+ class RumReactNavigationTracking {
79
+ readonly ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE =
80
+ 'A navigation change was detected but the RUM ViewEvent was dropped as the route was undefined.';
81
+ readonly NULL_NAVIGATION_REF_ERROR_MESSAGE =
82
+ 'Cannot track views with a null navigationRef.';
83
+ readonly NAVIGATION_REF_IN_USE_ERROR_MESSAGE =
84
+ 'Cannot track new navigation container while another one is still tracked. Please call `DdRumReactNavigationTracking.stopTrackingViews` on the previous container reference.';
85
+
86
+ private _navigationTimeline?: NavigationTimeline;
87
+ private get navigationTimeline(): NavigationTimeline | undefined {
38
88
  if (!this.__INTERNAL._enableNavigationTimeline) {
39
89
  return undefined;
40
90
  }
@@ -44,36 +94,31 @@ export class DdRumReactNavigationTracking {
44
94
  return this._navigationTimeline;
45
95
  }
46
96
 
47
- private static registeredContainer: NavigationContainerRef | null;
97
+ private registeredContainer: NavigationContainerRef | null = null;
48
98
 
49
- private static navigationStateChangeListener: NavigationListener | null = null;
99
+ private navigationStateChangeListener: NavigationListener | null = null;
50
100
 
51
- private static previousRoute: string | object | undefined = undefined;
101
+ private previousRoute: string | object | undefined = undefined;
52
102
 
53
- private static viewNamePredicate: ViewNamePredicate;
103
+ private viewNamePredicate: ViewNamePredicate = defaultViewNamePredicate;
104
+ private viewTrackingPredicate: ViewTrackingPredicate = defaultViewTrackingPredicate;
105
+ private paramsTrackingPredicate: ParamsTrackingPredicate = defaultParamsPredicate;
54
106
 
55
- private static backHandler: NativeEventSubscription | null;
107
+ private backHandler: NativeEventSubscription | null = null;
56
108
 
57
- private static appStateSubscription?: NativeEventSubscription;
109
+ private appStateSubscription?: NativeEventSubscription;
58
110
 
59
- private static previousAppState: AppStateStatus | undefined;
111
+ private previousAppState: AppStateStatus | undefined;
60
112
 
61
- private static previousRouteKey: string | undefined;
113
+ private previousRouteKey: string | undefined;
62
114
 
63
- private static trackingState: 'TRACKING' | 'NOT_TRACKING' = 'NOT_TRACKING';
64
-
65
- static ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE =
66
- 'A navigation change was detected but the RUM ViewEvent was dropped as the route was undefined.';
67
- static NULL_NAVIGATION_REF_ERROR_MESSAGE =
68
- 'Cannot track views with a null navigationRef.';
69
- static NAVIGATION_REF_IN_USE_ERROR_MESSAGE =
70
- 'Cannot track new navigation container while another one is still tracked. Please call `DdRumReactNavigationTracking.stopTrackingViews` on the previous container reference.';
115
+ private trackingState: 'TRACKING' | 'NOT_TRACKING' = 'NOT_TRACKING';
71
116
 
72
117
  /**
73
118
  * @internal
74
119
  * DO NOT USE: This API is for internal testing only.
75
120
  */
76
- static __INTERNAL = {
121
+ __INTERNAL = {
77
122
  /**
78
123
  * @internal
79
124
  * DO NOT USE: This API is for internal testing only.
@@ -88,21 +133,19 @@ export class DdRumReactNavigationTracking {
88
133
  }
89
134
  };
90
135
 
91
- static isAppExitingOnBackPress = (): boolean => {
92
- if (DdRumReactNavigationTracking.registeredContainer === null) {
136
+ isAppExitingOnBackPress = (): boolean => {
137
+ if (this.registeredContainer === null) {
93
138
  return false;
94
139
  }
95
- if (DdRumReactNavigationTracking.registeredContainer.canGoBack()) {
140
+ if (this.registeredContainer.canGoBack()) {
96
141
  return false;
97
142
  }
98
143
  return true;
99
144
  };
100
145
 
101
- static onBackPress = () => {
102
- if (DdRumReactNavigationTracking.isAppExitingOnBackPress()) {
103
- DdRumReactNavigationTracking.stopTrackingViews(
104
- DdRumReactNavigationTracking.registeredContainer
105
- );
146
+ onBackPress = () => {
147
+ if (this.isAppExitingOnBackPress()) {
148
+ this.stopTrackingViews(this.registeredContainer);
106
149
  }
107
150
  // We always return false so we make sure the react-navigation callback is called.
108
151
  // See https://reactnative.dev/docs/backhandler
@@ -112,50 +155,63 @@ export class DdRumReactNavigationTracking {
112
155
  /**
113
156
  * Starts tracking the NavigationContainer and sends a RUM View event every time the navigation route changed.
114
157
  * @param navigationRef the reference to the real NavigationContainer.
158
+ * @param trackingOptions the options object defining how views will be tracked. It contains:
159
+ * viewNamePredicate: the predicate to rename views.
160
+ * viewTrackingPredicate: the predicate to determine if a view should be tracked or not.
161
+ * paramsTrackingPredicate: the predicate to determine which parameters should be tracked for a given view.
115
162
  */
116
- static startTrackingViews(
163
+ startTrackingViews(
117
164
  navigationRef: NavigationContainerRef | null,
118
-
119
- // eslint-disable-next-line func-names
120
- viewNamePredicate: ViewNamePredicate = function (
121
- _route: Route<string, any | undefined>,
122
- trackedName: string
123
- ) {
124
- return trackedName;
125
- }
165
+ trackingOptions?: NavigationTrackingOptions
126
166
  ): void {
127
167
  this.navigationTimeline?.addStartTrackingEvent();
128
168
 
169
+ const {
170
+ viewNamePredicate = defaultViewNamePredicate,
171
+ viewTrackingPredicate = defaultViewTrackingPredicate,
172
+ paramsTrackingPredicate = defaultParamsPredicate
173
+ } = trackingOptions ?? {};
174
+
129
175
  if (navigationRef == null) {
130
176
  InternalLog.log(
131
- DdRumReactNavigationTracking.NULL_NAVIGATION_REF_ERROR_MESSAGE,
177
+ this.NULL_NAVIGATION_REF_ERROR_MESSAGE,
132
178
  SdkVerbosity.ERROR
133
179
  );
134
180
  return;
135
181
  }
136
182
 
137
183
  if (
138
- DdRumReactNavigationTracking.registeredContainer != null &&
184
+ this.registeredContainer != null &&
139
185
  this.registeredContainer !== navigationRef
140
186
  ) {
141
187
  InternalLog.log(
142
- DdRumReactNavigationTracking.NAVIGATION_REF_IN_USE_ERROR_MESSAGE,
188
+ this.NAVIGATION_REF_IN_USE_ERROR_MESSAGE,
143
189
  SdkVerbosity.ERROR
144
190
  );
145
- } else if (DdRumReactNavigationTracking.registeredContainer == null) {
146
- DdRumReactNavigationTracking.viewNamePredicate = viewNamePredicate;
147
- DdRumReactNavigationTracking.registeredContainer = navigationRef;
191
+ } else if (this.registeredContainer == null) {
192
+ if (viewNamePredicate) {
193
+ this.viewNamePredicate = viewNamePredicate;
194
+ }
195
+
196
+ if (viewTrackingPredicate) {
197
+ this.viewTrackingPredicate = viewTrackingPredicate;
198
+ }
148
199
 
149
- const listener = DdRumReactNavigationTracking.resolveNavigationStateChangeListener();
200
+ if (paramsTrackingPredicate) {
201
+ this.paramsTrackingPredicate = paramsTrackingPredicate;
202
+ }
203
+ this.registeredContainer = navigationRef;
204
+
205
+ const listener = this.resolveNavigationStateChangeListener();
150
206
  navigationRef.addListener('state', listener);
151
207
 
152
- DdRumReactNavigationTracking.backHandler = BackHandler.addEventListener(
208
+ this.backHandler = BackHandler.addEventListener(
153
209
  'hardwareBackPress',
154
- DdRumReactNavigationTracking.onBackPress
210
+ this.onBackPress
155
211
  );
156
212
  this.appStateSubscription = AppState.addEventListener(
157
213
  'change',
158
- DdRumReactNavigationTracking.appStateListener
214
+ this.appStateListener
159
215
  );
160
216
  }
161
217
  }
@@ -164,30 +220,22 @@ export class DdRumReactNavigationTracking {
164
220
  * Stops tracking the NavigationContainer.
165
221
  * @param navigationRef the reference to the real NavigationContainer.
166
222
  */
167
- static stopTrackingViews(
168
- navigationRef: NavigationContainerRef | null
169
- ): void {
223
+ stopTrackingViews(navigationRef: NavigationContainerRef | null): void {
170
224
  this.navigationTimeline?.addStopTrackingEvent();
171
225
  this.previousRoute = undefined;
172
226
  if (navigationRef != null) {
173
- if (DdRumReactNavigationTracking.navigationStateChangeListener) {
227
+ if (this.navigationStateChangeListener) {
174
228
  navigationRef.removeListener(
175
229
  'state',
176
- DdRumReactNavigationTracking.navigationStateChangeListener
230
+ this.navigationStateChangeListener
177
231
  );
178
232
  }
179
- DdRumReactNavigationTracking.backHandler?.remove();
180
- DdRumReactNavigationTracking.backHandler = null;
181
- DdRumReactNavigationTracking.registeredContainer = null;
182
- DdRumReactNavigationTracking.navigationStateChangeListener = null;
183
-
184
- // eslint-disable-next-line func-names
185
- DdRumReactNavigationTracking.viewNamePredicate = function (
186
- _route: Route<string, any | undefined>,
187
- trackedName: string
188
- ) {
189
- return trackedName;
190
- };
233
+ this.backHandler?.remove();
234
+ this.backHandler = null;
235
+ this.registeredContainer = null;
236
+ this.navigationStateChangeListener = null;
237
+
238
+ this.resetPredicates();
191
239
  }
192
240
 
193
241
  // For versions of React Native below 0.65, addEventListener does not return a subscription.
@@ -202,37 +250,51 @@ export class DdRumReactNavigationTracking {
202
250
  } else if (AppState.removeEventListener) {
203
251
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
204
252
  // @ts-ignore
205
- AppState.removeEventListener(
206
- 'change',
207
- DdRumReactNavigationTracking.appStateListener
208
- );
253
+ AppState.removeEventListener('change', this.appStateListener);
209
254
  }
210
255
  }
211
256
 
212
- private static handleRouteNavigation(
257
+ _resetInternalStateForTesting(): void {
258
+ this._navigationTimeline = undefined;
259
+ this.registeredContainer = null;
260
+ this.navigationStateChangeListener = null;
261
+ this.previousRoute = undefined;
262
+ this.backHandler = null;
263
+ this.appStateSubscription = undefined;
264
+ this.previousAppState = undefined;
265
+ this.previousRouteKey = undefined;
266
+ this.trackingState = 'NOT_TRACKING';
267
+ this.resetPredicates();
268
+ }
269
+
270
+ private resetPredicates() {
271
+ this.paramsTrackingPredicate = defaultParamsPredicate;
272
+ this.viewNamePredicate = defaultViewNamePredicate;
273
+ this.viewTrackingPredicate = defaultViewTrackingPredicate;
274
+ }
275
+
276
+ private handleRouteNavigation(
213
277
  route: Route<string, any | undefined> | undefined,
214
278
  appStateStatus: AppStateStatus,
215
279
  stateEvent: StateEvent | undefined
216
280
  ) {
217
281
  if (route === undefined || route === null) {
218
282
  InternalLog.log(
219
- DdRumReactNavigationTracking.ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE,
283
+ this.ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE,
220
284
  SdkVerbosity.WARN
221
285
  );
222
286
  // RUMM-1400 in some cases the route seem to be undefined
223
287
  return;
224
288
  }
225
289
  const key = route.key;
226
- const screenName = DdRumReactNavigationTracking.viewNamePredicate(
227
- route,
228
- route.name
229
- );
290
+ const screenName = this.viewNamePredicate(route, route.name);
291
+ const customKey = transformViewKey(key, screenName);
230
292
 
231
293
  if (key != null && screenName != null) {
232
294
  // On iOS, the app can start in either "active", "background" or "unknown" state
233
295
  if (appStateStatus !== 'background') {
234
296
  this.previousRoute = route;
235
- DdRumReactNavigationTracking.trackingState = 'TRACKING';
297
+ this.trackingState = 'TRACKING';
236
298
  this.navigationTimeline?.addNavigationStateEvent(
237
299
  'START_VIEW',
238
300
  key,
@@ -242,14 +304,21 @@ export class DdRumReactNavigationTracking {
242
304
  trackingState: this.trackingState
243
305
  }
244
306
  );
245
- DdRum.startView(key, screenName);
307
+ if (this.viewTrackingPredicate(route)) {
308
+ const params = this.paramsTrackingPredicate(route);
309
+ if (params) {
310
+ DdRum.startView(customKey, screenName, { params });
311
+ } else {
312
+ DdRum.startView(customKey, screenName);
313
+ }
314
+ }
246
315
  }
247
316
  }
248
317
 
249
318
  this.previousRouteKey = route.key;
250
319
  }
251
320
 
252
- private static handleAppStateChanged(
321
+ private handleAppStateChanged(
253
322
  route: Route<string, any | undefined>,
254
323
  appStateStatus: AppStateStatus
255
324
  ) {
@@ -258,14 +327,12 @@ export class DdRumReactNavigationTracking {
258
327
  appStateStatus
259
328
  );
260
329
  const key = route.key;
261
- const screenName = DdRumReactNavigationTracking.viewNamePredicate(
262
- route,
263
- route.name
264
- );
330
+ const screenName = this.viewNamePredicate(route, route.name);
331
+ const customKey = transformViewKey(key, screenName);
265
332
 
266
333
  if (key != null && screenName != null) {
267
334
  if (appStateStatus === 'background') {
268
- DdRumReactNavigationTracking.trackingState = 'NOT_TRACKING';
335
+ this.trackingState = 'NOT_TRACKING';
269
336
  this.navigationTimeline?.addNavigationStateEvent(
270
337
  'STOP_VIEW',
271
338
  key,
@@ -275,15 +342,15 @@ export class DdRumReactNavigationTracking {
275
342
  trackingState: this.trackingState
276
343
  }
277
344
  );
278
- DdRum.stopView(key);
345
+ DdRum.stopView(customKey);
279
346
  this.previousRoute = undefined;
280
347
  } else if (
281
348
  appStateStatus === 'active' &&
282
- DdRumReactNavigationTracking.trackingState === 'NOT_TRACKING'
349
+ this.trackingState === 'NOT_TRACKING'
283
350
  ) {
284
351
  // case when app goes into foreground,
285
352
  // in that case navigation listener won't be called
286
- DdRumReactNavigationTracking.handleRouteNavigation(
353
+ this.handleRouteNavigation(
287
354
  route,
288
355
  AppState.currentState,
289
356
  appStateChangeEvent
@@ -295,12 +362,10 @@ export class DdRumReactNavigationTracking {
295
362
  this.previousAppState = appStateStatus;
296
363
  }
297
364
 
298
- private static resolveNavigationStateChangeListener(): NavigationListener {
299
- if (
300
- DdRumReactNavigationTracking.navigationStateChangeListener == null
301
- ) {
302
- DdRumReactNavigationTracking.navigationStateChangeListener = () => {
303
- const route = DdRumReactNavigationTracking.registeredContainer?.getCurrentRoute();
365
+ private resolveNavigationStateChangeListener(): NavigationListener {
366
+ if (this.navigationStateChangeListener == null) {
367
+ this.navigationStateChangeListener = () => {
368
+ const route = this.registeredContainer?.getCurrentRoute();
304
369
 
305
370
  const newRouteStateEvent = this.navigationTimeline?.addNewRouteEvent(
306
371
  this.previousRouteKey,
@@ -309,7 +374,7 @@ export class DdRumReactNavigationTracking {
309
374
 
310
375
  if (route === undefined) {
311
376
  InternalLog.log(
312
- DdRumReactNavigationTracking.ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE,
377
+ this.ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE,
313
378
  SdkVerbosity.WARN
314
379
  );
315
380
  return;
@@ -329,22 +394,22 @@ export class DdRumReactNavigationTracking {
329
394
  return;
330
395
  }
331
396
 
332
- DdRumReactNavigationTracking.handleRouteNavigation(
397
+ this.handleRouteNavigation(
333
398
  route,
334
399
  AppState.currentState,
335
400
  newRouteStateEvent
336
401
  );
337
402
  };
338
403
 
339
- DdRumReactNavigationTracking.navigationStateChangeListener({});
404
+ this.navigationStateChangeListener({});
340
405
  }
341
- return DdRumReactNavigationTracking.navigationStateChangeListener;
406
+ return this.navigationStateChangeListener;
342
407
  }
343
408
 
344
- private static appStateListener: AppStateListener = (
409
+ private appStateListener: AppStateListener = (
345
410
  appStateStatus: AppStateStatus
346
411
  ) => {
347
- const currentRoute = DdRumReactNavigationTracking.registeredContainer?.getCurrentRoute();
412
+ const currentRoute = this.registeredContainer?.getCurrentRoute();
348
413
  if (currentRoute === undefined || currentRoute === null) {
349
414
  InternalLog.log(
350
415
  `We could not determine the route when changing the application state to: ${appStateStatus}. No RUM View event will be sent in this case.`,
@@ -353,9 +418,11 @@ export class DdRumReactNavigationTracking {
353
418
  return;
354
419
  }
355
420
 
356
- DdRumReactNavigationTracking.handleAppStateChanged(
357
- currentRoute,
358
- appStateStatus
359
- );
421
+ this.handleAppStateChanged(currentRoute, appStateStatus);
360
422
  };
361
423
  }
424
+
425
+ export const DdRumReactNavigationTracking = getGlobalInstance(
426
+ REACT_NAVIGATION_TRACKING_MODULE,
427
+ () => new RumReactNavigationTracking()
428
+ );
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Transforms the view key format from 'ScreenName-uniqueId' to 'ScreenName/uniqueId'.
3
+ *
4
+ * @example
5
+ * // Input: key='Elements-lUQ9cId5lwk1o94euVh2B', screenName='Elements'
6
+ * // Output: 'Elements/lUQ9cId5lwk1o94euVh2B'
7
+ *
8
+ * @param key - The route key (e.g., 'Elements-lUQ9cId5lwk1o94euVh2B')
9
+ * @param screenName - The screen name (e.g., 'Elements')
10
+ * @returns The transformed key, or the original key if not in expected format
11
+ */
12
+ export function transformViewKey(
13
+ key: string,
14
+ screenName: string | null
15
+ ): string {
16
+ if (!screenName || !key) {
17
+ return key;
18
+ }
19
+
20
+ const expectedPrefix = `${screenName}-`;
21
+ if (key.startsWith(expectedPrefix)) {
22
+ const uniqueId = key.slice(expectedPrefix.length);
23
+ return `${screenName}/${uniqueId}`;
24
+ }
25
+
26
+ // Key is not in the expected format, use original
27
+ return key;
28
+ }