🐛 - Fix PostHog dropping events & bump to 2.6.2#609
Conversation
Share a single PostHog client between the standalone instance and PostHogProvider so they no longer race on persistence. Upgrade posthog-react-native to 4.44.1 and posthog-react-native-session-replay to 1.5.6 (both pinned). Bump app version to 2.6.2.
There was a problem hiding this comment.
Code Review
This pull request updates the PostHog SDK to version 4.44.1 and refactors its integration by passing a configured client instance to the PostHogProvider. Additionally, the application version is bumped to 2.6.2 across Android and iOS configurations, and dependency versions are pinned in package.json. Feedback suggests externalizing the hardcoded PostHog host URL to environment variables to facilitate configuration across different environments.
| export const posthog = new PostHog(POSTHOG_API_KEY, { | ||
| host: "https://eu.i.posthog.com", | ||
| persistence: "file" as const, | ||
| persistence: "file", | ||
| customStorage: AsyncStorage, | ||
| enableSessionReplay: true | ||
| } | ||
|
|
||
| export const posthog = new PostHog(POSTHOG_API_KEY, posthogOptions) | ||
| enableSessionReplay: true, | ||
| }) |
There was a problem hiding this comment.
For better configuration management and to avoid hardcoded URLs, consider moving the PostHog host URL to your environment variables file (.env), similar to how POSTHOG_API_KEY is handled. This makes it easier to switch between different PostHog environments (e.g., development, staging, production) or regions if needed in the future.
Here is a potential implementation:
- Add
POSTHOG_HOST=https://eu.i.posthog.comto your.envfile. - Update the import in this file to include
POSTHOG_HOST. - Update the
posthoginitialization to use the new environment variable:
export const posthog = new PostHog(POSTHOG_API_KEY, {
host: POSTHOG_HOST,
persistence: "file",
customStorage: AsyncStorage,
enableSessionReplay: true,
});usePostHog() was being called in RootNavigator itself, outside the PostHogProvider it renders. The hook returned no client, so the screen() calls in onReady/onStateChange were silent no-ops. Use the imported singleton directly.
|
/publish |
new PostHog(...)inservices/analyticsand another via<PostHogProvider apiKey=... options=...>inroot-navigator. They raced on the shared queue and opt-in/out state.<PostHogProvider client={posthog} ...>) so events, identify, screen tracking, and feature flags all share one instance.posthog-react-nativeto4.44.1andposthog-react-native-session-replayto1.5.6, both pinned (no caret).