@@ -5,12 +5,19 @@ import { TelerikAnalyticsService } from './telerikAnalyticsService';
55import { AnalyticsBaseInfo , OperatingSystem } from './analyticsBaseInfo' ;
66import { Services } from '../services/extensionHostServices' ;
77import * as utils from '../common/utilities' ;
8+ import * as vscode from 'vscode' ;
89
910export class AnalyticsService {
11+ private static HAS_ANALYTICS_PROMPT_SHOWN = "nativescript.hasAnalyticsPromptShown" ;
12+ private static ANALYTICS_PROMPT_MESSAGE = "Help improve NativeScript Extension by allowing Progress to collect data usage." ;
13+ private static ANALYTICS_PROMPT_ACTION = "Track" ;
14+
15+ private _globalState : vscode . Memento ;
1016 private _baseInfo : AnalyticsBaseInfo ;
1117 private _gua : GUAService ;
1218 private _ta : TelerikAnalyticsService ;
1319 private _analyticsEnabled : boolean ;
20+ private disposables : vscode . Disposable [ ] = [ ] ;
1421
1522 public static generateMachineId ( ) : string {
1623 let machineId = '' ;
@@ -27,27 +34,20 @@ export class AnalyticsService {
2734 return machineId ;
2835 }
2936
30- constructor ( ) {
31- this . _analyticsEnabled = Services . workspaceConfigService ( ) . isAnalyticsEnabled ;
32- let operatingSystem = OperatingSystem . Other ;
33- switch ( process . platform ) {
34- case 'win32' : { operatingSystem = OperatingSystem . Windows ; break ; }
35- case 'darwin' : { operatingSystem = OperatingSystem . OSX ; break ; }
36- case 'linux' :
37- case 'freebsd' : { operatingSystem = OperatingSystem . Linux ; break ; }
38- } ;
37+ constructor ( globalState : vscode . Memento ) {
38+ this . _globalState = globalState ;
39+
40+ //TODO: Dispose
41+ vscode . workspace . onDidChangeConfiguration ( ( ) => this . updateAnalyticsEnabled ( ) ) ;
3942
4043 this . _baseInfo = {
4144 cliVersion : Services . cli ( ) . version . toString ( ) ,
4245 extensionVersion : utils . getInstalledExtensionVersion ( ) . toString ( ) ,
43- operatingSystem : operatingSystem ,
46+ operatingSystem : AnalyticsService . getOperatingSystem ( ) ,
4447 userId : AnalyticsService . generateMachineId ( )
4548 } ;
4649
47- if ( this . _analyticsEnabled ) {
48- this . _gua = new GUAService ( 'UA-111455-29' , this . _baseInfo ) ;
49- this . _ta = new TelerikAnalyticsService ( 'b8b2e51f188f43e9b0dfb899f7b71cc6' , this . _baseInfo ) ;
50- }
50+ this . initializeAnalytics ( ) ;
5151 }
5252
5353 public launchDebugger ( request : string , platform : string ) : Promise < any > {
@@ -73,4 +73,47 @@ export class AnalyticsService {
7373 }
7474 return Promise . resolve ( ) ;
7575 }
76+
77+ private static getOperatingSystem ( ) : OperatingSystem {
78+ switch ( process . platform ) {
79+ case 'win32' :
80+ return OperatingSystem . Windows ;
81+ case 'darwin' :
82+ return OperatingSystem . OSX ;
83+ case 'linux' :
84+ case 'freebsd' :
85+ return OperatingSystem . Linux ;
86+ default :
87+ return OperatingSystem . Other ;
88+ } ;
89+ }
90+
91+ private initializeAnalytics ( ) : void {
92+ const hasAnalyticsPromptShown = this . _globalState . get ( AnalyticsService . HAS_ANALYTICS_PROMPT_SHOWN ) ;
93+ if ( hasAnalyticsPromptShown ) {
94+ vscode . window . showInformationMessage ( AnalyticsService . ANALYTICS_PROMPT_MESSAGE , AnalyticsService . ANALYTICS_PROMPT_ACTION )
95+ . then ( result => this . onAnalyticsMessageConfirmation ( result ) ) ;
96+
97+ return ;
98+ }
99+
100+ this . updateAnalyticsEnabled ( ) ;
101+ }
102+
103+ private onAnalyticsMessageConfirmation ( result : string ) : void {
104+ const shouldEnableAnalytics = result === AnalyticsService . ANALYTICS_PROMPT_ACTION ? true : false ;
105+
106+ Services . workspaceConfigService ( ) . isAnalyticsEnabled = shouldEnableAnalytics ;
107+ this . updateAnalyticsEnabled ( ) ;
108+ this . _globalState . update ( AnalyticsService . HAS_ANALYTICS_PROMPT_SHOWN , true ) ;
109+ }
110+
111+ private updateAnalyticsEnabled ( ) {
112+ this . _analyticsEnabled = Services . workspaceConfigService ( ) . isAnalyticsEnabled ;
113+
114+ if ( this . _analyticsEnabled ) {
115+ this . _gua = this . _gua || new GUAService ( 'UA-111455-29' , this . _baseInfo ) ;
116+ this . _ta = this . _ta || new TelerikAnalyticsService ( 'b8b2e51f188f43e9b0dfb899f7b71cc6' , this . _baseInfo ) ;
117+ }
118+ }
76119}
0 commit comments