From 632263d4f0235b97b1bae7141b5cd4bf9f7aeeab Mon Sep 17 00:00:00 2001 From: MaxKless <34165455+MaxKless@users.noreply.github.com> Date: Thu, 2 Jan 2025 10:38:20 +0100 Subject: [PATCH] feat(vscode): track nx version in analytics (#2377) --- apps/vscode/src/main.ts | 2 +- .../src/lib/google-analytics-sender.ts | 17 +++++++++++++++-- libs/vscode/telemetry/src/lib/init.ts | 13 ++++++++++--- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/apps/vscode/src/main.ts b/apps/vscode/src/main.ts index 0a6f6a0c88..b5df814889 100644 --- a/apps/vscode/src/main.ts +++ b/apps/vscode/src/main.ts @@ -82,7 +82,7 @@ export async function activate(c: ExtensionContext) { createNxlsClient(context); - initTelemetry(context.extensionMode === ExtensionMode.Production); + initTelemetry(context); initNxInit(context); context.subscriptions.push( diff --git a/libs/vscode/telemetry/src/lib/google-analytics-sender.ts b/libs/vscode/telemetry/src/lib/google-analytics-sender.ts index d757d7b8ea..7b3ff6288f 100644 --- a/libs/vscode/telemetry/src/lib/google-analytics-sender.ts +++ b/libs/vscode/telemetry/src/lib/google-analytics-sender.ts @@ -1,15 +1,19 @@ import { platform } from 'os'; import { xhr, XHRResponse } from 'request-light'; -import { env, extensions, TelemetrySender } from 'vscode'; +import { env, ExtensionContext, extensions, TelemetrySender } from 'vscode'; import { getOutputChannel } from '@nx-console/vscode/output-channels'; // @ts-expect-error -- Browser Rollbar is not typed import Rollbar = require('rollbar/src/browser/rollbar'); +import { getNxVersion } from '@nx-console/vscode/nx-workspace'; +import { onWorkspaceRefreshed } from '@nx-console/vscode/lsp-client'; export class GoogleAnalyticsSender implements TelemetrySender { MEASUREMENT_ID = 'G-TNJ97NGX40'; API_TOKEN = '3J_QsvygSLKfjxMXFSG03Q'; + private _nxVersion: string | undefined; + private _version: string = extensions.getExtension('nrwl.angular-console')?.packageJSON?.version ?? '0.0.0'; @@ -22,7 +26,15 @@ export class GoogleAnalyticsSender implements TelemetrySender { environment: this._version, }); - constructor(private production: boolean) {} + constructor(private production: boolean, private context: ExtensionContext) { + getNxVersion().then((version) => (this._nxVersion = version?.full)); + context.subscriptions.push( + onWorkspaceRefreshed(async () => { + const version = await getNxVersion(); + this._nxVersion = version?.full; + }) + ); + } sendEventData(eventName: string, data?: Record): void { eventName = eventName.replace('nrwl.angular-console/', ''); @@ -70,6 +82,7 @@ export class GoogleAnalyticsSender implements TelemetrySender { editor: { value: 'vscode' }, os: { value: platform() }, appversion: { value: this._version }, + nxversion: { value: this._nxVersion ?? '0.0.0' }, }, events: [event], }; diff --git a/libs/vscode/telemetry/src/lib/init.ts b/libs/vscode/telemetry/src/lib/init.ts index edbc970944..cd91a7b254 100644 --- a/libs/vscode/telemetry/src/lib/init.ts +++ b/libs/vscode/telemetry/src/lib/init.ts @@ -1,5 +1,11 @@ import { getOutputChannel } from '@nx-console/vscode/output-channels'; -import { TelemetryLogger, TelemetrySender, env } from 'vscode'; +import { + ExtensionContext, + ExtensionMode, + TelemetryLogger, + TelemetrySender, + env, +} from 'vscode'; import { GoogleAnalyticsSender } from './google-analytics-sender'; import { LoggerSender } from './logger-sender'; import { TelemetryData, TelemetryEvents } from './telemetry-types'; @@ -10,9 +16,10 @@ export function getTelemetry() { return telemetry; } -export function initTelemetry(production: boolean) { +export function initTelemetry(context: ExtensionContext) { + const production = context.extensionMode === ExtensionMode.Production; const telemetrySender: TelemetrySender = production - ? new GoogleAnalyticsSender(production) + ? new GoogleAnalyticsSender(production, context) : new LoggerSender(); telemetry = env.createTelemetryLogger(telemetrySender, {