element-call/src/analytics/PosthogEvents.ts
Timo 2a6981c58d
Add quality survey at the end of the call (#1084)
Signed-off-by: Timo K <toger5@hotmail.de>
Co-authored-by: Robin <robin@robin.town>
2023-06-07 16:22:44 +02:00

183 lines
4.3 KiB
TypeScript

/*
Copyright 2022 The New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {
IPosthogEvent,
PosthogAnalytics,
RegistrationType,
} from "./PosthogAnalytics";
interface CallEnded extends IPosthogEvent {
eventName: "CallEnded";
callId: string;
callParticipantsOnLeave: number;
callParticipantsMax: number;
callDuration: number;
}
export class CallEndedTracker {
private cache: { startTime: Date; maxParticipantsCount: number } = {
startTime: new Date(0),
maxParticipantsCount: 0,
};
cacheStartCall(time: Date) {
this.cache.startTime = time;
}
cacheParticipantCountChanged(count: number) {
this.cache.maxParticipantsCount = Math.max(
count,
this.cache.maxParticipantsCount
);
}
track(callId: string, callParticipantsNow: number, sendInstantly: boolean) {
PosthogAnalytics.instance.trackEvent<CallEnded>(
{
eventName: "CallEnded",
callId: callId,
callParticipantsMax: this.cache.maxParticipantsCount,
callParticipantsOnLeave: callParticipantsNow,
callDuration: (Date.now() - this.cache.startTime.getTime()) / 1000,
},
{ send_instantly: sendInstantly }
);
}
}
interface CallStarted extends IPosthogEvent {
eventName: "CallStarted";
callId: string;
}
export class CallStartedTracker {
track(callId: string) {
PosthogAnalytics.instance.trackEvent<CallStarted>({
eventName: "CallStarted",
callId: callId,
});
}
}
interface Signup extends IPosthogEvent {
eventName: "Signup";
signupDuration: number;
}
export class SignupTracker {
private cache: { signupStart: Date; signupEnd: Date } = {
signupStart: new Date(0),
signupEnd: new Date(0),
};
cacheSignupStart(time: Date) {
this.cache.signupStart = time;
}
getSignupEndTime() {
return this.cache.signupEnd;
}
cacheSignupEnd(time: Date) {
this.cache.signupEnd = time;
}
track() {
PosthogAnalytics.instance.trackEvent<Signup>({
eventName: "Signup",
signupDuration: Date.now() - this.cache.signupStart.getTime(),
});
PosthogAnalytics.instance.setRegistrationType(RegistrationType.Registered);
}
}
interface Login extends IPosthogEvent {
eventName: "Login";
}
export class LoginTracker {
track() {
PosthogAnalytics.instance.trackEvent<Login>({
eventName: "Login",
});
PosthogAnalytics.instance.setRegistrationType(RegistrationType.Registered);
}
}
interface MuteMicrophone {
eventName: "MuteMicrophone";
targetMuteState: "mute" | "unmute";
callId: string;
}
export class MuteMicrophoneTracker {
track(targetIsMute: boolean, callId: string) {
PosthogAnalytics.instance.trackEvent<MuteMicrophone>({
eventName: "MuteMicrophone",
targetMuteState: targetIsMute ? "mute" : "unmute",
callId,
});
}
}
interface MuteCamera {
eventName: "MuteCamera";
targetMuteState: "mute" | "unmute";
callId: string;
}
export class MuteCameraTracker {
track(targetIsMute: boolean, callId: string) {
PosthogAnalytics.instance.trackEvent<MuteCamera>({
eventName: "MuteCamera",
targetMuteState: targetIsMute ? "mute" : "unmute",
callId,
});
}
}
interface UndecryptableToDeviceEvent {
eventName: "UndecryptableToDeviceEvent";
callId: string;
}
export class UndecryptableToDeviceEventTracker {
track(callId: string) {
PosthogAnalytics.instance.trackEvent<UndecryptableToDeviceEvent>({
eventName: "UndecryptableToDeviceEvent",
callId,
});
}
}
interface QualitySurveyEvent {
eventName: "QualitySurvey";
callId: string;
feedbackText: string;
stars: number;
}
export class QualitySurveyEventTracker {
track(callId: string, feedbackText: string, stars: number) {
PosthogAnalytics.instance.trackEvent<QualitySurveyEvent>({
eventName: "QualitySurvey",
callId,
feedbackText,
stars,
});
}
}