Skip to content

Commit

Permalink
TCHAP: add posthog analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
marc.sirisak committed Dec 17, 2024
1 parent 16ba383 commit 2452157
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 3 deletions.
4 changes: 4 additions & 0 deletions config.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,9 @@
"map_style_url": "https://openmaptiles.geo.data.gouv.fr/styles/osm-bright/style.json",
"element_call": {
"url": "https://element-call.tchap.incubateur.net/"
},
"posthog": {
"project_api_key": "phc_FFa4pkvmuWjF9nZOMmYJWUXMibuYnCnPyf3DqPGZs4L",
"api_host": "https://posthogdev.tchap.incubateur.net"
}
}
6 changes: 5 additions & 1 deletion config.preprod.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,9 @@
"tchap_sso_flow": {
"isActive": true
},
"map_style_url": "https://openmaptiles.geo.data.gouv.fr/styles/osm-bright/style.json"
"map_style_url": "https://openmaptiles.geo.data.gouv.fr/styles/osm-bright/style.json",
"posthog": {
"project_api_key": "phc_FFa4pkvmuWjF9nZOMmYJWUXMibuYnCnPyf3DqPGZs4L",
"api_host": "https://posthogdev.tchap.incubateur.net"
}
}
6 changes: 5 additions & 1 deletion config.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,9 @@
"tchap_sso_flow": {
"isActive": false
},
"map_style_url": "https://openmaptiles.geo.data.gouv.fr/styles/osm-bright/style.json"
"map_style_url": "https://openmaptiles.geo.data.gouv.fr/styles/osm-bright/style.json",
"posthog": {
"project_api_key": "phc_FFa4pkvmuWjF9nZOMmYJWUXMibuYnCnPyf3DqPGZs4L",
"api_host": "https://posthogdev.tchap.incubateur.net"
}
}
7 changes: 7 additions & 0 deletions patches/tchap-modifications.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,12 @@
"src/components/views/spaces/SpaceCreateMenu.tsx",
"src/createRoom.ts"
]
},
"metrics-call": {
"issue": "https://github.com/tchapgouv/tchap-web-v4/issues/1184",
"files": [
"src/components/views/settings/tabs/user/SecurityUserSettingsTab.tsx",
"src/LegacyCallHandler.tsx"
]
}
}
5 changes: 5 additions & 0 deletions res/themes/tchap-common/css/_tchap_custom.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@
.mx_E2EIcon_verified::after {
mask-image: url("$(res)/img/e2e/verified.svg");
background-color: var(--confirmation-color);
}

/* For analytics modal, using tchap icon */
.mx_AnalyticsLearnMoreDialog .mx_AnalyticsLearnMore_image_holder {
background-image: url("$(res)/themes/tchap/img/logos/tchap-logo.svg");
}
10 changes: 10 additions & 0 deletions src/LegacyCallHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { showCantStartACallDialog } from "./voice-broadcast/utils/showCantStartA
import { isNotNull } from "./Typeguards";
import { BackgroundAudio } from "./audio/BackgroundAudio";
import { Jitsi } from "./widgets/Jitsi.ts";
import TchapPosthog from "./tchap/util/TchapPosthog.ts";

export const PROTOCOL_PSTN = "m.protocol.pstn";
export const PROTOCOL_PSTN_PREFIXED = "im.vector.protocol.pstn";
Expand Down Expand Up @@ -618,6 +619,9 @@ export default class LegacyCallHandler extends EventEmitter {
if (isNotNull(mappedRoomId)) {
this.removeCallForRoom(mappedRoomId);
}
/** :TCHAP: metrics-call **/
TchapPosthog.instance.trackCallEnded(call);
/** end :TCHAP: **/

if (oldState === CallState.InviteSent && call.hangupParty === CallParty.Remote) {
this.play(AudioID.Busy);
Expand Down Expand Up @@ -842,12 +846,18 @@ export default class LegacyCallHandler extends EventEmitter {

this.setActiveCallRoomId(roomId);

/** :TCHAP: metrics-call **/
TchapPosthog.instance.trackCallStart(call);
/** end :TCHAP: **/
if (type === CallType.Voice) {
call.placeVoiceCall();
} else if (type === "video") {
call.placeVideoCall();
} else {
logger.error("Unknown conf call type: " + type);
/** :TCHAP: metrics-call **/
TchapPosthog.instance.trackCallEnded(call);
/** end :TCHAP: **/
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
};
privacySection = (
<SettingsSection heading={_t("common|privacy")}>
<DiscoverySettings />
{/* <DiscoverySettings /> :TCHAP: metrics-call we remove the discovery settings that appear when posthog is enabled */}
<SettingsSubsection
heading={_t("common|analytics")}
description={_t("settings|security|analytics_description")}
Expand Down
58 changes: 58 additions & 0 deletions src/tchap/util/TchapPosthog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { MatrixCall } from "matrix-js-sdk/src/matrix";
import { CallDirection, CallType } from "matrix-js-sdk/src/webrtc/call";
import { PosthogAnalytics } from "~tchap-web/src/PosthogAnalytics";
import { CallEnded as CallEndedEvent } from "@matrix-org/analytics-events/types/typescript/CallEnded";
import { CallStarted as CallStartEvent } from "@matrix-org/analytics-events/types/typescript/CallStarted";
import PerformanceMonitor from "~tchap-web/src/performance";

export default class TchapPosthog {

private static internalInstance: TchapPosthog;

public static get instance(): TchapPosthog {
if (!TchapPosthog.internalInstance) {
TchapPosthog.internalInstance = new TchapPosthog();
}
return TchapPosthog.internalInstance;
}


async trackCallStart(call: MatrixCall): Promise<void> {
this.startTimer(call.callId);

PosthogAnalytics.instance.trackEvent<CallStartEvent>({
eventName: "CallStarted",
placed: true,
isVideo: call.type == CallType.Video,
numParticipants: 2,
});
}

async trackCallEnded(call: MatrixCall): Promise<void> {
const durationMs = this.stopTimer(call.callId);

PosthogAnalytics.instance.trackEvent<CallEndedEvent>({
eventName: "CallEnded",
placed: call.direction! == CallDirection.Outbound,
isVideo: call.type == CallType.Video,
durationMs: durationMs,
numParticipants: 2,
});
}

private startTimer(id : string): void {
PerformanceMonitor.instance.start(id);
}

private stopTimer(id: string): number {
const perfMonitor = PerformanceMonitor.instance;

perfMonitor.stop(id);

const entries = perfMonitor.getEntries({ name: id });

const measurement = entries.pop();

return measurement ? measurement.duration : 0;
}
}

0 comments on commit 2452157

Please sign in to comment.