MCPcopy Index your code
hub / github.com/codeaashu/claude-code / logEventTo1PAsync

Function logEventTo1PAsync

src/services/analytics/firstPartyEventLogger.ts:156–207  ·  view source on GitHub ↗

* Log a 1st-party event for internal analytics (async version). * Events are batched and exported to /api/event_logging/batch * * This enriches the event with core metadata (model, session, env context, etc.) * at log time, similar to logEventToStatsig. * * @param eventName - Name of the event

(
  firstPartyEventLogger: Logger,
  eventName: string,
  metadata: Record<string, number | boolean | undefined> = {},
)

Source from the content-addressed store, hash-verified

154 * @param metadata - Additional metadata for the event (intentionally no strings, to avoid accidentally logging code/filepaths)
155 */
156async function logEventTo1PAsync(
157 firstPartyEventLogger: Logger,
158 eventName: string,
159 metadata: Record<string, number | boolean | undefined> = {},
160): Promise<void> {
161 try {
162 // Enrich with core metadata at log time (similar to Statsig pattern)
163 const coreMetadata = await getEventMetadata({
164 model: metadata.model,
165 betas: metadata.betas,
166 })
167
168 // Build attributes - OTel supports nested objects natively via AnyValueMap
169 // Cast through unknown since our nested objects are structurally compatible
170 // with AnyValue but TS doesn't recognize it due to missing index signatures
171 const attributes = {
172 event_name: eventName,
173 event_id: randomUUID(),
174 // Pass objects directly - no JSON serialization needed
175 core_metadata: coreMetadata,
176 user_metadata: getCoreUserData(true),
177 event_metadata: metadata,
178 } as unknown as AnyValueMap
179
180 // Add user_id if available
181 const userId = getOrCreateUserID()
182 if (userId) {
183 attributes.user_id = userId
184 }
185
186 // Debug logging when debug mode is enabled
187 if (process.env.USER_TYPE === 'ant') {
188 logForDebugging(
189 `[ANT-ONLY] 1P event: ${eventName} ${jsonStringify(metadata, null, 0)}`,
190 )
191 }
192
193 // Emit log record
194 firstPartyEventLogger.emit({
195 body: eventName,
196 attributes,
197 })
198 } catch (e) {
199 if (process.env.NODE_ENV === 'development') {
200 throw e
201 }
202 if (process.env.USER_TYPE === 'ant') {
203 logError(e as Error)
204 }
205 // swallow
206 }
207}
208
209/**
210 * Log a 1st-party event for internal analytics.

Callers 1

logEventTo1PFunction · 0.85

Calls 6

getEventMetadataFunction · 0.85
getOrCreateUserIDFunction · 0.85
logForDebuggingFunction · 0.85
jsonStringifyFunction · 0.85
emitMethod · 0.80
logErrorFunction · 0.50

Tested by

no test coverage detected