2017-02-09 22:10:16 +08:00
|
|
|
## Screen tracking and analytics
|
|
|
|
|
|
|
|
This example shows how to do screen tracking and send to Google Analytics. The approach can be adapted to any other mobile analytics SDK.
|
|
|
|
|
2017-02-24 07:18:06 +01:00
|
|
|
### Screen tracking
|
2017-02-09 22:10:16 +08:00
|
|
|
|
2017-02-24 07:18:06 +01:00
|
|
|
When using built-in navigation container, we can use `onNavigationStateChange` to track the screen.
|
2017-02-09 22:10:16 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
import { GoogleAnalyticsTracker } from 'react-native-google-analytics-bridge';
|
|
|
|
|
|
|
|
const tracker = new GoogleAnalyticsTracker(GA_TRACKING_ID);
|
|
|
|
|
|
|
|
// gets the current screen from navigation state
|
2017-03-03 14:58:58 +08:00
|
|
|
function getCurrentRouteName(navigationState) {
|
2017-02-24 07:18:06 +01:00
|
|
|
if (!navigationState) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-03-03 14:58:58 +08:00
|
|
|
const route = navigationState.routes[navigationState.index];
|
|
|
|
// dive into nested navigators
|
|
|
|
if (route.routes) {
|
|
|
|
return getCurrentRouteName(route);
|
|
|
|
}
|
|
|
|
return route.routeName;
|
2017-02-09 22:10:16 +08:00
|
|
|
}
|
|
|
|
|
2017-02-24 07:18:06 +01:00
|
|
|
const AppNavigator = StackNavigator(AppRouteConfigs);
|
|
|
|
|
|
|
|
export default () => (
|
|
|
|
<AppNavigator
|
|
|
|
onNavigationStateChange={(prevState, currentState) => {
|
2017-03-03 14:58:58 +08:00
|
|
|
const currentScreen = getCurrentRouteName(currentState);
|
|
|
|
const prevScreen = getCurrentRouteName(prevState);
|
2017-02-24 07:18:06 +01:00
|
|
|
|
2017-03-02 10:01:50 -05:00
|
|
|
if (prevScreen !== currentScreen) {
|
2017-02-24 07:18:06 +01:00
|
|
|
// the line below uses the Google Analytics tracker
|
|
|
|
// change the tracker here to use other Mobile analytics SDK.
|
2017-03-02 10:01:50 -05:00
|
|
|
tracker.trackScreenView(currentScreen);
|
2017-02-24 07:18:06 +01:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
### Screen tracking with Redux
|
|
|
|
|
|
|
|
When using Redux, we can write a Redux middleware to track the screen. For this purpose,
|
2017-03-03 14:58:58 +08:00
|
|
|
we will reuse `getCurrentRouteName` from the previous section.
|
2017-02-24 07:18:06 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
import { NavigationActions } from 'react-navigation';
|
|
|
|
import { GoogleAnalyticsTracker } from 'react-native-google-analytics-bridge';
|
|
|
|
|
|
|
|
const tracker = new GoogleAnalyticsTracker(GA_TRACKING_ID);
|
|
|
|
|
2017-02-09 22:10:16 +08:00
|
|
|
const screenTracking = ({ getState }) => next => (action) => {
|
2017-02-24 07:18:06 +01:00
|
|
|
if (
|
|
|
|
action.type !== NavigationActions.NAVIGATE
|
|
|
|
&& action.type !== NavigationActions.BACK
|
|
|
|
) {
|
|
|
|
return next(action);
|
|
|
|
}
|
2017-02-09 22:10:16 +08:00
|
|
|
|
2017-03-03 14:58:58 +08:00
|
|
|
const currentScreen = getCurrentRouteName(getState().navigation);
|
2017-02-09 22:10:16 +08:00
|
|
|
const result = next(action);
|
2017-03-03 14:58:58 +08:00
|
|
|
const nextScreen = getCurrentRouteName(getState().navigation);
|
2017-02-09 22:10:16 +08:00
|
|
|
if (nextScreen !== currentScreen) {
|
|
|
|
// the line below uses the Google Analytics tracker
|
|
|
|
// change the tracker here to use other Mobile analytics SDK.
|
|
|
|
tracker.trackScreenView(nextScreen);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default screenTracking;
|
|
|
|
```
|
|
|
|
|
|
|
|
### Create Redux store and apply the above middleware
|
|
|
|
|
|
|
|
The `screenTracking` middleware can be applied to the store during its creation. See [Redux Integration](Redux-Integration.md) for details.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const store = createStore(
|
|
|
|
combineReducers({
|
2017-02-23 15:10:40 +05:30
|
|
|
navigation: navigationReducer,
|
2017-02-09 22:10:16 +08:00
|
|
|
...
|
|
|
|
}),
|
|
|
|
applyMiddleware(
|
|
|
|
screenTracking,
|
|
|
|
...
|
|
|
|
),
|
|
|
|
);
|
|
|
|
```
|