-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathanalytics.js
101 lines (83 loc) · 2.34 KB
/
analytics.js
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
import * as cache from './cache';
import analytics from 'universal-analytics';
import { v4 as uuid } from 'uuid';
import _ from 'lodash';
import { getValue } from './cache-env';
import { analyticsTrackingId } from '../config/services';
import * as logger from './logger';
const reportData = {
commandName: null,
extensionCanonicalName: null,
appId: null,
argv: [],
reportSent: false,
};
async function getClientId() {
return await cache.get('ga-client-id', null, () => uuid());
}
async function getAnalyticsVisitor() {
const clientId = await getClientId();
const visitor = analytics(analyticsTrackingId, clientId);
reportData.clientId = clientId;
const { email } = (await getValue('developer')) || {};
if (email) {
visitor.set('userId', email);
reportData.userId = email;
}
visitor.set('isDeveloper', true);
reportData.isDeveloper = true;
return visitor;
}
async function reportEvent({ category, action, label }) {
const visitor = await getAnalyticsVisitor();
await visitor.event(category, action, label).send(err => {
if (err) {
console.error(err);
} else {
logger.info('GA Report completed', {
category,
action,
label,
clientId: reportData.clientId,
userId: reportData.userId,
isDeveloper: reportData.isDeveloper,
});
}
});
}
async function reportCliCommand(commandName, canonicalNameOrAppId) {
await reportEvent({
category: 'CLI',
action: commandName,
label: canonicalNameOrAppId,
});
}
async function finishReport() {
const { commandName, extensionCanonicalName, appId, reportSent } = reportData;
const label = extensionCanonicalName || appId;
if (commandName && !reportSent) {
await reportCliCommand(commandName, label);
reportData.reportSent = true;
}
}
export function setCommandName(name) {
reportData.commandName = name;
if (reportData.extensionCanonicalName || reportData.appId) {
finishReport().catch(console.err);
}
}
export function setAppId(appId) {
reportData.appId = appId;
if (reportData.commandName) {
finishReport().catch(console.error);
}
}
export function setExtensionCanonicalName(name) {
reportData.extensionCanonicalName = name;
if (reportData.commandName) {
finishReport().catch(console.error);
}
}
export function setArgv(argv) {
reportData.argv = _.drop(argv, 2);
}