|
| 1 | +import { init } from 'mixpanel'; |
| 2 | +import type { Mixpanel } from 'mixpanel'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as vscode from 'vscode'; |
| 5 | +import { getMachineId } from './machine-id-utils'; |
| 6 | +import { v5 as uuidv5 } from 'uuid'; |
| 7 | +import { version as extensionVersion } from '../../package.json'; |
| 8 | + |
| 9 | +export const VSCODE_TELEMETRY_TRACKING_TOKEN = '<VSCODE_TELEMETRY_TRACKING_TOKEN>'; |
| 10 | + |
| 11 | +export type TelemetryEvents = 'extension:activate' | 'extension:zmodel-preview' | 'extension:zmodel-save'; |
| 12 | + |
| 13 | +export class VSCodeTelemetry { |
| 14 | + private readonly mixpanel: Mixpanel | undefined; |
| 15 | + private readonly deviceId = this.getDeviceId(); |
| 16 | + private readonly _os_type = os.type(); |
| 17 | + private readonly _os_release = os.release(); |
| 18 | + private readonly _os_arch = os.arch(); |
| 19 | + private readonly _os_version = os.version(); |
| 20 | + private readonly _os_platform = os.platform(); |
| 21 | + private readonly vscodeAppName = vscode.env.appName; |
| 22 | + private readonly vscodeVersion = vscode.version; |
| 23 | + private readonly vscodeAppHost = vscode.env.appHost; |
| 24 | + |
| 25 | + constructor() { |
| 26 | + if (vscode.env.isTelemetryEnabled) { |
| 27 | + this.mixpanel = init(VSCODE_TELEMETRY_TRACKING_TOKEN, { |
| 28 | + geolocate: true, |
| 29 | + }); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private getDeviceId() { |
| 34 | + const hostId = getMachineId(); |
| 35 | + // namespace UUID for generating UUIDv5 from DNS 'zenstack.dev' |
| 36 | + return uuidv5(hostId, '133cac15-3efb-50fa-b5fc-4b90e441e563'); |
| 37 | + } |
| 38 | + |
| 39 | + track(event: TelemetryEvents, properties: Record<string, unknown> = {}) { |
| 40 | + if (this.mixpanel) { |
| 41 | + const payload = { |
| 42 | + distinct_id: this.deviceId, |
| 43 | + time: new Date(), |
| 44 | + $os: this._os_type, |
| 45 | + osType: this._os_type, |
| 46 | + osRelease: this._os_release, |
| 47 | + osPlatform: this._os_platform, |
| 48 | + osArch: this._os_arch, |
| 49 | + osVersion: this._os_version, |
| 50 | + nodeVersion: process.version, |
| 51 | + vscodeAppName: this.vscodeAppName, |
| 52 | + vscodeVersion: this.vscodeVersion, |
| 53 | + vscodeAppHost: this.vscodeAppHost, |
| 54 | + extensionVersion, |
| 55 | + ...properties, |
| 56 | + }; |
| 57 | + this.mixpanel.track(event, payload); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export default new VSCodeTelemetry(); |
0 commit comments