Skip to content

Commit

Permalink
feat(vscode): track nx version in analytics (#2377)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless authored Jan 2, 2025
1 parent 328532f commit 632263d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apps/vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function activate(c: ExtensionContext) {

createNxlsClient(context);

initTelemetry(context.extensionMode === ExtensionMode.Production);
initTelemetry(context);
initNxInit(context);

context.subscriptions.push(
Expand Down
17 changes: 15 additions & 2 deletions libs/vscode/telemetry/src/lib/google-analytics-sender.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<string, any>): void {
eventName = eventName.replace('nrwl.angular-console/', '');
Expand Down Expand Up @@ -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],
};
Expand Down
13 changes: 10 additions & 3 deletions libs/vscode/telemetry/src/lib/init.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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, {
Expand Down

0 comments on commit 632263d

Please sign in to comment.