# Product analytics for React Native

Add product analytics to a React Native or Expo app with Pug: app opens, installs, React Navigation and Expo Router screens, typed events, and consent.

Source: https://pug.sh/integrations/react-native

Pug’s React Native SDK is a TypeScript API over its native Android and iOS SDKs, so the queue, retries, sessions, and consent run on the device. Initialize it once and app opens and installs are tracked from there; trackNavigation adds a screen view for each route.

- Pug React Native SDK: one SDK for React Native
- @pug-sh/react-native
- Open source: MIT SDK, AGPL-3.0 server

## Add Pug to React Native

Four steps, start to first insight. Everything below uses the React Native SDK exactly as it ships.

### 1. Install and rebuild

The SDK needs React Native 0.83 or newer on the New Architecture. It has native code, so rebuild the app after installing: pod install and a new build for a bare app, or npx expo prebuild and a development build for Expo. Expo Go cannot load native code; there every call does nothing and one error says so.

```bash
npm install @pug-sh/react-native

# A bare app:
cd ios && pod install

# An Expo app:
npx expo prebuild
npx expo run:ios
```

### 2. Initialize once

Call init at the top of your app’s entry file, outside any component, with your project id and public key. It throws only for a blank project id, a key that is not the project’s public key (pub_…), or an endpoint that is not an http(s) URL. A second init, such as the one Fast Refresh makes, is ignored with a warning.

`index.ts`

```ts
import { init } from '@pug-sh/react-native'

init('YOUR_PROJECT_ID', {
  apiKey: 'pub_YOUR_PUBLIC_KEY',
  debug: __DEV__,
})
```

### 3. Track screens from the navigator

Activities and view controllers are not screens here, since a React Native app has one of each. Hand trackNavigation your navigation container’s ref and each change of the focused route is a screen_view named after the route; later events carry it as $url, with the one before as $referrer.

`App.tsx`

```tsx
import { trackNavigation } from '@pug-sh/react-native'
import {
  createNavigationContainerRef,
  NavigationContainer,
} from '@react-navigation/native'

const navigationRef = createNavigationContainerRef()
trackNavigation(navigationRef)

export const App = () => (
  <NavigationContainer ref={navigationRef}>
    {/* your navigators */}
  </NavigationContainer>
)
```

### 4. Identify people and track events

identify() links the events tracked anonymously so far to your user id, with optional traits, and reset() on logout starts a new anonymous id and session. A well-known kind passed to track() has its properties typed; anything else goes through the same call. Every call returns at once and never throws.

`checkout.ts`

```ts
import { identify, reset, track } from '@pug-sh/react-native'

identify('user_123', { plan: 'pro' })

// A well-known event, its properties typed:
track('purchase', { productId: 'sku-1', amount: 89, currency: 'USD' })

// Anything else, by name:
track('tutorial_finished', { stepCount: 4 })

// On logout:
reset()
```

## What to watch for in React Native

The specifics that decide whether the numbers you see are the numbers that happened.

### Expo Router names screens from segments

A route’s name is relative to its layout, so every index.tsx is named index. In the root layout, call screen() with the joined useSegments() in a useLayoutEffect: a page at app/(shop)/products/[id].tsx is then /(shop)/products/[id], whatever the id, and the page’s own effects see it.

### Consent is granted by default

To wait for a banner, set defaultTrackingConsent: 'denied'. While denied, events and identify calls are dropped rather than queued; the choice is saved across launches, and an app_install, app_open, or screen view that consent held back at launch is tracked when the user opts in.

### The native SDKs do the delivery

Events queue and send in batches from native code, with retries and backoff, and survive restarts in files the native SDKs own. Leaving the foreground sends what is queued and writes state to disk, on iOS inside a background task.

### Android and iOS only

On the web, in Expo Go, or in an app not rebuilt since installing, the native module is missing: every call does nothing and one error says so. Use the Web SDK in a browser. On iOS the package compiles its own copy of the iOS SDK, so do not add sdk-ios to the same app.

## Product analytics in your React Native app

### Auto-tracking from init()

app_open and app_close as the app comes and goes, and app_install or app_update on the first launch of each version, from the native SDKs underneath.

### Screens from your navigator

trackNavigation(ref) tracks a screen_view each time the focused route changes in React Navigation 7 or Expo Router, and screen() names any other.

### Typed well-known events

Pass a well-known kind to track() and your editor completes and checks its properties, with the required ones required; anything custom goes through the same call.

### Consent that survives restarts

Start denied, and useTrackingConsent() tells a banner whether to ask. The choice is saved, and what consent held back at launch is tracked once the user says yes.

## Read it in the docs

The React Native SDK reference goes deeper than a setup page can: every option, every event, and the edge cases.

- [React Native SDK README](https://github.com/pug-sh/sdk-react-native#readme): Every option, the automatic events, screens, consent, delivery, and logging.
- [Well-known events](https://docs.pug.sh/reference/well-known-events/): The generated event kinds that track() types.
- [Public keys](https://docs.pug.sh/get-started/authentication/#public-key-client-sdks): Why the write-scoped public key is safe to ship inside an app.
- [Anonymous to identified](https://docs.pug.sh/get-started/concepts/#anonymous-to-identified): How identify() joins an install’s anonymous history to one person.

* [Start from the beginning](https://docs.pug.sh/get-started/)
* [See all SDKs](https://pug.sh/sdks)
* [What to measure in a mobile app](https://pug.sh/learn/product-analytics-for-mobile-apps)
* [Firebase Analytics alternatives, compared](https://pug.sh/learn/best-firebase-analytics-alternatives)

## Pug + React Native: common questions

### Which React Native versions does it support?

React Native 0.83 or newer on the New Architecture, in bare apps and Expo apps with a development build. It runs on Pug’s Android and iOS SDKs, which it brings with it: there is nothing else to add.

### Does it work in Expo Go?

No. Expo Go cannot load native code, so there every call does nothing and one error says so. Build a development build with npx expo prebuild and npx expo run:android or run:ios, or with EAS.

### Does it track before the user consents?

Yes: consent is granted by default, and there is no cookieless mode in the mobile SDKs. To wait for a choice, start with defaultTrackingConsent: 'denied' and show your banner while useTrackingConsent() reports the choice pending.

### Does it send push notifications?

No. Push and device registration are out of scope for the React Native, Android, and iOS SDKs.

### Does the AGPL apply to my app?

No. The SDK you ship in your app is MIT-licensed; AGPL-3.0 covers the Pug server. Self-host it and point the SDK there with the endpoint option, or use Pug Cloud, free up to 100k events a month.

### Can one project hold my React Native app and my website?

Yes. Call identify() with the same user id from the app, the Web SDK, and your server, and the timelines resolve to one person, so a funnel can start on your site and finish in the app.
