forked from BeOnAuto/auto-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.ts
More file actions
64 lines (54 loc) · 1.58 KB
/
analytics.ts
File metadata and controls
64 lines (54 loc) · 1.58 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
import { Config } from './config.js';
import { createOutput } from './terminal.js';
export interface AnalyticsData {
command: string;
timestamp: number;
version: string;
nodeVersion: string;
platform: string;
success: boolean;
errorCode?: string;
}
export class Analytics {
private config: Config;
private output: ReturnType<typeof createOutput>;
private optedIn: boolean | null = null;
constructor(config: Config) {
this.config = config;
this.output = createOutput(config);
}
async promptForConsent(): Promise<boolean> {
if (this.optedIn !== null) {
return this.optedIn;
}
const isDisabled = process.env.AUTO_ENGINEER_ANALYTICS === 'false';
this.optedIn = !isDisabled;
return this.optedIn;
}
private prepareAnalyticsData(
data: Omit<AnalyticsData, 'timestamp' | 'version' | 'nodeVersion' | 'platform'>,
): AnalyticsData {
return {
...data,
timestamp: Date.now(),
version: process.env.npm_package_version ?? '0.1.2',
nodeVersion: process.version,
platform: process.platform,
};
}
async track(data: Omit<AnalyticsData, 'timestamp' | 'version' | 'nodeVersion' | 'platform'>): Promise<void> {
const consented = await this.promptForConsent();
if (!consented) {
return;
}
const analyticsData = this.prepareAnalyticsData(data);
this.output.debug(`Analytics: ${JSON.stringify(analyticsData)}`);
}
async trackCommand(command: string, success: boolean, errorCode?: string): Promise<void> {
await this.track({
command,
success,
errorCode,
});
}
}