(userId: string, properties?: Record<string, any>)
| 232 | } |
| 233 | |
| 234 | export function identifyUser(userId: string, properties?: Record<string, any>) { |
| 235 | if (!client) { |
| 236 | const error = new Error('Analytics client not initialized') |
| 237 | logAnalyticsError(error, { |
| 238 | stage: AnalyticsErrorStage.Identify, |
| 239 | properties, |
| 240 | }) |
| 241 | throw error |
| 242 | } |
| 243 | |
| 244 | const { isProd } = resolveDeps() |
| 245 | const previousAnonymousId = anonymousId |
| 246 | |
| 247 | // Store the real user ID for future events |
| 248 | currentUserId = userId |
| 249 | identified = true |
| 250 | |
| 251 | if (!isProd) { |
| 252 | if (DEBUG_ANALYTICS) { |
| 253 | logAnalyticsDebug('[analytics] user identified', { |
| 254 | userId, |
| 255 | previousAnonymousId, |
| 256 | properties, |
| 257 | }) |
| 258 | } |
| 259 | return |
| 260 | } |
| 261 | |
| 262 | try { |
| 263 | // If we had an anonymous ID, alias it FIRST to the real user ID |
| 264 | // This must be called BEFORE identify to properly merge the event histories |
| 265 | // See: https://posthog.com/docs/libraries/node |
| 266 | if (previousAnonymousId) { |
| 267 | client.alias({ |
| 268 | distinctId: userId, |
| 269 | alias: previousAnonymousId, |
| 270 | }) |
| 271 | } |
| 272 | |
| 273 | // Then identify the user with their properties |
| 274 | client.identify({ |
| 275 | distinctId: userId, |
| 276 | properties, |
| 277 | }) |
| 278 | } catch (error) { |
| 279 | logAnalyticsError(error, { |
| 280 | stage: AnalyticsErrorStage.Identify, |
| 281 | properties, |
| 282 | }) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | export function logError( |
| 287 | error: any, |
no test coverage detected