| // 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. |
| |
| import * as vscode from 'vscode'; |
| import * as PersistentStatus from './persistent_status'; |
| import * as logger from '../logger'; |
| |
| // Analytics initializers. |
| 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; |
| } |
| |
| 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(); |
| })); |
| |
| let watcher; |
| if (AnalyticsInfo.isInternalUser) { |
| watcher = new PersistentStatus.InternalPropertyWatcher(); |
| } else { |
| watcher = new PersistentStatus.EnabledPropertyWatcher(); |
| } |
| |
| watcher.onDidChange(() => { |
| logger.debug('Analytics persistent state changed. Updating runtime state...', 'analytics'); |
| void AnalyticsState.updateOptInLevel(); |
| }); |
| context.subscriptions.push(watcher); |
| } |
| |
| /** |
| * Show an analytics consent message via the walkthrough. |
| * @param messageId - The message ID to show in the walkthrough |
| */ |
| async function showMessage(messageId: string): Promise<void> { |
| // Set the context key to control which walkthrough step is visible. |
| await vscode.commands.executeCommand('setContext', 'fuchsia.analytics.messageId', messageId); |
| |
| await vscode.commands.executeCommand( |
| 'workbench.action.openWalkthrough', |
| { |
| category: 'fuchsia-authors.vscode-fuchsia#fuchsia.welcome', |
| step: `fuchsia-authors.vscode-fuchsia#fuchsia.analytics.${messageId}`, |
| }, |
| false, // force: false |
| ); |
| } |
| |
| export const external = { |
| /** |
| * External analytics users: init as the first run of the first Fuchsia tools |
| */ |
| async initFirstRunOfFirstTool(): Promise<void> { |
| if (vscode.env.isTelemetryEnabled) { |
| await showMessage('first_run_first_tool_telemetry_on'); |
| } else { |
| await showMessage('first_run_first_tool_telemetry_off'); |
| } |
| |
| await PersistentStatus.external.enable(); |
| await PersistentStatus.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. |
| */ |
| async initFirstRunOfOtherTool(): Promise<void> { |
| if (await PersistentStatus.external.isEnabled()) { |
| if (vscode.env.isTelemetryEnabled) { |
| await showMessage('first_run_other_tool_enabled_telemetry_on'); |
| } else { |
| await showMessage('first_run_other_tool_enabled_telemetry_off'); |
| } |
| } else { |
| await showMessage('first_run_other_tool_disabled'); |
| } |
| await PersistentStatus.external.markAsDirectlyLaunched(); |
| }, |
| |
| /** |
| * Analytics initialization process specific to external users. |
| */ |
| async init(): Promise<void> { |
| if (await PersistentStatus.external.isFirstLaunchOfFirstTool()) { |
| await external.initFirstRunOfFirstTool(); |
| } else if (await PersistentStatus.external.isFirstDirectLaunch()) { |
| await external.initFirstRunOfOtherTool(); |
| } |
| }, |
| }; |
| |
| export const internal = { |
| /** |
| * Analytics initialization process specific to internal users. |
| */ |
| async init(): Promise<void> { |
| if (await PersistentStatus.internal.isNewUser() || |
| await PersistentStatus.internal.isExistingUnmigratedOptedInUser()) { |
| if (vscode.env.isTelemetryEnabled) { |
| await showMessage('internal_on'); |
| } else { |
| await showMessage('internal_off'); |
| } |
| } else if (await PersistentStatus.internal.isExistingUnmigratedOptedOutUser()) { |
| console.log('Analytics: OPTOUT!!!!!!!'); |
| await PersistentStatus.internal.optOut(); |
| } |
| }, |
| }; |