diff --git a/src/utils/telemetry/client.ts b/src/utils/telemetry/client.ts index e8b37af..627cf7b 100644 --- a/src/utils/telemetry/client.ts +++ b/src/utils/telemetry/client.ts @@ -1,4 +1,4 @@ -import { PostHog } from "posthog-node" +import type { PostHog } from "posthog-node" import type { TelemetryConfig } from "./types" const POSTHOG_API_KEY = process.env.POSTHOG_API_KEY || "" @@ -29,7 +29,7 @@ export class TelemetryClient { private sessionStartTime: number | null = null /* c8 ignore start -- requires PostHog API key */ - init(config: TelemetryConfig): void { + async init(config: TelemetryConfig): Promise { if (this.initialized || !config.isEnabled() || !POSTHOG_API_KEY) return this.config = config @@ -37,7 +37,15 @@ export class TelemetryClient { this.sessionId = crypto.randomUUID() this.sessionStartTime = Date.now() - this.posthog = new PostHog(POSTHOG_API_KEY, { + let PostHogClass: typeof PostHog + try { + ;({ PostHog: PostHogClass } = await import("posthog-node")) + } catch { + // posthog-node is unavailable (e.g. browser/web context) + return + } + + this.posthog = new PostHogClass(POSTHOG_API_KEY, { host: POSTHOG_HOST, disableGeoip: true, flushAt: FLUSH_AT, diff --git a/src/utils/telemetry/vscode.ts b/src/utils/telemetry/vscode.ts index 54de704..2729e6c 100644 --- a/src/utils/telemetry/vscode.ts +++ b/src/utils/telemetry/vscode.ts @@ -91,7 +91,7 @@ export async function initVSCodeTelemetry( vscode.extensions.getExtension("FastAPILabs.fastapi-vscode")?.packageJSON ?.version ?? "unknown" - client.init({ + await client.init({ userId, clientInfo: getClientInfo(), extensionVersion,