React Native Error Tracking installation

Install with the wizard

Run this command in your project directory to add PostHog to your React Native project. The wizard detects your framework, installs the SDK, and sets up your first events.

Or set it up manually by following the rest of this guide.

  1. Install the package

    Required

    Install the PostHog React Native library and its dependencies:

    npx expo install posthog-react-native@5 expo-file-system expo-application expo-device expo-localization
  2. Configure PostHog

    Required

    PostHog is most easily used via the PostHogProvider component. Wrap your app with the provider:

    App.tsx
    import { PostHogProvider } from 'posthog-react-native'
    export function MyApp() {
    return (
    <PostHogProvider
    projectToken="<ph_project_token>"
    options={{
    host: "https://us.i.posthog.com",
    }}
    >
    <RestOfApp />
    </PostHogProvider>
    )
    }
  3. Send events

    Recommended

    Once installed, PostHog will automatically start capturing events. You can also manually send events using the usePostHog hook:

    Component.tsx
    import { usePostHog } from 'posthog-react-native'
    function MyComponent() {
    const posthog = usePostHog()
    const handlePress = () => {
    posthog.capture('button_pressed', {
    button_name: 'signup'
    })
    }
    return <Button onPress={handlePress} title="Sign Up" />
    }
  4. Set up exception autocapture

    Recommended
    Client-side configuration only

    Support for remote configuration in the error tracking settings requires SDK version 4.35.0 or higher.

    You can autocapture exceptions by configuring the errorTracking when setting up PostHog:

    React Native
    export const posthog = new PostHog('<ph_project_token>', {
    errorTracking: {
    autocapture: {
    uncaughtExceptions: true,
    unhandledRejections: true,
    console: ['error', 'warn'],
    nativeCrashes: true, // native iOS/Android crashes (see below)
    },
    },
    })

    Configuration options:

    OptionDescription
    uncaughtExceptionsCaptures Uncaught exceptions (ReactNativeGlobal.ErrorUtils.setGlobalHandler)
    unhandledRejectionsCaptures Unhandled rejections (ReactNativeGlobal.onunhandledrejection)
    consoleCaptures console logs as errors according to the reported LogLevel
    nativeCrashesCaptures native iOS/Android crashes. Requires @posthog/react-native-plugin and uploaded native symbols (see below)
    Capturing native crashes

    nativeCrashes captures native iOS and Android crashes that the JavaScript layer can't see. Beyond the config above, it needs:

    1. The optional native plugin installed — npx expo install @posthog/react-native-plugin@3 (Expo) or npm i @posthog/react-native-plugin@3 (bare React Native). Plugin 3.0 requires posthog-react-native 4.88.4+, React Native 0.75+, iOS 15+, and Xcode 26+. On React Native 0.75 and 0.76, including Expo SDK 52, Android builds also need Kotlin 2.0 or later. If it's missing, native capture is a no-op and your JS-level autocapture is unaffected.
    2. Your project's Enable exception autocapture setting enabled in error tracking settings — the same server-side setting that gates JavaScript autocapture.
    3. Native debug symbols uploaded at build time, so crash stack traces are readable. See native crash symbolication.
  5. Set up error boundaries

    Optional

    You can use the PostHogErrorBoundary component to capture rendering errors thrown by components:

    React Native
    import { PostHogProvider, PostHogErrorBoundary } from 'posthog-react-native'
    import { View, Text } from 'react-native'
    const App = () => {
    return (
    <PostHogProvider projectToken="<ph_project_token>">
    <PostHogErrorBoundary
    fallback={YourFallbackComponent}
    additionalProperties={{ screen: "home" }}
    >
    <YourApp />
    </PostHogErrorBoundary>
    </PostHogProvider>
    )
    }
    const YourFallbackComponent = ({ error, componentStack }) => {
    return (
    <View>
    <Text>Something went wrong!</Text>
    <Text>{error instanceof Error ? error.message : String(error)}</Text>
    </View>
    )
    }
    Duplicate errors with console capture

    If you have both PostHogErrorBoundary and console capture enabled in your errorTracking config, render errors will be captured twice. This is because React logs all errors to the console by default. To avoid this, set console: [] on errorTracking.autocapture (for example, errorTracking: { autocapture: { console: [] } }) when using PostHogErrorBoundary.

    Dev mode behavior

    In development mode, React propagates all errors to the global error handler even when they are caught by an error boundary. This means you may see errors reported twice in dev builds. This is expected React behavior and does not occur in production builds.

  6. Manually capture exceptions

    Optional

    You can manually capture exceptions using the captureException method:

    React Native
    try {
    // Your awesome code that may throw
    someRiskyOperation();
    } catch (error) {
    posthog.captureException(error)
    }

    This is helpful if you've built your own error handling logic or want to capture exceptions that are handled by your application code.

  7. Future features

    Optional

    We currently don't support the following features:

    • No automatic source map uploads on React Native web

    This will be added in a future release. We recommend you stay up to date with the latest version of the PostHog React Native SDK.

  8. Verify error tracking

    Recommended
    Confirm events are being sent to PostHog
    Before proceeding, let's make sure exception events are being captured and sent to PostHog. You should see events appear in the activity feed.
    Activity feed with events
    Check for exceptions in PostHog
  9. Upload source maps & native symbols

    Required

    Great, you're capturing exceptions! The next step is to upload source maps (for JavaScript stack traces) and native symbols (for native iOS/Android crash symbolication) so PostHog can generate accurate stack traces.

    Let's continue to the next section.

    Upload source maps & native symbols

iOS dependency resolution for native crash capture

CocoaPods is becoming read-only. CocoaPods trunk becomes read-only on December 2, 2026, and posthog-ios 4.0 and later is only published through Swift Package Manager (SPM).

@posthog/react-native-plugin 3.0 and later always resolves posthog-ios through SPM, including on the CocoaPods path, so upgrading the plugin is enough. See Upgrading to @posthog/react-native-plugin 3.0.

Plugin 2.x stays on posthog-ios 3.x on every iOS dependency path, including the hybrid and full SPM setups, so switching paths doesn't bring newer iOS SDK versions. To get posthog-ios 4.0 and later, upgrade to plugin 3.0.

This change doesn't affect @posthog/react-native-plugin releases on npm or Android.

The optional @posthog/react-native-plugin package adds native features such as session replay and native crash capture. Install it as described in the guide for the feature that you use.

@posthog/react-native-plugin 3.0 and later requires React Native 0.75 or later and, on every iOS dependency path, Xcode 26 or later and an iOS 15 or later deployment target (macOS 12 or later for React Native macOS apps). See Upgrading to @posthog/react-native-plugin 3.0.

Then choose one iOS dependency path. From 3.0, posthog-ios always comes from Swift Package Manager; the path only decides how the plugin itself is installed:

  • CocoaPods. CocoaPods builds the plugin from node_modules. From @posthog/react-native-plugin 3.0, the plugin's podspec adds posthog-ios as a Swift package, so CocoaPods never installs posthog-ios. In 2.x, CocoaPods installs posthog-ios 3.x. This remains the default path, including for Expo.
  • CocoaPods with posthog-ios through Swift Package Manager. @posthog/react-native-plugin 2.x only. CocoaPods resolves the plugin, and Swift Package Manager resolves posthog-ios. Needs React Native 0.75 or later and a CocoaPods project with dynamic frameworks. From 3.0, the CocoaPods path always does this.
  • Full Swift Package Manager. React Native's experimental Swift Package Manager integration resolves the plugin and posthog-ios, without CocoaPods. Needs @posthog/react-native-plugin 2.4.0 or later, Xcode 16 or later (Xcode 26 or later for plugin 3.0 and later), and an iOS 15.1 or later app deployment target.

PostHog verifies the full Swift Package Manager path with an iOS-only React Native 0.87.1 app and React Native Community CLI 20.2.0. This verification does not cover Expo or other React Native versions. Use the CocoaPods path unless you validate full Swift Package Manager for your configuration. On plugin 3.0, both paths get posthog-ios from Swift Package Manager.

CocoaPods

Use the standard React Native CocoaPods flow:

Terminal
cd ios
pod install

The plugin podspec adds posthog-ios for you. You do not need to add posthog-ios separately.

From @posthog/react-native-plugin 3.0, CocoaPods builds only the plugin's own code, and posthog-ios comes from Swift Package Manager:

  1. React Native's autolinking (use_native_modules!) finds the plugin in node_modules and reads its podspec from there. Nothing is downloaded from CocoaPods trunk, so its read-only date doesn't affect the plugin.
  2. The podspec declares posthog-ios with React Native's spm_dependency helper instead of as a pod.
  3. During pod install, React Native adds the posthog-ios Swift package (https://github.com/PostHog/posthog-ios.git, 4.2.x) to Pods.xcodeproj and links it to the plugin's pod target.
  4. Xcode fetches and builds posthog-ios as a Swift package when you build, and records the version in your workspace's Package.resolved.

Static libraries, static frameworks and dynamic frameworks all work. Plugin 2.x adds posthog-ios 3.x as a CocoaPods dependency instead, unless you set posthog.useSpm (below).

CocoaPods with posthog-ios through Swift Package Manager

This path applies to @posthog/react-native-plugin 2.x only. From 3.0, the CocoaPods path already resolves posthog-ios through Swift Package Manager and the plugin ignores posthog.useSpm.

Add the following property to ios/Podfile.properties.json:

JSON
{
"posthog.useSpm": "true"
}

Add dynamic frameworks to your ios/Podfile:

Ruby
use_frameworks! :linkage => :dynamic

Then install the pods:

Terminal
cd ios
pod install

This setting changes only how the plugin resolves posthog-ios. The plugin and other React Native dependencies still use CocoaPods.

Full Swift Package Manager

This path uses React Native's experimental CocoaPods-free iOS integration. Every native dependency in your app must support React Native's full Swift Package Manager integration. Use CocoaPods (or, on plugin 2.x, the hybrid path) if a dependency does not support it.

Install your JavaScript dependencies first. Make a clean commit or a backup of your iOS project before the conversion. Then run this command from the ios directory:

Terminal
npx react-native spm add --deintegrate --yes

The --deintegrate option removes the complete CocoaPods integration from the iOS project. React Native then finds the plugin's ios/Package.swift manifest. Swift Package Manager resolves the plugin and posthog-ios. Do not run pod install for this path.

PostHog CI verifies this path with the configuration above. The verified app sets its deployment target to iOS 15.1. The plugin package manifest has a separate iOS 15 minimum. On the CocoaPods and hybrid paths, the plugin podspec's minimum is iOS 15 from @posthog/react-native-plugin 3.0, and iOS 13 in earlier versions.

Filtering native events with addBeforeSend

before_send runs only on events captured in JavaScript. Events the native SDKs capture themselves (native crash reports from the previous launch, push notification opens, and session replay) never reach it. To filter or edit those, add a native hook with PosthogReactNativePlugin.addBeforeSend (requires @posthog/react-native-plugin 3.0.0 or later). Fatal JavaScript errors, which the plugin sends natively, pass through both.

Hooks run in the order you add them. Return the event, changed or not, to send it, or null (nil in Swift) to drop it. On Android, a hook that throws drops the event. Add hooks before React Native starts, so they also see the crash reported from the previous launch.

On iOS, add the hook in application(_:didFinishLaunchingWithOptions:):

Swift
import posthog_react_native_plugin // `import PosthogReactNativePlugin` with full Swift Package Manager
PosthogReactNativePlugin.addBeforeSend { event in
event.event == "$exception" ? nil : event
}

On Android, add it in MainApplication.onCreate:

Kotlin
import com.posthogreactnativeplugin.PosthogReactNativePlugin
PosthogReactNativePlugin.addBeforeSend { event ->
if (event.event == "\$exception") null else event
}

Upgrading to @posthog/react-native-plugin 3.0

@posthog/react-native-plugin 3.0 uses posthog-ios 4.2.2 or a later 4.2.x version and posthog-android 4.2.3 or a later 4.2.x version. For before-and-after examples and a checklist, see the migration guide. The plugin requires:

  • posthog-react-native 4.88.4 or later. The plugin reads the project token only from projectToken, which posthog-react-native sends from 4.88.4. With an older version, the plugin logs setup() needs sdkOptions.projectToken and native features (session replay, native crash reports, fatal JavaScript error capture) stay off. JavaScript events are unaffected.
  • React Native 0.75 or later
  • Xcode 26 or later, on every iOS dependency path
  • An iOS 15 or later deployment target, and macOS 12 or later for React Native macOS apps
  • Kotlin 2.0 or later on Android. React Native 0.75 and 0.76 templates, including Expo SDK 52, use Kotlin 1.9, so set kotlinVersion to 2.0.21 and pin the Kotlin Gradle plugin to it. On Expo SDK 52, npx expo prebuild --clean wipes these edits, so make them with a config plugin or upgrade to Expo SDK 53 or later. The migration guide has the details.

On iOS, the plugin now always resolves posthog-ios through Swift Package Manager:

  • Remove "posthog.useSpm" from ios/Podfile.properties.json if you set it. The plugin no longer reads it.
  • The plugin no longer needs use_frameworks! :linkage => :dynamic. Keep it only if another dependency needs it.
  • Before you upgrade, remove the legacy posthog-react-native-session-replay package and any pod 'PostHog' line in your Podfile. Both install posthog-ios 3.x, which clashes with the plugin's 4.x, so pod install stops with an error that names the conflicting pod. If your own native code imports PostHog, add posthog-ios to that target as a Swift package instead. The check only sees pods your Podfile adds directly. Another pod that depends on PostHog still installs, and the Xcode build then fails with Redefinition of module 'PostHog', so remove or update that pod too.
  • Remove these Podfile settings if you use them, because pod install now fails with them: :generate_multiple_pod_projects => true, :deduplicate_targets => false, and targets that use the plugin with different platform or use_frameworks! settings.
  • Make sure build machines can reach https://github.com/PostHog/posthog-ios.git, because Xcode now fetches posthog-ios from there.
  • If your app target has import PostHog, check that it still compiles. React Native's helper adds the package only to the plugin's pod target. This case isn't tested, but if the import no longer resolves, add posthog-ios to your app target as a Swift package with a compatible version.
  • Apps on React Native's full Swift Package Manager integration have no Podfile to change. Build with Xcode 26 or later, and resolve packages again if Package.resolved still pins posthog-ios 3.x. If your app target adds posthog-ios as a package too, require 4.2.2 or a later 4.2.x version, because the plugin pins posthog-ios to 4.2.x. These apps no longer bundle the native surveys from posthog-ios, which the plugin doesn't use because surveys render in JavaScript.
  • If a custom dSYM upload script calls ${PODS_ROOT}/PostHog/build-tools/upload-symbols.sh, change it to ${BUILD_DIR%/Build/*}/SourcePackages/checkouts/posthog-ios/build-tools/upload-symbols.sh. The uploadNativeSymbols build phase from the Expo config plugin already checks both paths.

Apps that use the plugin only through posthog-react-native need no code changes. If your code calls the plugin directly:

  • Call setup() instead of the removed start() method. setup() takes three arguments and reads the replay settings from pluginConfig.sessionReplay. Replay starts only when sessionReplay.enabled is true, which defaults to false:

    TypeScript
    // Before
    start(sessionId, sdkOptions, sdkReplayConfig, decideReplayConfig)
    // After
    setup(sessionId, sdkOptions, {
    sessionReplay: { enabled: true, sdkReplayConfig, decideReplayConfig },
    })
  • Pass throttleDelayMs instead of iOSdebouncerDelayMs or androidDebouncerDelayMs, which the plugin no longer reads. posthog-react-native still maps these deprecated options to throttleDelayMs.

  • Pass the project token in sdkOptions as projectToken. The plugin no longer reads the deprecated apiKey.

On iOS, native uploads (session replay and native crash reports) pause while cellular data is turned off for your app, instead of sending requests that fail.

On iOS and Android, events that the plugin sends natively go to capture v1 (/i/v1/analytics/events) in posthog-ios 4.0 and posthog-android 4.0. These are native crash reports, fatal JavaScript errors, iOS rage clicks ($rageclick), and push-opened events. Events captured in JavaScript still go to /batch. If you self-host PostHog or use a reverse proxy:

  • Forward /i/v1/* to PostHog.
  • Pass these request headers through unchanged: Authorization, Content-Type, Content-Encoding, User-Agent, PostHog-Sdk-Info, PostHog-Attempt, PostHog-Request-Id and PostHog-Request-Timestamp.
  • Return PostHog's response status, body and Retry-After header unchanged. The SDKs read them to decide which events to retry or drop, and drop the events on a 2xx whose body isn't PostHog's.
  • Proxy setups from our guides before October 29, 2026 strip Authorization, so PostHog answers 401 and drops the native events. Update yours from the current guide.
  • Don't rely on a custom Authorization header in requestHeaders for these requests. The SDK replaces it with Bearer <projectToken>, so authenticate your proxy with a different header. Session replay uploads to /s/ still send your Authorization header.
  • Don't redirect these requests to another origin. The SDKs follow only a 307 or 308 redirect to the same origin as host, and drop the events on any other redirect.
  • If the path isn't served and the proxy answers 404, each running app retries about every 30 seconds, and the native events stay queued on the device only until a full queue evicts them (1000 events by default), oldest first. Other errors, such as 401, 403 or 405, drop them.

These native events also change shape:

  • A native event whose distinct ID is empty, whitespace-only or over 200 UTF-8 bytes is dropped. Plugin 2.x shortened a long ID to 200 characters and kept the event. JavaScript events still shorten it, so with a long distinct ID, such as a JWT, your app keeps its JavaScript events but loses its native ones. Keep distinct IDs at 200 bytes or shorter.
  • On iOS, a property whose value is null is left out instead of sent as null. On Android, a null element in a list is kept at its position instead of being removed. $set and $set_once still send null values.

Native events report $locale as the device's preferred language, as a full language tag with region, such as de-CH or zh-Hant-TW instead of de or zh-TW. On iOS this used to be the language your app bundle resolved, so an English-only app on a French device now sends fr-FR instead of en. Native events also add $app_language, the language your app shows, once you call setAppLanguage(). On Android, $locale also uses the current codes he, id and yi instead of the legacy iw, in and ji, so in-ID becomes id-ID. Update insights that filter on a two-letter $locale or on those codes.

If your app creates PostHog clients for more than one project, native events take $app_language only from clients of the project the native SDK was set up for.

If native code in your app, or a native library it uses, calls posthog-ios or posthog-android directly, update it for 4.0. See the posthog-ios migration guide and the posthog-android migration guide. Android code compiled against posthog-android 3.x fails at runtime with errors such as NoSuchMethodError.

Session replay records only masked screenshots on iOS and Android, because posthog-ios 4.0 and posthog-android 4.0 remove wireframe capture. The plugin already forced screenshot mode, so recordings keep the same mode. If native code in your app sets screenshotMode (iOS) or screenshot (Android), remove it, because it no longer compiles.

On iOS, secure text inputs stay masked inside a ph-no-mask view. On Android, touches inside a ph-no-capture view are no longer recorded, as on iOS since plugin 2.13.2. If you upgrade from plugin 2.13.1 or earlier, iOS masking also covers content a masked view draws outside its bounds, such as unclipped images, layer shadows and button titles. If a masked view sits inside a view backed by its own CALayer subclass, such as a lottie-react-native animation, the SDK masks the whole window for that frame, and fast scrolling on such a screen may drop replay frames.

The plugin ignores sessionReplayConfig.screenshotModeBackgroundCapture, because posthog-ios 4.0 removed background screenshot capture, and posthog-react-native logs a warning if you set it. Remove it, or try the experimental iOSCaptureMethod: 'gpu' (see Troubleshooting).

In iOS native crash reports, only frames from your app's executable are in-app. Frames from dynamic frameworks, such as Hermes, are no longer marked as your code, so some existing native crash issues may be grouped again once.

Still have questions?

Was this page useful?