Sign inGet started
Tracking

React Native and Expo

View as MarkdownOpen in ChatGPTOpen in Claude
Connect your agent
Add to CursorAdd to VS Code

Add nextanalytica to a React Native or Expo app: screens, your own events, app versions, JavaScript errors and failed requests, read by your agent.

Updated

@nextanalytica/react-native counts what your React Native or Expo app does: screens, your own events, app versions, JavaScript errors and failed requests to your API. It is plain JavaScript on the same tracker as the tag, so it works in Expo Go. Your agent reads the numbers like any other project's.

How do you add it?

Your agent does the steps, with the project's MCP tools:

  1. Register each platform. Your agent calls add_app once for the iOS bundle identifier and once for the Android application id (ios.bundleIdentifier and android.package in app.json). A project holds up to 10 apps. A batch from an app the project doesn't know is dropped, like a batch from another site.
  2. Add the SDK. Your agent calls get_tag, which returns the install line, the provider and the navigation hook for your app.
  3. Run a development build. check_setup lists the apps and the development builds it has seen. dev_report shows what they hit.
  4. Ship a release build. The first release build of a registered app marks it verified, and the project live. The batch that verifies the app isn't stored: counting starts with the batches after it. The Tracking screen lists each app as verified or waiting.

To do it by hand, your project key (na_…) is the Project token row on the Tracking screen in nextanalytica.

How do you install it?

In an Expo app, with the optional modules that give the SDK the app's version, build and device model, and a place to keep its queue:

Terminal
npx expo install @nextanalytica/react-native expo-application expo-device @react-native-async-storage/async-storage

In a React Native app without Expo:

Terminal
npm install @nextanalytica/react-native @react-native-async-storage/async-storage

Without expo-application, pass appId, version and build yourself. Without AsyncStorage everything stays in memory for the launch. On the web (react-native-web) the SDK does nothing: use the tag.

How do you start it?

Wrap the app's root in the provider. It starts the SDK once, before your screens render.

App.tsx
import { NextanalyticsProvider } from "@nextanalytica/react-native";

export default function App() {
  return (
    <NextanalyticsProvider projectKey="na_…" apiHosts={["api.shop.example"]}>
      {/* your app */}
    </NextanalyticsProvider>
  );
}

Outside React, init("na_…", options) does the same and returns the tracker.

How do you count screens?

With Expo Router, call the hook once in the root layout. Every path the app shows is a screen view.

app/_layout.tsx
import { Stack } from "expo-router";
import { NextanalyticsProvider, useNextanalyticsExpoRouter } from "@nextanalytica/react-native";

export default function RootLayout() {
  useNextanalyticsExpoRouter();
  return (
    <NextanalyticsProvider projectKey="na_…" apiHosts={["api.shop.example"]}>
      <Stack />
    </NextanalyticsProvider>
  );
}

With React Navigation, pass the container's ref. Every screen is a screen view at /<route name>.

App.tsx
import { NavigationContainer, useNavigationContainerRef } from "@react-navigation/native";
import { NextanalyticsProvider, useNextanalyticsNavigation } from "@nextanalytica/react-native";

export default function App() {
  const ref = useNavigationContainerRef();
  useNextanalyticsNavigation(ref);
  return (
    <NextanalyticsProvider projectKey="na_…" apiHosts={["api.shop.example"]}>
      <NavigationContainer ref={ref}>{/* screens */}</NavigationContainer>
    </NextanalyticsProvider>
  );
}

For anything else, call screen("Checkout", "/checkout"): the route is the screen's path and the name is its title. A screen is stored as a page view, so every page metric reads screens too, with the app's id as the host.

How do you send your own events?

checkout.ts
import { identify, reset, track } from "@nextanalytica/react-native";

track("signup_completed", { plan: "team" });
identify("user_8412", { plan: "team" }); // only after a yes
reset(); // on sign-out

Name events in snake_case, as on the web. See Custom events and Identify people.

Call consent at every launch, from the answer your own consent screen keeps:

consent.ts
import { consent } from "@nextanalytica/react-native";

consent("granted"); // or "denied"

The SDK doesn't remember the answer itself, as a website's tag doesn't between pages. Before a yes, nothing is kept on the phone: the session lives in memory for the launch and there is no device id. After consent("granted") the SDK keeps a random device id and any batch the network didn't take. consent("denied") removes everything the SDK kept. The SDK never asks for App Tracking Transparency, because nothing is shared across apps.

What are the options?

OptionWhat it does
apiHostsYour API's hosts (api.shop.example, subdomains included). Failed requests to them are reported. None by default, so nothing is reported until you name your API.
releaseYour own release label. By default the app's version+build.
appId, version, buildThe app's id, version and build, when expo-application isn't installed.
captureRejectionsReport unhandled promise rejections (Hermes). true by default.
endpointWhere batches go. https://t.nextanalytica.app/e by default.

Which requests are reported as failed?

Requests to your apiHosts that answer 5xx or get no answer, with the method and the path (no query, scrubbed), at most 5 a screen. Requests to other hosts are never reported.

Which errors are reported?

  • JavaScript errors, with their stack, from React Native's global handler. Your previous handler still runs. Stacks are scrubbed and cut to 4096 characters. The same error in the next build is the same group, because the group ignores line and column numbers.
  • Unhandled promise rejections on Hermes.
  • Fatal errors, sent at once rather than with the next batch. In a release build delivery is best-effort: the app can end before the request does.

Hermes keeps one rejection tracker. With Sentry present the SDK leaves it to Sentry. With another crash reporter that takes it, pass captureRejections: false.

When a batch gets no answer or a 5xx, the SDK tries it again later. A batch answered with a 4xx is dropped. Nothing the SDK does throws into your app.

What is the release of an app?

The release is the app's version and build, 1.4.0+57. With expo-updates running an over-the-air update, .ota- and the first 8 characters of the update's id follow: 1.4.0+57.ota-3f2a1b9c. Set release to use your own label, 1 to 80 letters, digits and . _ : + / @ -, not starting with marker:.

check_release compares the newest release with at least 5 visits against the newest one with as many visits first seen before it, as it does for a website. See Check your work.

What about development builds?

A development build is one running with __DEV__, or, with expo-device installed, a simulator or emulator. Its events are never stored. They show in your agent's dev_report, under <platform>.<app id> in lower case (ios.com.example.shop), and in check_setup. A release build's numbers are what your agent reads with query and check_release.

What does it send?

  • The app's id, platform, version and build.
  • The operating system and its version, the device model, and whether it is a development build.
  • Screens, your events, JavaScript errors and failed requests as above.

It never reads what a person types, and it sends no advertising id. The visit dimensions platform, release, os_version and device_model split visits, so your agent can ask for sessions by phone model or system version.

What is coming?

Native crashes, native iOS and Android SDKs and Flutter are coming. Until then the SDK sees JavaScript errors only: a crash in native code ends the app without a report.

Next steps

Related
  • What the tag collects: Page views, clicks, forms, scroll, errors and page speed, counted without code. What never leaves the page.
  • Add the tag: Put one script line in the head of every page. Where it goes in plain HTML, site builders and each framework.
  • WordPress and WooCommerce: The nextanalytica plugin for WordPress: connect in a few clicks, cookie banners through the WP Consent API and WooCommerce orders as purchases.
  • Cookieless counting and consent: The tag counts without cookies until a visitor agrees. How consent works, from your banner or your code.
  • Custom events: Send your own events and orders, such as sign-ups, demo requests and purchases, with one line of JavaScript each, so revenue ties to the visit.
  • Identify people: Name signed-in people with identify, so their visits on every device make one profile your agent can read. It works only after the visitor's consent.
Still have questions?
Was this page useful?