# Screen tracking

Track which screens people view, automatically with Expo Router or with one call anywhere else.

Enable `analytics: { screens: true }` first (see [Analytics setup](/docs/analytics/setup)). Views feed the **Screen Usage** card on your app's **Analytics** page in [Native Notify](https://dashboard.nativenotify.com).

## Expo Router

Add the hook to your root layout. It tracks every route change: pushes, replaces, back gestures and deep links.

```jsx
import { Slot } from "expo-router";
import { useNativeNotifyScreenTracking } from "native-notify";

export default function RootLayout() {
  useNativeNotifyScreenTracking();

  return <Slot />;
}
```

## Any other navigator

Call `trackScreen(name)` wherever you know the active screen:

```js
import { trackScreen } from "native-notify";

trackScreen("Home");
trackScreen("Settings");
trackScreen(`/products/${productId}`);
```

With React Navigation, call it from `onReady` and `onStateChange`:

```jsx
import { useRef } from "react";
import { NavigationContainer, useNavigationContainerRef } from "@react-navigation/native";
import { trackScreen } from "native-notify";

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

  return (
    <NavigationContainer
      ref={navigationRef}
      onReady={() => {
        // The first screen: onStateChange does not fire for it.
        routeNameRef.current = navigationRef.getCurrentRoute()?.name;
        if (routeNameRef.current) trackScreen(routeNameRef.current);
      }}
      onStateChange={() => {
        const previousRouteName = routeNameRef.current;
        const currentRouteName = navigationRef.getCurrentRoute()?.name;

        if (currentRouteName && previousRouteName !== currentRouteName) {
          trackScreen(currentRouteName);
        }
        routeNameRef.current = currentRouteName;
      }}
    >
      {/* your navigators */}
    </NavigationContainer>
  );
}
```

For any other navigator, `useNativeNotifyScreenTracking()` takes a reader function and reports whenever the name it returns changes on a re-render (native-notify **v5.2+**):

```jsx
useNativeNotifyScreenTracking(() => currentRouteName);
```

## Screen names

Names are trimmed to 120 characters and become the ranking rows, so keep them short and stable. If the list grows long, use `/products/[id]` rather than `/products/123`.

## How reporting works

- Views are batched and sent about two seconds later, or at once when a tracked session ends (with `sessions` on).
- The same screen twice in a row counts once.
- Unique views count distinct devices: the stable `deviceId` when it's on, otherwise the Expo push token.
- Reporting is best-effort and never blocks navigation.
