e30e75b5d4
Code Quality / Oxlint + Oxfmt (push) Waiting to run
Code Quality / Template Sync (push) Waiting to run
Code Quality / Build Changed Packages (push) Waiting to run
Code Quality / Test Changed Packages (push) Waiting to run
Deploy Expo Example / Deploy Production (push) Waiting to run
Deploy Ink Example / Deploy Production (push) Waiting to run
Python Tests / pytest (assistant-stream, 3.10) (push) Waiting to run
Python Tests / pytest (assistant-stream, 3.12) (push) Waiting to run
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Waiting to run
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Waiting to run
Deploy Shadcn Registry / Deploy Production (push) Waiting to run
Template Metrics / LOC + Bundle Size (push) Waiting to run
Changesets / Create Version PR (push) Has been cancelled
26 lines
714 B
TypeScript
26 lines
714 B
TypeScript
import { PostHog } from "posthog-node";
|
|
|
|
const apiKey = process.env.NEXT_PUBLIC_POSTHOG_API_KEY;
|
|
|
|
export const posthogServer = apiKey
|
|
? new PostHog(apiKey, { host: "https://us.i.posthog.com" })
|
|
: null;
|
|
|
|
export function getDistinctId(req: Request): string {
|
|
const cookie = req.headers.get("cookie");
|
|
const posthogCookieMatch = cookie?.match(/ph_[^=]+=([^;]+)/);
|
|
|
|
if (posthogCookieMatch) {
|
|
try {
|
|
const decoded = decodeURIComponent(posthogCookieMatch[1]!);
|
|
const parsed = JSON.parse(decoded);
|
|
if (parsed.distinct_id) return parsed.distinct_id;
|
|
} catch {
|
|
// ignore parse errors
|
|
}
|
|
}
|
|
|
|
const ip = req.headers.get("x-forwarded-for") ?? "unknown";
|
|
return `anon_${ip}`;
|
|
}
|