···11+import posthog from 'posthog-js';
22+33+/**
44+ * Check if analytics should be captured
55+ * Centralizes the logic for when to track events
66+ */
77+export function shouldCaptureAnalytics(): boolean {
88+ return (
99+ process.env.NEXT_PUBLIC_VERCEL_ENV === 'production' && posthog.__loaded
1010+ );
1111+}
···99import { usePathname } from 'next/navigation';
1010import posthog from 'posthog-js';
1111import { isInternalUser, isEarlyTester } from '@/lib/userLists';
1212+import { shouldCaptureAnalytics } from '@/features/analytics/utils';
12131314const ENABLE_AUTH_LOGGING = true;
1415···3738 }
38393940 // Reset PostHog user identity
4040- if (
4141- process.env.NEXT_PUBLIC_VERCEL_ENV === 'production' &&
4242- posthog.__loaded
4343- ) {
4141+ if (shouldCaptureAnalytics()) {
4442 posthog.reset();
4543 if (ENABLE_AUTH_LOGGING) {
4644 console.log('[useAuth] PostHog user identity reset');
···7270 useEffect(() => {
7371 const user = query.data;
74727575- // Only identify in production and when user is available
7676- if (
7777- process.env.NEXT_PUBLIC_VERCEL_ENV === 'production' &&
7878- user &&
7979- posthog.__loaded
8080- ) {
7373+ // Only identify when analytics is enabled and user is available
7474+ if (shouldCaptureAnalytics() && user) {
8175 posthog.identify(user.id, {
8276 name: user.name,
8377 handle: user.handle,
8478 is_internal: isInternalUser(user.handle),
8579 is_early_tester: isEarlyTester(user.handle),
8680 });
8787-8888- if (ENABLE_AUTH_LOGGING) {
8989- console.log('[useAuth] User identified in PostHog:', user.id);
9090- }
9181 }
9282 }, [query.data]);
9383