From 379050ff8063577e1f24bb2c5c212b70916219fe Mon Sep 17 00:00:00 2001 From: Timo <16718859+toger5@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:21:37 +0200 Subject: [PATCH] Add back posthog connection time tracking (#2316) Signed-off-by: Timo K Co-authored-by: Robin --- src/analytics/PosthogAnalytics.ts | 2 ++ src/analytics/PosthogEvents.ts | 36 +++++++++++++++++++++++++++++ src/livekit/useECConnectionState.ts | 10 ++++++++ 3 files changed, 48 insertions(+) diff --git a/src/analytics/PosthogAnalytics.ts b/src/analytics/PosthogAnalytics.ts index c1778f8bd..cfeb1e7a1 100644 --- a/src/analytics/PosthogAnalytics.ts +++ b/src/analytics/PosthogAnalytics.ts @@ -31,6 +31,7 @@ import { UndecryptableToDeviceEventTracker, QualitySurveyEventTracker, CallDisconnectedEventTracker, + CallConnectDurationTracker, } from "./PosthogEvents"; import { Config } from "../config/Config"; import { getUrlParams } from "../UrlParams"; @@ -444,4 +445,5 @@ export class PosthogAnalytics { public eventUndecryptableToDevice = new UndecryptableToDeviceEventTracker(); public eventQualitySurvey = new QualitySurveyEventTracker(); public eventCallDisconnected = new CallDisconnectedEventTracker(); + public eventCallConnectDuration = new CallConnectDurationTracker(); } diff --git a/src/analytics/PosthogEvents.ts b/src/analytics/PosthogEvents.ts index c200f74b9..778392bab 100644 --- a/src/analytics/PosthogEvents.ts +++ b/src/analytics/PosthogEvents.ts @@ -15,6 +15,7 @@ limitations under the License. */ import { DisconnectReason } from "livekit-client"; +import { logger } from "matrix-js-sdk/src/logger"; import { IPosthogEvent, @@ -201,3 +202,38 @@ export class CallDisconnectedEventTracker { }); } } + +interface CallConnectDuration extends IPosthogEvent { + eventName: "CallConnectDuration"; + totalDuration: number; + websocketDuration: number; + peerConnectionDuration: number; +} + +export class CallConnectDurationTracker { + private connectStart = 0; + private websocketConnected = 0; + public cacheConnectStart(): void { + this.connectStart = Date.now(); + } + public cacheWsConnect(): void { + this.websocketConnected = Date.now(); + } + + public track(options = { log: false }): void { + const now = Date.now(); + const totalDuration = now - this.connectStart; + const websocketDuration = this.websocketConnected - this.connectStart; + const peerConnectionDuration = now - this.websocketConnected; + PosthogAnalytics.instance.trackEvent({ + eventName: "CallConnectDuration", + totalDuration, + websocketDuration, + peerConnectionDuration, + }); + if (options.log) + logger.log( + `Time to connect:\ntotal: ${totalDuration}ms\npeerConnection: ${websocketDuration}ms\nwebsocket: ${peerConnectionDuration}ms`, + ); + } +} diff --git a/src/livekit/useECConnectionState.ts b/src/livekit/useECConnectionState.ts index e874fca1f..ae9160b19 100644 --- a/src/livekit/useECConnectionState.ts +++ b/src/livekit/useECConnectionState.ts @@ -27,6 +27,7 @@ import { logger } from "matrix-js-sdk/src/logger"; import * as Sentry from "@sentry/react"; import { SFUConfig, sfuConfigEquals } from "./openIDSFU"; +import { PosthogAnalytics } from "../analytics/PosthogAnalytics"; declare global { interface Window { @@ -131,6 +132,11 @@ async function connectAndPublish( micTrack: LocalTrack | undefined, screenshareTracks: MediaStreamTrack[], ): Promise { + const tracker = PosthogAnalytics.instance.eventCallConnectDuration; + // Track call connect duration + tracker.cacheConnectStart(); + livekitRoom.once(RoomEvent.SignalConnected, tracker.cacheWsConnect); + await livekitRoom!.connect(sfuConfig!.url, sfuConfig!.jwt, { // Due to stability issues on Firefox we are testing the effect of different // timeouts, and allow these values to be set through the console @@ -138,6 +144,10 @@ async function connectAndPublish( websocketTimeout: window.websocketTimeout ?? 45000, }); + // remove listener in case the connect promise rejects before `SignalConnected` is emitted. + livekitRoom.off(RoomEvent.SignalConnected, tracker.cacheWsConnect); + tracker.track({ log: true }); + if (micTrack) { logger.info(`Publishing precreated mic track`); await livekitRoom.localParticipant.publishTrack(micTrack, {