Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/dashboard/app/providers/posthog-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
<PHProvider client={posthog}>
<SuspendedPostHogPageView />
Expand Down
17 changes: 10 additions & 7 deletions app/dashboard/components/posthog-user-identifier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Comment on lines 11 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Avoid gating user identification on posthog.__loaded

The effect now skips identify until posthog.__loaded is true, but the dependency array only tracks the PostHog object reference and the user. When the user data resolves before the PostHog script finishes loading, the effect runs with __loaded === false and exits, then never re‑runs after initialization because neither dependency changes. The user is therefore never identified and all subsequent events remain anonymous. Consider removing the __loaded guard or triggering the effect again when the initialization flag flips.

Useful? React with 👍 / 👎.

email: user.email || undefined,
name: user.full_name || undefined,
});
} else if (!user) {
posthog.reset();
}
}
}, [posthog, user]);

Expand Down