~james/core

core/web/src/lib/server/posthog.ts -rw-r--r-- 735 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import type { WebConfig } from "#lib/server/config.ts";

// best-effort: resolves either way, and does nothing without an api key
export const capture = async (
	config: WebConfig,
	event: string,
	distinctId: string,
	properties: Record<string, unknown> = {}
): Promise<void> => {
	if (!config.posthogApiKey) return;
	try {
		await fetch(`${config.posthogHost}/capture/`, {
			method: "POST",
			headers: { "content-type": "application/json" },
			body: JSON.stringify({
				api_key: config.posthogApiKey,
				event,
				distinct_id: distinctId,
				properties: { ...properties, environment: config.posthogEnv },
				timestamp: new Date().toISOString()
			})
		});
	} catch {
		// analytics never breaks the request it rides on
	}
};