@@ -4,6 +4,9 @@ title: Screen tracking for analytics
44sidebar_label : Screen tracking for analytics
55---
66
7+ import Tabs from '@theme/Tabs ';
8+ import TabItem from '@theme/TabItem ';
9+
710To track the currently active screen, we need to:
811
9121 . 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
1518
1619This example shows how the approach can be adapted to any mobile analytics SDK.
1720
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 >
19103
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
21108import {
22109 NavigationContainer ,
23110 useNavigationContainerRef ,
24111} 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 ();
25135
26- export default () => {
136+ // codeblock-focus-start
137+
138+ export default function App () {
27139 const navigationRef = useNavigationContainerRef ();
28- const routeNameRef = useRef ();
140+ const routeNameRef = React . useRef ();
29141
30142 return (
31143 < NavigationContainer
32144 ref= {navigationRef}
33145 onReady= {() => {
34- routeNameRef .current = navigationRef .getCurrentRoute ().name ;
146+ routeNameRef .current = navigationRef .current . getCurrentRoute ().name ;
35147 }}
36148 onStateChange= {async () => {
37149 const previousRouteName = routeNameRef .current ;
38- const currentRouteName = navigationRef .getCurrentRoute ().name ;
150+ const currentRouteName = navigationRef .current . getCurrentRoute ().name ;
39151 const trackScreenView = () => {
40152 // Your implementation of analytics goes here!
41153 };
42154
43155 if (previousRouteName !== currentRouteName) {
44- // Save the current route name for later comparison
45- routeNameRef .current = currentRouteName;
46-
47156 // Replace the line below to add the tracker from a mobile analytics SDK
48157 await trackScreenView (currentRouteName);
49158 }
159+
160+ // Save the current route name for later comparison
161+ routeNameRef .current = currentRouteName;
50162 }}
51163 >
52164 {/* ... */ }
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
53171 < / NavigationContainer>
54172 );
55- };
173+ }
174+ // codeblock-focus-end
56175```
176+
177+ </TabItem >
178+ </Tabs >
0 commit comments