From 1c5b483a7ee2d87a5b1834722586759218ab57c2 Mon Sep 17 00:00:00 2001 From: Timo K Date: Tue, 7 Apr 2026 14:11:55 +0200 Subject: [PATCH] Only send callEndedEvent if the user has joined the call. --- src/analytics/PosthogEvents.ts | 50 +++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/analytics/PosthogEvents.ts b/src/analytics/PosthogEvents.ts index f0f059f5..5553829a 100644 --- a/src/analytics/PosthogEvents.ts +++ b/src/analytics/PosthogEvents.ts @@ -27,8 +27,8 @@ interface CallEnded extends IPosthogEvent { } export class CallEndedTracker { - private cache: { startTime: Date; maxParticipantsCount: number } = { - startTime: new Date(0), + private cache: { startTime?: Date; maxParticipantsCount: number } = { + startTime: undefined, maxParticipantsCount: 0, }; @@ -49,26 +49,32 @@ export class CallEndedTracker { sendInstantly: boolean, rtcSession: MatrixRTCSession, ): void { - PosthogAnalytics.instance.trackEvent( - { - eventName: "CallEnded", - callId: callId, - callParticipantsMax: this.cache.maxParticipantsCount, - callParticipantsOnLeave: callParticipantsNow, - callDuration: (Date.now() - this.cache.startTime.getTime()) / 1000, - roomEventEncryptionKeysSent: - rtcSession.statistics.counters.roomEventEncryptionKeysSent, - roomEventEncryptionKeysReceived: - rtcSession.statistics.counters.roomEventEncryptionKeysReceived, - roomEventEncryptionKeysReceivedAverageAge: - rtcSession.statistics.counters.roomEventEncryptionKeysReceived > 0 - ? rtcSession.statistics.totals - .roomEventEncryptionKeysReceivedTotalAge / - rtcSession.statistics.counters.roomEventEncryptionKeysReceived - : 0, - }, - { send_instantly: sendInstantly }, - ); + if (this.cache.startTime) { + PosthogAnalytics.instance.trackEvent( + { + eventName: "CallEnded", + callId: callId, + callParticipantsMax: this.cache.maxParticipantsCount, + callParticipantsOnLeave: callParticipantsNow, + callDuration: (Date.now() - this.cache.startTime.getTime()) / 1000, + roomEventEncryptionKeysSent: + rtcSession.statistics.counters.roomEventEncryptionKeysSent, + roomEventEncryptionKeysReceived: + rtcSession.statistics.counters.roomEventEncryptionKeysReceived, + roomEventEncryptionKeysReceivedAverageAge: + rtcSession.statistics.counters.roomEventEncryptionKeysReceived > 0 + ? rtcSession.statistics.totals + .roomEventEncryptionKeysReceivedTotalAge / + rtcSession.statistics.counters.roomEventEncryptionKeysReceived + : 0, + }, + { send_instantly: sendInstantly }, + ); + } else { + logger.warn( + "[PosthogEvents] Failed to send posthog callEnded event due to missing startTime", + ); + } } }