-
Notifications
You must be signed in to change notification settings - Fork 1
/
analytics.js
72 lines (64 loc) · 1.83 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
const Analytics = require('analytics-node')
const fs = require('fs')
const os = require('os')
const path = require('path')
const BOTONIC_HOME_PATH = path.join(os.homedir(), '.botonic')
const BOTONIC_CREDENTIALS_PATH = path.join(
BOTONIC_HOME_PATH,
'credentials.json'
)
const ANALYTICS_KEY = 'YD0jpJHNGW12uhLNbgB4wbdTRQ4Cy1Zu'
let credentials
const analytics = new Analytics(ANALYTICS_KEY)
function analyticsEnabled() {
return process.env.BOTONIC_DISABLE_ANALYTICS !== '1'
}
function getSystemInformation() {
return {
platform: os.platform(),
arch: os.arch(),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
timestamp: new Date().toISOString(),
}
}
function initializeCredentials() {
if (!fs.existsSync(BOTONIC_HOME_PATH)) fs.mkdirSync(BOTONIC_HOME_PATH)
const anonymous_id = Math.round(Math.random() * 100000000)
fs.writeFileSync(
BOTONIC_CREDENTIALS_PATH,
JSON.stringify({ analytics: { anonymous_id } })
)
}
function readCredentials() {
if (!fs.existsSync(BOTONIC_CREDENTIALS_PATH)) {
initializeCredentials()
}
try {
credentials = JSON.parse(fs.readFileSync(BOTONIC_CREDENTIALS_PATH, 'utf8'))
} catch (e) {
if (fs.existsSync(BOTONIC_CREDENTIALS_PATH)) {
console.warn('Credentials could not be loaded', e)
}
}
}
function track(event, properties = {}) {
if (analyticsEnabled() && analytics && credentials && credentials.analytics) {
properties = {
...properties,
...getSystemInformation(),
}
analytics.track({
event: event,
anonymousId: credentials.analytics.anonymous_id,
properties: properties,
})
}
}
const MODE = process.argv[2]
try {
readCredentials()
if (MODE === 'serve') track('Served Botonic CLI')
if (MODE === 'train') track('Trained with Botonic train')
} catch (e) {
console.warn('Error tracking event', e)
}