Add Posthog events for Call reconnect including the reason

This commit is contained in:
fkwp
2026-05-07 10:36:36 +02:00
parent 147d0f96e0
commit 4a0759e9b8
7 changed files with 102 additions and 2 deletions

View File

@@ -560,6 +560,7 @@ export function createCallViewModel$(
connectionManager,
matrixRTCSession,
localTransport$,
callId: matrixRoom.roomId,
logger: logger.getChild(`[${Date.now()}]`),
});

View File

@@ -22,6 +22,7 @@ import {
switchMap,
of,
delay,
combineLatest,
} from "rxjs";
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
@@ -36,9 +37,15 @@ import { type NodeStyleEventEmitter } from "../../../utils/test";
*/
const logger = rootLogger.getChild("[HomeserverConnected]");
export type HomeserverDisconnectReason =
| "syncing"
| "membershipConnected"
| "certainlyConnected";
export interface HomeserverConnected {
combined$: Behavior<boolean>;
rtsSession$: Behavior<Status>;
disconnectReason$: Behavior<HomeserverDisconnectReason | null>;
}
/**
@@ -116,5 +123,17 @@ export function createHomeserverConnected$(
),
);
return { combined$, rtsSession$ };
const disconnectReason$ = scope.behavior(
combineLatest([syncing$, membershipConnected$, certainlyConnected$]).pipe(
map(([syncing, membership, certainly]) => {
if (!syncing) return "syncing" as const;
if (!membership) return "membershipConnected" as const;
if (!certainly) return "certainlyConnected" as const;
return null;
}),
),
null,
);
return { combined$, rtsSession$, disconnectReason$ };
}

View File

@@ -217,7 +217,9 @@ describe("LocalMembership", () => {
homeserverConnected: {
combined$: constant(true),
rtsSession$: constant(RTCMemberStatus.Connected),
disconnectReason$: constant(null),
},
callId: "!test-room-id:example.org",
};
it("throws error on missing RTC config error", () => {

View File

@@ -25,6 +25,7 @@ import {
catchError,
combineLatest,
distinctUntilChanged,
filter,
from,
fromEvent,
map,
@@ -34,6 +35,7 @@ import {
startWith,
switchMap,
tap,
withLatestFrom,
} from "rxjs";
import { type Logger } from "matrix-js-sdk/lib/logger";
import { deepCompare } from "matrix-js-sdk/lib/utils";
@@ -129,6 +131,7 @@ interface Props {
createPublisherFactory: (connection: Connection) => Publisher;
joinMatrixRTC: (transport: LivekitTransportConfig) => void;
homeserverConnected: HomeserverConnected;
callId: string;
localTransport$: Behavior<LocalTransport>;
matrixRTCSession: Pick<
MatrixRTCSession,
@@ -152,6 +155,7 @@ interface Props {
* @param props.logger The logger to use.
* @param props.muteStates The mute states for video and audio.
* @param props.matrixRTCSession The matrix RTC session to join.
* @param props.callId The room ID used as the call identifier in analytics events.
* @returns
* - publisher: The handle to create tracks and publish them to the room.
* - connected$: the current connection state. Including matrix server and livekit server connection. (only considering the livekit server we are using for our own media publication)
@@ -169,6 +173,7 @@ export const createLocalMembership$ = ({
logger: parentLogger,
muteStates,
matrixRTCSession,
callId,
}: Props): {
/**
* This request to start audio and video tracks.
@@ -519,6 +524,19 @@ export const createLocalMembership$ = ({
false,
);
reconnecting$
.pipe(
distinctUntilChanged(),
filter(Boolean),
withLatestFrom(homeserverConnected.disconnectReason$, localConnectionState$),
scope.bind(),
)
.subscribe(([_, homeserverReason]) => {
const reason = homeserverReason !== null ? homeserverReason : "livekit";
PosthogAnalytics.instance.eventCallReconnecting.track(callId, reason);
PosthogAnalytics.instance.eventCallEnded.cacheReconnecting(reason);
});
// inform the widget about the connect and disconnect intent from the user.
scope
.behavior(joinAndPublishRequested$.pipe(pairwise(), scope.bind()), [