Files
formbricks/apps/web/lib/posthog.ts
Matti Nannt 6cd3878700 make all env variables required at build time #110 (#111)
* make all env variables required at build time

* update env files

* add .env.docker file with basic docker configuration

* update Readme with new instructions
2022-10-16 11:10:20 +02:00

28 lines
785 B
TypeScript

import { hashString } from "./utils";
const enabled =
process.env.NODE_ENV === "production" && process.env.POSTHOG_API_HOST && process.env.POSTHOG_API_KEY;
export const capturePosthogEvent = async (userId, eventName, properties = {}) => {
if (!enabled) {
return;
}
try {
await fetch(`${process.env.POSTHOG_API_HOST}/capture/`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
api_key: process.env.POSTHOG_API_KEY,
event: eventName,
properties: {
distinct_id: hashString(userId.toString()),
...properties,
},
timestamp: new Date().toISOString(),
}),
});
} catch (error) {
console.error("error sending posthog event:", error);
}
};