1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/tracker.ts
Roman 4270ec4240 tracker / user-store refactoring, part 1
Signed-off-by: Roman <ixrock@gmail.com>
2020-07-05 17:16:50 +03:00

49 lines
1.3 KiB
TypeScript

import { app, App, remote } from "electron"
import ua from "universal-analytics"
import { machineIdSync } from "node-machine-id"
import Singleton from "./utils/singleton";
import { userStore } from "./user-store"
export class Tracker extends Singleton {
static readonly GA_ID = "UA-159377374-1"
protected visitor: ua.Visitor
protected machineId: string = null;
protected ip: string = null;
protected appVersion: string;
protected locale: string;
protected electronUA: string;
private constructor(app: App) {
super();
try {
this.visitor = ua(Tracker.GA_ID, machineIdSync(), { strictCidFormat: false })
} catch (error) {
this.visitor = ua(Tracker.GA_ID)
}
this.visitor.set("dl", "https://lensapptelemetry.lakendlabs.com")
}
protected async isTelemetryAllowed(): Promise<boolean> {
return userStore.preferences.allowTelemetry;
}
async event(eventCategory: string, eventAction: string, otherParams = {}) {
try {
const allowed = await this.isTelemetryAllowed();
if (!allowed) {
return;
}
this.visitor.event({
ec: eventCategory,
ea: eventAction,
...otherParams,
}).send()
} catch (err) {
console.error(`Failed to track "${eventCategory}:${eventAction}"`, err)
}
}
}
export const tracker: Tracker = Tracker.getInstance(app || remote.app);