# Add Analytics to an Expo App

Yes. An agent adds PostHog to your Expo app with screen events from React Navigation, identify on login and reset on logout, keeps Jest green and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-analytics/expo/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the repository into its own cloud sandbox and reads how the app starts and navigates before it adds a tracker. In an app like the `boilerplate/` of infinitered/ignite, `package.json` is on `expo` 55.0.5 and React Native 0.83.2 with `@react-navigation/native` 7, `react-native-mmkv` 3.3.3, `jest-expo` and no analytics package. `app/utils/crashReporting.ts` is a stub with Sentry, Bugsnag and Crashlytics calls commented out, so there is no existing vendor to fit around. `app/navigators/navigationUtilities.ts` already knows when the screen changes: `useNavigationPersistence` keeps the previous route name in a ref, computes the active one with `getActiveRouteName(state)` on every state change, and under a `// track screens.` comment does nothing but `console.log` it in development. `app/context/AuthContext.tsx` stores `authToken` and `authEmail` in MMKV through `useMMKVString` and exposes `setAuthToken` and `logout`. `app/config/config.base.ts` is typed, merged with `config.dev.ts` or `config.prod.ts` by `app/config/index.ts`, and its header says plainly that nothing in the JavaScript bundle is secret.

It installs `posthog-react-native` and the Expo peer packages PostHog's own install guide lists, `expo-file-system`, `expo-application`, `expo-device` and `expo-localization`, with `npx expo install` so each version matches SDK 55; `expo-localization` is already there. It adds `POSTHOG_API_KEY` and `POSTHOG_HOST` to `ConfigBaseProps` and the two environment files, and creates `app/services/analytics/index.ts` that builds one `PostHog` client from them with `disabled: __DEV__`, so a development build, a Jest run and the Maestro flows send nothing. `app/app.tsx` wraps the provider tree in `PostHogProvider` with that client, placed outside `AuthProvider` so the auth context can reach it, and with `autocapture` set to capture touches but not screens.

Screens are tracked by hand because React Navigation 7, which this app uses, is where PostHog's own docs say automatic screen capture stops working. The agent replaces the `console.log` in `onNavigationStateChange` with `analytics.screen(currentRouteName)`, which fires once per route change and never twice for the same screen, since the function already compares against `routeNameRef`. In `AuthContext.tsx`, `setAuthToken` is wrapped so a successful login calls `identify(authEmail)` and `logout` calls `reset()`, which is the order PostHog needs so events after a sign-out belong to a new anonymous id. No `capture` call is added to the demo screens; the pull request shows the one line to add where a product event belongs.

For tests, `test/setup.ts` already mocks `react-native` and `i18next`, and the agent adds a `jest.mock("posthog-react-native")` there whose client stubs `screen`, `identify`, `reset` and `capture`. A new `app/services/analytics/analytics.test.ts` calls the navigation state handler with a nested state and asserts one `screen` call with the leaf route name, then a second call with the same route and no new event. It runs `npm run compile` (`tsc --noEmit`), `npm run lint:check`, `npm test` and `npx expo export --platform web` in the sandbox to show the bundle still builds with the provider in the tree, and leaves `.maestro/flows/` as they are.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add PostHog analytics to this Expo app. Read app/app.tsx, app/config, app/context/AuthContext.tsx and app/navigators/navigationUtilities.ts first. Install posthog-react-native and its Expo peer packages with npx expo install, add the project key and host to the typed config files, create one client under app/services/analytics that is disabled in development, and wrap the app in PostHogProvider outside the auth provider. Track screens manually where onNavigationStateChange already detects a route change, call identify with the signed-in email after login and reset on logout. Mock the client in the Jest setup, add a test for the screen tracking, run the type check, lint, Jest and a web export, and open a pull request that lists every event the app now sends.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch that changes `package.json`, `app/app.tsx`, `app/config/config.base.ts`, `config.dev.ts`, `config.prod.ts`, `app/context/AuthContext.tsx`, `app/navigators/navigationUtilities.ts` and `test/setup.ts`, and adds `app/services/analytics/index.ts` with its test. The description lists each event the app now sends, `$screen` per route change, the identify call and the reset, says which config value holds the project key and why it is not a secret, and pastes the `tsc`, ESLint, Jest and `expo export` output from the sandbox.

The run's record keeps every command with its output, including the `npx expo install` resolution that chose package versions for SDK 55, so a reviewer can see what was installed without repeating it.

Before merging, a reviewer sets the real project key and host per environment, decides whether the EU or US PostHog host applies, confirms the app's privacy disclosure covers analytics on both stores, and opens a development build on a device, because the sandbox proves the bundle compiles and the tests pass but cannot show an event arrive in the PostHog dashboard.

## Run it on every Linear issue labelled analytics

In the console, an automation in the Project that has the repository, with a `linear` trigger on `linear.issue.labeled` and `labels` set to `analytics`, starts one run per issue with the issue as its context after the prompt; the saved prompt is the whole instruction, and the trigger fills in no placeholder. Once this first pull request is in, later issues such as a `capture` call for a podcast favourite or a funnel on the login screen are each one run that adds the event, its properties and a test on top of the client this run introduced.

From your own tracker, a handler sends one `POST /o/{org}/sessions` with this body, the repository in `resources` and a `request_id` that is a UUID v5 of the issue id, so a repeated delivery returns the run already started instead of a second pull request. With a `webhook_url` on the key, Rush posts `run.completed` with the `session_url` when the pull request is up.

## Why run it on Rush?

Adding a tracker touches the provider tree, the auth context, the navigation container and the test setup at once, and the mistakes are quiet ones: a provider inside the auth boundary so identify cannot be called, screen capture that silently stops on React Navigation 7, events sent from every developer's simulator, or a Jest suite that now tries to reach the network. The agent reads the real navigation utilities and config layering, and runs the type check, the tests and a web export in its own sandbox before the pull request exists.

The sandbox is a computer of its own with the Node version from `engines` and the Expo tooling the repository pins, and every install and test it runs stays in the run's record. Several apps can get the same instrumentation at once in separate sandboxes, each ending in its own pull request that a reviewer reads with the command output beside it.

## Frequently asked questions

### Will development builds and tests send events?

No. The client is created with `disabled: __DEV__`, so a dev client, the Jest run and the Maestro flows all call a client that drops events, and the Jest setup mocks the package as well. A production build sends events as soon as the project key is set in `config.prod.ts`.

### Why track screens by hand instead of autocapture?

The app is on React Navigation 7, and PostHog's documentation says screen autocapture applies to version 6 and lower, so on 7 it recommends calling `screen()` yourself. Ignite already detects every route change in `onNavigationStateChange`, so one line there covers every screen, including nested tab screens, through `getActiveRouteName`.

### Is the PostHog key safe in the config files?

It is a project token, not a secret, and `app/config/index.ts` already explains that anything in the bundle can be read from the app. The pull request keeps the key in the typed config next to `API_URL`; an `EXPO_PUBLIC_` environment variable works the same way if you prefer to set it per EAS profile.

### What happens to a user's events after sign-out?

`logout` calls `reset()`, which clears the identified distinct id and starts a fresh anonymous one, so the next person on the same device is not merged into the previous account. A new sign-in calls `identify` again with that user's email.

## More Expo jobs

- [Add Biometric Login to an Expo App from a Linear Issue](https://getrush.ai/run/add-biometric-auth/expo/)
- [Add Crash Reporting to an Expo App](https://getrush.ai/run/add-crash-reporting/expo/)
- [Add Deep Links to an Expo App](https://getrush.ai/run/add-deep-links/expo/)

## The same job on other stacks

- [Add Google Analytics to a Gatsby Blog](https://getrush.ai/run/add-analytics/gatsby/)
- [Add Analytics to an Astro Blog](https://getrush.ai/run/add-analytics/astro/)

## More jobs like this: Turn a Linear label into a PR

- [Add StatusPages Error Handling to a Ktor API](https://getrush.ai/run/add-api-error-handling/ktor/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
