()
| 4 | let instance: PostHog | null = null; |
| 5 | |
| 6 | export default function PostHogClient(): Pick< |
| 7 | PostHog, |
| 8 | 'capture' | 'isFeatureEnabled' | 'getFeatureFlag' | 'debug' | 'alias' |
| 9 | > { |
| 10 | if (instance) return instance; |
| 11 | |
| 12 | const key = getEnvVariable('NEXT_PUBLIC_POSTHOG_KEY') ?? ''; |
| 13 | const isProduction = process.env.NODE_ENV === 'production'; |
| 14 | if (!isProduction) { |
| 15 | return { |
| 16 | capture: () => {}, |
| 17 | isFeatureEnabled: async () => false, |
| 18 | getFeatureFlag: async () => undefined, |
| 19 | debug: () => {}, |
| 20 | alias: () => {}, |
| 21 | }; |
| 22 | } |
| 23 | // Single shared PostHog client for the process. |
| 24 | // Disabled outside production to avoid sending real events during tests/dev. |
| 25 | instance = new PostHog(isProduction ? key : key || 'disabled', { |
| 26 | host: 'https://us.i.posthog.com', |
| 27 | flushAt: 1, |
| 28 | flushInterval: 0, |
| 29 | disabled: !isProduction, |
| 30 | }); |
| 31 | |
| 32 | // if (!isProduction) { |
| 33 | // // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 34 | // (instance as any).capture = function (...args: any[]) { |
| 35 | // console.log('POSTHOG CAPTURE', ...args); |
| 36 | // }; |
| 37 | // } |
| 38 | |
| 39 | return instance; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Flushes buffered events. A no-op outside production, where no shared |
no test coverage detected