Skip to content

docs: update screen tracking for React Navigation #553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 14 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,42 +372,28 @@ Our [example app](./example) is set up with screen tracking using React Navigati

Essentially what we'll do is find the root level navigation container and call `screen()` whenever user has navigated to a new screen.

Find the file where you've used the `NavigationContainer` - the main top level container for React Navigation. In this component, create a new state variable to store the current route name:
Find the file where you've used the `NavigationContainer` - the main top level container for React Navigation. In this component, create 2 new refs to store the `navigation` object and the current route name:

```js
const [routeName, setRouteName] = useState('Unknown');
const navigationRef = useRef(null);
const routeNameRef = useRef(null);
```

Now, outside of the component, create a utility function for determining the name of the selected route:

```js
const getActiveRouteName = (
state: NavigationState | PartialState<NavigationState> | undefined
): string => {
if (!state || typeof state.index !== 'number') {
return 'Unknown';
}

const route = state.routes[state.index];

if (route.state) {
return getActiveRouteName(route.state);
}

return route.name;
};
```

Finally, pass a function in the `onStateChange` prop of your `NavigationContainer` that checks for the active route name and calls `client.screen()` if the route has changes. You can pass in any additional screen parameters as the second argument for screen call as needed.
Next, pass the ref to `NavigationContainer` and a function in the `onReady` prop to store the initial route name. Finally, pass a function in the `onStateChange` prop of your `NavigationContainer` that checks for the active route name and calls `client.screen()` if the route has changes. You can pass in any additional screen parameters as the second argument for screen call as needed.

```js
<NavigationContainer
onStateChange={(state) => {
const newRouteName = getActiveRouteName(state);
ref={navigationRef}
onReady={() => {
routeNameRef.current = navigationRef.current.getCurrentRoute().name;
}}
onStateChange={() => {
const previousRouteName = routeNameRef.current;
const currentRouteName = navigationRef.current?.getCurrentRoute().name;

if (routeName !== newRouteName) {
segmentClient.screen(newRouteName);
setRouteName(newRouteName);
if (previousRouteName !== currentRouteName) {
segmentClient.screen(currentRouteName);
routeNameRef.current = currentRouteName;
}
}}
>
Expand Down