@@ -3,23 +3,6 @@ import { PostHog } from 'posthog-node'
33
44import type { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
55
6- export enum AnalyticsErrorStage {
7- Init = 'init' ,
8- Track = 'track' ,
9- Identify = 'identify' ,
10- Flush = 'flush' ,
11- CaptureException = 'captureException' ,
12- }
13-
14- type AnalyticsErrorContext = {
15- stage : AnalyticsErrorStage
16- } & Record < string , unknown >
17-
18- type AnalyticsErrorLogger = (
19- error : unknown ,
20- context : AnalyticsErrorContext ,
21- ) => void
22-
236// Prints the events to console
247// It's very noisy, so recommended you set this to true
258// only when you're actively adding new analytics
@@ -30,41 +13,30 @@ let currentUserId: string | undefined
3013let client : PostHog | undefined
3114
3215export let identified : boolean = false
33- let analyticsErrorLogger : AnalyticsErrorLogger | undefined
3416
35- export function setAnalyticsErrorLogger ( loggerFn : AnalyticsErrorLogger ) {
36- analyticsErrorLogger = loggerFn
17+ function isProdEnv ( ) : boolean {
18+ return env . NEXT_PUBLIC_CB_ENVIRONMENT === 'prod'
3719}
3820
39- function logAnalyticsError ( error : unknown , context : AnalyticsErrorContext ) {
40- try {
41- analyticsErrorLogger ?.( error , context )
42- } catch {
43- // Never throw from error reporting
44- }
21+ function analyticsConfigured ( ) : boolean {
22+ return Boolean ( env . NEXT_PUBLIC_POSTHOG_API_KEY && env . NEXT_PUBLIC_POSTHOG_HOST_URL )
4523}
4624
4725export function initAnalytics ( ) {
48- if ( ! env . NEXT_PUBLIC_POSTHOG_API_KEY || ! env . NEXT_PUBLIC_POSTHOG_HOST_URL ) {
49- const error = new Error (
26+ if ( ! analyticsConfigured ( ) ) {
27+ // In non-prod environments we skip analytics entirely when keys are missing
28+ if ( ! isProdEnv ( ) ) {
29+ return
30+ }
31+ throw new Error (
5032 'NEXT_PUBLIC_POSTHOG_API_KEY or NEXT_PUBLIC_POSTHOG_HOST_URL is not set' ,
5133 )
52- logAnalyticsError ( error , {
53- stage : AnalyticsErrorStage . Init ,
54- missingEnv : true ,
55- } )
56- throw error
5734 }
5835
59- try {
60- client = new PostHog ( env . NEXT_PUBLIC_POSTHOG_API_KEY , {
61- host : env . NEXT_PUBLIC_POSTHOG_HOST_URL ,
62- enableExceptionAutocapture : env . NEXT_PUBLIC_CB_ENVIRONMENT === 'prod' ,
63- } )
64- } catch ( error ) {
65- logAnalyticsError ( error , { stage : AnalyticsErrorStage . Init } )
66- throw error
67- }
36+ client = new PostHog ( env . NEXT_PUBLIC_POSTHOG_API_KEY , {
37+ host : env . NEXT_PUBLIC_POSTHOG_HOST_URL ,
38+ enableExceptionAutocapture : env . NEXT_PUBLIC_CB_ENVIRONMENT === 'prod' ,
39+ } )
6840}
6941
7042export async function flushAnalytics ( ) {
@@ -76,7 +48,6 @@ export async function flushAnalytics() {
7648 } catch ( error ) {
7749 // Silently handle PostHog network errors - don't log to console or logger
7850 // This prevents PostHog errors from cluttering the user's console
79- logAnalyticsError ( error , { stage : AnalyticsErrorStage . Flush } )
8051 }
8152}
8253
@@ -89,14 +60,8 @@ export function trackEvent(
8960 return
9061 }
9162 if ( ! client ) {
92- if ( env . NEXT_PUBLIC_CB_ENVIRONMENT === 'prod' ) {
93- const error = new Error ( 'Analytics client not initialized' )
94- logAnalyticsError ( error , {
95- stage : AnalyticsErrorStage . Track ,
96- event,
97- properties,
98- } )
99- throw error
63+ if ( isProdEnv ( ) ) {
64+ throw new Error ( 'Analytics client not initialized' )
10065 }
10166 return
10267 }
@@ -111,35 +76,25 @@ export function trackEvent(
11176 return
11277 }
11378
114- try {
115- client . capture ( {
116- distinctId,
117- event,
118- properties,
119- } )
120- } catch ( error ) {
121- logAnalyticsError ( error , {
122- stage : AnalyticsErrorStage . Track ,
123- event,
124- properties,
125- } )
126- }
79+ client . capture ( {
80+ distinctId,
81+ event,
82+ properties,
83+ } )
12784}
12885
12986export function identifyUser ( userId : string , properties ?: Record < string , any > ) {
13087 // Store the user ID for future events
13188 currentUserId = userId
13289
13390 if ( ! client ) {
134- const error = new Error ( 'Analytics client not initialized' )
135- logAnalyticsError ( error , {
136- stage : AnalyticsErrorStage . Identify ,
137- properties,
138- } )
139- throw error
91+ if ( isProdEnv ( ) ) {
92+ throw new Error ( 'Analytics client not initialized' )
93+ }
94+ return
14095 }
14196
142- if ( env . NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod' ) {
97+ if ( ! isProdEnv ( ) ) {
14398 if ( DEBUG_DEV_EVENTS ) {
14499 console . log ( 'Identify event sent' , {
145100 userId,
@@ -149,17 +104,10 @@ export function identifyUser(userId: string, properties?: Record<string, any>) {
149104 return
150105 }
151106
152- try {
153- client . identify ( {
154- distinctId : userId ,
155- properties,
156- } )
157- } catch ( error ) {
158- logAnalyticsError ( error , {
159- stage : AnalyticsErrorStage . Identify ,
160- properties,
161- } )
162- }
107+ client . identify ( {
108+ distinctId : userId ,
109+ properties,
110+ } )
163111}
164112
165113export function logError (
@@ -180,9 +128,5 @@ export function logError(
180128 } catch ( postHogError ) {
181129 // Silently handle PostHog errors - don't log them to console
182130 // This prevents PostHog connection issues from cluttering the user's console
183- logAnalyticsError ( postHogError , {
184- stage : AnalyticsErrorStage . CaptureException ,
185- properties,
186- } )
187131 }
188132}
0 commit comments