send posthog callEnded events instantly in embedded mode (prohibit missing events) (#816)
Signed-off-by: Timo K <timok@element.io> Co-authored-by: Timo K <timok@element.io>
This commit is contained in:
parent
1f5c22e325
commit
05be247946
3 changed files with 21 additions and 15 deletions
|
@ -54,10 +54,6 @@ export interface IPosthogEvent {
|
||||||
$set_once?: void;
|
$set_once?: void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IPostHogEventOptions {
|
|
||||||
timestamp?: Date;
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum Anonymity {
|
export enum Anonymity {
|
||||||
Disabled,
|
Disabled,
|
||||||
Anonymous,
|
Anonymous,
|
||||||
|
@ -373,7 +369,7 @@ export class PosthogAnalytics {
|
||||||
|
|
||||||
public async trackEvent<E extends IPosthogEvent>(
|
public async trackEvent<E extends IPosthogEvent>(
|
||||||
{ eventName, ...properties }: E,
|
{ eventName, ...properties }: E,
|
||||||
options?: IPostHogEventOptions
|
options?: CaptureOptions
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (this.identificationPromise) {
|
if (this.identificationPromise) {
|
||||||
// only make calls to posthog after the identificaion is done
|
// only make calls to posthog after the identificaion is done
|
||||||
|
|
|
@ -45,14 +45,17 @@ export class CallEndedTracker {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
track(callId: string, callParticipantsNow: number) {
|
track(callId: string, callParticipantsNow: number, sendInstantly: boolean) {
|
||||||
PosthogAnalytics.instance.trackEvent<CallEnded>({
|
PosthogAnalytics.instance.trackEvent<CallEnded>(
|
||||||
eventName: "CallEnded",
|
{
|
||||||
callId: callId,
|
eventName: "CallEnded",
|
||||||
callParticipantsMax: this.cache.maxParticipantsCount,
|
callId: callId,
|
||||||
callParticipantsOnLeave: callParticipantsNow,
|
callParticipantsMax: this.cache.maxParticipantsCount,
|
||||||
callDuration: (Date.now() - this.cache.startTime.getTime()) / 1000,
|
callParticipantsOnLeave: callParticipantsNow,
|
||||||
});
|
callDuration: (Date.now() - this.cache.startTime.getTime()) / 1000,
|
||||||
|
},
|
||||||
|
{ send_instantly: sendInstantly }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -176,7 +176,7 @@ export function GroupCallView({
|
||||||
const [left, setLeft] = useState(false);
|
const [left, setLeft] = useState(false);
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
const onLeave = useCallback(() => {
|
const onLeave = useCallback(async () => {
|
||||||
setLeft(true);
|
setLeft(true);
|
||||||
|
|
||||||
let participantCount = 0;
|
let participantCount = 0;
|
||||||
|
@ -184,13 +184,20 @@ export function GroupCallView({
|
||||||
participantCount += deviceMap.size;
|
participantCount += deviceMap.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In embedded/widget mode the iFrame will be killed right after the call ended prohibiting the posthog event from getting sent,
|
||||||
|
// therefore we want the event to be sent instantly without getting queued/batched.
|
||||||
|
const sendInstantly = !!widget;
|
||||||
PosthogAnalytics.instance.eventCallEnded.track(
|
PosthogAnalytics.instance.eventCallEnded.track(
|
||||||
groupCall.groupCallId,
|
groupCall.groupCallId,
|
||||||
participantCount
|
participantCount,
|
||||||
|
sendInstantly
|
||||||
);
|
);
|
||||||
|
|
||||||
leave();
|
leave();
|
||||||
if (widget) {
|
if (widget) {
|
||||||
|
// we need to wait until the callEnded event is tracked. Otherwise the iFrame gets killed before tracking the event.
|
||||||
|
await new Promise((resolve) => window.setTimeout(resolve, 500)); // 500ms
|
||||||
|
PosthogAnalytics.instance.logout();
|
||||||
widget.api.transport.send(ElementWidgetActions.HangupCall, {});
|
widget.api.transport.send(ElementWidgetActions.HangupCall, {});
|
||||||
widget.api.setAlwaysOnScreen(false);
|
widget.api.setAlwaysOnScreen(false);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue