continue-custom/core/util/posthog.ts
Nate Sesti 8fed0cb985
Convert core/ to NodeNext (#1877)
* convert to nodenext in core

* experimental vm modules

* use imports

* a few ts-ignores

---------

Co-authored-by: Patrick Erichsen <patrick.a.erichsen@gmail.com>
2024-07-30 19:04:30 -07:00

62 lines
1.5 KiB
TypeScript

import os from "node:os";
import { TeamAnalytics } from "../control-plane/TeamAnalytics.js";
export class Telemetry {
// Set to undefined whenever telemetry is disabled
static client: any = undefined;
static uniqueId = "NOT_UNIQUE";
static os: string | undefined = undefined;
static extensionVersion: string | undefined = undefined;
static async capture(
event: string,
properties: { [key: string]: any },
sendToTeam: boolean = false,
) {
Telemetry.client?.capture({
distinctId: Telemetry.uniqueId,
event,
properties: {
...properties,
os: Telemetry.os,
extensionVersion: Telemetry.extensionVersion,
},
});
if (sendToTeam) {
TeamAnalytics.capture(event, properties);
}
}
static shutdownPosthogClient() {
Telemetry.client?.shutdown();
}
static async setup(
allow: boolean,
uniqueId: string,
extensionVersion: string,
) {
Telemetry.uniqueId = uniqueId;
Telemetry.os = os.platform();
Telemetry.extensionVersion = extensionVersion;
if (!allow) {
Telemetry.client = undefined;
} else {
try {
if (!Telemetry.client) {
const { PostHog } = await import("posthog-node");
Telemetry.client = new PostHog(
"phc_JS6XFROuNbhJtVCEdTSYk6gl5ArRrTNMpCcguAXlSPs",
{
host: "https://app.posthog.com",
},
);
}
} catch (e) {
console.error(`Failed to setup telemetry: ${e}`);
}
}
}
}