Skip to content

Commit 2a263bd

Browse files
committed
refactor(analytics): inject client env config
Avoid importing client env at module import time; callers inject the needed NEXT_PUBLIC_* config.
1 parent edd498a commit 2a263bd

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

common/src/analytics.ts

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,66 @@
11
import { PostHog } from 'posthog-node'
22

3-
import type { ClientEnv } from '@codebuff/common/types/contracts/env'
43
import type { AnalyticsEvent } from './constants/analytics-events'
54
import type { Logger } from '@codebuff/common/types/contracts/logger'
5+
import type { ClientEnv } from './env-schema'
66

77
let client: PostHog | undefined
88

9-
// Lazy load env to avoid validation at import time in test environments
10-
let _cachedEnv: ClientEnv | undefined
11-
let _cachedIsProd: boolean | undefined
9+
type EnvName = 'dev' | 'test' | 'prod'
1210

13-
const getEnv = (): ClientEnv => {
14-
if (_cachedEnv === undefined) {
15-
_cachedEnv = require('@codebuff/common/env').env as ClientEnv
16-
}
17-
return _cachedEnv
11+
type AnalyticsConfig = {
12+
envName: EnvName
13+
posthogApiKey: string
14+
posthogHostUrl: string
1815
}
1916

20-
const getIsProd = (): boolean => {
21-
if (_cachedIsProd === undefined) {
22-
_cachedIsProd = require('@codebuff/common/env').IS_PROD as boolean
23-
}
24-
return _cachedIsProd
17+
let analyticsConfig: AnalyticsConfig | null = null
18+
19+
export const configureAnalytics = (config: AnalyticsConfig | null) => {
20+
analyticsConfig = config
21+
client = undefined
22+
}
23+
24+
const getConfigFromClientEnv = (
25+
clientEnv: Pick<
26+
ClientEnv,
27+
| 'NEXT_PUBLIC_CB_ENVIRONMENT'
28+
| 'NEXT_PUBLIC_POSTHOG_API_KEY'
29+
| 'NEXT_PUBLIC_POSTHOG_HOST_URL'
30+
>,
31+
): AnalyticsConfig | null => {
32+
const envName = clientEnv.NEXT_PUBLIC_CB_ENVIRONMENT
33+
const posthogApiKey = clientEnv.NEXT_PUBLIC_POSTHOG_API_KEY
34+
const posthogHostUrl = clientEnv.NEXT_PUBLIC_POSTHOG_HOST_URL
35+
36+
if (!envName) return null
37+
if (!posthogApiKey || !posthogHostUrl) return null
38+
39+
return { envName, posthogApiKey, posthogHostUrl }
2540
}
2641

27-
export function initAnalytics({ logger }: { logger: Logger }) {
28-
const env = getEnv()
29-
if (!env.NEXT_PUBLIC_POSTHOG_API_KEY || !env.NEXT_PUBLIC_POSTHOG_HOST_URL) {
30-
logger.warn(
31-
'Analytics environment variables not set - analytics will be disabled',
32-
)
42+
export function initAnalytics({
43+
logger,
44+
clientEnv,
45+
}: {
46+
logger: Logger
47+
clientEnv?: Parameters<typeof getConfigFromClientEnv>[0]
48+
}) {
49+
if (clientEnv) {
50+
configureAnalytics(getConfigFromClientEnv(clientEnv))
51+
}
52+
53+
if (analyticsConfig?.envName !== 'prod') {
54+
return
55+
}
56+
57+
if (!analyticsConfig) {
3358
return
3459
}
3560

3661
try {
37-
client = new PostHog(env.NEXT_PUBLIC_POSTHOG_API_KEY, {
38-
host: env.NEXT_PUBLIC_POSTHOG_HOST_URL,
62+
client = new PostHog(analyticsConfig.posthogApiKey, {
63+
host: analyticsConfig.posthogHostUrl,
3964
flushAt: 1,
4065
flushInterval: 0,
4166
})
@@ -50,7 +75,7 @@ export async function flushAnalytics() {
5075
}
5176
try {
5277
await client.flush()
53-
} catch (error) {}
78+
} catch {}
5479
}
5580

5681
export function trackEvent({
@@ -64,7 +89,7 @@ export function trackEvent({
6489
properties?: Record<string, any>
6590
logger: Logger
6691
}) {
67-
if (!getIsProd()) {
92+
if (analyticsConfig?.envName !== 'prod') {
6893
// Note (James): This log was too noisy. Reenable it as you need to test something.
6994
// logger.info({ payload: { event, properties } }, event)
7095
return

web/src/lib/server-init.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { initAnalytics } from '@codebuff/common/analytics'
2+
import { env } from '@codebuff/common/env'
23
// Errors if this file is included in client bundles
34
import 'server-only'
45

@@ -14,6 +15,7 @@ export function initializeServer({ logger }: { logger: Logger }) {
1415
try {
1516
initAnalytics({
1617
logger,
18+
clientEnv: env,
1719
})
1820
// Initialize other services as needed
1921
initialized = true

0 commit comments

Comments
 (0)