2022-02-09 17:25:58 +03:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-09 17:42:08 +03:00
|
|
|
import { PureComponent, SyntheticEvent } from "react";
|
2022-02-10 17:38:31 +03:00
|
|
|
import { WebScreen as ScreenEvent } from "matrix-analytics-events/types/typescript/WebScreen";
|
2022-02-09 17:42:08 +03:00
|
|
|
import { Interaction as InteractionEvent } from "matrix-analytics-events/types/typescript/Interaction";
|
2022-02-09 17:25:58 +03:00
|
|
|
|
|
|
|
import PageType from "./PageTypes";
|
|
|
|
import Views from "./Views";
|
|
|
|
import { PosthogAnalytics } from "./PosthogAnalytics";
|
|
|
|
|
2022-02-10 17:38:31 +03:00
|
|
|
export type ScreenName = ScreenEvent["$current_url"];
|
2022-02-10 13:02:34 +03:00
|
|
|
export type InteractionName = InteractionEvent["name"];
|
2022-02-09 17:25:58 +03:00
|
|
|
|
|
|
|
const notLoggedInMap: Record<Exclude<Views, Views.LOGGED_IN>, ScreenName> = {
|
2022-02-10 17:38:31 +03:00
|
|
|
[Views.LOADING]: "Loading",
|
2022-02-09 17:25:58 +03:00
|
|
|
[Views.WELCOME]: "Welcome",
|
|
|
|
[Views.LOGIN]: "Login",
|
|
|
|
[Views.REGISTER]: "Register",
|
|
|
|
[Views.FORGOT_PASSWORD]: "ForgotPassword",
|
2022-02-10 17:38:31 +03:00
|
|
|
[Views.COMPLETE_SECURITY]: "CompleteSecurity",
|
|
|
|
[Views.E2E_SETUP]: "E2ESetup",
|
|
|
|
[Views.SOFT_LOGOUT]: "SoftLogout",
|
2022-02-09 17:25:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const loggedInPageTypeMap: Record<PageType, ScreenName> = {
|
|
|
|
[PageType.HomePage]: "Home",
|
|
|
|
[PageType.RoomView]: "Room",
|
|
|
|
[PageType.UserView]: "User",
|
2022-03-23 02:07:37 +03:00
|
|
|
[PageType.LegacyGroupView]: "Group",
|
2022-02-09 17:25:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
private pageType?: PageType = null;
|
|
|
|
private override?: ScreenName = null;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
const screenName = this.view === Views.LOGGED_IN
|
|
|
|
? loggedInPageTypeMap[this.pageType]
|
|
|
|
: notLoggedInMap[this.view];
|
|
|
|
PosthogAnalytics.instance.trackEvent<ScreenEvent>({
|
2022-02-10 17:38:31 +03:00
|
|
|
eventName: "$pageview",
|
|
|
|
$current_url: screenName,
|
2022-02-09 17:25:58 +03:00
|
|
|
durationMs,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public trackOverride(screenName: ScreenName): void {
|
|
|
|
if (!screenName) return;
|
|
|
|
this.override = screenName;
|
|
|
|
PosthogAnalytics.instance.trackEvent<ScreenEvent>({
|
2022-02-10 17:38:31 +03:00
|
|
|
eventName: "$pageview",
|
|
|
|
$current_url: screenName,
|
2022-02-09 17:25:58 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public clearOverride(screenName: ScreenName): void {
|
|
|
|
if (screenName !== this.override) return;
|
|
|
|
this.override = null;
|
|
|
|
this.trackPage();
|
|
|
|
}
|
2022-02-09 17:42:08 +03:00
|
|
|
|
2022-02-10 16:53:07 +03:00
|
|
|
public static trackInteraction(name: InteractionName, ev?: SyntheticEvent, index?: number): void {
|
2022-02-09 17:42:08 +03: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 16:53:07 +03:00
|
|
|
index,
|
2022-02-09 17:42:08 +03:00
|
|
|
name,
|
|
|
|
});
|
|
|
|
}
|
2022-02-09 17:25:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PosthogScreenTracker extends PureComponent<{ screenName: ScreenName }> {
|
|
|
|
componentDidMount() {
|
|
|
|
PosthogTrackers.instance.trackOverride(this.props.screenName);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
PosthogTrackers.instance.clearOverride(this.props.screenName);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return null; // no need to render anything, we just need to hook into the React lifecycle
|
|
|
|
}
|
|
|
|
}
|