-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGoogleAnalytics.ts
More file actions
257 lines (231 loc) · 8.12 KB
/
GoogleAnalytics.ts
File metadata and controls
257 lines (231 loc) · 8.12 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { createHash} from "crypto";
import * as fs from "fs";
import * as path from "path";
import { GoogleAnalyticsParameters } from "../types";
import { App } from "./App";
import { ProjectConfig } from "./ProjectConfig";
import { Util } from "./Util";
class GoogleAnalytics {
protected static userDataFolder: string = process.env.APPDATA ||
(process.platform === "darwin" ? "/Users/Shared" : process.env.HOME + "/.npm/");
protected static appFolder = "igniteui-cli";
protected static userSettings: string = "user-settings.json";
protected static appVersion: string;
protected static npmVersion: string;
// GA4 Measurement ID and API Secret
// To obtain these credentials:
// 1. Go to Google Analytics Admin panel (https://analytics.google.com/analytics/web/)
// 2. Navigate to Admin > Data Streams > Select your stream
// 3. Measurement ID is shown at the top (format: G-XXXXXXXXXX)
// 4. For API Secret: Scroll down to "Measurement Protocol API secrets" > "Create" to generate a new secret
//
// These can be overridden via environment variables:
// - IGNITEUI_CLI_GA4_MEASUREMENT_ID
// - IGNITEUI_CLI_GA4_API_SECRET
//
// NOTE: If environment variables are not set and placeholder values remain, analytics will silently fail.
// This matches the current behavior where invalid credentials result in silent failures.
protected static measurementID = process.env.IGNITEUI_CLI_GA4_MEASUREMENT_ID || "G-XXXXXXXXXX"; // TODO: Replace default with actual GA4 Measurement ID
protected static apiSecret = process.env.IGNITEUI_CLI_GA4_API_SECRET || "XXXXXXXXXXXXXXXXXXXXXXXX"; // TODO: Replace default with actual GA4 API Secret
// GA4 requires engagement_time_msec to be set for events to be processed
// Using a minimal value to satisfy the requirement
protected static readonly GA4_MIN_ENGAGEMENT_TIME = "100";
/**
* Generates http post request with provided parameters and sends it to GA4
* @param parameters Object containing all the parameters to send
*/
public static post(parameters: GoogleAnalyticsParameters) {
const config = ProjectConfig.getConfig();
if (config.disableAnalytics) {
return;
}
// Get application version if not set
if (!this.appVersion) {
this.appVersion = Util.version();
}
// Get user agent info
const nodeVersion = process.version;
const os = this.getOsForUserAgent();
const npmVersion = this.getNpmVersion();
const userAgent = `node/${nodeVersion} (${os}) npm/${npmVersion}`;
// Get client ID (user ID)
const clientId = this.getUUID();
// Build GA4 event payload
const eventName = this.mapHitTypeToEventName(parameters.t || "event");
const eventParams: any = {
app_name: App.appName,
app_version: parameters.av || this.appVersion,
engagement_time_msec: this.GA4_MIN_ENGAGEMENT_TIME
};
// Map custom dimensions to event parameters
if (parameters.cd) eventParams.screen_name = parameters.cd;
if (parameters.ec) eventParams.event_category = parameters.ec;
if (parameters.ea) eventParams.event_action = parameters.ea;
if (parameters.el) eventParams.event_label = parameters.el;
if (parameters.exd) eventParams.exception_description = parameters.exd;
// Map custom dimensions
if (parameters.cd1) eventParams.framework = parameters.cd1;
if (parameters.cd2) eventParams.project_type = parameters.cd2;
if (parameters.cd3) eventParams.project_name = parameters.cd3;
if (parameters.cd4) eventParams.action = parameters.cd4;
if (parameters.cd5) eventParams.component_group = parameters.cd5;
if (parameters.cd6) eventParams.component_name = parameters.cd6;
if (parameters.cd7) eventParams.template_name = parameters.cd7;
if (parameters.cd8) eventParams.custom_view_name = parameters.cd8;
if (parameters.cd9) eventParams.extra_config = parameters.cd9;
if (parameters.cd10 !== undefined) eventParams.skip_config = parameters.cd10;
if (parameters.cd11 !== undefined) eventParams.skip_git = parameters.cd11;
if (parameters.cd12 !== undefined) eventParams.global = parameters.cd12;
if (parameters.cd13) eventParams.search_term = parameters.cd13;
if (parameters.cd14) eventParams.theme = parameters.cd14;
// Build GA4 request payload
const payload = {
client_id: clientId,
user_properties: {
user_agent: {
value: userAgent
}
},
events: [{
name: eventName,
params: eventParams
}]
};
const payloadString = JSON.stringify(payload);
const fullPath = `/mp/collect?measurement_id=${this.measurementID}&api_secret=${this.apiSecret}`;
const options = {
host: "www.google-analytics.com",
path: fullPath,
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(payloadString)
}
};
const https = require("https");
const req = https.request(options);
req.on("error", e => {
// Silently fail analytics errors to avoid disrupting CLI functionality
// In the future, this could log to a file or queue for retry
// Error details: e.message, e.code
});
req.write(payloadString);
req.end();
}
/**
* Maps Universal Analytics hit types to GA4 event names
*/
protected static mapHitTypeToEventName(hitType: string): string {
switch (hitType) {
case "screenview":
return "screen_view";
case "event":
return "cli_event";
case "exception":
return "exception";
default:
return "cli_event";
}
}
protected static getUUID(): string {
const absolutePath = path.join(this.userDataFolder, this.appFolder, this.userSettings);
let UUID = "";
if (fs.existsSync(absolutePath)) {
UUID = require(absolutePath).UUID;
} else {
const dirName = path.dirname(absolutePath);
if (!fs.existsSync(dirName)) {
fs.mkdirSync(dirName, { recursive: true });
}
UUID = this.getUserID();
fs.writeFileSync(absolutePath, JSON.stringify({ UUID }));
}
return UUID;
}
protected static getUserID(): string {
const platform = process.platform;
let result: string = "";
try {
result = this.getMachineID(platform, result);
} catch {
result = this.createRandomGuid()
.replace(/\r+|\n+|\s+/ig, "")
.toLowerCase();
}
result = createHash("sha256").update(result).digest("hex");
return result;
}
protected static getMachineID(platform: string, result: string) {
switch (platform) {
case "darwin":
result = Util.execSync("ioreg -rd1 -c IOPlatformExpertDevice").toString()
.split("IOPlatformUUID")[1]
.split("\n")[0].replace(/\=|\s+|\"/ig, "")
.toLowerCase();
break;
case "win32":
result = Util.execSync("REG QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid")
.toString()
.split("REG_SZ")[1]
.replace(/\r+|\n+|\s+/ig, "")
.toLowerCase();
break;
case "linux":
result =
Util.execSync("( cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname ) | head -n 1 || :")
.toString()
.replace(/\r+|\n+|\s+/ig, "")
.toLowerCase();
break;
case "freebsd":
result = Util.execSync("kenv -q smbios.system.uuid").toString()
.replace(/\r+|\n+|\s+/ig, "")
.toLowerCase();
break;
}
return result;
}
protected static createRandomGuid(): string {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
const currentDate = new Date();
const currentTime = currentDate.getTime().toString();
return currentTime + s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
}
protected static getOsForUserAgent(): string {
const platform = process.platform;
switch (platform) {
case "darwin":
return "Mac OS";
case "win32":
return "Windows NT";
case "linux":
return "Linux";
case "freebsd":
return "OpenBSD";
default:
return "";
}
}
protected static getNpmVersion(): string {
if (!this.npmVersion) {
this.npmVersion = "";
const buffer = Util.execSync("npm -v");
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < buffer.length; i += 1) {
this.npmVersion += String.fromCharCode(+buffer[i]).toString();
}
}
return this.npmVersion.trim();
}
}
export { GoogleAnalytics };
process.on("uncaughtException", err => {
GoogleAnalytics.post({
t: "exception",
exd: err.message
});
});