()
| 63 | } |
| 64 | |
| 65 | async function flushEvents(): Promise<void> { |
| 66 | if (queue.length === 0) return; |
| 67 | |
| 68 | const batch = queue.map((e) => ({ |
| 69 | event: e.event, |
| 70 | properties: { ...e.properties, $ip: null }, |
| 71 | distinct_id: getDistinctId(), |
| 72 | timestamp: e.timestamp, |
| 73 | })); |
| 74 | queue = []; |
| 75 | |
| 76 | const controller = new AbortController(); |
| 77 | const timeout = setTimeout(() => controller.abort(), FLUSH_TIMEOUT_MS); |
| 78 | |
| 79 | try { |
| 80 | await fetch(`${POSTHOG_HOST}/batch/`, { |
| 81 | method: "POST", |
| 82 | headers: { "Content-Type": "application/json" }, |
| 83 | body: JSON.stringify({ api_key: POSTHOG_API_KEY, batch }), |
| 84 | signal: controller.signal, |
| 85 | }); |
| 86 | } catch { |
| 87 | // Telemetry must never break the studio |
| 88 | } finally { |
| 89 | clearTimeout(timeout); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (typeof window !== "undefined") { |
| 94 | window.addEventListener("visibilitychange", () => { |
nothing calls this directly
no test coverage detected