Navigation

Recording a ux.view span per screen with @sophonz/react-native-navigation for expo-router, React Navigation and react-native-navigation — setup, span names and attributes, app state handling and options.

@sophonz/react-native-navigation turns screen changes into spans. It listens to the navigation library you already use, ends the previous screen's span when the route changes and starts one for the new route, so the session timeline shows which screen the user was on when something happened. The package is pure JavaScript; it records through the tracer provider.

Install

npm install @sophonz/react-native-navigation@0.1.0 @sophonz/react-native-tracer-provider@0.1.0
LibraryComponentref to pass
expo-routerSophonzNavigationTrackeruseNavigationContainerRef() from expo-router
@react-navigation/nativeSophonzNavigationTrackeruseRef(useNavigationContainerRef())
react-native-navigationSophonzNativeNavigationTrackeruseRef(Navigation.events())

@react-navigation/native and react-native-navigation are optional peer dependencies; install only the one you use.

expo-router

Wrap the root stack in app/_layout.tsx. Wait for the tracer provider before rendering the tracker, so the first screen's span is not lost.

app/_layout.tsx
import {Stack, useNavigationContainerRef} from "expo-router";
import {useSophonz} from "@sophonz/react-native";
import {useSophonzNativeTracerProvider} from "@sophonz/react-native-tracer-provider";
import {SophonzNavigationTracker} from "@sophonz/react-native-navigation";
 
// Expo with the config plugin starts the SDK natively; these options still apply
const SOPHONZ_CONFIG = {trackUnhandledRejections: true};
 
export default function RootLayout() {
  const {isStarted} = useSophonz(SOPHONZ_CONFIG);
  const {tracerProvider} = useSophonzNativeTracerProvider(undefined, isStarted);
  const navigationRef = useNavigationContainerRef();
 
  if (!tracerProvider) {
    return null; // or a splash screen
  }
 
  return (
    <SophonzNavigationTracker
      ref={navigationRef}
      tracerProvider={tracerProvider}
      screenAttributes={{"app.flavor": "production"}}>
      <Stack>
        <Stack.Screen name="(tabs)" options={{headerShown: false}} />
        <Stack.Screen name="+not-found" />
      </Stack>
    </SophonzNavigationTracker>
  );
}

React Navigation

useNavigationContainerRef from @react-navigation/native returns the navigation object itself rather than a ref. Pass that object to NavigationContainer, and a useRef holding it to the tracker.

App.tsx
import {useRef} from "react";
import {NavigationContainer, useNavigationContainerRef} from "@react-navigation/native";
import {createNativeStackNavigator} from "@react-navigation/native-stack";
import {useSophonz} from "@sophonz/react-native";
import {useSophonzNativeTracerProvider} from "@sophonz/react-native-tracer-provider";
import {SophonzNavigationTracker} from "@sophonz/react-native-navigation";
 
const Stack = createNativeStackNavigator();
const SOPHONZ_CONFIG = {trackUnhandledRejections: true};
 
export default function App() {
  const {isStarted} = useSophonz(SOPHONZ_CONFIG);
  const {tracerProvider} = useSophonzNativeTracerProvider(undefined, isStarted);
 
  const navigation = useNavigationContainerRef();
  const trackerRef = useRef(navigation);
 
  if (!tracerProvider) {
    return null;
  }
 
  return (
    <NavigationContainer ref={navigation}>
      <SophonzNavigationTracker ref={trackerRef} tracerProvider={tracerProvider}>
        <Stack.Navigator>
          <Stack.Screen name="Cart" component={CartScreen} />
          <Stack.Screen name="Checkout" component={CheckoutScreen} />
        </Stack.Navigator>
      </SophonzNavigationTracker>
    </NavigationContainer>
  );
}

react-native-navigation

SophonzNativeNavigationTracker listens to componentDidAppear and componentDidDisappear on Navigation.events(). Those events are global, so wrap one component that stays registered — usually the first screen — rather than every screen, or each wrapper adds its own listeners.

index.tsx
import {useRef} from "react";
import {Navigation} from "react-native-navigation";
import {initialize} from "@sophonz/react-native";
import {SophonzNativeTracerProvider} from "@sophonz/react-native-tracer-provider";
import {SophonzNativeNavigationTracker} from "@sophonz/react-native-navigation";
 
const start = async () => {
  await initialize({sdkConfig: {trackUnhandledRejections: true}});
 
  // the SDK must be running before the provider is created
  const tracerProvider = new SophonzNativeTracerProvider();
 
  Navigation.registerComponent(
    "Home",
    () => props => {
      const events = useRef(Navigation.events());
      return (
        <SophonzNativeNavigationTracker ref={events} tracerProvider={tracerProvider}>
          <HomeScreen {...props} />
        </SophonzNativeNavigationTracker>
      );
    },
    () => HomeScreen,
  );
 
  Navigation.events().registerAppLaunchedListener(() => {
    Navigation.setRoot({root: {stack: {children: [{component: {name: "Home"}}]}}});
  });
};
 
start();

What is recorded

One span per screen visit.

FieldValue
Span nameThe route name (getCurrentRoute().name), or the component name for react-native-navigation
spz.typeux.view. It always wins over a spz.type in screenAttributes
view.nameThe route name again, for querying
view.launchtrue for the first screen after the tracker mounts, false afterwards
view.state.endThe app state when the span ended: active for a normal navigation, background or inactive when the app left the foreground
view.unmounttrue on the span that ended because the tracker unmounted
Your attributesEverything in screenAttributes

A screen span starts when the route name changes. Navigating to the same route with different params, or re-rendering, does not start a new one.

With expo-router the route name is the file-based name the router reports, such as index or (tabs), not the URL. To group dynamic routes, add the value you want to screenAttributes or record custom spans.

App state

The tracker follows AppState as well:

  • When the app goes to background or inactive, the current screen's span ends with view.state.end set to that state.
  • When it returns to active, a new span starts for the same screen, with view.launch: false.

So one screen visit interrupted by a trip to another app appears as two spans. On iOS, inactive also happens briefly for system overlays such as Control Center or an incoming call banner.

Options

PropTypeDefaultDescription
refrefrequiredSee the table in Install
tracerProviderTracerProviderthe global providerThe provider to record with. Pass the Sophonz provider, or register it globally first
screenAttributesAttributes{}Added to every screen span
tracerOptionsTracerOptionsunsetPassed to getTracer, for a schemaUrl
debugbooleantruePrints what the tracker does to the console, prefixed [Sophonz]. Set false in production builds
childrennoderequiredYour navigator

The tracer is named @sophonz/react-native-navigation with version 0.1.0. On iOS tracer names are not recorded.

Native screen capture

The native SDKs record native screens on their own — on Android usually a single MainActivity, on iOS the view controllers the navigation library creates. With this package installed, those spans duplicate or contradict the route spans. Turn native capture off if the route spans are what you want:

android/app/src/main/sophonz-config.json
{
  "sdk_config": {
    "view_config": {"enable_automatic_activity_capture": false}
  }
}
// iOS, when JavaScript starts the SDK
await initialize({sdkConfig: {ios: {collectorUrl: "https://in.sophonz.ai", appKey: "sk_ios_replace_me", disableAutomaticViewCapture: true}}});

When the SDK is started natively on iOS, remove the service in SophonzInitializer.swift instead:

let services = CaptureServicesOptionsBuilder()
  .addDefaults()
  .remove(ofType: ViewCaptureService.self)
  .build()
 
try Sophonz.start(
  options: .withCollector(
    url: "https://in.sophonz.ai",
    appKey: "sk_ios_replace_me",
    platform: .reactNative,
    captureServices: services
  )
)

Troubleshooting a missing screen

With debug on, the console says why:

  • Navigation ref is not available — the ref had no value when the tracker rendered. For React Navigation, pass useRef(useNavigationContainerRef()), not the navigation object itself.
  • No TracerProvider found. Using global tracer instead. — no tracerProvider prop, and nothing registered with trace.setGlobalTracerProvider. Spans go to a no-op tracer.
  • no tracer available, not creating a span — the tracker rendered before the provider existed. Render it only once tracerProvider is set.