Skip to main content

Screen tracking for analytics

To track the currently active screen, we need to:

  1. Add a callback to get notified of state changes
  2. Get the root navigator state and find the active route name

To get notified of state changes, we can use the onStateChange prop on NavigationContainer. To get the root navigator state, we can use the getRootState method on the container's ref. Please note that onStateChange is not called on initial render so you have to set your initial screen separately.

Example

This example shows how the approach can be adapted to any mobile analytics SDK.

import {
createStaticNavigation,
useNavigationContainerRef,
useNavigation,
} from '@react-navigation/native';

export default function App() {
const navigationRef = useNavigationContainerRef();
const routeNameRef = React.useRef();

return (
<Navigation
ref={navigationRef}
onReady={() => {
routeNameRef.current = navigationRef.current.getCurrentRoute().name;
}}
onStateChange={async () => {
const previousRouteName = routeNameRef.current;
const currentRouteName = navigationRef.current.getCurrentRoute().name;
const trackScreenView = () => {
// Your implementation of analytics goes here!
};

if (previousRouteName !== currentRouteName) {
// Replace the line below to add the tracker from a mobile analytics SDK
await trackScreenView(currentRouteName);
}

// Save the current route name for later comparison
routeNameRef.current = currentRouteName;
}}
/>
);
}
Try on Snack
note

If you are building a library that wants to provide screen tracking integration with React Navigation, you can accept a ref to the navigation container and use the ready and state events instead of onReady and onStateChange props to keep your logic self-contained.

Libraries with built-in integration

Here are some popular telemetry and analytics libraries that have built-in integration with React Navigation for screen tracking:

PostHog

Open source product analytics platform with self-hosted and cloud-hosted options. Learn more.

Embrace

Observability platform for mobile and web, powered by OpenTelemetry. Learn more.

Vexo

Analytics for web and React Native. Learn more.

Datadog

Real User Monitoring and error tracking platform. Learn more.

Sentry

Application performance monitoring and error tracking platform. Learn more.

Segment

Customer data platform that supports React Native. Learn more.

Luciq

Mobile observability and experience platform. Learn more.