Skip to content

Commit

Permalink
add metrics on call event
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalinghee committed Aug 12, 2024
1 parent 4a63013 commit ad05403
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
43 changes: 42 additions & 1 deletion linked-dependencies/matrix-react-sdk/src/LegacyCallHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
import React from "react";
import { MatrixError, RuleId, TweakName, SyncState } from "matrix-js-sdk/src/matrix";
import {
CallDirection,
CallError,
CallErrorCode,
CallEvent,
Expand All @@ -29,6 +30,8 @@ import {
FALLBACK_ICE_SERVER,
MatrixCall,
} from "matrix-js-sdk/src/webrtc/call";
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 { logger } from "matrix-js-sdk/src/logger";
import EventEmitter from "events";
import { PushProcessor } from "matrix-js-sdk/src/pushprocessor";
Expand Down Expand Up @@ -66,6 +69,7 @@ import { localNotificationsAreSilenced } from "./utils/notifications";
import { SdkContextClass } from "./contexts/SDKContext";
import { showCantStartACallDialog } from "./voice-broadcast/utils/showCantStartACallDialog";
import { isNotNull } from "./Typeguards";
import { PosthogAnalytics } from "./PosthogAnalytics";

export const PROTOCOL_PSTN = "m.protocol.pstn";
export const PROTOCOL_PSTN_PREFIXED = "im.vector.protocol.pstn";
Expand Down Expand Up @@ -695,9 +699,13 @@ export default class LegacyCallHandler extends EventEmitter {
this.removeCallForRoom(mappedRoomId);
}

/** :TCHAP: metrics-call **/
this.trackCallEnded(call);
/** end :TCHAP: **/

if (oldState === CallState.InviteSent && call.hangupParty === CallParty.Remote) {
this.play(AudioID.Busy);

// Don't show a modal when we got rejected/the call was hung up
if (!hangupReason || [CallErrorCode.UserHangup, "user hangup"].includes(hangupReason)) break;

Expand Down Expand Up @@ -734,6 +742,30 @@ export default class LegacyCallHandler extends EventEmitter {
}
};

/** :TCHAP: metrics-call **/
private async trackCallStart(call: MatrixCall): Promise<void> {
PosthogAnalytics.instance.trackEvent<CallStartEvent>({
eventName: "CallStarted",
placed: true,
isVideo: call.type == CallType.Video,
numParticipants: 2,
});
}

private async trackCallEnded(call: MatrixCall): Promise<void> {
let durationMs: number = call.getCallStartTime() == undefined ?
0
: Math.round((Date.now() - call.getCallStartTime()!));
PosthogAnalytics.instance.trackEvent<CallEndedEvent>({
eventName: "CallEnded",
placed: call.direction! == CallDirection.Outbound,
isVideo: call.type == CallType.Video,
durationMs: durationMs,
numParticipants: 2,
});
}
/** end :TCHAP: **/

private async logCallStats(call: MatrixCall, mappedRoomId: string): Promise<void> {
const stats = await call.getCurrentCallStats();
logger.debug(
Expand Down Expand Up @@ -921,8 +953,14 @@ export default class LegacyCallHandler extends EventEmitter {

if (type === CallType.Voice) {
call.placeVoiceCall();
/** :TCHAP: metrics-call **/
this.trackCallStart(call)
/** end :TCHAP: **/
} else if (type === "video") {
call.placeVideoCall();
/** :TCHAP: metrics-call **/
this.trackCallStart(call)
/** end :TCHAP: **/
} else {
logger.error("Unknown conf call type: " + type);
}
Expand Down Expand Up @@ -1036,6 +1074,9 @@ export default class LegacyCallHandler extends EventEmitter {

call.answer();
this.setActiveCallRoomId(roomId);
/** :TCHAP: metrics-call **/
this.trackCallStart(call)
/** end :TCHAP: **/
dis.dispatch<ViewRoomPayload>({
action: Action.ViewRoom,
room_id: roomId,
Expand Down
17 changes: 17 additions & 0 deletions patches/metrics-call/matrix-js-sdk+33.1.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
diff --git a/node_modules/matrix-js-sdk/src/webrtc/call.ts b/node_modules/matrix-js-sdk/src/webrtc/call.ts
index 3f5c61a..a223f4a 100644
--- a/node_modules/matrix-js-sdk/src/webrtc/call.ts
+++ b/node_modules/matrix-js-sdk/src/webrtc/call.ts
@@ -1572,6 +1572,12 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
return callOnHold;
}

+ // :TCHAP: metrics-call-js-sdk
+ public getCallStartTime(): number| undefined {
+ return this.callStartTime!;
+ }
+ // end :TCHAP:
+
/**
* Sends a DTMF digit to the other party
* @param digit - The digit (nb. string - '#' and '*' are dtmf too)
6 changes: 6 additions & 0 deletions patches/patches.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
"src/http-api/fetch.ts",
"src/sync.ts"
]
},
"metrics-call": {
"package": "matrix-js-sdk",
"files": [
"src/webrtc/call.ts"
]
}
}
7 changes: 7 additions & 0 deletions patches/subtree-modifications.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@
"src/hooks/useUserOnboardingTasks.ts",
"src/hooks/useUserOnboardingContext.ts"

]
},
"metrics-call": {
"issue": "https://github.com/tchapgouv/tchap-web-v4/issues/1088",
"files": [
"src/LegacyCallHandler.tsx"

]
}
}

0 comments on commit ad05403

Please sign in to comment.