Skip to content

Commit bf0ffb7

Browse files
committed
refactor(common): consolidate analytics (Commit 2.8)
Consolidates analytics utilities into a dedicated directory structure: - common/src/analytics/index.ts: Unified re-exports - common/src/analytics/core.ts: PostHog client factory, types - common/src/analytics/dispatcher.ts: Cross-platform event dispatcher with buffering - common/src/analytics/log-helpers.ts: Log data conversion, PII filtering - common/src/analytics/track-event.ts: Server-side tracking with lazy client init Key features: - PII filtering with allowlist/blocklist approach - MAX_BUFFER_SIZE=100 prevents unbounded memory growth - Lazy PostHog client initialization in production only - resetAnalyticsClient() helper exported for testing Comprehensive unit tests added: - track-event.test.ts: 16 tests for trackEvent, flushAnalytics, resetAnalyticsClient - Tests cover lazy init, error handling, env gating, edge cases (empty userId)
1 parent f3a68e3 commit bf0ffb7

File tree

12 files changed

+872
-12
lines changed

12 files changed

+872
-12
lines changed

cli/src/utils/__tests__/analytics-client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, test, expect, beforeEach, mock } from 'bun:test'
22

33
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
44

5-
import type { AnalyticsClientWithIdentify } from '@codebuff/common/analytics-core'
5+
import type { AnalyticsClientWithIdentify } from '@codebuff/common/analytics'
66

77
import {
88
initAnalytics,

cli/src/utils/analytics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
generateAnonymousId,
44
type AnalyticsClientWithIdentify,
55
type PostHogClientOptions,
6-
} from '@codebuff/common/analytics-core'
6+
} from '@codebuff/common/analytics'
77
import {
88
env as defaultEnv,
99
IS_PROD as defaultIsProd,
@@ -14,7 +14,7 @@ import type { AnalyticsEvent } from '@codebuff/common/constants/analytics-events
1414

1515

1616
// Re-export types from core for backwards compatibility
17-
export type { AnalyticsClientWithIdentify as AnalyticsClient } from '@codebuff/common/analytics-core'
17+
export type { AnalyticsClientWithIdentify as AnalyticsClient } from '@codebuff/common/analytics'
1818

1919
export enum AnalyticsErrorStage {
2020
Init = 'init',

cli/src/utils/logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { format as stringFormat } from 'util'
44
import { pino } from 'pino'
55

66
import { env, IS_DEV, IS_TEST, IS_CI } from '@codebuff/common/env'
7-
import { createAnalyticsDispatcher } from '@codebuff/common/util/analytics-dispatcher'
7+
import { createAnalyticsDispatcher } from '@codebuff/common/analytics'
88
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
9-
import { getAnalyticsEventId } from '@codebuff/common/util/analytics-log'
9+
import { getAnalyticsEventId } from '@codebuff/common/analytics'
1010

1111
import {
1212
flushAnalytics,

0 commit comments

Comments
 (0)