forked from crc-org/tray-electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telemetry.js
executable file
·150 lines (126 loc) · 3.75 KB
/
telemetry.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
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
const Analytics = require('analytics-node');
const { app } = require('electron');
const path = require('path');
const os = require('os');
const fs = require('fs');
const {v4: uuidv4 } = require('uuid');
const hash = require('object-hash');
let userIdHashPath = path.join(os.homedir(), '.crc', "segmentIdentityHash");
let userIdPath = path.join(os.homedir(), '.redhat', 'anonymousId');
if (app.isPackaged) {
writeKey = 'cvpHsNcmGCJqVzf6YxrSnVlwFSAZaYtp';
}
module.exports = class Telemetry {
#traits = {
tray_os_version: os.version(), // Windows 10 Pro
tray_os_release: os.release(), // 10.0.19043
tray_os: os.platform(), // win32
}
#context = {
ip: "0.0.0.0"
}
constructor(telemetryEnabled, writeKey) {
this.telemetryEnabled = telemetryEnabled
if (!telemetryEnabled) {
return
}
this.analytics = new Analytics(writeKey, { flushAt: 1 });
// get user identity UUID and cache it in userID
this.userId = getUserId()
let identity = {
userId: this.userId,
traits: this.#traits
}
// get the hash of the user identity
this.userIdHash = getUserIdHash()
const idHash = hash.sha1(identity)
if (idHash !== this.userIdHash) {
// update userIdHash
this.userIdHash = idHash
writeUserIdHash(idHash)
// send identify event to segment
this.analytics.identify({
userId: this.userId,
traits: this.#traits,
context: this.#context
})
}
}
trackError(errorMsg) {
if (!this.telemetryEnabled) {
return
}
let properties = genProperties(errorMsg)
this.analytics.track({
userId: this.userId,
event: 'tray error occured',
context: this.#context,
properties: properties
})
}
trackSuccess(successMsg) {
if (!this.telemetryEnabled) {
return
}
let properties = genProperties(successMsg)
this.analytics.track({
userId: this.userId,
event: 'tray operation successful',
context: this.#context,
properties: properties
})
}
}
// fetch userID from ~/.redhat/anonymousID if exists
// or else generate, write to disk and return the it
function getUserId() {
try {
let data = fs.readFileSync(userIdPath)
return data.toString()
} catch (err) {
console.log(err)
let uuid = writeNewUuid()
return uuid
}
}
function writeNewUuid() {
let dir = path.join(app.getPath('home'), '.redhat')
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
let uuid = uuidv4()
try {
fs.writeFileSync(userIdPath, uuid)
return uuid
} catch (err) {
console.log(err)
return
}
}
function writeUserIdHash(userIdHash) {
try {
fs.writeFileSync(userIdHashPath, userIdHash)
console.log(`wrote new identity hash to: ${userIdHashPath}`)
} catch (err) {
console.log(err)
return
}
}
function getUserIdHash() {
try {
let data = fs.readFileSync(userIdHashPath)
return data.toString()
} catch (err) {
console.log(err)
return ""
}
}
function genProperties(message) {
const properties = {
source: "tray-electron",
tray_version: app.getVersion(),
crc_version: "",
message: message
}
return properties
}