@@ -5,47 +5,34 @@ import { AnalyticsBaseInfo, OperatingSystem } from './analyticsBaseInfo';
55import { Services } from '../services/extensionHostServices' ;
66import * as utils from '../common/utilities' ;
77import * as vscode from 'vscode' ;
8+ import * as uuid from "uuid" ;
89
910export class AnalyticsService {
10- private static HAS_ANALYTICS_PROMPT_SHOWN = "nativescript.hasAnalyticsPromptShown" ;
11- private static ANALYTICS_PROMPT_MESSAGE = "Help improve NativeScript Extension by allowing Progress to collect data usage." ;
12- private static ANALYTICS_PROMPT_ACTION = "Track" ;
11+ private static HAS_ANALYTICS_PROMPT_SHOWN_KEY = "nativescript.hasAnalyticsPromptShown" ;
12+ private static CLIENT_ID_KEY = "nativescript.analyticsClientId" ;
13+ private static ANALYTICS_PROMPT_MESSAGE = "Help improve NativeScript Extension by allowing Progress to collect data usage. " +
14+ "Read our [privacy statement](https://www.telerik.com/about/privacy-policy) and " +
15+ "learn how to [disable analytics settings.](https://github.com/NativeScript/nativescript-vscode-extension/blob/master/README.md#how-to-disable-the-analytics). " +
16+ "Do you want to enable analytics?" ;
17+ private static ANALYTICS_PROMPT_ACCEPT_ACTION = "Yes" ;
18+ private static ANALYTICS_PROMPT_DENY_ACTION = "No" ;
1319
1420 private _globalState : vscode . Memento ;
1521 private _baseInfo : AnalyticsBaseInfo ;
1622 private _gua : GUAService ;
1723 private _analyticsEnabled : boolean ;
18- private disposables : vscode . Disposable [ ] = [ ] ;
19-
20- public static generateMachineId ( ) : string {
21- let machineId = '' ;
22- try {
23- let netInterfaces = os . networkInterfaces ( ) ;
24- Object . keys ( netInterfaces ) . forEach ( interfName => {
25- netInterfaces [ interfName ] . forEach ( interf => {
26- if ( ! interf . internal ) {
27- machineId += `${ interf . mac } -` ;
28- }
29- } ) ;
30- } ) ;
31- } catch ( e ) { }
32- return machineId ;
33- }
3424
3525 constructor ( globalState : vscode . Memento ) {
3626 this . _globalState = globalState ;
3727
38- //TODO: Dispose
3928 vscode . workspace . onDidChangeConfiguration ( ( ) => this . updateAnalyticsEnabled ( ) ) ;
4029
4130 this . _baseInfo = {
4231 cliVersion : Services . cli ( ) . version . toString ( ) ,
4332 extensionVersion : utils . getInstalledExtensionVersion ( ) . toString ( ) ,
4433 operatingSystem : AnalyticsService . getOperatingSystem ( ) ,
45- userId : AnalyticsService . generateMachineId ( )
34+ clientId : this . getOrGenerateClientId ( )
4635 } ;
47-
48- this . initializeAnalytics ( ) ;
4936 }
5037
5138 public launchDebugger ( request : string , platform : string ) : Promise < any > {
@@ -82,10 +69,13 @@ export class AnalyticsService {
8269 } ;
8370 }
8471
85- private initializeAnalytics ( ) : void {
86- const hasAnalyticsPromptShown = this . _globalState . get ( AnalyticsService . HAS_ANALYTICS_PROMPT_SHOWN ) ;
87- if ( hasAnalyticsPromptShown ) {
88- vscode . window . showInformationMessage ( AnalyticsService . ANALYTICS_PROMPT_MESSAGE , AnalyticsService . ANALYTICS_PROMPT_ACTION )
72+ public initialize ( ) : void {
73+ const hasAnalyticsPromptShown = this . _globalState . get < boolean > ( AnalyticsService . HAS_ANALYTICS_PROMPT_SHOWN_KEY ) ;
74+ if ( ! hasAnalyticsPromptShown ) {
75+ vscode . window . showInformationMessage ( AnalyticsService . ANALYTICS_PROMPT_MESSAGE ,
76+ AnalyticsService . ANALYTICS_PROMPT_ACCEPT_ACTION ,
77+ AnalyticsService . ANALYTICS_PROMPT_DENY_ACTION
78+ )
8979 . then ( result => this . onAnalyticsMessageConfirmation ( result ) ) ;
9080
9181 return ;
@@ -94,19 +84,31 @@ export class AnalyticsService {
9484 this . updateAnalyticsEnabled ( ) ;
9585 }
9686
87+ private getOrGenerateClientId ( ) : string {
88+ let clientId = this . _globalState . get < string > ( AnalyticsService . CLIENT_ID_KEY ) ;
89+
90+ if ( ! clientId ) {
91+ clientId = uuid . v4 ( ) ;
92+ this . _globalState . update ( AnalyticsService . CLIENT_ID_KEY , clientId ) ;
93+ }
94+
95+ return clientId ;
96+ }
97+
9798 private onAnalyticsMessageConfirmation ( result : string ) : void {
98- const shouldEnableAnalytics = result === AnalyticsService . ANALYTICS_PROMPT_ACTION ? true : false ;
99+ const shouldEnableAnalytics = result === AnalyticsService . ANALYTICS_PROMPT_ACCEPT_ACTION ? true : false ;
100+
101+ this . _globalState . update ( AnalyticsService . HAS_ANALYTICS_PROMPT_SHOWN_KEY , true ) ;
99102
100103 Services . workspaceConfigService ( ) . isAnalyticsEnabled = shouldEnableAnalytics ;
101104 this . updateAnalyticsEnabled ( ) ;
102- this . _globalState . update ( AnalyticsService . HAS_ANALYTICS_PROMPT_SHOWN , true ) ;
103105 }
104106
105107 private updateAnalyticsEnabled ( ) {
106108 this . _analyticsEnabled = Services . workspaceConfigService ( ) . isAnalyticsEnabled ;
107109
108- if ( this . _analyticsEnabled ) {
109- this . _gua = this . _gua || new GUAService ( 'UA-111455-29' , this . _baseInfo ) ;
110+ if ( this . _analyticsEnabled && ! this . _gua ) {
111+ this . _gua = new GUAService ( 'UA-111455-29' , this . _baseInfo ) ;
110112 }
111113 }
112114}
0 commit comments