Skip to content

Commit 9a1a3a7

Browse files
gzzobrentvatne
authored andcommitted
Update screen tracking page with appContainer (react-navigation#379)
Fixes react-navigation#378 * Use appContainer for onNavigationStateChange * Create versioned screen-tracking.md * Remove redux content
1 parent 51e769f commit 9a1a3a7

File tree

2 files changed

+51
-54
lines changed

2 files changed

+51
-54
lines changed

docs/screen-tracking.md

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ This example shows how to do screen tracking and send to Google Analytics. The a
88

99
## Listening to State Changes
1010

11-
Most users can use `onNavigationStateChange` to track the screen. If you manually control the top-level navigator (if you have integrated redux), this will not work for you.
12-
1311
```js
12+
import { createAppContainer, createStackNavigator } from 'react-navigation';
1413
import { GoogleAnalyticsTracker } from 'react-native-google-analytics-bridge';
1514

1615
const tracker = new GoogleAnalyticsTracker(GA_TRACKING_ID);
@@ -29,10 +28,11 @@ function getActiveRouteName(navigationState) {
2928
}
3029

3130
const AppNavigator = createStackNavigator(AppRouteConfigs);
31+
const AppContainer = createAppContainer(AppNavigator);
3232

3333
export default () => (
34-
<AppNavigator
35-
onNavigationStateChange={(prevState, currentState) => {
34+
<AppContainer
35+
onNavigationStateChange={(prevState, currentState, action) => {
3636
const currentScreen = getActiveRouteName(currentState);
3737
const prevScreen = getActiveRouteName(prevState);
3838

@@ -45,53 +45,3 @@ export default () => (
4545
/>
4646
);
4747
```
48-
49-
## Screen tracking with Redux
50-
51-
When using Redux, we can write a Redux middleware to track the screen. For this purpose,
52-
we will reuse `getActiveRouteName` from the previous section.
53-
54-
```js
55-
import { NavigationActions } from 'react-navigation';
56-
import { GoogleAnalyticsTracker } from 'react-native-google-analytics-bridge';
57-
58-
const tracker = new GoogleAnalyticsTracker(GA_TRACKING_ID);
59-
60-
const screenTracking = ({ getState }) => next => (action) => {
61-
if (
62-
action.type !== NavigationActions.NAVIGATE
63-
&& action.type !== NavigationActions.BACK
64-
) {
65-
return next(action);
66-
}
67-
68-
const currentScreen = getActiveRouteName(getState().navigation);
69-
const result = next(action);
70-
const nextScreen = getActiveRouteName(getState().navigation);
71-
if (nextScreen !== currentScreen) {
72-
// the line below uses the Google Analytics tracker
73-
// change the tracker here to use other Mobile analytics SDK.
74-
tracker.trackScreenView(nextScreen);
75-
}
76-
return result;
77-
};
78-
79-
export default screenTracking;
80-
```
81-
82-
### Create Redux store and apply the above middleware
83-
84-
The `screenTracking` middleware can be applied to the store during its creation. See [Redux Integration](redux-integration.html) for details.
85-
86-
```js
87-
const store = createStore(
88-
combineReducers({
89-
navigation: navigationReducer,
90-
...
91-
}),
92-
applyMiddleware(
93-
screenTracking,
94-
...
95-
),
96-
);
97-
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
id: screen-tracking
3+
title: Screen tracking
4+
sidebar_label: Screen tracking
5+
---
6+
7+
This example shows how to do screen tracking and send to Google Analytics. The approach can be adapted to any other analytics SDK.
8+
9+
## Listening to State Changes
10+
11+
```js
12+
import { createAppContainer, createStackNavigator } from 'react-navigation';
13+
import { GoogleAnalyticsTracker } from 'react-native-google-analytics-bridge';
14+
15+
const tracker = new GoogleAnalyticsTracker(GA_TRACKING_ID);
16+
17+
// gets the current screen from navigation state
18+
function getActiveRouteName(navigationState) {
19+
if (!navigationState) {
20+
return null;
21+
}
22+
const route = navigationState.routes[navigationState.index];
23+
// dive into nested navigators
24+
if (route.routes) {
25+
return getActiveRouteName(route);
26+
}
27+
return route.routeName;
28+
}
29+
30+
const AppNavigator = createStackNavigator(AppRouteConfigs);
31+
const AppContainer = createAppContainer(AppNavigator);
32+
33+
export default () => (
34+
<AppContainer
35+
onNavigationStateChange={(prevState, currentState, action) => {
36+
const currentScreen = getActiveRouteName(currentState);
37+
const prevScreen = getActiveRouteName(prevState);
38+
39+
if (prevScreen !== currentScreen) {
40+
// the line below uses the Google Analytics tracker
41+
// change the tracker here to use other Mobile analytics SDK.
42+
tracker.trackScreenView(currentScreen);
43+
}
44+
}}
45+
/>
46+
);
47+
```

0 commit comments

Comments
 (0)