-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathanalyticsService.ts
114 lines (93 loc) · 4.27 KB
/
analyticsService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import * as os from 'os';
import { Version } from '../common/version';
import { GUAService } from './guaService';
import { AnalyticsBaseInfo, OperatingSystem } from './analyticsBaseInfo';
import { Services } from '../services/extensionHostServices';
import * as utils from '../common/utilities';
import * as vscode from 'vscode';
import * as uuid from "uuid";
export class AnalyticsService {
private static HAS_ANALYTICS_PROMPT_SHOWN_KEY = "nativescript.hasAnalyticsPromptShown";
private static CLIENT_ID_KEY = "nativescript.analyticsClientId";
private static ANALYTICS_PROMPT_MESSAGE = "Help improve NativeScript Extension by allowing Progress to collect data usage. " +
"Read our [privacy statement](https://www.telerik.com/about/privacy-policy) and " +
"learn how to [disable analytics settings.](https://github.com/NativeScript/nativescript-vscode-extension/blob/master/README.md#how-to-disable-the-analytics). " +
"Do you want to enable analytics?";
private static ANALYTICS_PROMPT_ACCEPT_ACTION = "Yes";
private static ANALYTICS_PROMPT_DENY_ACTION = "No";
private _globalState: vscode.Memento;
private _baseInfo: AnalyticsBaseInfo;
private _gua: GUAService;
private _analyticsEnabled: boolean;
constructor(globalState: vscode.Memento) {
this._globalState = globalState;
vscode.workspace.onDidChangeConfiguration(() => this.updateAnalyticsEnabled());
this._baseInfo = {
cliVersion: Services.cli().version.toString(),
extensionVersion: utils.getInstalledExtensionVersion().toString(),
operatingSystem: AnalyticsService.getOperatingSystem(),
clientId: this.getOrGenerateClientId()
};
}
public launchDebugger(request: string, platform: string): Promise<any> {
if(this._analyticsEnabled) {
try {
return this._gua.launchDebugger(request, platform);
} catch(e) {}
}
return Promise.resolve();
}
public runRunCommand(platform: string): Promise<any> {
if(this._analyticsEnabled) {
try {
return this._gua.runRunCommand(platform);
} catch(e) { }
}
return Promise.resolve();
}
private static getOperatingSystem() : OperatingSystem {
switch(process.platform) {
case 'win32':
return OperatingSystem.Windows;
case 'darwin':
return OperatingSystem.OSX;
case 'linux':
case 'freebsd':
return OperatingSystem.Linux;
default:
return OperatingSystem.Other;
};
}
public initialize() : void {
const hasAnalyticsPromptShown = this._globalState.get<boolean>(AnalyticsService.HAS_ANALYTICS_PROMPT_SHOWN_KEY);
if(!hasAnalyticsPromptShown) {
vscode.window.showInformationMessage(AnalyticsService.ANALYTICS_PROMPT_MESSAGE,
AnalyticsService.ANALYTICS_PROMPT_ACCEPT_ACTION,
AnalyticsService.ANALYTICS_PROMPT_DENY_ACTION
)
.then(result => this.onAnalyticsMessageConfirmation(result));
return;
}
this.updateAnalyticsEnabled();
}
private getOrGenerateClientId(): string {
let clientId = this._globalState.get<string>(AnalyticsService.CLIENT_ID_KEY);
if(!clientId) {
clientId = uuid.v4();
this._globalState.update(AnalyticsService.CLIENT_ID_KEY, clientId);
}
return clientId;
}
private onAnalyticsMessageConfirmation(result: string) : void {
const shouldEnableAnalytics = result === AnalyticsService.ANALYTICS_PROMPT_ACCEPT_ACTION ? true : false;
this._globalState.update(AnalyticsService.HAS_ANALYTICS_PROMPT_SHOWN_KEY, true);
Services.workspaceConfigService().isAnalyticsEnabled = shouldEnableAnalytics;
this.updateAnalyticsEnabled();
}
private updateAnalyticsEnabled() {
this._analyticsEnabled = Services.workspaceConfigService().isAnalyticsEnabled;
if(this._analyticsEnabled && !this._gua) {
this._gua = new GUAService('UA-111455-29', this._baseInfo);
}
}
}