forked from dataform-co/dataform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics.ts
76 lines (70 loc) · 1.99 KB
/
analytics.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
import Analytics from "analytics-node";
import { getConfigSettings, getConfigSettingsPath, upsertConfigSettings } from "df/cli/config";
import { ynQuestion } from "df/cli/console";
import { v4 as uuidv4 } from "uuid";
const analytics = new Analytics("eR24ln3MniE3TKZXkvAkOGkiSN02xXqw");
let currentCommand: string;
export async function maybeConfigureAnalytics() {
const settings = await getConfigSettings();
// We should only ask if users want to track analytics if they are in an interactive terminal;
if (!process.stdout.isTTY) {
return;
}
if (settings.allowAnonymousAnalytics !== undefined) {
return;
}
const optInResponse = ynQuestion(
`
To help improve the quality of our products, we collect anonymized usage data and anonymized stacktraces when crashes are encountered.
This can be changed at any point by modifying your settings file: ${getConfigSettingsPath()}
Would you like to opt-in to anonymous usage and error tracking?`,
false
);
await upsertConfigSettings({
allowAnonymousAnalytics: optInResponse,
anonymousUserId: uuidv4()
});
}
export async function trackCommand(command: string) {
currentCommand = command;
const config = await getConfigSettings();
if (!config.allowAnonymousAnalytics) {
return;
}
await new Promise(resolve => {
analytics.track(
{
userId: config.anonymousUserId,
event: "event_dataform_cli_command",
properties: {
command
}
},
() => {
resolve();
// Silently fail on tracking errors.
}
);
});
}
export async function trackError() {
const config = await getConfigSettings();
if (!config.allowAnonymousAnalytics) {
return;
}
await new Promise(resolve => {
analytics.track(
{
userId: config.anonymousUserId,
event: "event_dataform_cli_error",
properties: {
currentCommand
}
},
() => {
resolve();
// Silently fail on tracking errors.
}
);
});
}