UNPKG

8.43 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.AnalyticsCollector = void 0;
11const core_1 = require("@angular-devkit/core");
12const child_process_1 = require("child_process");
13const debug = require("debug");
14const https = require("https");
15const os = require("os");
16const querystring = require("querystring");
17const version_1 = require("./version");
18/**
19 * See: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
20 */
21class AnalyticsCollector {
22 constructor(trackingId, userId) {
23 this.trackingEventsQueue = [];
24 this.parameters = {};
25 this.analyticsLogDebug = debug('ng:analytics:log');
26 // API Version
27 this.parameters['v'] = '1';
28 // User ID
29 this.parameters['cid'] = userId;
30 // Tracking
31 this.parameters['tid'] = trackingId;
32 this.parameters['ds'] = 'cli';
33 this.parameters['ua'] = _buildUserAgentString();
34 this.parameters['ul'] = _getLanguage();
35 // @angular/cli with version.
36 this.parameters['an'] = '@angular/cli';
37 this.parameters['av'] = version_1.VERSION.full;
38 // We use the application ID for the Node version. This should be "node v12.10.0".
39 const nodeVersion = `node ${process.version}`;
40 this.parameters['aid'] = nodeVersion;
41 // Custom dimentions
42 // We set custom metrics for values we care about.
43 this.parameters['cd' + core_1.analytics.NgCliAnalyticsDimensions.CpuCount] = os.cpus().length;
44 // Get the first CPU's speed. It's very rare to have multiple CPUs of different speed (in most
45 // non-ARM configurations anyway), so that's all we care about.
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 // The below is needed so that if flush is called multiple times,
73 // we don't report the same event multiple times.
74 const pendingTrackingEvents = this.trackingEventsQueue;
75 this.trackingEventsQueue = [];
76 try {
77 await this.send(pendingTrackingEvents);
78 }
79 catch (error) {
80 // Failure to report analytics shouldn't crash the CLI.
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 * Creates the dimension and metrics variables to add to the queue.
116 * @private
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}
126exports.AnalyticsCollector = AnalyticsCollector;
127// These are just approximations of UA strings. We just try to fool Google Analytics to give us the
128// data we want.
129// See https://developers.whatismybrowser.com/useragents/
130const 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 // We stop here because we try to math out the version for anything greater than 10, and it
147 // works. Those versions are standardized using a calculation now.
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 * Build a fake User Agent string. This gets sent to Analytics so it shows the proper OS version.
161 * @private
162 */
163function _buildUserAgentString() {
164 switch (os.platform()) {
165 case 'darwin': {
166 let v = osVersionMap.darwin[os.release()];
167 if (!v) {
168 // Remove 4 to tie Darwin version to OSX version, add other info.
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 * Get a language code.
188 * @private
189 */
190function _getLanguage() {
191 // Note: Windows does not expose the configured language by default.
192 return (process.env.LANG || // Default Unix env variable.
193 process.env.LC_CTYPE || // For C libraries. Sometimes the above isn't set.
194 process.env.LANGSPEC || // For Windows, sometimes this will be set (not always).
195 _getWindowsLanguageCode() ||
196 '??'); // ¯\_(ツ)_/¯
197}
198/**
199 * Attempt to get the Windows Language Code string.
200 * @private
201 */
202function _getWindowsLanguageCode() {
203 if (!os.platform().startsWith('win')) {
204 return undefined;
205 }
206 try {
207 // This is true on Windows XP, 7, 8 and 10 AFAIK. Would return empty string or fail if it
208 // doesn't work.
209 return child_process_1.execSync('wmic.exe os get locale').toString().trim();
210 }
211 catch { }
212 return undefined;
213}