(events: TrackEventItem[])
| 21 | } |
| 22 | |
| 23 | export function sendAnalyticEvents(events: TrackEventItem[]) { |
| 24 | if (typeof window === "undefined") { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | if (!process.env.NEXT_PUBLIC_ANALYTIC_ENABLED) return; |
| 29 | |
| 30 | /** |
| 31 | * Some error send at very high rate, we need to throttle them |
| 32 | * to prevent the server from being overwhelmed. |
| 33 | */ |
| 34 | const finalEvents = events.filter((e) => { |
| 35 | if (!["unhandledrejection", "error"].includes(e.name)) return true; |
| 36 | return throttleEvent(e.name, 5, 5000); |
| 37 | }); |
| 38 | |
| 39 | if (finalEvents.length === 0) return; |
| 40 | |
| 41 | let deviceId = localStorage.getItem("od-id"); |
| 42 | if (!deviceId) { |
| 43 | deviceId = generateId(); |
| 44 | localStorage.setItem("od-id", deviceId); |
| 45 | } |
| 46 | |
| 47 | fetch("/api/events", { |
| 48 | method: "POST", |
| 49 | body: JSON.stringify({ |
| 50 | events: finalEvents, |
| 51 | }), |
| 52 | headers: { |
| 53 | "Content-Type": "application/json", |
| 54 | "x-od-id": deviceId, |
| 55 | }, |
| 56 | }) |
| 57 | .then() |
| 58 | .catch(); |
| 59 | } |
no test coverage detected