|
9 | 9 |
|
10 | 10 | import type { AnalyticsEvent } from './constants/analytics-events' |
11 | 11 | import type { Logger } from '@codebuff/common/types/contracts/logger' |
| 12 | +import { env } from '@codebuff/common/env' |
12 | 13 |
|
13 | 14 | // Re-export types from core for backwards compatibility |
14 | 15 | export type { AnalyticsClient, AnalyticsConfig } from './analytics-core' |
@@ -47,45 +48,6 @@ export const configureAnalytics = (config: AnalyticsConfig | null) => { |
47 | 48 | client = undefined |
48 | 49 | } |
49 | 50 |
|
50 | | -export function initAnalytics({ |
51 | | - logger, |
52 | | - clientEnv, |
53 | | -}: { |
54 | | - logger: Logger |
55 | | - clientEnv?: Parameters<typeof getConfigFromEnv>[0] |
56 | | -}) { |
57 | | - if (clientEnv) { |
58 | | - configureAnalytics(getConfigFromEnv(clientEnv)) |
59 | | - } |
60 | | - |
61 | | - logger.info( |
62 | | - { |
63 | | - analyticsConfig: { |
64 | | - envName: analyticsConfig?.envName, |
65 | | - posthogApiKey: !!analyticsConfig?.posthogApiKey, |
66 | | - posthogHostUrl: analyticsConfig?.posthogHostUrl, |
67 | | - }, |
68 | | - }, |
69 | | - '🔵 [analytics] initAnalytics() called', |
70 | | - ) |
71 | | - |
72 | | - if (!isProdEnv(analyticsConfig?.envName)) { |
73 | | - return |
74 | | - } |
75 | | - |
76 | | - const createClient = getCreateClient() |
77 | | - |
78 | | - try { |
79 | | - client = createClient(analyticsConfig!.posthogApiKey, { |
80 | | - host: analyticsConfig!.posthogHostUrl, |
81 | | - flushAt: 1, |
82 | | - flushInterval: 0, |
83 | | - }) |
84 | | - } catch (error) { |
85 | | - logger.warn({ error }, 'Failed to initialize analytics client') |
86 | | - } |
87 | | -} |
88 | | - |
89 | 51 | export async function flushAnalytics(logger?: Logger) { |
90 | 52 | if (!client) { |
91 | 53 | return |
@@ -116,13 +78,23 @@ export function trackEvent({ |
116 | 78 | } |
117 | 79 |
|
118 | 80 | if (!client) { |
119 | | - // Don't attempt to re-initialize here - initAnalytics requires clientEnv |
120 | | - // which we don't have in this context. Just warn and skip. |
121 | | - logger.warn( |
122 | | - { event, userId }, |
123 | | - 'Analytics client not initialized, skipping event tracking', |
| 81 | + configureAnalytics(getConfigFromEnv(env)) |
| 82 | + const createClient = getCreateClient() |
| 83 | + |
| 84 | + try { |
| 85 | + client = createClient(analyticsConfig!.posthogApiKey, { |
| 86 | + host: analyticsConfig!.posthogHostUrl, |
| 87 | + flushAt: 1, |
| 88 | + flushInterval: 0, |
| 89 | + }) |
| 90 | + } catch (error) { |
| 91 | + logger.warn({ error }, 'Failed to initialize analytics client') |
| 92 | + return |
| 93 | + } |
| 94 | + logger.info( |
| 95 | + { envName: analyticsConfig?.envName }, |
| 96 | + 'Analytics client initialized', |
124 | 97 | ) |
125 | | - return |
126 | 98 | } |
127 | 99 |
|
128 | 100 | try { |
|
0 commit comments