Roo-Code/webview-ui/src/utils/TelemetryClient.ts
Matt Rubens 33fe6fb9c6
Make Posthog telemetry the default (#7909)
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
2025-09-11 15:50:40 -04:00

47 lines
1.2 KiB
TypeScript

import posthog from "posthog-js"
import type { TelemetrySetting } from "@roo-code/types"
class TelemetryClient {
private static instance: TelemetryClient
private static telemetryEnabled: boolean = false
public updateTelemetryState(telemetrySetting: TelemetrySetting, apiKey?: string, distinctId?: string) {
posthog.reset()
if (telemetrySetting !== "disabled" && apiKey && distinctId) {
TelemetryClient.telemetryEnabled = true
posthog.init(apiKey, {
api_host: "https://us.i.posthog.com",
persistence: "localStorage",
loaded: () => posthog.identify(distinctId),
capture_pageview: false,
capture_pageleave: false,
autocapture: false,
})
} else {
TelemetryClient.telemetryEnabled = false
}
}
public static getInstance(): TelemetryClient {
if (!TelemetryClient.instance) {
TelemetryClient.instance = new TelemetryClient()
}
return TelemetryClient.instance
}
public capture(eventName: string, properties?: Record<string, any>) {
if (TelemetryClient.telemetryEnabled) {
try {
posthog.capture(eventName, properties)
} catch (_error) {
// Silently fail if there's an error capturing an event.
}
}
}
}
export const telemetryClient = TelemetryClient.getInstance()