Skip to content

Commit

Permalink
Setup analytics system for IDX (#7999)
Browse files Browse the repository at this point in the history
* Setup analytics system for IDX

* remove console log
  • Loading branch information
hlshen authored Dec 9, 2024
1 parent b911744 commit 1b421a7
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 71 deletions.
30 changes: 18 additions & 12 deletions firebase-vscode/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export enum DATA_CONNECT_EVENT_NAME {
}

export class AnalyticsLogger {
readonly logger: TelemetryLogger;
readonly logger: TelemetryLogger | IDXLogger;
private disposable: vscode.Disposable;
private sessionCharCount = 0; // Track total chars for the session

constructor() {
this.logger = env.createTelemetryLogger(
constructor(context: vscode.ExtensionContext) {
this.logger = monospaceEnv.value.isMonospace ? new IDXLogger(new GA4TelemetrySender(pluginLogger), context) : env.createTelemetryLogger(
new GA4TelemetrySender(pluginLogger),
);

Expand Down Expand Up @@ -146,6 +146,19 @@ export class AnalyticsLogger {
}
}

export class IDXLogger {
constructor(private sender: GA4TelemetrySender, private context: vscode.ExtensionContext) {}
public logUsage(eventName: string, data?: any) {
const packageJson = this.context.extension.packageJSON;
data = { ...data, extversion: packageJson.version, extname: this.context.extension.id, isidx: true };
this.sender.sendEventData(eventName, data);
}

public logError() {
// TODO
}
}

class GA4TelemetrySender implements TelemetrySender {
private hasSentData = false;
constructor(readonly pluginLogger: { warn: (s: string) => void }) {}
Expand All @@ -154,12 +167,6 @@ class GA4TelemetrySender implements TelemetrySender {
eventName: string,
data?: Record<string, any> | undefined,
): void {
// telemtry flag does not exist in monospace
if (!env.isTelemetryEnabled && !monospaceEnv.value.isMonospace) {
this.pluginLogger.warn("Telemetry is not enabled.");
return;
}

// telemetry logger adds prefixes to eventName and params that are disallowed in GA4
eventName = eventName.replace(
"GoogleCloudTools.firebase-dataconnect-vscode/",
Expand All @@ -176,15 +183,14 @@ class GA4TelemetrySender implements TelemetrySender {
}
}
data = { ...data };
const idxPrepend = monospaceEnv.value.isMonospace ? "idx_" : "";
if (!this.hasSentData) {
trackVSCode(
`${idxPrepend}${DATA_CONNECT_EVENT_NAME.EXTENSION_USED}`,
DATA_CONNECT_EVENT_NAME.EXTENSION_USED,
data as AnalyticsParams,
);
this.hasSentData = true;
}
trackVSCode(`${idxPrepend}${eventName}`, data as AnalyticsParams);
trackVSCode(eventName, data as AnalyticsParams);
}

sendErrorData(error: Error, data?: Record<string, any> | undefined): void {
Expand Down
11 changes: 6 additions & 5 deletions firebase-vscode/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import vscode, { Disposable, ExtensionContext, TelemetryLogger } from "vscode";
import vscode, { Disposable, ExtensionContext } from "vscode";
import { ExtensionBrokerImpl } from "../extension-broker";
import { getRootFolders, registerConfig } from "./config";
import { EmulatorsController } from "./emulators";
Expand All @@ -14,11 +14,12 @@ import { upsertFile } from "../data-connect/file-utils";
import { registerWebhooks } from "./webhook";
import { createE2eMockable } from "../utils/test_hooks";
import { runTerminalTask } from "../data-connect/terminal";
import { AnalyticsLogger } from "../analytics";

export async function registerCore(
broker: ExtensionBrokerImpl,
context: ExtensionContext,
telemetryLogger: TelemetryLogger,
analyticsLogger: AnalyticsLogger,
): Promise<[EmulatorsController, vscode.Disposable]> {
const settings = getSettings();

Expand Down Expand Up @@ -69,7 +70,7 @@ export async function registerCore(
? `${settings.firebasePath} init dataconnect --project ${currentProjectId.value}`
: `${settings.firebasePath} init dataconnect`;

initSpy.call("firebase init", initCommand, {focus: true});
initSpy.call("firebase init", initCommand, { focus: true });
});

const emulatorsController = new EmulatorsController(broker);
Expand Down Expand Up @@ -103,8 +104,8 @@ export async function registerCore(
initSpy,
registerOptions(context),
registerEnv(broker),
registerUser(broker, telemetryLogger),
registerProject(broker, telemetryLogger),
registerUser(broker, analyticsLogger),
registerProject(broker, analyticsLogger),
registerQuickstart(broker),
await registerWebhooks(),
{ dispose: sub1 },
Expand Down
12 changes: 7 additions & 5 deletions firebase-vscode/src/core/project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import vscode, { Disposable, TelemetryLogger } from "vscode";
import vscode, { Disposable } from "vscode";
import { ExtensionBrokerImpl } from "../extension-broker";
import { computed, effect, Signal } from "@preact/signals-react";
import { firebaseRC, updateFirebaseRCProject } from "./config";
Expand All @@ -9,7 +9,7 @@ import { pluginLogger } from "../logger-wrapper";
import { globalSignal } from "../utils/globals";
import { firstWhereDefined } from "../utils/signal";
import { User } from "../types/auth";
import { DATA_CONNECT_EVENT_NAME } from "../analytics";
import { DATA_CONNECT_EVENT_NAME, AnalyticsLogger } from "../analytics";
/** Available projects */
export const projects = globalSignal<Record<string, FirebaseProjectMetadata[]>>(
{},
Expand All @@ -30,7 +30,7 @@ const userScopedProjects = computed<FirebaseProjectMetadata[] | undefined>(

export function registerProject(
broker: ExtensionBrokerImpl,
telemetryLogger: TelemetryLogger,
analyticsLogger: AnalyticsLogger,
): Disposable {
// For testing purposes.
const demoProjectCommand = vscode.commands.registerCommand(
Expand Down Expand Up @@ -103,7 +103,7 @@ export function registerProject(
return;
} else {
try {
telemetryLogger.logUsage(
analyticsLogger.logger.logUsage(
DATA_CONNECT_EVENT_NAME.PROJECT_SELECT_CLICKED,
);

Expand All @@ -120,7 +120,9 @@ export function registerProject(
}
: undefined,
});
telemetryLogger.logUsage(DATA_CONNECT_EVENT_NAME.PROJECT_SELECTED);
analyticsLogger.logger.logUsage(
DATA_CONNECT_EVENT_NAME.PROJECT_SELECTED,
);
} catch (e: any) {
vscode.window.showErrorMessage(e.message);
}
Expand Down
8 changes: 4 additions & 4 deletions firebase-vscode/src/core/user.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Signal, computed, effect } from "@preact/signals-react";
import { Disposable, TelemetryLogger } from "vscode";
import { Disposable } from "vscode";
import { ServiceAccountUser } from "../types";
import { User as AuthUser } from "../../../src/types/auth";
import { ExtensionBrokerImpl } from "../extension-broker";
import { login, logoutUser, requireAuthWrapper } from "../cli";
import { globalSignal } from "../utils/globals";
import { DATA_CONNECT_EVENT_NAME } from "../analytics";
import { DATA_CONNECT_EVENT_NAME, AnalyticsLogger } from "../analytics";
import * as vscode from "vscode";

type User = ServiceAccountUser | AuthUser;
Expand All @@ -24,7 +24,7 @@ export async function checkLogin() {

export function registerUser(
broker: ExtensionBrokerImpl,
telemetryLogger: TelemetryLogger,
analyticsLogger: AnalyticsLogger,
): Disposable {
// For testing purposes.
const userMockCommand = vscode.commands.registerCommand(
Expand Down Expand Up @@ -58,7 +58,7 @@ export function registerUser(
});

const addUserSub = broker.on("addUser", async () => {
telemetryLogger.logUsage(DATA_CONNECT_EVENT_NAME.LOGIN);
analyticsLogger.logger.logUsage(DATA_CONNECT_EVENT_NAME.LOGIN);
const { user } = await login();
currentUser.value = user;
});
Expand Down
9 changes: 5 additions & 4 deletions firebase-vscode/src/data-connect/ad-hoc-mutations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import vscode, { Disposable, TelemetryLogger } from "vscode";
import vscode, { Disposable } from "vscode";
import {
DocumentNode,
GraphQLInputField,
Expand All @@ -18,10 +18,11 @@ import { DataConnectService } from "./service";
import { DATA_CONNECT_EVENT_NAME } from "../analytics";
import { dataConnectConfigs } from "./config";
import { firstWhereDefined } from "../utils/signal";
import {AnalyticsLogger} from "../analytics";

export function registerAdHoc(
dataConnectService: DataConnectService,
telemetryLogger: TelemetryLogger,
analyticsLogger: AnalyticsLogger,
): Disposable {
const defaultScalarValues = {
Any: "{}",
Expand Down Expand Up @@ -262,14 +263,14 @@ query {
vscode.commands.registerCommand(
"firebase.dataConnect.schemaAddData",
(ast, uri) => {
telemetryLogger.logUsage(DATA_CONNECT_EVENT_NAME.ADD_DATA);
analyticsLogger.logger.logUsage(DATA_CONNECT_EVENT_NAME.ADD_DATA);
schemaAddData(ast, uri);
},
),
vscode.commands.registerCommand(
"firebase.dataConnect.schemaReadData",
(document, ast, uri) => {
telemetryLogger.logUsage(DATA_CONNECT_EVENT_NAME.READ_DATA);
analyticsLogger.logger.logUsage(DATA_CONNECT_EVENT_NAME.READ_DATA);
schemaReadData(document, ast, uri);
},
),
Expand Down
9 changes: 5 additions & 4 deletions firebase-vscode/src/data-connect/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import vscode, {
ExtensionContext,
InputBoxValidationMessage,
InputBoxValidationSeverity,
TelemetryLogger,
} from "vscode";
import { ExtensionBrokerImpl } from "../extension-broker";
import {
Expand Down Expand Up @@ -39,13 +38,13 @@ import { DataConnectService } from "./service";
import { OperationLocation } from "./types";
import { checkIfFileExists } from "./file-utils";
import * as path from "path";
import { DATA_CONNECT_EVENT_NAME } from "../analytics";
import { DATA_CONNECT_EVENT_NAME, AnalyticsLogger } from "../analytics";

export function registerConnectors(
context: ExtensionContext,
broker: ExtensionBrokerImpl,
dataConnectService: DataConnectService,
telemetryLogger: TelemetryLogger,
analyticsLogger: AnalyticsLogger,
): Disposable {
async function moveOperationToConnector(
defIndex: number, // The index of the definition to move.
Expand Down Expand Up @@ -477,7 +476,9 @@ export function registerConnectors(
vscode.commands.registerCommand(
"firebase.dataConnect.moveOperationToConnector",
(number, location, connectorPath) => {
telemetryLogger.logUsage(DATA_CONNECT_EVENT_NAME.MOVE_TO_CONNECTOR);
analyticsLogger.logger.logUsage(
DATA_CONNECT_EVENT_NAME.MOVE_TO_CONNECTOR,
);
moveOperationToConnector(number, location, connectorPath);
},
),
Expand Down
8 changes: 4 additions & 4 deletions firebase-vscode/src/data-connect/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dataConnectConfigs } from "./config";
import { createE2eMockable } from "../utils/test_hooks";
import { runCommand } from "./terminal";
import { ExtensionBrokerImpl } from "../extension-broker";
import { DATA_CONNECT_EVENT_NAME } from "../analytics";
import { DATA_CONNECT_EVENT_NAME, AnalyticsLogger } from "../analytics";
import { getSettings } from "../utils/settings";

function createDeployOnlyCommand(serviceConnectorMap: {
Expand All @@ -28,7 +28,7 @@ function createDeployOnlyCommand(serviceConnectorMap: {

export function registerFdcDeploy(
broker: ExtensionBrokerImpl,
telemetryLogger: vscode.TelemetryLogger,
analyticsLogger: AnalyticsLogger,
): vscode.Disposable {
const settings = getSettings();

Expand All @@ -42,14 +42,14 @@ export function registerFdcDeploy(
);

const deployAllCmd = vscode.commands.registerCommand("fdc.deploy-all", () => {
telemetryLogger.logUsage(DATA_CONNECT_EVENT_NAME.DEPLOY_ALL, {
analyticsLogger.logger.logUsage(DATA_CONNECT_EVENT_NAME.DEPLOY_ALL, {
firebase_binary_kind: settings.firebaseBinaryKind,
});
deploySpy.call(`${settings.firebasePath} deploy --only dataconnect`);
});

const deployCmd = vscode.commands.registerCommand("fdc.deploy", async () => {
telemetryLogger.logUsage(DATA_CONNECT_EVENT_NAME.DEPLOY_INDIVIDUAL, {
analyticsLogger.logger.logUsage(DATA_CONNECT_EVENT_NAME.DEPLOY_INDIVIDUAL, {
firebase_binary_kind: settings.firebaseBinaryKind,
});
const configs = await firstWhereDefined(dataConnectConfigs).then(
Expand Down
7 changes: 3 additions & 4 deletions firebase-vscode/src/data-connect/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import vscode, {
ConfigurationTarget,
Disposable,
ExtensionContext,
TelemetryLogger,
} from "vscode";
import { ExtensionBrokerImpl } from "../extension-broker";
import { registerWebview } from "../webview";
Expand All @@ -23,13 +22,13 @@ import { DataConnectService } from "./service";
import { DataConnectError, toSerializedError } from "../../common/error";
import { OperationLocation } from "./types";
import { InstanceType } from "./code-lens-provider";
import { DATA_CONNECT_EVENT_NAME } from "../analytics";
import { DATA_CONNECT_EVENT_NAME, AnalyticsLogger } from "../analytics";

export function registerExecution(
context: ExtensionContext,
broker: ExtensionBrokerImpl,
dataConnectService: DataConnectService,
telemetryLogger: TelemetryLogger,
analyticsLogger: AnalyticsLogger,
): Disposable {
const treeDataProvider = new ExecutionHistoryTreeDataProvider();
const executionHistoryTreeView = vscode.window.createTreeView(
Expand Down Expand Up @@ -190,7 +189,7 @@ export function registerExecution(
vscode.commands.registerCommand(
"firebase.dataConnect.executeOperation",
(ast, location, instanceType: InstanceType) => {
telemetryLogger.logUsage(
analyticsLogger.logger.logUsage(
instanceType === InstanceType.LOCAL
? DATA_CONNECT_EVENT_NAME.RUN_LOCAL
: DATA_CONNECT_EVENT_NAME.RUN_PROD,
Expand Down
16 changes: 8 additions & 8 deletions firebase-vscode/src/data-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import { Result } from "../result";
import { LanguageClient } from "vscode-languageclient/node";
import { registerTerminalTasks } from "./terminal";
import { registerWebview } from "../webview";

import { DataConnectToolkit } from "./toolkit";
import { registerFdcSdkGeneration } from "./sdk-generation";
import { registerDiagnostics } from "./diagnostics";
import { AnalyticsLogger } from "../analytics";

class CodeActionsProvider implements vscode.CodeActionProvider {
constructor(
Expand Down Expand Up @@ -133,7 +133,7 @@ export function registerFdc(
broker: ExtensionBrokerImpl,
authService: AuthService,
emulatorController: EmulatorsController,
telemetryLogger: TelemetryLogger,
analyticsLogger: AnalyticsLogger,
): Disposable {
registerDiagnostics(context, dataConnectConfigs);
const dataConnectToolkit = new DataConnectToolkit(broker);
Expand Down Expand Up @@ -222,14 +222,14 @@ export function registerFdc(
selectedProjectStatus.show();
}),
},
registerExecution(context, broker, fdcService, telemetryLogger),
registerExecution(context, broker, fdcService, analyticsLogger),
registerExplorer(context, broker, fdcService),
registerWebview({ name: "data-connect", context, broker }),
registerAdHoc(fdcService, telemetryLogger),
registerConnectors(context, broker, fdcService, telemetryLogger),
registerFdcDeploy(broker, telemetryLogger),
registerFdcSdkGeneration(broker, telemetryLogger),
registerTerminalTasks(broker, telemetryLogger),
registerAdHoc(fdcService, analyticsLogger),
registerConnectors(context, broker, fdcService, analyticsLogger),
registerFdcDeploy(broker, analyticsLogger),
registerFdcSdkGeneration(broker, analyticsLogger),
registerTerminalTasks(broker, analyticsLogger),
operationCodeLensProvider,
vscode.languages.registerCodeLensProvider(
// **Hack**: For testing purposes, enable code lenses on all graphql files
Expand Down
Loading

0 comments on commit 1b421a7

Please sign in to comment.