@@ -4,6 +4,9 @@ title: Screen tracking for analytics
4
4
sidebar_label : Screen tracking for analytics
5
5
---
6
6
7
+ import Tabs from '@theme/Tabs ';
8
+ import TabItem from '@theme/TabItem ';
9
+
7
10
To track the currently active screen, we need to:
8
11
9
12
1 . Add a callback to get notified of state changes
@@ -15,42 +18,161 @@ To get notified of state changes, we can use the `onStateChange` prop on `Naviga
15
18
16
19
This example shows how the approach can be adapted to any mobile analytics SDK.
17
20
18
- <samp id =" screen-tracking-for-analytics " />
21
+ <Tabs groupId =" config " queryString =" config " >
22
+ <TabItem value =" static " label =" Static " default >
23
+
24
+ ``` js name="Screen tracking for analytics" snack version=7
25
+ import * as React from ' react' ;
26
+ import { View } from ' react-native' ;
27
+ // codeblock-focus-start
28
+ import {
29
+ createStaticNavigation ,
30
+ useNavigationContainerRef ,
31
+ useNavigation ,
32
+ } from ' @react-navigation/native' ;
33
+ // codeblock-focus-end
34
+ import { Button } from ' @react-navigation/elements' ;
35
+ import { createNativeStackNavigator } from ' @react-navigation/native-stack' ;
36
+
37
+ function Home () {
38
+ const navigation = useNavigation ();
39
+
40
+ return (
41
+ < View style= {{ flex: 1 , alignItems: ' center' , justifyContent: ' center' }}>
42
+ < Button onPress= {() => navigation .navigate (' Settings' )}>
43
+ Go to Settings
44
+ < / Button>
45
+ < / View>
46
+ );
47
+ }
48
+
49
+ function Settings () {
50
+ const navigation = useNavigation ();
51
+
52
+ return (
53
+ < View style= {{ flex: 1 , alignItems: ' center' , justifyContent: ' center' }}>
54
+ < Button onPress= {() => navigation .navigate (' Home' )}> Go to Home< / Button>
55
+ < / View>
56
+ );
57
+ }
58
+
59
+ const RootStack = createNativeStackNavigator ({
60
+ screens: {
61
+ Home: Home,
62
+ Settings: Settings,
63
+ },
64
+ });
65
+
66
+ const Navigation = createStaticNavigation (RootStack);
67
+
68
+ // codeblock-focus-start
69
+
70
+ export default function App () {
71
+ const navigationRef = useNavigationContainerRef ();
72
+ const routeNameRef = React .useRef ();
73
+
74
+ return (
75
+ < Navigation
76
+ ref= {navigationRef}
77
+ onReady= {() => {
78
+ routeNameRef .current = navigationRef .current .getCurrentRoute ().name ;
79
+ }}
80
+ onStateChange= {async () => {
81
+ const previousRouteName = routeNameRef .current ;
82
+ const currentRouteName = navigationRef .current .getCurrentRoute ().name ;
83
+ const trackScreenView = () => {
84
+ // Your implementation of analytics goes here!
85
+ };
86
+
87
+ if (previousRouteName !== currentRouteName) {
88
+ // Replace the line below to add the tracker from a mobile analytics SDK
89
+ await trackScreenView (currentRouteName);
90
+ }
91
+
92
+ // Save the current route name for later comparison
93
+ routeNameRef .current = currentRouteName;
94
+ }}
95
+ / >
96
+ );
97
+ }
98
+ // codeblock-focus-end
99
+ ```
100
+
101
+ </TabItem >
102
+ <TabItem value =" dynamic " label =" Dynamic " default >
19
103
20
- ``` js
104
+ ``` js name="Screen tracking for anylytics" snack version=7
105
+ import * as React from ' react' ;
106
+ import { View } from ' react-native' ;
107
+ // codeblock-focus-start
21
108
import {
22
109
NavigationContainer ,
23
110
useNavigationContainerRef ,
24
111
} from ' @react-navigation/native' ;
112
+ // codeblock-focus-end
113
+ import { Button } from ' @react-navigation/elements' ;
114
+ import { createStackNavigator } from ' @react-navigation/stack' ;
115
+
116
+ function Home ({ navigation }) {
117
+ return (
118
+ < View style= {{ flex: 1 , alignItems: ' center' , justifyContent: ' center' }}>
119
+ < Button onPress= {() => navigation .navigate (' Settings' )}>
120
+ Go to Settings
121
+ < / Button>
122
+ < / View>
123
+ );
124
+ }
125
+
126
+ function Settings ({ navigation }) {
127
+ return (
128
+ < View style= {{ flex: 1 , alignItems: ' center' , justifyContent: ' center' }}>
129
+ < Button onPress= {() => navigation .navigate (' Home' )}> Go to Home< / Button>
130
+ < / View>
131
+ );
132
+ }
133
+
134
+ const Stack = createStackNavigator ();
25
135
26
- export default () => {
136
+ // codeblock-focus-start
137
+
138
+ export default function App () {
27
139
const navigationRef = useNavigationContainerRef ();
28
- const routeNameRef = useRef ();
140
+ const routeNameRef = React . useRef ();
29
141
30
142
return (
31
143
< NavigationContainer
32
144
ref= {navigationRef}
33
145
onReady= {() => {
34
- routeNameRef .current = navigationRef .getCurrentRoute ().name ;
146
+ routeNameRef .current = navigationRef .current . getCurrentRoute ().name ;
35
147
}}
36
148
onStateChange= {async () => {
37
149
const previousRouteName = routeNameRef .current ;
38
- const currentRouteName = navigationRef .getCurrentRoute ().name ;
150
+ const currentRouteName = navigationRef .current . getCurrentRoute ().name ;
39
151
const trackScreenView = () => {
40
152
// Your implementation of analytics goes here!
41
153
};
42
154
43
155
if (previousRouteName !== currentRouteName) {
44
- // Save the current route name for later comparison
45
- routeNameRef .current = currentRouteName;
46
-
47
156
// Replace the line below to add the tracker from a mobile analytics SDK
48
157
await trackScreenView (currentRouteName);
49
158
}
159
+
160
+ // Save the current route name for later comparison
161
+ routeNameRef .current = currentRouteName;
50
162
}}
51
163
>
52
164
{/* ... */ }
165
+ // codeblock-focus-end
166
+ < Stack .Navigator >
167
+ < Stack .Screen name= " Home" component= {Home} / >
168
+ < Stack .Screen name= " Settings" component= {Settings} / >
169
+ < / Stack .Navigator >
170
+ // codeblock-focus-start
53
171
< / NavigationContainer>
54
172
);
55
- };
173
+ }
174
+ // codeblock-focus-end
56
175
```
176
+
177
+ </TabItem >
178
+ </Tabs >
0 commit comments