|
| 1 | +import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events' |
| 2 | + |
| 3 | +export type AnalyticsLogData = { |
| 4 | + eventId?: unknown |
| 5 | + userId?: unknown |
| 6 | + user_id?: unknown |
| 7 | + user?: { id?: unknown } |
| 8 | + [key: string]: unknown |
| 9 | +} |
| 10 | + |
| 11 | +export type TrackableAnalyticsPayload = { |
| 12 | + event: AnalyticsEvent |
| 13 | + userId: string |
| 14 | + properties: Record<string, unknown> |
| 15 | +} |
| 16 | + |
| 17 | +const analyticsEvents = new Set<AnalyticsEvent>(Object.values(AnalyticsEvent)) |
| 18 | + |
| 19 | +const toStringOrNull = (value: unknown): string | null => |
| 20 | + typeof value === 'string' ? value : null |
| 21 | + |
| 22 | +const getUserId = ( |
| 23 | + record: AnalyticsLogData, |
| 24 | + fallbackUserId?: string, |
| 25 | +): string | null => |
| 26 | + toStringOrNull(record.userId) ?? |
| 27 | + toStringOrNull(record.user_id) ?? |
| 28 | + toStringOrNull(record.user?.id) ?? |
| 29 | + toStringOrNull(fallbackUserId) |
| 30 | + |
| 31 | +export function getAnalyticsEventId(data: unknown): AnalyticsEvent | null { |
| 32 | + if (!data || typeof data !== 'object') { |
| 33 | + return null |
| 34 | + } |
| 35 | + const eventId = (data as AnalyticsLogData).eventId |
| 36 | + return analyticsEvents.has(eventId as AnalyticsEvent) |
| 37 | + ? (eventId as AnalyticsEvent) |
| 38 | + : null |
| 39 | +} |
| 40 | + |
| 41 | +// Allowlist of properties safe to send to analytics. |
| 42 | +// Be conservative - only include properties that are clearly non-PII. |
| 43 | +const SAFE_ANALYTICS_PROPERTIES = new Set([ |
| 44 | + // Event metadata |
| 45 | + 'eventId', |
| 46 | + 'level', |
| 47 | + 'msg', |
| 48 | + // Timing/metrics |
| 49 | + 'duration', |
| 50 | + 'durationMs', |
| 51 | + 'latency', |
| 52 | + 'latencyMs', |
| 53 | + 'timestamp', |
| 54 | + // Counts/sizes |
| 55 | + 'count', |
| 56 | + 'size', |
| 57 | + 'length', |
| 58 | + 'total', |
| 59 | + // Status/type identifiers |
| 60 | + 'status', |
| 61 | + 'type', |
| 62 | + 'action', |
| 63 | + 'source', |
| 64 | + 'target', |
| 65 | + 'category', |
| 66 | + // Agent/model info |
| 67 | + 'agentId', |
| 68 | + 'agentType', |
| 69 | + 'modelId', |
| 70 | + 'modelName', |
| 71 | + // Feature flags/versions |
| 72 | + 'version', |
| 73 | + 'feature', |
| 74 | + 'variant', |
| 75 | + // Error info (without stack traces or sensitive details) |
| 76 | + 'errorCode', |
| 77 | + 'errorType', |
| 78 | + // Boolean flags |
| 79 | + 'success', |
| 80 | + 'enabled', |
| 81 | + 'cached', |
| 82 | + // Run/step identifiers |
| 83 | + 'runId', |
| 84 | + 'stepNumber', |
| 85 | + 'stepId', |
| 86 | +]) |
| 87 | + |
| 88 | +// Properties that should never be sent to analytics (PII/sensitive) |
| 89 | +const BLOCKED_ANALYTICS_PROPERTIES = new Set([ |
| 90 | + 'userId', |
| 91 | + 'user_id', |
| 92 | + 'user', |
| 93 | + 'email', |
| 94 | + 'name', |
| 95 | + 'password', |
| 96 | + 'token', |
| 97 | + 'apiKey', |
| 98 | + 'secret', |
| 99 | + 'authorization', |
| 100 | + 'cookie', |
| 101 | + 'session', |
| 102 | + 'ip', |
| 103 | + 'ipAddress', |
| 104 | + 'fingerprint', |
| 105 | + 'deviceId', |
| 106 | +]) |
| 107 | + |
| 108 | +function extractSafeProperties( |
| 109 | + record: AnalyticsLogData, |
| 110 | +): Record<string, unknown> { |
| 111 | + const safeProps: Record<string, unknown> = {} |
| 112 | + |
| 113 | + for (const [key, value] of Object.entries(record)) { |
| 114 | + // Skip blocked properties |
| 115 | + if (BLOCKED_ANALYTICS_PROPERTIES.has(key)) continue |
| 116 | + // Skip complex objects that might contain PII |
| 117 | + if (value !== null && typeof value === 'object') continue |
| 118 | + // Only include properties in the allowlist |
| 119 | + if (SAFE_ANALYTICS_PROPERTIES.has(key)) { |
| 120 | + safeProps[key] = value |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return safeProps |
| 125 | +} |
| 126 | + |
| 127 | +export function toTrackableAnalyticsPayload({ |
| 128 | + data, |
| 129 | + level, |
| 130 | + msg, |
| 131 | + fallbackUserId, |
| 132 | +}: { |
| 133 | + data: unknown |
| 134 | + level: string |
| 135 | + msg: string |
| 136 | + fallbackUserId?: string |
| 137 | +}): TrackableAnalyticsPayload | null { |
| 138 | + if (!data || typeof data !== 'object') { |
| 139 | + return null |
| 140 | + } |
| 141 | + |
| 142 | + const record = data as AnalyticsLogData |
| 143 | + const eventId = getAnalyticsEventId(record) |
| 144 | + if (!eventId) { |
| 145 | + return null |
| 146 | + } |
| 147 | + |
| 148 | + const userId = getUserId(record, fallbackUserId) |
| 149 | + |
| 150 | + if (!userId) { |
| 151 | + return null |
| 152 | + } |
| 153 | + |
| 154 | + return { |
| 155 | + event: eventId, |
| 156 | + userId, |
| 157 | + properties: { |
| 158 | + ...extractSafeProperties(record), |
| 159 | + level, |
| 160 | + msg, |
| 161 | + }, |
| 162 | + } |
| 163 | +} |
0 commit comments