-
Notifications
You must be signed in to change notification settings - Fork 20
/
analytics.ts
184 lines (165 loc) · 5 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
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
import taqAnalytics, { Consent, EventParams } from '@taqueria/analytics';
import * as Settings from '@taqueria/protocol/Settings';
import * as TaqError from '@taqueria/protocol/TaqError';
import { attemptP, chain, chainRej, FutureInstance as Future, map, mapRej, resolve } from 'fluture';
import { pipe } from 'fun';
import { getMachineId } from 'machine-id';
import { omit } from 'rambda';
import type { UsageAnalyticsDeps } from './taqueria-types.ts';
import * as utils from './taqueria-utils/taqueria-utils.ts';
const {
mkdir,
doesPathExist,
readJsonFile,
writeJsonFile,
taqResolve,
} = utils.inject({
stdout: Deno.stdout,
stderr: Deno.stderr,
});
const consentPrompt =
'Help improve Taqueria by sharing anonymous usage statistics in accordance with the privacy policy? (Y/n)';
const optInConfirmationPrompt = consentPrompt;
const optOutConfirmationPrompt = 'Are you sure you want to turn off usage statistic reporting? (Y/n)';
const OPT_IN = 'opt_in';
const OPT_OUT = 'opt_out';
export const inject = (deps: UsageAnalyticsDeps) => {
const { env, parsedArgs, machineInfo } = deps;
const settingsFolder = Deno.env.get('HOME') + '/.taq-settings';
const settingsFilePath = settingsFolder + '/taq-settings.json';
const didUserChooseYes = (input: string | null) => parsedArgs.yes || input === null || /^y(es)?$/i.test(input);
const getSettings = () =>
pipe(
doesPathExist(settingsFolder),
chainRej(_ => mkdir(settingsFolder)),
chain(_ => doesPathExist(settingsFilePath)),
chainRej(_ =>
pipe(
Settings.of({ consent: 'unspecified' }),
chain(writeJsonFile(settingsFilePath)),
chain(() => doesPathExist(settingsFilePath)),
)
),
chain(readJsonFile),
chain(Settings.of),
);
const getTrackingConsent = () =>
/*:Future<TaqError.t, Consent>*/ pipe(
getSettings(),
chain(settings =>
settings.consent !== 'unspecified' ? taqResolve(settings.consent) : promptForConsent(OPT_IN, 'y')
),
);
const promptForConsent = (option: Consent, defaultAnswer: string, noActionOnNegative: boolean = false) => {
const promptText = option === OPT_IN ? optInConfirmationPrompt : optOutConfirmationPrompt;
const handlePrompt = (): Future<TaqError.t, string | null> => {
const input = parsedArgs.yes ? defaultAnswer : prompt(promptText);
if (input === null || /^y(es)?$/i.test(input)) {
return updateSettingsAndReturn(option);
} else if (/^n(o)?$/i.test(input)) {
return noActionOnNegative ? taqResolve(null) : updateSettingsAndReturn(option === OPT_IN ? OPT_OUT : OPT_IN);
} else {
console.log('Invalid response. Please enter "y" or "n".');
return handlePrompt();
}
};
return pipe(
getSettings(),
chain(() => handlePrompt()),
);
};
const updateSettingsAndReturn = (consent: Consent) =>
pipe(
readJsonFile<Settings.t>(settingsFilePath),
map((settingsContent: Settings.t) => {
settingsContent.consent = consent;
return settingsContent;
}),
chain(writeJsonFile(settingsFilePath)),
map(() => consent),
);
const askToOptIn = () =>
pipe(
promptForConsent(OPT_IN, 'y'),
map(consent =>
consent === OPT_IN
? 'You have successfully opted-in to sharing anonymous usage analytics.'
: 'You have opted-out from sharing anonymous usage analytics.'
),
);
const askToOptOut = () =>
pipe(
promptForConsent(OPT_OUT, 'n', true), // Passing true for noActionOnNegative
map(consent =>
consent === OPT_OUT
? 'You have successfully opted-out from sharing anonymous usage analytics.'
: 'No changes were made.'
),
);
// No-operation
// noop: () -> void
const noop = (input?: unknown) => {
if (parsedArgs.debug) console.error(input);
};
const toEventFields = (fields: EventParams) => ({
...omit(
[
'_',
'setBuild',
'setVersion',
'projectDir',
'disableState',
'maxConcurrency',
'fromVsCode',
'version',
'build',
'quickstart',
'lock',
'$0',
'help',
],
Object.entries(parsedArgs).reduce(
(retval, [key, value]) => key.includes('-') || key.length === 1 ? retval : { ...retval, [key]: value },
{},
),
),
...fields,
...machineInfo,
taq_os: machineInfo.os,
});
const sendEvent = (fields: EventParams) =>
pipe(
getTrackingConsent(),
chain(_ => getSettings()),
chain(settings => {
const analytics = taqAnalytics.inject({
getMachineId,
settings,
isCICD: env.get('CI') !== undefined,
isTesting: env.get('TEST') !== undefined,
taqBuild: parsedArgs.setBuild,
taqVersion: parsedArgs.setVersion,
operatingSystem: machineInfo.os,
fetch,
fields: {
taq_ui: parsedArgs.fromVsCode ? 'VSCode' : 'CLI',
},
});
return attemptP(() =>
analytics.trackEvent('taq_task_executed', toEventFields(fields))
.then(() => {
if (parsedArgs.debug) console.error(analytics.getEvents());
})
.then(() => analytics.sendTrackedEvents())
.catch(noop)
);
}),
chainRej(() => taqResolve(noop)),
map(noop),
);
return {
askToOptIn,
askToOptOut,
sendEvent,
};
};