React Native and Expo analytics: track screens and events
Track screens and events in a mobile app built with Expo or React Native.
Install#
npx expo install @shipabase/jsWorks in Expo and bare React Native: the SDK uses fetch and stores nothing on the device.
Expo Router#
Call init once, then record a screen view each time the route changes:
import { useEffect } from "react";import { Stack, usePathname } from "expo-router";import { init, trackPageview } from "@shipabase/js"; init(process.env.EXPO_PUBLIC_SHIPABASE_KEY!); export default function RootLayout() { const pathname = usePathname(); useEffect(() => trackPageview(pathname), [pathname]); return <Stack />;}React Navigation#
import { NavigationContainer, useNavigationContainerRef } from "@react-navigation/native";import { init, trackPageview } from "@shipabase/js"; init(process.env.EXPO_PUBLIC_SHIPABASE_KEY!); export default function App() { const ref = useNavigationContainerRef(); return ( <NavigationContainer ref={ref} onStateChange={() => trackPageview(ref.getCurrentRoute()?.name)}> {/* … */} </NavigationContainer> );}Track events#
import { trackEvent } from "@shipabase/js"; trackEvent("workout_completed", { minutes: 25 });Screens show up in the Pages card, and every event is attached to the screen it happened on.
Related guides
DocsAutomatic page view tracking for SPAs and mobile screensPages are tracked automatically in the browser. Screens in mobile apps take one line.DocsHow sessions are counted without cookies (30-minute gap)How visits are grouped, and how conversions are counted.DocsCheck if analytics is workingSee your first event, and fix it when nothing shows up.