📈 Posthog package (#1431)

This commit is contained in:
Luke Vella 2024-11-09 11:30:14 +00:00 committed by GitHub
parent 0fc7d0a0c8
commit a5da319d82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
43 changed files with 189 additions and 133 deletions

View file

@ -0,0 +1,23 @@
import { waitUntil } from "@vercel/functions";
import { PostHog } from "posthog-node";
function PostHogClient() {
if (!process.env.NEXT_PUBLIC_POSTHOG_API_KEY) return null;
const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_API_KEY, {
host: process.env.NEXT_PUBLIC_POSTHOG_API_HOST,
flushAt: 1,
flushInterval: 0,
});
return posthogClient;
}
export const posthog = PostHogClient();
export function posthogApiHandler() {
try {
waitUntil(Promise.all([posthog?.shutdown()]));
} catch (error) {
console.error("Failed to flush PostHog events:", error);
}
}