jiti-meet/react/features/analytics/handlers/AmplitudeHandler.ts

113 lines
3.0 KiB
TypeScript
Raw Normal View History

import logger from '../logger';
2022-09-06 12:51:50 +00:00
import AbstractHandler, { IEvent } from './AbstractHandler';
import { fixDeviceID } from './amplitude/fixDeviceID';
import amplitude from './amplitude/lib';
2019-01-03 13:54:02 +00:00
/**
* Analytics handler for Amplitude.
*/
export default class AmplitudeHandler extends AbstractHandler {
2022-09-06 12:51:50 +00:00
_deviceId: string;
_userId: Object;
2019-01-03 13:54:02 +00:00
/**
* Creates new instance of the Amplitude analytics handler.
*
* @param {Object} options -
* @param {string} options.amplitudeAPPKey - The Amplitude app key required
* by the Amplitude API.
*/
2022-09-06 12:51:50 +00:00
constructor(options: any) {
super(options);
2019-01-03 13:54:02 +00:00
const { amplitudeAPPKey, user } = options;
2019-01-03 13:54:02 +00:00
this._enabled = true;
2022-09-06 12:51:50 +00:00
const onError = (e: Error) => {
logger.error('Error initializing Amplitude', e);
this._enabled = false;
};
if (navigator.product === 'ReactNative') {
amplitude.getInstance().init(amplitudeAPPKey); // @ts-ignore
fixDeviceID(amplitude.getInstance()).then(() => {
amplitude.getInstance().getDeviceId()
// @ts-ignore
2022-09-06 12:51:50 +00:00
.then((deviceId: string) => {
this._deviceId = deviceId;
});
});
} else {
const amplitudeOptions = {
includeReferrer: true,
onError
};
// @ts-ignore
amplitude.getInstance().init(amplitudeAPPKey, undefined, amplitudeOptions);
fixDeviceID(amplitude.getInstance());
}
2019-05-29 15:24:50 +00:00
if (user) {
this._userId = user;
amplitude.getInstance().setUserId(user);
2019-05-29 15:24:50 +00:00
}
2019-01-03 13:54:02 +00:00
}
/**
* Sets the Amplitude user properties.
*
* @param {Object} userProps - The user portperties.
2019-01-03 13:54:02 +00:00
* @returns {void}
*/
2022-11-01 12:36:32 +00:00
setUserProperties(userProps: any) {
2019-01-03 13:54:02 +00:00
if (this._enabled) {
amplitude.getInstance().setUserProperties(userProps);
2019-01-03 13:54:02 +00:00
}
}
/**
* Sends an event to Amplitude. The format of the event is described
* in AnalyticsAdapter in lib-jitsi-meet.
*
* @param {Object} event - The event in the format specified by
* lib-jitsi-meet.
* @returns {void}
*/
2022-09-06 12:51:50 +00:00
sendEvent(event: IEvent) {
2019-01-03 13:54:02 +00:00
if (this._shouldIgnore(event)) {
return;
}
2022-11-01 12:36:32 +00:00
// @ts-ignore
amplitude.getInstance().logEvent(this._extractName(event) ?? '', event);
2019-01-03 13:54:02 +00:00
}
/**
* Return amplitude identity information.
*
* @returns {Object}
*/
getIdentityProps() {
if (navigator.product === 'ReactNative') {
return {
deviceId: this._deviceId,
userId: this._userId
};
}
return {
sessionId: amplitude.getInstance().getSessionId(),
2022-11-01 12:36:32 +00:00
// @ts-ignore
deviceId: amplitude.getInstance().options.deviceId,
2022-11-01 12:36:32 +00:00
// @ts-ignore
userId: amplitude.getInstance().options.userId
};
}
2019-01-03 13:54:02 +00:00
}