-
Notifications
You must be signed in to change notification settings - Fork 43
/
analytics.ts
48 lines (41 loc) · 1.46 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
import service from "./services/service";
import AnalyticsLight from "./utils/analytics-light";
interface EnvironmentInfo {
machineId: string;
version: string;
platform: string;
}
class AnalyticsFacade {
private _analytics: AnalyticsLight;
private _environmentInfo?: EnvironmentInfo;
private _environmentInfoPromise?: Promise<EnvironmentInfo>;
private _clientId?: string;
private async _getEnviromentInfo() {
if (this._environmentInfo != null) return this._environmentInfo;
if (this._environmentInfoPromise != null) {
return await this._environmentInfoPromise;
} else {
this._environmentInfoPromise = service.api.getEnviromentInfo();
this._environmentInfo = await this._environmentInfoPromise;
this._clientId = localStorage.getItem("clientID") || undefined;
this._analytics.set("uid", this._environmentInfo.machineId);
return this._environmentInfo;
}
}
constructor() {
this._analytics = new AnalyticsLight("UA-115442088-3");
}
async pageView(path: string): Promise<any> {
const env = await this._getEnviromentInfo();
try {
const result = await this._analytics.pageview("electron.hokus.com", path, undefined, this._clientId);
//ugly
this._clientId = this._clientId || result.clientID;
if (this._clientId != null) {
localStorage.setItem("clientID", this._clientId);
}
} catch (e) {}
}
}
const analytics = new AnalyticsFacade();
export default analytics;