2022-02-09 22:25:58 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-02-09 22:25:58 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-02-09 22:25:58 +08:00
|
|
|
*/
|
|
|
|
|
2022-02-09 22:42:08 +08:00
|
|
|
import { PureComponent, SyntheticEvent } from "react";
|
2022-06-08 03:08:36 +08:00
|
|
|
import { WebScreen as ScreenEvent } from "@matrix-org/analytics-events/types/typescript/WebScreen";
|
|
|
|
import { Interaction as InteractionEvent } from "@matrix-org/analytics-events/types/typescript/Interaction";
|
2024-09-05 22:11:55 +08:00
|
|
|
import { PinUnpinAction } from "@matrix-org/analytics-events/types/typescript/PinUnpinAction";
|
2022-02-09 22:25:58 +08:00
|
|
|
|
|
|
|
import PageType from "./PageTypes";
|
|
|
|
import Views from "./Views";
|
|
|
|
import { PosthogAnalytics } from "./PosthogAnalytics";
|
|
|
|
|
2022-02-10 22:38:31 +08:00
|
|
|
export type ScreenName = ScreenEvent["$current_url"];
|
2022-02-10 18:02:34 +08:00
|
|
|
export type InteractionName = InteractionEvent["name"];
|
2022-02-09 22:25:58 +08:00
|
|
|
|
|
|
|
const notLoggedInMap: Record<Exclude<Views, Views.LOGGED_IN>, ScreenName> = {
|
2022-02-10 22:38:31 +08:00
|
|
|
[Views.LOADING]: "Loading",
|
2023-08-24 16:28:43 +08:00
|
|
|
[Views.CONFIRM_LOCK_THEFT]: "ConfirmStartup",
|
2022-02-09 22:25:58 +08:00
|
|
|
[Views.WELCOME]: "Welcome",
|
|
|
|
[Views.LOGIN]: "Login",
|
|
|
|
[Views.REGISTER]: "Register",
|
2022-07-13 21:43:44 +08:00
|
|
|
[Views.USE_CASE_SELECTION]: "UseCaseSelection",
|
2022-02-09 22:25:58 +08:00
|
|
|
[Views.FORGOT_PASSWORD]: "ForgotPassword",
|
2022-02-10 22:38:31 +08:00
|
|
|
[Views.COMPLETE_SECURITY]: "CompleteSecurity",
|
|
|
|
[Views.E2E_SETUP]: "E2ESetup",
|
|
|
|
[Views.SOFT_LOGOUT]: "SoftLogout",
|
2023-08-24 16:28:43 +08:00
|
|
|
[Views.LOCK_STOLEN]: "SessionLockStolen",
|
2022-02-09 22:25:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const loggedInPageTypeMap: Record<PageType, ScreenName> = {
|
|
|
|
[PageType.HomePage]: "Home",
|
|
|
|
[PageType.RoomView]: "Room",
|
|
|
|
[PageType.UserView]: "User",
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class PosthogTrackers {
|
|
|
|
private static internalInstance: PosthogTrackers;
|
|
|
|
|
|
|
|
public static get instance(): PosthogTrackers {
|
|
|
|
if (!PosthogTrackers.internalInstance) {
|
|
|
|
PosthogTrackers.internalInstance = new PosthogTrackers();
|
|
|
|
}
|
|
|
|
return PosthogTrackers.internalInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private view: Views = Views.LOADING;
|
2023-02-24 23:28:40 +08:00
|
|
|
private pageType?: PageType;
|
|
|
|
private override?: ScreenName;
|
2022-02-09 22:25:58 +08:00
|
|
|
|
|
|
|
public trackPageChange(view: Views, pageType: PageType | undefined, durationMs: number): void {
|
|
|
|
this.view = view;
|
|
|
|
this.pageType = pageType;
|
|
|
|
if (this.override) return;
|
|
|
|
this.trackPage(durationMs);
|
|
|
|
}
|
|
|
|
|
|
|
|
private trackPage(durationMs?: number): void {
|
2022-12-12 19:24:14 +08:00
|
|
|
const screenName =
|
2023-02-24 23:28:40 +08:00
|
|
|
this.view === Views.LOGGED_IN ? loggedInPageTypeMap[this.pageType!] : notLoggedInMap[this.view];
|
2022-02-09 22:25:58 +08:00
|
|
|
PosthogAnalytics.instance.trackEvent<ScreenEvent>({
|
2022-02-10 22:38:31 +08:00
|
|
|
eventName: "$pageview",
|
|
|
|
$current_url: screenName,
|
2022-02-09 22:25:58 +08:00
|
|
|
durationMs,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public trackOverride(screenName: ScreenName): void {
|
|
|
|
if (!screenName) return;
|
|
|
|
this.override = screenName;
|
|
|
|
PosthogAnalytics.instance.trackEvent<ScreenEvent>({
|
2022-02-10 22:38:31 +08:00
|
|
|
eventName: "$pageview",
|
|
|
|
$current_url: screenName,
|
2022-02-09 22:25:58 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public clearOverride(screenName: ScreenName): void {
|
|
|
|
if (screenName !== this.override) return;
|
2023-02-24 23:28:40 +08:00
|
|
|
this.override = undefined;
|
2022-02-09 22:25:58 +08:00
|
|
|
this.trackPage();
|
|
|
|
}
|
2022-02-09 22:42:08 +08:00
|
|
|
|
2023-12-14 19:10:01 +08:00
|
|
|
public static trackInteraction(name: InteractionName, ev?: SyntheticEvent | Event, index?: number): void {
|
2022-02-09 22:42:08 +08:00
|
|
|
let interactionType: InteractionEvent["interactionType"];
|
|
|
|
if (ev?.type === "click") {
|
|
|
|
interactionType = "Pointer";
|
|
|
|
} else if (ev?.type.startsWith("key")) {
|
|
|
|
interactionType = "Keyboard";
|
|
|
|
}
|
|
|
|
|
|
|
|
PosthogAnalytics.instance.trackEvent<InteractionEvent>({
|
|
|
|
eventName: "Interaction",
|
|
|
|
interactionType,
|
2022-02-10 21:53:07 +08:00
|
|
|
index,
|
2022-02-09 22:42:08 +08:00
|
|
|
name,
|
|
|
|
});
|
|
|
|
}
|
2024-09-05 22:11:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Track a pin or unpin action on a message.
|
|
|
|
* @param kind - Is pin or unpin.
|
|
|
|
* @param from - From where the action is triggered.
|
|
|
|
*/
|
|
|
|
public static trackPinUnpinMessage(kind: PinUnpinAction["kind"], from: PinUnpinAction["from"]): void {
|
|
|
|
PosthogAnalytics.instance.trackEvent<PinUnpinAction>({
|
|
|
|
eventName: "PinUnpinAction",
|
|
|
|
kind,
|
|
|
|
from,
|
|
|
|
});
|
|
|
|
}
|
2022-02-09 22:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PosthogScreenTracker extends PureComponent<{ screenName: ScreenName }> {
|
2023-01-12 21:25:14 +08:00
|
|
|
public componentDidMount(): void {
|
2022-02-09 22:25:58 +08:00
|
|
|
PosthogTrackers.instance.trackOverride(this.props.screenName);
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:25:14 +08:00
|
|
|
public componentDidUpdate(): void {
|
2022-02-09 22:25:58 +08:00
|
|
|
// We do not clear the old override here so that we do not send the non-override screen as a transition
|
|
|
|
PosthogTrackers.instance.trackOverride(this.props.screenName);
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:25:14 +08:00
|
|
|
public componentWillUnmount(): void {
|
2022-02-09 22:25:58 +08:00
|
|
|
PosthogTrackers.instance.clearOverride(this.props.screenName);
|
|
|
|
}
|
|
|
|
|
2023-02-14 01:01:43 +08:00
|
|
|
public render(): React.ReactNode {
|
2022-02-09 22:25:58 +08:00
|
|
|
return null; // no need to render anything, we just need to hook into the React lifecycle
|
|
|
|
}
|
|
|
|
}
|