workadventure/front/src/Administration/AnalyticsClient.ts

92 lines
2.6 KiB
TypeScript
Raw Normal View History

import { POSTHOG_API_KEY, POSTHOG_URL } from "../Enum/EnvironmentVariable";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare let window: any;
class AnalyticsClient {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2021-11-24 16:20:07 +01:00
private posthogPromise: Promise<any>|undefined;
constructor() {
if (POSTHOG_API_KEY && POSTHOG_URL) {
this.posthogPromise = import("posthog-js").then(({ default: posthog }) => {
posthog.init(POSTHOG_API_KEY, { api_host: POSTHOG_URL, disable_cookie: true });
//the posthog toolbar need a reference in window to be able to work
window.posthog = posthog;
return posthog;
});
}
}
identifyUser(uuid: string, email: string | null) {
2021-11-24 16:20:07 +01:00
this.posthogPromise
?.then((posthog) => {
posthog.identify(uuid, { uuid, email, wa: true });
});
}
loggedWithSso() {
2021-11-24 16:20:07 +01:00
this.posthogPromise
?.then((posthog) => {
posthog.capture("wa-logged-sso");
});
}
loggedWithToken() {
2021-11-24 16:20:07 +01:00
this.posthogPromise
?.then((posthog) => {
posthog.capture("wa-logged-token");
});
}
enteredRoom(roomId: string, roomGroup: string | null) {
2021-11-24 16:20:07 +01:00
this.posthogPromise
?.then((posthog) => {
posthog.capture("$pageView", { roomId, roomGroup });
posthog.capture("enteredRoom");
});
}
openedMenu() {
2021-11-24 16:20:07 +01:00
this.posthogPromise
?.then((posthog) => {
posthog.capture("wa-opened-menu");
});
}
launchEmote(emote: string) {
2021-11-24 16:20:07 +01:00
this.posthogPromise
?.then((posthog) => {
posthog.capture("wa-emote-launch", { emote });
});
}
enteredJitsi(roomName: string, roomId: string) {
2021-11-24 16:20:07 +01:00
this.posthogPromise
?.then((posthog) => {
posthog.capture("wa-entered-jitsi", { roomName, roomId });
});
}
validationName() {
2021-11-24 16:20:07 +01:00
this.posthogPromise
?.then((posthog) => {
posthog.capture("wa-name-validation");
});
}
validationWoka(scene: string) {
2021-11-24 16:20:07 +01:00
this.posthogPromise
?.then((posthog) => {
posthog.capture("wa-woka-validation", { scene });
});
}
validationVideo() {
2021-11-24 16:20:07 +01:00
this.posthogPromise
?.then((posthog) => {
posthog.capture("wa-video-validation");
});
}
}
export const analyticsClient = new AnalyticsClient();