Skip to content

Commit 660ed2e

Browse files
committed
Lazy-init analytics
1 parent 868a87c commit 660ed2e

File tree

2 files changed

+17
-76
lines changed

2 files changed

+17
-76
lines changed

common/src/analytics.ts

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
import type { AnalyticsEvent } from './constants/analytics-events'
1111
import type { Logger } from '@codebuff/common/types/contracts/logger'
12+
import { env } from '@codebuff/common/env'
1213

1314
// Re-export types from core for backwards compatibility
1415
export type { AnalyticsClient, AnalyticsConfig } from './analytics-core'
@@ -47,45 +48,6 @@ export const configureAnalytics = (config: AnalyticsConfig | null) => {
4748
client = undefined
4849
}
4950

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-
8951
export async function flushAnalytics(logger?: Logger) {
9052
if (!client) {
9153
return
@@ -116,13 +78,23 @@ export function trackEvent({
11678
}
11779

11880
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',
12497
)
125-
return
12698
}
12799

128100
try {

web/src/instrumentation.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)