| // Copyright 2025 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| // Analytics initializers. |
| |
| import * as vscode from 'vscode'; |
| import * as messages from './messages'; |
| import * as persistent_status from './persistent_status'; |
| import * as logger from '../logger'; |
| |
| |
| import { AnalyticsInfo } from './info'; |
| import { isDisabledByEnvironment, isRunByBot } from './environment_status'; |
| import { AnalyticsState } from './state'; |
| |
| |
| /** |
| * Init analytics |
| * @param context - VS Code extension context |
| */ |
| export async function init(context: vscode.ExtensionContext): Promise<void> { |
| if (isDisabledByEnvironment() || isRunByBot()) { |
| return; |
| } |
| |
| await AnalyticsInfo.init(context); |
| AnalyticsState.initDebugLevel(); |
| |
| if (AnalyticsInfo.isInternalUser) { |
| await internal.init(); |
| } else { |
| await external.init(); |
| } |
| |
| await AnalyticsInfo.readUuid(); |
| await AnalyticsState.updateOptInLevel(); |
| |
| console.log(`Analytics optInLevel: ${AnalyticsState.optInLevel}`); |
| |
| context.subscriptions.push(vscode.env.onDidChangeTelemetryEnabled(async (_) => { |
| await AnalyticsState.updateOptInLevel(); |
| })); |
| |
| var watcher; |
| if (AnalyticsInfo.isInternalUser) { |
| watcher = new persistent_status.InternalPropertyWatcher(); |
| } else { |
| watcher = new persistent_status.EnabledPropertyWatcher(); |
| } |
| |
| watcher.onDidChange(async (_) => { |
| logger.debug('Analytics persistent state changed. Updating runtime state...', 'analytics'); |
| await AnalyticsState.updateOptInLevel(); |
| }); |
| context.subscriptions.push(watcher); |
| } |
| |
| /** |
| * Show a modal message to the user. |
| * @param message - The message to show |
| */ |
| function showMessage(message: string): void { |
| void vscode.window.showInformationMessage(message, { modal: true }); |
| } |
| |
| namespace external { |
| /** |
| * External analytics users: init as the first run of the first Fuchsia tools |
| */ |
| export async function initFirstRunOfFirstTool(): Promise<void> { |
| if (vscode.env.isTelemetryEnabled) { |
| showMessage(messages.MESSAGE_FIRST_RUN_FIRST_TOOL_TELEMETRY_ON); |
| } else { |
| showMessage(messages.MESSAGE_FIRST_RUN_FIRST_TOOL_TELEMETRY_OFF); |
| } |
| |
| await persistent_status.external.enable(); |
| await persistent_status.external.markAsDirectlyLaunched(); |
| // Analytics is not collected on the very first run |
| AnalyticsState.disableForSession(); |
| } |
| |
| /** |
| * External analytics users: init as the first run of the other Fuchsia tools, |
| * i.e. when other Fuchsia tools have been launched before VS Code. |
| */ |
| export async function initFirstRunOfOtherTool(): Promise<void> { |
| if (await persistent_status.external.isEnabled()) { |
| if (vscode.env.isTelemetryEnabled) { |
| showMessage(messages.MESSAGE_FIRST_RUN_OTHER_TOOL_ENABLED_TELEMETRY_ON); |
| } else { |
| showMessage(messages.MESSAGE_FIRST_RUN_OTHER_TOOL_ENABLED_TELEMETRY_OFF); |
| } |
| } else { |
| showMessage(messages.MESSAGE_FIRST_RUN_OTHER_TOOL_DISABLED); |
| } |
| await persistent_status.external.markAsDirectlyLaunched(); |
| } |
| |
| /** |
| * Analytics initialization process specific to external users. |
| */ |
| export async function init(): Promise<void> { |
| |
| if (await persistent_status.external.isFirstLaunchOfFirstTool()) { |
| await external.initFirstRunOfFirstTool(); |
| } else if (await persistent_status.external.isFirstDirectLaunch()) { |
| await external.initFirstRunOfOtherTool(); |
| } |
| } |
| } |
| |
| namespace internal { |
| /** |
| * Analytics initialization process specific to internal users. |
| */ |
| export async function init(): Promise<void> { |
| if (await persistent_status.internal.isNewUser() |
| || await persistent_status.internal.isExistingUnmigratedOptedInUser()) { |
| if (vscode.env.isTelemetryEnabled) { |
| showMessage(messages.MESSAGE_INTERNAL_TELEMETRY_ON); |
| } else { |
| showMessage(messages.MESSAGE_INTERNAL_TELEMETRY_OFF); |
| } |
| } else if (await persistent_status.internal.isExistingUnmigratedOptedOutUser()) { |
| console.log('Analytics: OPTOUT!!!!!!!'); |
| await persistent_status.internal.optOut(); |
| } |
| } |
| } |
| |
| |