|
| 1 | +import * as LDClient from 'launchdarkly-node-client-sdk'; |
| 2 | +import { Disposable, env } from 'vscode'; |
| 3 | + |
| 4 | +const CLIENT_SIDE_ID = process.env.LD_ANALYTICS_CLIENT_ID; |
| 5 | +const BASE_URL = process.env.LD_ANALYTICS_BASE_URL; |
| 6 | +const STREAM_URL = process.env.LD_ANALYTICS_STREAM_URL; |
| 7 | +const EVENTS_URL = process.env.LD_ANALYTICS_EVENTS_URL; |
| 8 | + |
| 9 | +/** |
| 10 | + * Internal analytics client using the LaunchDarkly Node Client SDK. |
| 11 | + * Uses a client-side ID (safe to embed — not a secret) to send |
| 12 | + * usage telemetry events. Respects VS Code telemetry settings. |
| 13 | + */ |
| 14 | +class AnalyticsClient implements Disposable { |
| 15 | + private client: LDClient.LDClient | null = null; |
| 16 | + private enabled = false; |
| 17 | + private initPromise: Promise<void> | null = null; |
| 18 | + |
| 19 | + async initialize(extensionVersion: string): Promise<void> { |
| 20 | + console.log('env.isTelemetryEnabled', env.isTelemetryEnabled); |
| 21 | + console.log('LD_ANALYTICS_CLIENT_ID', CLIENT_SIDE_ID); |
| 22 | + console.log('LD_ANALYTICS_BASE_URL', BASE_URL); |
| 23 | + console.log('LD_ANALYTICS_STREAM_URL', STREAM_URL); |
| 24 | + console.log('LD_ANALYTICS_EVENTS_URL', EVENTS_URL); |
| 25 | + |
| 26 | + if (!CLIENT_SIDE_ID || !BASE_URL || !STREAM_URL || !EVENTS_URL || !env.isTelemetryEnabled) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (this.initPromise) { |
| 31 | + return this.initPromise; |
| 32 | + } |
| 33 | + |
| 34 | + this.initPromise = this.doInitialize(extensionVersion); |
| 35 | + return this.initPromise; |
| 36 | + } |
| 37 | + |
| 38 | + private async doInitialize(extensionVersion: string): Promise<void> { |
| 39 | + try { |
| 40 | + const context: LDClient.LDContext = { |
| 41 | + kind: 'ld-vscode-user', |
| 42 | + key: env.machineId, |
| 43 | + vscodeVersion: env.appName, |
| 44 | + extensionVersion, |
| 45 | + }; |
| 46 | + |
| 47 | + this.client = LDClient.initialize(CLIENT_SIDE_ID, context, { |
| 48 | + baseUrl: BASE_URL, |
| 49 | + streamUrl: STREAM_URL, |
| 50 | + eventsUrl: EVENTS_URL, |
| 51 | + }); |
| 52 | + await this.client.waitForInitialization(); |
| 53 | + this.enabled = true; |
| 54 | + |
| 55 | + env.onDidChangeTelemetryEnabled((telemetryEnabled) => { |
| 56 | + this.enabled = telemetryEnabled; |
| 57 | + }); |
| 58 | + } catch (err) { |
| 59 | + console.error('[LaunchDarkly Analytics] Failed to initialize:', err); |
| 60 | + this.client = null; |
| 61 | + this.enabled = false; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + track(event: string, data?: LDClient.LDFlagValue): void { |
| 66 | + if (!this.enabled || !this.client) { |
| 67 | + return; |
| 68 | + } |
| 69 | + try { |
| 70 | + this.client.track(event, data); |
| 71 | + } catch (err) { |
| 72 | + console.error(`[LaunchDarkly Analytics] Failed to track event "${event}":`, err); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + async dispose(): Promise<void> { |
| 77 | + if (this.client) { |
| 78 | + try { |
| 79 | + await this.client.flush(); |
| 80 | + } catch { |
| 81 | + // Best-effort flush on shutdown |
| 82 | + } |
| 83 | + this.client.close(); |
| 84 | + this.client = null; |
| 85 | + } |
| 86 | + this.enabled = false; |
| 87 | + this.initPromise = null; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export const analytics = new AnalyticsClient(); |
0 commit comments