LemuAIDocs

Settings · 4 min read

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

Open in ChatGPTOpen in ClaudeMarkdown

@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.

Install#

Shell
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

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",
});

If the source lists 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.

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.

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.

The SDK sends these for you:

EventWhenProperties
app_installedFirst launchversion, build
app_updatedFirst launch after the version or build changedversion, build, previous_version, previous_build
app_openedEvery launch and every return from the backgroundfrom_background, and url when a link opened the app
app_backgroundedThe app goes to the background. Queued events are sent then too
deep_link_openedA link opens the app or arrives while it runsurl, 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.

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#

OptionDefaultWhat it does
apiHosthttps://app.lemuai.comWhere to send events
debugfalseLogs calls, requests and responses
requireConsentfalseHolds events until setConsent(true)
trackLifecycletrueapp_installed, app_updated, app_opened, app_backgrounded
trackDeepLinkstruedeep_link_opened and campaign parameters from links
flushAt20Sends when this many events are queued
flushInterval30000Sends queued events after this many milliseconds
maxQueueSize1000The most events kept on the device
appVersion, appBuildfrom react-native-device-infoSet them when that package isn't installed
appName, appNamespacefrom react-native-device-infoThe app's name, and its bundle id or package name
storageAsyncStorageAny { get, set, remove } storage, for example MMKV
deviceInfofound automaticallyPass 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 and App Privacy answers in App Store Connect apply.

Was this page helpful?