Refactoring Analytics to remove errors

This commit is contained in:
David Négrier 2021-11-24 10:21:19 +01:00
parent 02ed853399
commit 806b97afab

View file

@ -4,7 +4,7 @@ declare let window: any;
class AnalyticsClient { class AnalyticsClient {
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
private posthogPromise: Promise<any>; private posthogPromise: Promise<any>|undefined;
constructor() { constructor() {
if (POSTHOG_API_KEY && POSTHOG_URL) { if (POSTHOG_API_KEY && POSTHOG_URL) {
@ -14,65 +14,56 @@ class AnalyticsClient {
window.posthog = posthog; window.posthog = posthog;
return posthog; return posthog;
}); });
} else {
this.posthogPromise = Promise.reject();
} }
} }
identifyUser(uuid: string, email: string | null) { identifyUser(uuid: string, email: string | null) {
this.posthogPromise this.posthogPromise
.then((posthog) => { ?.then((posthog) => {
posthog.identify(uuid, { uuid, email, wa: true }); posthog.identify(uuid, { uuid, email, wa: true });
}) });
.catch();
} }
loggedWithSso() { loggedWithSso() {
this.posthogPromise this.posthogPromise
.then((posthog) => { ?.then((posthog) => {
posthog.capture("wa-logged-sso"); posthog.capture("wa-logged-sso");
}) });
.catch();
} }
loggedWithToken() { loggedWithToken() {
this.posthogPromise this.posthogPromise
.then((posthog) => { ?.then((posthog) => {
posthog.capture("wa-logged-token"); posthog.capture("wa-logged-token");
}) });
.catch();
} }
enteredRoom(roomId: string, roomGroup: string | null) { enteredRoom(roomId: string, roomGroup: string | null) {
this.posthogPromise this.posthogPromise
.then((posthog) => { ?.then((posthog) => {
posthog.capture("$pageView", { roomId, roomGroup }); posthog.capture("$pageView", { roomId, roomGroup });
}) });
.catch();
} }
openedMenu() { openedMenu() {
this.posthogPromise this.posthogPromise
.then((posthog) => { ?.then((posthog) => {
posthog.capture("wa-opened-menu"); posthog.capture("wa-opened-menu");
}) });
.catch();
} }
launchEmote(emote: string) { launchEmote(emote: string) {
this.posthogPromise this.posthogPromise
.then((posthog) => { ?.then((posthog) => {
posthog.capture("wa-emote-launch", { emote }); posthog.capture("wa-emote-launch", { emote });
}) });
.catch();
} }
enteredJitsi(roomName: string, roomId: string) { enteredJitsi(roomName: string, roomId: string) {
this.posthogPromise this.posthogPromise
.then((posthog) => { ?.then((posthog) => {
posthog.capture("wa-entered-jitsi", { roomName, roomId }); posthog.capture("wa-entered-jitsi", { roomName, roomId });
}) });
.catch();
} }
} }
export const analyticsClient = new AnalyticsClient(); export const analyticsClient = new AnalyticsClient();