@datadog/mobile-react-navigation 1.0.0-beta4 → 1.0.0-rc4

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 (27) hide show
  1. package/README.md +51 -1
  2. package/lib/commonjs/index.js +8 -4
  3. package/lib/commonjs/index.js.map +1 -1
  4. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js +36 -18
  5. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  6. package/lib/module/index.js +2 -2
  7. package/lib/module/index.js.map +1 -1
  8. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js +35 -17
  9. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  10. package/lib/typescript/core/src/DdSdkReactNative.d.ts +11 -0
  11. package/lib/typescript/core/src/DdSdkReactNativeConfiguration.d.ts +6 -0
  12. package/lib/typescript/core/src/NativeSdkVerbosity.d.ts +9 -0
  13. package/lib/typescript/core/src/ProxyConfiguration.d.ts +56 -0
  14. package/lib/typescript/core/src/Timer.d.ts +20 -0
  15. package/lib/typescript/core/src/index.d.ts +2 -1
  16. package/lib/typescript/core/src/rum/instrumentation/DdEventsInterceptor.d.ts +2 -0
  17. package/lib/typescript/core/src/rum/instrumentation/DdRumErrorTracking.d.ts +2 -0
  18. package/lib/typescript/core/src/rum/instrumentation/DdRumResourceTracking.d.ts +4 -0
  19. package/lib/typescript/core/src/rum/instrumentation/DdRumXhr.d.ts +2 -2
  20. package/lib/typescript/core/src/types.d.ts +25 -24
  21. package/lib/typescript/core/src/version.d.ts +1 -0
  22. package/lib/typescript/react-navigation/src/index.d.ts +2 -2
  23. package/lib/typescript/react-navigation/src/rum/instrumentation/DdRumReactNavigationTracking.d.ts +5 -3
  24. package/package.json +6 -10
  25. package/src/__tests__/rum/instrumentation/DdRumReactNavigationTracking.test.tsx +397 -0
  26. package/src/index.tsx +2 -2
  27. package/src/rum/instrumentation/DdRumReactNavigationTracking.tsx +44 -17
@@ -10,12 +10,20 @@ import { AppState, AppStateStatus } from 'react-native';
10
10
 
11
11
  declare type NavigationListener = (event: EventArg<string, boolean, any>) => void | null
12
12
 
13
+ // AppStateStatus can have values:
14
+ // 'active' - The app is running in the foreground
15
+ // 'background' - The app is running in the background. The user is either in another app or on the home screen
16
+ // 'inactive' [iOS] - This is a transition state that currently never happens for typical React Native apps.
17
+ // 'unknown' [iOS] - Initial value until the current app state is determined
18
+ // 'extension' [iOS] - The app is running as an app extension
13
19
  declare type AppStateListener = (appStateStatus: AppStateStatus) => void | null
14
20
 
21
+ export type ViewNamePredicate = (route: Route<string, any | undefined>, trackedName: string) => string
22
+
15
23
  /**
16
24
  * Provides RUM integration for the [ReactNavigation](https://reactnavigation.org/) API.
17
25
  */
18
- export default class DdRumReactNavigationTracking {
26
+ export class DdRumReactNavigationTracking {
19
27
 
20
28
  private static registeredContainer: NavigationContainerRef | null;
21
29
 
@@ -23,24 +31,33 @@ export default class DdRumReactNavigationTracking {
23
31
 
24
32
  private static appStateListener: AppStateListener;
25
33
 
34
+ private static viewNamePredicate: ViewNamePredicate;
35
+
26
36
  /**
27
37
  * Starts tracking the NavigationContainer and sends a RUM View event every time the navigation route changed.
28
38
  * @param navigationRef the reference to the real NavigationContainer.
29
39
  */
30
- static startTrackingViews(navigationRef: NavigationContainerRef | null): void {
40
+ static startTrackingViews(
41
+ navigationRef: NavigationContainerRef | null,
42
+ viewNamePredicate: ViewNamePredicate = function (_route: Route<string, any | undefined>, trackedName: string) { return trackedName; }
43
+ ): void {
44
+
31
45
  if (navigationRef == null) {
46
+ console.log ("Cannot track views with a null navigationRef");
32
47
  return;
33
48
  }
34
49
 
35
50
  if (DdRumReactNavigationTracking.registeredContainer != null && this.registeredContainer !== navigationRef) {
36
51
  console.error('Cannot track new navigation container while another one is still tracked');
37
52
  } else if (DdRumReactNavigationTracking.registeredContainer == null) {
53
+ DdRumReactNavigationTracking.viewNamePredicate = viewNamePredicate;
38
54
  const listener = DdRumReactNavigationTracking.resolveNavigationStateChangeListener();
39
55
  DdRumReactNavigationTracking.handleRouteNavigation(navigationRef.getCurrentRoute());
40
56
  navigationRef.addListener("state", listener);
41
57
  DdRumReactNavigationTracking.registeredContainer = navigationRef;
42
58
  }
43
59
 
60
+
44
61
  DdRumReactNavigationTracking.registerAppStateListenerIfNeeded();
45
62
  }
46
63
 
@@ -52,14 +69,31 @@ export default class DdRumReactNavigationTracking {
52
69
  if (navigationRef != null) {
53
70
  navigationRef.removeListener("state", DdRumReactNavigationTracking.navigationStateChangeListener);
54
71
  DdRumReactNavigationTracking.registeredContainer = null;
72
+ DdRumReactNavigationTracking.viewNamePredicate = function (_route: Route<string, any | undefined>, trackedName: string) { return trackedName; }
55
73
  }
56
74
  }
57
75
 
58
- private static handleRouteNavigation(route: Route<string, any | undefined> | undefined) {
59
- const key = route?.key;
60
- const screenName = route?.name;
76
+ private static handleRouteNavigation(
77
+ route: Route<string, any | undefined> | undefined,
78
+ appStateStatus: AppStateStatus | undefined = undefined
79
+ ) {
80
+ if (route == undefined || route == null) {
81
+ // RUMM-1400 in some cases the route seem to be undefined
82
+ return
83
+ }
84
+ const key = route.key;
85
+
86
+ const predicate = DdRumReactNavigationTracking.viewNamePredicate;
87
+ const screenName = predicate(route, route.name) ?? route.name;
88
+
61
89
  if (key != null && screenName != null) {
62
- DdRum.startView(key, screenName, Date.now(), {});
90
+ if (appStateStatus === 'background') {
91
+ DdRum.stopView(key);
92
+ } else if (appStateStatus === 'active' || appStateStatus == undefined) {
93
+ // case when app goes into foreground,
94
+ // in that case navigation listener won't be called
95
+ DdRum.startView(key, screenName);
96
+ }
63
97
  }
64
98
  }
65
99
 
@@ -93,18 +127,11 @@ export default class DdRumReactNavigationTracking {
93
127
  DdRumReactNavigationTracking.appStateListener = (appStateStatus: AppStateStatus) => {
94
128
 
95
129
  const currentRoute = DdRumReactNavigationTracking.registeredContainer?.getCurrentRoute();
96
- const currentViewKey = currentRoute?.key;
97
- const currentViewName = currentRoute?.name;
98
-
99
- if (currentViewKey != null && currentViewName != null) {
100
- if (appStateStatus === 'background') {
101
- DdRum.stopView(currentViewKey, Date.now(), {});
102
- } else if (appStateStatus === 'active') {
103
- // case when app goes into foreground, in that case navigation listener
104
- // won't be called
105
- DdRum.startView(currentViewKey, currentViewName, Date.now(), {});
106
- }
130
+ if (currentRoute == undefined) {
131
+ return;
107
132
  }
133
+
134
+ DdRumReactNavigationTracking.handleRouteNavigation(currentRoute, appStateStatus);
108
135
  };
109
136
 
110
137
  // AppState is singleton, so we should add a listener only once in the app lifetime