* Drain the in-memory queue into a PostHog `/batch/` payload string. * Returns null when there's nothing to send. Resets the queue as a side effect * so callers can fire-and-forget the resulting payload. * * $ip:null tells PostHog not to record the request IP for any of these events. * Server-s
()
| 106 | * Server-side "Discard client IP data" is also enabled in project settings. |
| 107 | */ |
| 108 | function drainQueueToPayload(): string | null { |
| 109 | if (eventQueue.length === 0) return null; |
| 110 | const config = readConfig(); |
| 111 | const batch = eventQueue.map((e) => ({ |
| 112 | event: e.event, |
| 113 | properties: { ...e.properties, $ip: null }, |
| 114 | distinct_id: e.distinctId ?? config.anonymousId, |
| 115 | timestamp: e.timestamp, |
| 116 | })); |
| 117 | eventQueue = []; |
| 118 | return JSON.stringify({ api_key: POSTHOG_API_KEY, batch }); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Flush all queued events to PostHog via async HTTP POST. |
no test coverage detected