# React Native and Expo

> Add Lemu to a React Native or Expo app with @lemuai/react-native: events, purchases, known users, screens, app installs and opens, and deep links

URL: https://docs.lemuai.com/sdks/react-native

`@lemuai/react-native` sends events, purchases, known users and screens from your React Native or Expo app, and records installs, app opens and the deep links that bring people in. It's pure JavaScript with no native module of its own, so it works in Expo Go and in bare React Native apps.

You need the write key of an **App** source. See [Get a write key](https://docs.lemuai.com/sdks/overview#get-a-write-key).

## Install

**React Native**

```bash
npm install @lemuai/react-native @react-native-async-storage/async-storage
# optional, for the app version, bundle id and device model:
npm install react-native-device-info
```

**Expo**

```bash
npx expo install @lemuai/react-native @react-native-async-storage/async-storage
```

`react-native-device-info` needs a development build in Expo. Without it, pass the app details yourself (see below).

## Set up

Call `init` once, as early as possible, for example in `index.js` or your root component:
```tsx
import { lemu } from "@lemuai/react-native";

lemu.init("lpk_...");
```

Calls made before storage has loaded are kept and sent in order.

Without `react-native-device-info`, for example in Expo Go, pass the app version, build and id yourself:
```tsx
import * as Application from "expo-application";
import { lemu } from "@lemuai/react-native";

lemu.init("lpk_...", {
  appVersion: Application.nativeApplicationVersion ?? undefined,
  appBuild: Application.nativeBuildVersion ?? undefined,
  appNamespace: "com.brand.app",
});
```

**Warning:** If the source lists [Allowed apps](https://docs.lemuai.com/website-pixel/overview#allowed-apps), Lemu refuses events that don't carry an allowed app id. The SDK reads the id from `react-native-device-info`. Without that package, set `appNamespace` to your bundle id or package name, or leave **Allowed apps** empty.

## Send events
```tsx
import { lemu, LemuEvent } from "@lemuai/react-native";

lemu.track(LemuEvent.AddToCart, { value: 999, currency: "INR", content_ids: ["SKU1"] });
lemu.track("coupon_applied", { code: "DIWALI10" });
```

`LemuEvent` has a constant for each of Meta's standard events and for the app events. Any other name works as a custom event. See the [event reference](https://docs.lemuai.com/sdks/events).

Keep contact details out of event properties. Lemu removes them. Send them with `identify` instead.

## Purchases
```tsx
lemu.purchase({
  value: 1499.5,
  currency: "INR",
  order_id: "1042",
  contents: [{ id: "SKU1", quantity: 1, item_price: 1499.5 }],
});
```

`purchase` sends a `Purchase` event and sends the queue right away. Always pass the order id your backend uses: the same order is often reported by the app, your backend and your payment provider, and Lemu counts one conversion per `order_id`. See [Conversion events](https://docs.lemuai.com/website-pixel/standard-events#conversion-events).

## Identify users
```tsx
// after sign-up or login
lemu.identify("crm-123", { email: "asha@example.com", phone: "+919876543210", first_name: "Asha" });

// on logout
lemu.reset();
```

The user id is stored and sent with every later event, across restarts, until `reset`. `reset` also starts a new anonymous id.

## Screens

Screens aren't tracked automatically. Send one yourself:
```tsx
lemu.screen("Checkout", { step: 2 });
```

Or record every route change in React Navigation:
```tsx
import { NavigationContainer, useNavigationContainerRef } from "@react-navigation/native";
import { useLemuNavigationTracking } from "@lemuai/react-native";

export function App() {
  const navigationRef = useNavigationContainerRef();
  useLemuNavigationTracking(navigationRef);
  return <NavigationContainer ref={navigationRef}>{/* screens */}</NavigationContainer>;
}
```

Outside a component, `trackNavigationState(navigationRef)` does the same and returns a function that stops it. Only the route name is sent, not route params, because params can hold personal data. Screens are stored as `screen_viewed`.

## App events and deep links

The SDK sends these for you:

| Event | When | Properties |
|---|---|---|
| `app_installed` | First launch | `version`, `build` |
| `app_updated` | First launch after the version or build changed | `version`, `build`, `previous_version`, `previous_build` |
| `app_opened` | Every launch and every return from the background | `from_background`, and `url` when a link opened the app |
| `app_backgrounded` | The app goes to the background. Queued events are sent then too | |
| `deep_link_opened` | A link opens the app or arrives while it runs | `url`, `referrer` |

UTM parameters (`utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content`) and `gclid` / `fbclid` in a deep link are kept with that event and every later event in the session, so Lemu can credit what happens next, such as a purchase, to the campaign.

Turn these off with `trackLifecycle: false` and `trackDeepLinks: false`.

## Consent and opt-out
```tsx
lemu.init("lpk_...", { requireConsent: true });

// once the user agrees
lemu.setConsent(true);
```

- Until consent, nothing is sent and nothing is written to storage, not even the anonymous id. Up to 50 events wait in memory and are sent on consent.
- `setConsent(false)` discards them. A granted consent is remembered across launches, and `setConsent(false)` forgets it.
- `lemu.optOut()` stops all tracking and is remembered across launches. `lemu.optIn()` resumes.

## Debug mode
```tsx
lemu.init("lpk_...", { debug: true });
```

Every call, request and response is logged with a `[lemu]` prefix. Bad arguments are logged and ignored; the SDK never throws into your app.

## Options

| Option | Default | What it does |
|---|---|---|
| `apiHost` | `https://app.lemuai.com` | Where to send events |
| `debug` | `false` | Logs calls, requests and responses |
| `requireConsent` | `false` | Holds events until `setConsent(true)` |
| `trackLifecycle` | `true` | `app_installed`, `app_updated`, `app_opened`, `app_backgrounded` |
| `trackDeepLinks` | `true` | `deep_link_opened` and campaign parameters from links |
| `flushAt` | `20` | Sends when this many events are queued |
| `flushInterval` | `30000` | Sends queued events after this many milliseconds |
| `maxQueueSize` | `1000` | The most events kept on the device |
| `appVersion`, `appBuild` | from `react-native-device-info` | Set them when that package isn't installed |
| `appName`, `appNamespace` | from `react-native-device-info` | The app's name, and its bundle id or package name |
| `storage` | AsyncStorage | Any `{ get, set, remove }` storage, for example MMKV |
| `deviceInfo` | found automatically | Pass `react-native-device-info` yourself |

## API

`init(writeKey, options?)`, `track(event, properties?)`, `screen(name, properties?)`, `purchase({ value, currency, order_id, ... })`, `identify(userId?, traits?)`, `reset()`, `flush()`, `setConsent(granted)`, `optOut()`, `optIn()` and `anonymousId()`, plus `useLemuNavigationTracking`, `trackNavigationState`, `createLemu()` for a separate client, and `LemuEvent`.

## Offline and delivery

Events are kept in AsyncStorage (or the storage you pass) and survive restarts. They are sent every 30 seconds, every 20 events, when the app goes to the background, and right after a purchase. Failed requests are retried with increasing waits, up to 5 minutes apart. At most 1,000 events are kept; when the queue is full, the oldest event that isn't a purchase is dropped. Events older than 7 days are dropped.

Requests carry a `User-Agent` such as `lemu-react-native/1.0.0 (iOS 17.4; iPhone15,2)`. No advertising ids or hardware ids are collected. For app store forms, the SDK collects the same data as the native SDKs, so the answers under [Google Play data safety](https://docs.lemuai.com/sdks/android#google-play-data-safety) and [App Privacy answers in App Store Connect](https://docs.lemuai.com/sdks/ios#app-privacy-answers-in-app-store-connect) apply.
