Contents

Getting started

The React Native SDK runs in iOS and Android apps, including Expo apps.

To send your first event, install posthog-react-native and the peer dependencies that the install guide lists for Expo or the React Native CLI.

Then wrap your app in PostHogProvider with your project token and host from your project settings, and call capture on the client that usePostHog returns.

Install

 1npm install posthog-react-native

Initialize

Wrap your app in PostHogProvider.

 1import { PostHogProvider } from 'posthog-react-native'
 2
 3export default function App() {
 4  return (
 5    <PostHogProvider projectToken="<ph_project_token>" options={{ host: 'https://us.i.posthog.com' }}>
 6      <YourApp />
 7    </PostHogProvider>
 8  )
 9}

Capture an event

 1import { usePostHog } from 'posthog-react-native'
 2
 3const posthog = usePostHog()
 4posthog.capture('signed_up', { plan: 'pro' })

Identify a user

 1posthog.identify('user_123', { email: 'user@example.com' })

Offline behavior

The SDK keeps capturing events while the device is offline. With the default persistence: 'file', events stay queued in device file storage across app restarts and flush once the device is online.

  • maxQueueSize sets the maximum number of queued events.
  • When the queue is full, the oldest event is evicted first.

Disable for local development

Pass disabled: __DEV__ in PostHogProvider options to turn off tracking during development. Calls through usePostHog() still work as safe no-ops.

 1import { PostHogProvider } from 'posthog-react-native'
 2
 3export function App() {
 4  return (
 5    <PostHogProvider
 6      projectToken="<ph_project_token>"
 7      options={{ host: 'https://us.i.posthog.com', disabled: __DEV__ }}
 8    >
 9      <YourApp />
10    </PostHogProvider>
11  )
12}

Proxy, projects, and allowlists

Choose an iOS dependency path for the native plugin

The optional @posthog/react-native-plugin package needs posthog-ios. Choose one path to resolve it.

Path Requirements Setup
CocoaPods A project that uses CocoaPods Run pod install in ios. The plugin podspec adds posthog-ios for you. This is the default.
CocoaPods with posthog-ios through Swift Package Manager React Native 0.75 or later and dynamic frameworks Set "posthog.useSpm": "true" in ios/Podfile.properties.json. Add use_frameworks! :linkage => :dynamic to ios/Podfile. Then run pod install.
Full Swift Package Manager @posthog/react-native-plugin 2.4.0 or later, Xcode 16 or later, and an iOS 15.1 or later deployment target Run npx react-native spm add --deintegrate --yes in ios. Don't run pod install.

The hybrid path changes only how the plugin resolves posthog-ios. The plugin and your other dependencies still use CocoaPods.

The full path uses React Native's experimental integration without CocoaPods. Every native dependency in your app must support it. --deintegrate removes CocoaPods from the iOS project, so commit or back up the project first. PostHog verifies this path with an iOS-only React Native 0.87.1 app and React Native Community CLI 20.2.0. It is not verified with Expo.

The CocoaPods and hybrid paths keep the plugin's iOS 13 minimum.

Minimum versions

  • disableRemoteFeatureFlags needs 4.49.0 or later.

Upgrade from an older major

From Change
1.x.x Replace PostHog.setup(...) with new PostHog(token, options) or PostHogProvider. PostHogProvider replaces captureApplicationLifecycleEvents and recordScreenViews. Remove captureDeepLinks, android, and iOS.
2.x.x Replace await PostHog.initAsync(...) with new PostHog(token, options).
3.x.x Rename captureNativeAppLifecycleEvents and autocapture={{ captureLifecycleEvents: true }} to captureAppLifecycleEvents. Remove captureMode and maskPhotoLibraryImages. Call setPersonPropertiesForFlags and setGroupPropertiesForFlags in place of personProperties and groupProperties.

1.x.x wrapped the native iOS and Android SDKs. Later majors are written in JavaScript. On iOS, the SDK migrates the persisted distinct ID and anonymous ID. On Android, it can't load them, so call identify when the app loads. Anonymous Android users appear as new users.

The SDK can't detect a real install. It stores a marker the first time it loads and sends Application Installed then. Expect more Application Installed events after you upgrade from 1.x.x.