|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, injectable, optional } from 'inversify'; |
| 7 | +import { IApplicationShell } from '../common/application/types'; |
| 8 | +import '../common/extensions'; |
| 9 | +import { traceDecorators } from '../common/logger'; |
| 10 | +import { |
| 11 | + IBrowserService, IPersistentStateFactory, IRandom |
| 12 | +} from '../common/types'; |
| 13 | +import { Common, ExtensionSurveyBanner, LanguageService } from '../common/utils/localize'; |
| 14 | +import { sendTelemetryEvent } from '../telemetry'; |
| 15 | +import { EventName } from '../telemetry/constants'; |
| 16 | +import { IExtensionSingleActivationService } from './types'; |
| 17 | + |
| 18 | +// persistent state names, exported to make use of in testing |
| 19 | +export enum extensionSurveyStateKeys { |
| 20 | + doNotShowAgain = 'doNotShowExtensionSurveyAgain', |
| 21 | + disableSurveyForTime = 'doNotShowExtensionSurveyUntilTime' |
| 22 | +} |
| 23 | + |
| 24 | +const timeToDisableSurveyFor = 1000 * 60 * 60 * 24 * 7 * 12; // 12 weeks |
| 25 | +const WAIT_TIME_TO_SHOW_SURVEY = 1000 * 60 * 60 * 3; // 3 hours |
| 26 | + |
| 27 | +@injectable() |
| 28 | +export class ExtensionSurveyPrompt implements IExtensionSingleActivationService { |
| 29 | + constructor( |
| 30 | + @inject(IApplicationShell) private appShell: IApplicationShell, |
| 31 | + @inject(IBrowserService) private browserService: IBrowserService, |
| 32 | + @inject(IPersistentStateFactory) private persistentState: IPersistentStateFactory, |
| 33 | + @inject(IRandom) private random: IRandom, |
| 34 | + @optional() private sampleSizePerOneHundredUsers: number = 10, |
| 35 | + @optional() private waitTimeToShowSurvey: number = WAIT_TIME_TO_SHOW_SURVEY) { } |
| 36 | + |
| 37 | + public async activate(): Promise<void> { |
| 38 | + const show = this.shouldShowBanner(); |
| 39 | + if (!show) { |
| 40 | + return; |
| 41 | + } |
| 42 | + setTimeout(() => this.showSurvey().ignoreErrors(), this.waitTimeToShowSurvey); |
| 43 | + } |
| 44 | + |
| 45 | + @traceDecorators.error('Failed to check whether to display prompt for extension survey') |
| 46 | + public shouldShowBanner(): boolean { |
| 47 | + const doNotShowSurveyAgain = this.persistentState.createGlobalPersistentState(extensionSurveyStateKeys.doNotShowAgain, false); |
| 48 | + if (doNotShowSurveyAgain.value) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + const isSurveyDisabledForTimeState = this.persistentState.createGlobalPersistentState(extensionSurveyStateKeys.disableSurveyForTime, false, timeToDisableSurveyFor); |
| 52 | + if (isSurveyDisabledForTimeState.value) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + // we only want 10% of folks to see this survey. |
| 56 | + const randomSample: number = this.random.getRandomInt(0, 100); |
| 57 | + if (randomSample >= this.sampleSizePerOneHundredUsers) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + @traceDecorators.error('Failed to display prompt for extension survey') |
| 64 | + public async showSurvey() { |
| 65 | + const prompts = [LanguageService.bannerLabelYes(), ExtensionSurveyBanner.maybeLater(), Common.doNotShowAgain()]; |
| 66 | + const telemetrySelections: ['Yes', 'Maybe later', 'Do not show again'] = ['Yes', 'Maybe later', 'Do not show again']; |
| 67 | + const selection = await this.appShell.showInformationMessage(ExtensionSurveyBanner.bannerMessage(), ...prompts); |
| 68 | + sendTelemetryEvent(EventName.EXTENSION_SURVEY_PROMPT, undefined, { selection: selection ? telemetrySelections[prompts.indexOf(selection)] : undefined }); |
| 69 | + if (!selection) { |
| 70 | + return; |
| 71 | + } |
| 72 | + if (selection === LanguageService.bannerLabelYes()) { |
| 73 | + this.launchSurvey(); |
| 74 | + // Disable survey for a few weeks |
| 75 | + await this.persistentState.createGlobalPersistentState(extensionSurveyStateKeys.disableSurveyForTime, false, timeToDisableSurveyFor).updateValue(true); |
| 76 | + } else if (selection === Common.doNotShowAgain()) { |
| 77 | + // Never show the survey again |
| 78 | + await this.persistentState.createGlobalPersistentState(extensionSurveyStateKeys.doNotShowAgain, false).updateValue(true); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private launchSurvey() { |
| 83 | + this.browserService.launch('https://aka.ms/AA5rjx5'); |
| 84 | + } |
| 85 | +} |
0 commit comments