| 169 | } |
| 170 | |
| 171 | async function forwardToPostHog(env: Env, batch: PostHogEvent[]): Promise<void> { |
| 172 | try { |
| 173 | const res = await fetch(`${env.POSTHOG_HOST}/batch/`, { |
| 174 | method: 'POST', |
| 175 | headers: { 'content-type': 'application/json' }, |
| 176 | body: JSON.stringify({ api_key: env.POSTHOG_KEY, batch }), |
| 177 | signal: AbortSignal.timeout(5000), |
| 178 | }); |
| 179 | if (!res.ok) { |
| 180 | console.error(JSON.stringify({ msg: 'posthog forward failed', status: res.status, events: batch.length })); |
| 181 | } |
| 182 | } catch (err) { |
| 183 | console.error(JSON.stringify({ msg: 'posthog forward error', err: String(err), events: batch.length })); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | export default { |
| 188 | async fetch(request, env, ctx): Promise<Response> { |