( event: AnalyticsEvent, properties?: Record<string, any>, )
| 177 | } |
| 178 | |
| 179 | export function trackEvent( |
| 180 | event: AnalyticsEvent, |
| 181 | properties?: Record<string, any>, |
| 182 | ) { |
| 183 | const { isProd } = resolveDeps() |
| 184 | const distinctId = getDistinctId() |
| 185 | |
| 186 | if (!client) { |
| 187 | if (isProd) { |
| 188 | const error = new Error('Analytics client not initialized') |
| 189 | logAnalyticsError(error, { |
| 190 | stage: AnalyticsErrorStage.Track, |
| 191 | event, |
| 192 | properties, |
| 193 | }) |
| 194 | throw error |
| 195 | } |
| 196 | return |
| 197 | } |
| 198 | |
| 199 | if (!distinctId) { |
| 200 | // This shouldn't happen if initAnalytics was called, but handle gracefully |
| 201 | return |
| 202 | } |
| 203 | |
| 204 | if (!isProd) { |
| 205 | if (DEBUG_ANALYTICS) { |
| 206 | logAnalyticsDebug(`[analytics] ${event}`, { |
| 207 | event, |
| 208 | properties, |
| 209 | distinctId, |
| 210 | }) |
| 211 | } |
| 212 | return |
| 213 | } |
| 214 | |
| 215 | if (!shouldTrackAnalyticsEvent({ event, distinctId, properties })) { |
| 216 | return |
| 217 | } |
| 218 | |
| 219 | try { |
| 220 | client.capture({ |
| 221 | distinctId, |
| 222 | event, |
| 223 | properties, |
| 224 | }) |
| 225 | } catch (error) { |
| 226 | logAnalyticsError(error, { |
| 227 | stage: AnalyticsErrorStage.Track, |
| 228 | event, |
| 229 | properties, |
| 230 | }) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | export function identifyUser(userId: string, properties?: Record<string, any>) { |
| 235 | if (!client) { |
no test coverage detected