@@ -5,7 +5,6 @@ import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
55import type { AnalyticsClient } from '../analytics-core'
66
77import {
8- initAnalytics ,
98 trackEvent ,
109 flushAnalytics ,
1110 configureAnalytics ,
@@ -48,15 +47,12 @@ describe('server-side analytics', () => {
4847 resetServerAnalyticsState ( createTestDeps ( ) )
4948 } )
5049
51- describe ( 'initAnalytics' , ( ) => {
52- test ( 'should configure analytics from clientEnv' , ( ) => {
53- initAnalytics ( {
54- logger : mockLogger ,
55- clientEnv : {
56- NEXT_PUBLIC_CB_ENVIRONMENT : 'prod' ,
57- NEXT_PUBLIC_POSTHOG_API_KEY : 'test-key' ,
58- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
59- } ,
50+ describe ( 'configureAnalytics' , ( ) => {
51+ test ( 'should configure analytics with config object' , ( ) => {
52+ configureAnalytics ( {
53+ envName : 'prod' ,
54+ posthogApiKey : 'test-key' ,
55+ posthogHostUrl : 'https://posthog.test' ,
6056 } )
6157
6258 const config = getAnalyticsConfig ( )
@@ -67,13 +63,10 @@ describe('server-side analytics', () => {
6763 } )
6864
6965 test ( 'should not create client in non-prod environments' , ( ) => {
70- initAnalytics ( {
71- logger : mockLogger ,
72- clientEnv : {
73- NEXT_PUBLIC_CB_ENVIRONMENT : 'dev' ,
74- NEXT_PUBLIC_POSTHOG_API_KEY : 'test-key' ,
75- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
76- } ,
66+ configureAnalytics ( {
67+ envName : 'dev' ,
68+ posthogApiKey : 'test-key' ,
69+ posthogHostUrl : 'https://posthog.test' ,
7770 } )
7871
7972 // Track an event - should not call capture since not prod
@@ -87,13 +80,10 @@ describe('server-side analytics', () => {
8780 } )
8881
8982 test ( 'should create client in prod environment' , ( ) => {
90- initAnalytics ( {
91- logger : mockLogger ,
92- clientEnv : {
93- NEXT_PUBLIC_CB_ENVIRONMENT : 'prod' ,
94- NEXT_PUBLIC_POSTHOG_API_KEY : 'test-key' ,
95- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
96- } ,
83+ configureAnalytics ( {
84+ envName : 'prod' ,
85+ posthogApiKey : 'test-key' ,
86+ posthogHostUrl : 'https://posthog.test' ,
9787 } )
9888
9989 // Track an event - should call capture since prod
@@ -106,15 +96,8 @@ describe('server-side analytics', () => {
10696 expect ( captureMock ) . toHaveBeenCalledTimes ( 1 )
10797 } )
10898
109- test ( 'should set config to null if env vars are missing' , ( ) => {
110- initAnalytics ( {
111- logger : mockLogger ,
112- clientEnv : {
113- NEXT_PUBLIC_CB_ENVIRONMENT : 'prod' ,
114- NEXT_PUBLIC_POSTHOG_API_KEY : '' , // Missing
115- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
116- } ,
117- } )
99+ test ( 'should set config to null when passed null' , ( ) => {
100+ configureAnalytics ( null )
118101
119102 const config = getAnalyticsConfig ( )
120103 expect ( config ) . toBeNull ( )
@@ -123,13 +106,10 @@ describe('server-side analytics', () => {
123106
124107 describe ( 'trackEvent' , ( ) => {
125108 test ( 'should send events with correct parameters in prod' , ( ) => {
126- initAnalytics ( {
127- logger : mockLogger ,
128- clientEnv : {
129- NEXT_PUBLIC_CB_ENVIRONMENT : 'prod' ,
130- NEXT_PUBLIC_POSTHOG_API_KEY : 'test-key' ,
131- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
132- } ,
109+ configureAnalytics ( {
110+ envName : 'prod' ,
111+ posthogApiKey : 'test-key' ,
112+ posthogHostUrl : 'https://posthog.test' ,
133113 } )
134114
135115 trackEvent ( {
@@ -146,8 +126,8 @@ describe('server-side analytics', () => {
146126 } )
147127 } )
148128
149- test ( 'should warn when client is not initialized ' , ( ) => {
150- // Configure as prod but don't initialize properly
129+ test ( 'should lazily initialize client in prod when tracking events ' , ( ) => {
130+ // Configure as prod
151131 configureAnalytics ( {
152132 envName : 'prod' ,
153133 posthogApiKey : 'test-key' ,
@@ -168,18 +148,15 @@ describe('server-side analytics', () => {
168148 logger : mockLogger ,
169149 } )
170150
171- expect ( mockLogger . warn ) . toHaveBeenCalled ( )
172- expect ( captureMock ) . not . toHaveBeenCalled ( )
151+ // With lazy initialization, the client should be created and capture should be called
152+ expect ( captureMock ) . toHaveBeenCalled ( )
173153 } )
174154
175155 test ( 'should silently skip events in non-prod environments' , ( ) => {
176- initAnalytics ( {
177- logger : mockLogger ,
178- clientEnv : {
179- NEXT_PUBLIC_CB_ENVIRONMENT : 'test' ,
180- NEXT_PUBLIC_POSTHOG_API_KEY : 'test-key' ,
181- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
182- } ,
156+ configureAnalytics ( {
157+ envName : 'test' ,
158+ posthogApiKey : 'test-key' ,
159+ posthogHostUrl : 'https://posthog.test' ,
183160 } )
184161
185162 trackEvent ( {
@@ -193,13 +170,10 @@ describe('server-side analytics', () => {
193170 } )
194171
195172 test ( 'should handle capture errors gracefully' , ( ) => {
196- initAnalytics ( {
197- logger : mockLogger ,
198- clientEnv : {
199- NEXT_PUBLIC_CB_ENVIRONMENT : 'prod' ,
200- NEXT_PUBLIC_POSTHOG_API_KEY : 'test-key' ,
201- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
202- } ,
173+ configureAnalytics ( {
174+ envName : 'prod' ,
175+ posthogApiKey : 'test-key' ,
176+ posthogHostUrl : 'https://posthog.test' ,
203177 } )
204178
205179 // Make capture throw an error
@@ -221,14 +195,18 @@ describe('server-side analytics', () => {
221195 } )
222196
223197 describe ( 'flushAnalytics' , ( ) => {
224- test ( 'should call client flush in prod' , async ( ) => {
225- initAnalytics ( {
198+ test ( 'should call client flush in prod after tracking an event' , async ( ) => {
199+ configureAnalytics ( {
200+ envName : 'prod' ,
201+ posthogApiKey : 'test-key' ,
202+ posthogHostUrl : 'https://posthog.test' ,
203+ } )
204+
205+ // Track an event to initialize the client
206+ trackEvent ( {
207+ event : AnalyticsEvent . APP_LAUNCHED ,
208+ userId : 'user-123' ,
226209 logger : mockLogger ,
227- clientEnv : {
228- NEXT_PUBLIC_CB_ENVIRONMENT : 'prod' ,
229- NEXT_PUBLIC_POSTHOG_API_KEY : 'test-key' ,
230- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
231- } ,
232210 } )
233211
234212 await flushAnalytics ( mockLogger )
@@ -242,13 +220,17 @@ describe('server-side analytics', () => {
242220 } )
243221
244222 test ( 'should log errors but not throw on flush failure' , async ( ) => {
245- initAnalytics ( {
223+ configureAnalytics ( {
224+ envName : 'prod' ,
225+ posthogApiKey : 'test-key' ,
226+ posthogHostUrl : 'https://posthog.test' ,
227+ } )
228+
229+ // Track an event to initialize the client
230+ trackEvent ( {
231+ event : AnalyticsEvent . APP_LAUNCHED ,
232+ userId : 'user-123' ,
246233 logger : mockLogger ,
247- clientEnv : {
248- NEXT_PUBLIC_CB_ENVIRONMENT : 'prod' ,
249- NEXT_PUBLIC_POSTHOG_API_KEY : 'test-key' ,
250- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
251- } ,
252234 } )
253235
254236 flushMock . mockImplementation ( ( ) => {
@@ -260,7 +242,7 @@ describe('server-side analytics', () => {
260242 } )
261243 } )
262244
263- describe ( 'configureAnalytics' , ( ) => {
245+ describe ( 'configureAnalytics manual configuration ' , ( ) => {
264246 test ( 'should allow manual configuration' , ( ) => {
265247 configureAnalytics ( {
266248 envName : 'prod' ,
@@ -289,13 +271,10 @@ describe('server-side analytics', () => {
289271
290272 describe ( 'edge cases' , ( ) => {
291273 test ( 'should handle events with undefined properties' , ( ) => {
292- initAnalytics ( {
293- logger : mockLogger ,
294- clientEnv : {
295- NEXT_PUBLIC_CB_ENVIRONMENT : 'prod' ,
296- NEXT_PUBLIC_POSTHOG_API_KEY : 'test-key' ,
297- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
298- } ,
274+ configureAnalytics ( {
275+ envName : 'prod' ,
276+ posthogApiKey : 'test-key' ,
277+ posthogHostUrl : 'https://posthog.test' ,
299278 } )
300279
301280 trackEvent ( {
@@ -313,13 +292,10 @@ describe('server-side analytics', () => {
313292 } )
314293
315294 test ( 'should handle events with empty properties' , ( ) => {
316- initAnalytics ( {
317- logger : mockLogger ,
318- clientEnv : {
319- NEXT_PUBLIC_CB_ENVIRONMENT : 'prod' ,
320- NEXT_PUBLIC_POSTHOG_API_KEY : 'test-key' ,
321- NEXT_PUBLIC_POSTHOG_HOST_URL : 'https://posthog.test' ,
322- } ,
295+ configureAnalytics ( {
296+ envName : 'prod' ,
297+ posthogApiKey : 'test-key' ,
298+ posthogHostUrl : 'https://posthog.test' ,
323299 } )
324300
325301 trackEvent ( {
0 commit comments