forked from release-it/release-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.js
73 lines (67 loc) · 1.73 KB
/
metrics.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
const { EOL } = require('os');
const got = require('got');
const { v4: uuidv4 } = require('uuid');
const osName = require('os-name');
const isCi = require('is-ci');
const debug = require('debug')('release-it:metrics');
const pkg = require('../package.json');
const noop = Promise.resolve();
const cast = value => (value ? 1 : 0);
const cid = uuidv4();
const payload = config => ({
v: 1,
tid: 'UA-108828841-1',
cid,
cd1: pkg.version,
cd2: process.version,
cd3: osName(),
cd4: cast(!config.isCI),
cd5: cast(config.isDryRun),
cd6: cast(config.isVerbose),
cd7: cast(config.isDebug),
cd8: null,
cd9: config.preReleaseId,
cd11: cast(isCi),
cd12: cast(config.git.tag),
cd13: cast(config.npm.publish),
cd14: cast(config.github.release),
cd15: config.increment,
cd16: cast(config.gitlab.release)
});
class Metrics {
constructor({ isEnabled = true, request = got } = {}) {
this.isEnabled = isEnabled;
this.request = request;
}
send(payload) {
return !this.isEnabled
? noop
: this.request('http://www.google-analytics.com/collect', {
timeout: 300,
retries: 0,
method: 'POST',
form: payload
})
.then(res => {
const { url, statusCode, statusMessage } = res;
debug({ url, statusCode, statusMessage, payload: new URLSearchParams(payload).toString() });
})
.catch(debug);
}
trackEvent(action, config) {
return this.send(
Object.assign(config ? payload(config) : {}, {
t: 'event',
ec: 'session',
ea: action
})
);
}
trackException(err) {
return this.send({
t: 'exception',
exd: err.toString().split(EOL)[0]
});
}
}
module.exports = Metrics;