track connection time on posthog

Signed-off-by: Timo K <toger5@hotmail.de>
This commit is contained in:
Timo K
2024-04-15 19:27:42 +02:00
parent fc4ced7bb3
commit 271656bc76
3 changed files with 46 additions and 0 deletions

View File

@@ -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();
}

View File

@@ -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;
websockedDuration: 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 websockedDuration = this.websocketConnected - this.connectStart;
const peerConnectionDuration = now - this.websocketConnected;
PosthogAnalytics.instance.trackEvent<CallConnectDuration>({
eventName: "CallConnectDuration",
totalDuration,
websockedDuration: this.websocketConnected - this.connectStart,
peerConnectionDuration: Date.now() - this.websocketConnected,
});
if (options.log)
logger.log(
`Time to connect:\ntotal: ${totalDuration}ms\npeerConnection: ${websockedDuration}ms\nwebsocket: ${peerConnectionDuration}ms`,
);
}
}

View File

@@ -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<void> {
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,8 @@ async function connectAndPublish(
websocketTimeout: window.websocketTimeout ?? 45000,
});
tracker.track({ log: true });
if (micTrack) {
logger.info(`Publishing precreated mic track`);
await livekitRoom.localParticipant.publishTrack(micTrack, {