2024-11-10 00:40:07 +00:00
|
|
|
import { PostHog } from 'posthog-node';
|
|
|
|
|
import { PosthogEvent, PosthogEventMap } from './posthogEvents.js';
|
2025-03-18 04:22:05 +00:00
|
|
|
import { env } from './env.js';
|
2024-11-10 00:40:07 +00:00
|
|
|
|
2024-11-26 05:04:52 +00:00
|
|
|
let posthog: PostHog | undefined = undefined;
|
|
|
|
|
|
2025-03-18 04:22:05 +00:00
|
|
|
if (env.POSTHOG_PAPIK) {
|
2024-11-26 05:04:52 +00:00
|
|
|
posthog = new PostHog(
|
2025-03-18 04:22:05 +00:00
|
|
|
env.POSTHOG_PAPIK,
|
2024-11-26 05:04:52 +00:00
|
|
|
{
|
2025-03-18 04:22:05 +00:00
|
|
|
host: env.POSTHOG_HOST,
|
2024-11-26 05:04:52 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-11-10 00:40:07 +00:00
|
|
|
|
|
|
|
|
export function captureEvent<E extends PosthogEvent>(event: E, properties: PosthogEventMap[E]) {
|
2025-03-18 04:22:05 +00:00
|
|
|
if (env.SOURCEBOT_TELEMETRY_DISABLED) {
|
2024-11-10 00:40:07 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 05:04:52 +00:00
|
|
|
posthog?.capture({
|
2025-03-18 04:22:05 +00:00
|
|
|
distinctId: env.SOURCEBOT_INSTALL_ID,
|
2024-11-10 00:40:07 +00:00
|
|
|
event: event,
|
|
|
|
|
properties: {
|
|
|
|
|
...properties,
|
2025-03-18 04:22:05 +00:00
|
|
|
sourcebot_version: env.SOURCEBOT_VERSION,
|
2024-11-10 00:40:07 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 05:04:52 +00:00
|
|
|
await posthog?.shutdown();
|