1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.AnalyticsCollector = void 0;
|
11 | const core_1 = require("@angular-devkit/core");
|
12 | const child_process_1 = require("child_process");
|
13 | const debug = require("debug");
|
14 | const https = require("https");
|
15 | const os = require("os");
|
16 | const querystring = require("querystring");
|
17 | const version_1 = require("./version");
|
18 |
|
19 |
|
20 |
|
21 | class AnalyticsCollector {
|
22 | constructor(trackingId, userId) {
|
23 | this.trackingEventsQueue = [];
|
24 | this.parameters = {};
|
25 | this.analyticsLogDebug = debug('ng:analytics:log');
|
26 |
|
27 | this.parameters['v'] = '1';
|
28 |
|
29 | this.parameters['cid'] = userId;
|
30 |
|
31 | this.parameters['tid'] = trackingId;
|
32 | this.parameters['ds'] = 'cli';
|
33 | this.parameters['ua'] = _buildUserAgentString();
|
34 | this.parameters['ul'] = _getLanguage();
|
35 |
|
36 | this.parameters['an'] = '@angular/cli';
|
37 | this.parameters['av'] = version_1.VERSION.full;
|
38 |
|
39 | const nodeVersion = `node ${process.version}`;
|
40 | this.parameters['aid'] = nodeVersion;
|
41 |
|
42 |
|
43 | this.parameters['cd' + core_1.analytics.NgCliAnalyticsDimensions.CpuCount] = os.cpus().length;
|
44 |
|
45 |
|
46 | this.parameters['cd' + core_1.analytics.NgCliAnalyticsDimensions.CpuSpeed] = Math.floor(os.cpus()[0].speed);
|
47 | this.parameters['cd' + core_1.analytics.NgCliAnalyticsDimensions.RamInGigabytes] = Math.round(os.totalmem() / (1024 * 1024 * 1024));
|
48 | this.parameters['cd' + core_1.analytics.NgCliAnalyticsDimensions.NodeVersion] = nodeVersion;
|
49 | }
|
50 | event(ec, ea, options = {}) {
|
51 | const { label: el, value: ev, metrics, dimensions } = options;
|
52 | this.addToQueue('event', { ec, ea, el, ev, metrics, dimensions });
|
53 | }
|
54 | pageview(dp, options = {}) {
|
55 | const { hostname: dh, title: dt, metrics, dimensions } = options;
|
56 | this.addToQueue('pageview', { dp, dh, dt, metrics, dimensions });
|
57 | }
|
58 | timing(utc, utv, utt, options = {}) {
|
59 | const { label: utl, metrics, dimensions } = options;
|
60 | this.addToQueue('timing', { utc, utv, utt, utl, metrics, dimensions });
|
61 | }
|
62 | screenview(cd, an, options = {}) {
|
63 | const { appVersion: av, appId: aid, appInstallerId: aiid, metrics, dimensions } = options;
|
64 | this.addToQueue('screenview', { cd, an, av, aid, aiid, metrics, dimensions });
|
65 | }
|
66 | async flush() {
|
67 | const pending = this.trackingEventsQueue.length;
|
68 | this.analyticsLogDebug(`flush queue size: ${pending}`);
|
69 | if (!pending) {
|
70 | return;
|
71 | }
|
72 |
|
73 |
|
74 | const pendingTrackingEvents = this.trackingEventsQueue;
|
75 | this.trackingEventsQueue = [];
|
76 | try {
|
77 | await this.send(pendingTrackingEvents);
|
78 | }
|
79 | catch (error) {
|
80 |
|
81 | this.analyticsLogDebug('send error: %j', error);
|
82 | }
|
83 | }
|
84 | addToQueue(eventType, parameters) {
|
85 | const { metrics, dimensions, ...restParameters } = parameters;
|
86 | const data = {
|
87 | ...this.parameters,
|
88 | ...restParameters,
|
89 | ...this.customVariables({ metrics, dimensions }),
|
90 | t: eventType,
|
91 | };
|
92 | this.analyticsLogDebug('add event to queue: %j', data);
|
93 | this.trackingEventsQueue.push(data);
|
94 | }
|
95 | async send(data) {
|
96 | this.analyticsLogDebug('send event: %j', data);
|
97 | return new Promise((resolve, reject) => {
|
98 | const request = https.request({
|
99 | host: 'www.google-analytics.com',
|
100 | method: 'POST',
|
101 | path: data.length > 1 ? '/batch' : '/collect',
|
102 | }, (response) => {
|
103 | if (response.statusCode !== 200) {
|
104 | reject(new Error(`Analytics reporting failed with status code: ${response.statusCode}.`));
|
105 | return;
|
106 | }
|
107 | });
|
108 | request.on('error', reject);
|
109 | const queryParameters = data.map((p) => querystring.stringify(p)).join('\n');
|
110 | request.write(queryParameters);
|
111 | request.end(resolve);
|
112 | });
|
113 | }
|
114 | |
115 |
|
116 |
|
117 |
|
118 | customVariables(options) {
|
119 | const additionals = {};
|
120 | const { dimensions, metrics } = options;
|
121 | dimensions === null || dimensions === void 0 ? void 0 : dimensions.forEach((v, i) => (additionals[`cd${i}`] = v));
|
122 | metrics === null || metrics === void 0 ? void 0 : metrics.forEach((v, i) => (additionals[`cm${i}`] = v));
|
123 | return additionals;
|
124 | }
|
125 | }
|
126 | exports.AnalyticsCollector = AnalyticsCollector;
|
127 |
|
128 |
|
129 |
|
130 | const osVersionMap = {
|
131 | darwin: {
|
132 | '1.3.1': '10_0_4',
|
133 | '1.4.1': '10_1_0',
|
134 | '5.1': '10_1_1',
|
135 | '5.2': '10_1_5',
|
136 | '6.0.1': '10_2',
|
137 | '6.8': '10_2_8',
|
138 | '7.0': '10_3_0',
|
139 | '7.9': '10_3_9',
|
140 | '8.0': '10_4_0',
|
141 | '8.11': '10_4_11',
|
142 | '9.0': '10_5_0',
|
143 | '9.8': '10_5_8',
|
144 | '10.0': '10_6_0',
|
145 | '10.8': '10_6_8',
|
146 |
|
147 |
|
148 | },
|
149 | win32: {
|
150 | '6.3.9600': 'Windows 8.1',
|
151 | '6.2.9200': 'Windows 8',
|
152 | '6.1.7601': 'Windows 7 SP1',
|
153 | '6.1.7600': 'Windows 7',
|
154 | '6.0.6002': 'Windows Vista SP2',
|
155 | '6.0.6000': 'Windows Vista',
|
156 | '5.1.2600': 'Windows XP',
|
157 | },
|
158 | };
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | function _buildUserAgentString() {
|
164 | switch (os.platform()) {
|
165 | case 'darwin': {
|
166 | let v = osVersionMap.darwin[os.release()];
|
167 | if (!v) {
|
168 |
|
169 | const x = parseFloat(os.release());
|
170 | if (x > 10) {
|
171 | v = `10_` + (x - 4).toString().replace('.', '_');
|
172 | }
|
173 | }
|
174 | const cpuModel = os.cpus()[0].model.match(/^[a-z]+/i);
|
175 | const cpu = cpuModel ? cpuModel[0] : os.cpus()[0].model;
|
176 | return `(Macintosh; ${cpu} Mac OS X ${v || os.release()})`;
|
177 | }
|
178 | case 'win32':
|
179 | return `(Windows NT ${os.release()})`;
|
180 | case 'linux':
|
181 | return `(X11; Linux i686; ${os.release()}; ${os.cpus()[0].model})`;
|
182 | default:
|
183 | return os.platform() + ' ' + os.release();
|
184 | }
|
185 | }
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | function _getLanguage() {
|
191 |
|
192 | return (process.env.LANG ||
|
193 | process.env.LC_CTYPE ||
|
194 | process.env.LANGSPEC ||
|
195 | _getWindowsLanguageCode() ||
|
196 | '??');
|
197 | }
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | function _getWindowsLanguageCode() {
|
203 | if (!os.platform().startsWith('win')) {
|
204 | return undefined;
|
205 | }
|
206 | try {
|
207 |
|
208 |
|
209 | return child_process_1.execSync('wmic.exe os get locale').toString().trim();
|
210 | }
|
211 | catch { }
|
212 | return undefined;
|
213 | }
|