SurfSense/surfsense_web/lib/posthog/server.ts

18 lines
526 B
TypeScript

import { PostHog } from "posthog-node";
export default function PostHogClient() {
if (!process.env.NEXT_PUBLIC_POSTHOG_KEY) {
throw new Error("NEXT_PUBLIC_POSTHOG_KEY is not set");
}
const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
// Because server-side functions in Next.js can be short-lived,
// we set flushAt to 1 and flushInterval to 0 to ensure events are sent immediately
flushAt: 1,
flushInterval: 0,
});
return posthogClient;
}