| 9 | let client: AnalyticsClient | undefined |
| 10 | |
| 11 | export async function flushAnalytics(logger?: Logger) { |
| 12 | if (!client) { |
| 13 | return |
| 14 | } |
| 15 | try { |
| 16 | await client.flush() |
| 17 | } catch (error) { |
| 18 | // Log the error but don't throw - flushing is best-effort |
| 19 | logger?.warn({ error }, 'Failed to flush analytics') |
| 20 | |
| 21 | // Track the flush failure event (will be queued for next successful flush) |
| 22 | try { |
| 23 | client.capture({ |
| 24 | distinctId: 'system', |
| 25 | event: AnalyticsEvent.FLUSH_FAILED, |
| 26 | properties: { |
| 27 | error: error instanceof Error ? error.message : String(error), |
| 28 | }, |
| 29 | }) |
| 30 | } catch { |
| 31 | // Silently ignore if we can't even track the failure |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | export function withDefaultProperties( |
| 37 | trackEventFn: TrackEventFn, |