mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-22 19:39:01 -05:00
14b162aabc
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
35 lines
910 B
TypeScript
35 lines
910 B
TypeScript
import { PostHog } from "posthog-node";
|
|
|
|
const enabled =
|
|
process.env.NODE_ENV === "production" &&
|
|
process.env.NEXT_PUBLIC_POSTHOG_API_HOST &&
|
|
process.env.NEXT_PUBLIC_POSTHOG_API_KEY;
|
|
|
|
export const capturePosthogEnvironmentEvent = async (
|
|
environmentId: string,
|
|
eventName: string,
|
|
properties: any = {}
|
|
) => {
|
|
if (
|
|
!enabled ||
|
|
typeof process.env.NEXT_PUBLIC_POSTHOG_API_HOST !== "string" ||
|
|
typeof process.env.NEXT_PUBLIC_POSTHOG_API_KEY !== "string"
|
|
) {
|
|
return;
|
|
}
|
|
try {
|
|
const client = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_API_KEY, {
|
|
host: process.env.NEXT_PUBLIC_POSTHOG_API_HOST,
|
|
});
|
|
client.capture({
|
|
distinctId: environmentId,
|
|
event: eventName,
|
|
groups: { environment: environmentId },
|
|
properties,
|
|
});
|
|
await client.shutdownAsync();
|
|
} catch (error) {
|
|
console.error("error sending posthog event:", error);
|
|
}
|
|
};
|