diff --git a/app/dashboard/app/providers/posthog-provider.tsx b/app/dashboard/app/providers/posthog-provider.tsx index cff2eec39..563423014 100644 --- a/app/dashboard/app/providers/posthog-provider.tsx +++ b/app/dashboard/app/providers/posthog-provider.tsx @@ -12,8 +12,8 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) { useEffect(() => { const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY; - // Only initialize PostHog if we have a valid key - if (posthogKey) { + // Only initialize PostHog if we have a valid key and we're not in development + if (posthogKey && posthogKey.trim() !== '' && process.env.NODE_ENV !== 'development') { posthog.init(posthogKey, { api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com', person_profiles: 'identified_only', // or 'always' to create profiles for anonymous users as well @@ -22,6 +22,11 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) { } }, []); + // If no PostHog key, return children without PostHog provider + if (!process.env.NEXT_PUBLIC_POSTHOG_KEY || process.env.NEXT_PUBLIC_POSTHOG_KEY.trim() === '') { + return <>{children}; + } + return ( diff --git a/app/dashboard/components/posthog-user-identifier.tsx b/app/dashboard/components/posthog-user-identifier.tsx index 1f52fe21b..95ec52517 100644 --- a/app/dashboard/components/posthog-user-identifier.tsx +++ b/app/dashboard/components/posthog-user-identifier.tsx @@ -9,13 +9,16 @@ export function PostHogUserIdentifier() { const posthog = usePostHog(); useEffect(() => { - if (posthog && user?.id) { - posthog.identify(user.id, { - email: user.email || undefined, - name: user.full_name || undefined, - }); - } else if (posthog && !user) { - posthog.reset(); + // Only proceed if PostHog is available and initialized + if (posthog && posthog.__loaded) { + if (user?.id) { + posthog.identify(user.id, { + email: user.email || undefined, + name: user.full_name || undefined, + }); + } else if (!user) { + posthog.reset(); + } } }, [posthog, user]);