From e4b98c084db2bf6c258eb646dc33a144e5b636df Mon Sep 17 00:00:00 2001 From: Fortune Oluwasemilore Alebiosu Date: Tue, 6 Jan 2026 08:11:00 -0500 Subject: [PATCH] fixed posthog web error --- frontend/constants/posthog.ts | 42 ++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/frontend/constants/posthog.ts b/frontend/constants/posthog.ts index 8d830a7..59bb47d 100644 --- a/frontend/constants/posthog.ts +++ b/frontend/constants/posthog.ts @@ -1,26 +1,38 @@ import { PostHog } from 'posthog-react-native'; +import { Platform } from 'react-native'; const apiKey = process.env.EXPO_PUBLIC_POSTHOG_API_KEY; const host = process.env.EXPO_PUBLIC_POSTHOG_HOST; -if (!apiKey || !host) { +if (!apiKey || !host) { if (__DEV__) { console.warn('PostHog environment variables not configured. Analytics will be disabled.'); } } -export const posthog = apiKey && host +// No-op mock for PostHog +const noOpPostHog = { + capture: (_event: string, _properties?: object) => Promise.resolve(), + identify: (_distinctId: string, _properties?: object) => Promise.resolve(), + reset: () => Promise.resolve(), + screen: (_screenName: string, _properties?: object) => Promise.resolve(), + group: (_groupType: string, _groupKey: string, _properties?: object) => Promise.resolve(), + alias: (_alias: string) => Promise.resolve(), + reloadFeatureFlags: () => {}, + isFeatureEnabled: () => false, + getFeatureFlag: () => null, + getFeatureFlagPayload: () => null, +} as unknown as PostHog; + +// Only initialize PostHog if: +// 1. API key and host are configured +// 2. Not on web platform (posthog-react-native doesn't work on web/SSR) +// Skip PostHog on web entirely to avoid SSR errors during development +const shouldInitializePostHog = + apiKey && + host && + Platform.OS !== 'web'; + +export const posthog = shouldInitializePostHog ? new PostHog(apiKey, { host }) - : { - // Provide a no-op mock for when PostHog is not configured - capture: (_event: string, _properties?: object) => Promise.resolve(), - identify: (_distinctId: string, _properties?: object) => Promise.resolve(), - reset: () => Promise.resolve(), - screen: (_screenName: string, _properties?: object) => Promise.resolve(), - group: (_groupType: string, _groupKey: string, _properties?: object) => Promise.resolve(), - alias: (_alias: string) => Promise.resolve(), - reloadFeatureFlags: () => {}, - isFeatureEnabled: () => false, - getFeatureFlag: () => null, - getFeatureFlagPayload: () => null, - } as unknown as PostHog; \ No newline at end of file + : noOpPostHog; \ No newline at end of file