Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@fontsource/fira-code": "^5",
"@fontsource/inter": "^5",
"@formatjs/intl-segmenter": "^12.0.0",
"@matrix-org/analytics-events": "^0.31.0",
"@matrix-org/analytics-events": "^0.32.0",
"@matrix-org/emojibase-bindings": "^1.5.0",
"@matrix-org/react-sdk-module-api": "^2.4.0",
"@sentry/browser": "^10.0.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions src/PosthogTrackers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { PureComponent, type SyntheticEvent } from "react";
import { type WebScreen as ScreenEvent } from "@matrix-org/analytics-events/types/typescript/WebScreen";
import { type Interaction as InteractionEvent } from "@matrix-org/analytics-events/types/typescript/Interaction";
import { type PinUnpinAction } from "@matrix-org/analytics-events/types/typescript/PinUnpinAction";
import { type RoomListSortingAlgorithmChanged } from "@matrix-org/analytics-events/types/typescript/RoomListSortingAlgorithmChanged";

import PageType from "./PageTypes";
import Views from "./Views";
import { PosthogAnalytics } from "./PosthogAnalytics";
import { SortingAlgorithm } from "./stores/room-list-v3/skip-list/sorters";

export type ScreenName = ScreenEvent["$current_url"];
export type InteractionName = InteractionEvent["name"];
Expand All @@ -38,6 +40,12 @@ const loggedInPageTypeMap: Record<PageType | string, ScreenName> = {
[PageType.UserView]: "User",
};

const SortingAlgorithmMap: Record<SortingAlgorithm, RoomListSortingAlgorithmChanged["newAlgorithm"]> = {
[SortingAlgorithm.Recency]: "Activity",
[SortingAlgorithm.Unread]: "Unread",
[SortingAlgorithm.Alphabetic]: "Alphabetic",
};

export default class PosthogTrackers {
private static internalInstance: PosthogTrackers;

Expand Down Expand Up @@ -112,6 +120,22 @@ export default class PosthogTrackers {
from,
});
}

/**
* Track when the user chooses a different sorting algorithm for the room-list.
* @param oldAlgorithm - The old algorithm.
* @param newAlgorithm - The new algorithm.
*/
public static trackRoomListSortingAlgorithmChange(
oldAlgorithm: SortingAlgorithm,
newAlgorithm: SortingAlgorithm,
): void {
PosthogAnalytics.instance.trackEvent<RoomListSortingAlgorithmChanged>({
eventName: "RoomListSortingAlgorithmChanged",
oldAlgorithm: SortingAlgorithmMap[oldAlgorithm],
newAlgorithm: SortingAlgorithmMap[newAlgorithm],
});
}
}

export class PosthogScreenTracker extends PureComponent<{ screenName: ScreenName }> {
Expand Down
16 changes: 11 additions & 5 deletions src/viewmodels/room-list/RoomListHeaderViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,26 @@ export class RoomListHeaderViewModel
};

public sort = (option: SortOption): void => {
let sortingAlgorithm: SortingAlgorithm;
const oldSortingAlgorithm = RoomListStoreV3.instance.activeSortAlgorithm;
let newSortingAlgorithm: SortingAlgorithm;
switch (option) {
case "alphabetical":
sortingAlgorithm = SortingAlgorithm.Alphabetic;
newSortingAlgorithm = SortingAlgorithm.Alphabetic;
break;
case "recent":
sortingAlgorithm = SortingAlgorithm.Recency;
newSortingAlgorithm = SortingAlgorithm.Recency;
break;
case "unread-first":
sortingAlgorithm = SortingAlgorithm.Unread;
newSortingAlgorithm = SortingAlgorithm.Unread;
break;
}
RoomListStoreV3.instance.resort(sortingAlgorithm);
RoomListStoreV3.instance.resort(newSortingAlgorithm);
this.snapshot.merge({ activeSortOption: option });

// Record analytics for this action
if (oldSortingAlgorithm) {
PosthogTrackers.trackRoomListSortingAlgorithmChange(oldSortingAlgorithm, newSortingAlgorithm);
}
};

public toggleMessagePreview = (): void => {
Expand Down
18 changes: 17 additions & 1 deletion test/viewmodels/room-list/RoomListHeaderViewModel-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "../../../src/utils/space";
import { createTestClient, mkSpace } from "../../test-utils";
import { createRoom, hasCreateRoomRights } from "../../../src/viewmodels/room-list/utils";
import PosthogTrackers from "../../../src/PosthogTrackers";

jest.mock("../../../src/PosthogTrackers", () => ({
trackInteraction: jest.fn(),
Expand Down Expand Up @@ -276,10 +277,25 @@ describe("RoomListHeaderViewModel", () => {
const resortSpy = jest.spyOn(RoomListStoreV3.instance, "resort").mockImplementation(jest.fn());
vm = new RoomListHeaderViewModel({ matrixClient, spaceStore: SpaceStore.instance });
vm.sort(option);

expect(resortSpy).toHaveBeenCalledWith(expectedAlgorithm);
});

it("should track analytics on resort", () => {
jest.spyOn(RoomListStoreV3.instance, "activeSortAlgorithm", "get").mockReturnValue(
SortingAlgorithm.Alphabetic,
);
PosthogTrackers.trackRoomListSortingAlgorithmChange = jest.fn();

vm = new RoomListHeaderViewModel({ matrixClient, spaceStore: SpaceStore.instance });
jest.spyOn(RoomListStoreV3.instance, "resort").mockImplementation(jest.fn());
vm.sort("unread-first");

expect(PosthogTrackers.trackRoomListSortingAlgorithmChange).toHaveBeenCalledWith(
SortingAlgorithm.Alphabetic,
SortingAlgorithm.Unread,
);
});

it("should toggle message preview from enabled to disabled", () => {
jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName: string) => {
if (settingName === "RoomList.showMessagePreview") return true;
Expand Down
Loading