forked from getgridea/gridea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analytics.ts
69 lines (57 loc) · 1.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
import GA from 'electron-google-analytics'
import macaddress from 'macaddress'
import * as pkg from '../../package.json'
const isDevelopment = process.env.NODE_ENV !== 'production'
interface EvOptions {
evLabel?: any
evValue?: any
}
const hostname = 'http://client.gridea.dev'
class Analytics {
private readonly ga: any
private clientId: any
constructor() {
this.ga = new GA('UA-113307620-4', { debug: isDevelopment })
this.ga.set('version', (pkg as any).version)
}
public getClientId(callback: any) {
if (this.clientId) {
callback(this.clientId)
}
macaddress.one((err: any, mac: string) => {
this.clientId = mac
callback(mac)
})
}
public async pageView(url: string, title?: string) {
this.getClientId(async (clientId: any) => {
try {
await this.ga.pageview(hostname, url, title, 1, clientId)
} catch (e) {
console.error(e)
}
})
}
public async event(evCategory: string, evAction: string, options: EvOptions) {
this.getClientId(async (clientId: any) => {
try {
await this.ga.event(evCategory, evAction, {
...options,
clientID: clientId,
})
} catch (e) {
console.error(e)
}
})
}
public async exception(exDesc: string, exFatal: any) {
this.getClientId(async (clientId: any) => {
try {
await this.ga.exception(exDesc, exFatal)
} catch (e) {
console.error(e)
}
})
}
}
export default new Analytics()