PostHog Integration for React Native
Set up PostHog analytics in a React Native application: install the SDK, configure the provider, implement event tracking, identify users, and add error tracking.
Workflow
Follow these steps in order:
basic-integration-1.0-begin.md- PostHog Setup - Begin Start herebasic-integration-1.1-edit.md- PostHog Setup - Editbasic-integration-1.2-revise.md- PostHog Setup - Revisebasic-integration-1.3-conclude.md- PostHog Setup - Conclusion
Reference files
references/EXAMPLE.md- React Native example project codereferences/react-native.md- React Native SDK docsreferences/identify-users.md- User identification docs
Framework guidelines
- Install
posthog-react-nativeand its required peer dependencyreact-native-svg - Use
react-native-configto loadPOSTHOG_PROJECT_TOKENandPOSTHOG_HOSTfrom.env(embedded at build time, not runtime) - Place
PostHogProviderINSIDENavigationContainerfor React Navigation v7 compatibility
Minimal provider setup:
import PostHog, { PostHogProvider } from 'posthog-react-native';
import Config from 'react-native-config';
const posthogClient = new PostHog(Config.POSTHOG_PROJECT_TOKEN, {
host: Config.POSTHOG_HOST,
});
// Inside your App component, wrap content within NavigationContainer:
<NavigationContainer>
<PostHogProvider client={posthogClient}>
{/* app content */}
</PostHogProvider>
</NavigationContainer>
Identifying users
Identify users during login and signup events. Pass the client-side session and distinct ID using X-POSTHOG-DISTINCT-ID and X-POSTHOG-SESSION-ID headers to maintain frontend/backend correlation. Refer to references/identify-users.md and references/EXAMPLE.md for the correct pattern.
Error tracking
Add PostHog error tracking around critical user flows and API boundaries. Capture errors with posthog.capture('$exception', { $exception_message: error.message }).
Verification
After completing the integration:
- Run the app and trigger a test event
- Confirm the event appears in the PostHog project dashboard within 60 seconds
- Verify user identification by checking the "Persons" tab for the identified user