2019-01-03 13:54:02 +00:00
|
|
|
import AbstractHandler from './AbstractHandler';
|
2020-01-30 16:57:41 +00:00
|
|
|
import { amplitude, fixDeviceID } from './amplitude';
|
2019-01-03 13:54:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Analytics handler for Amplitude.
|
|
|
|
*/
|
2019-01-24 15:37:55 +00:00
|
|
|
export default class AmplitudeHandler extends AbstractHandler {
|
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.
|
|
|
|
*/
|
|
|
|
constructor(options) {
|
2019-08-30 15:16:05 +00:00
|
|
|
super(options);
|
2019-01-03 13:54:02 +00:00
|
|
|
|
2019-05-29 15:24:50 +00:00
|
|
|
const { amplitudeAPPKey, host, user } = options;
|
2019-01-03 13:54:02 +00:00
|
|
|
|
|
|
|
if (!amplitudeAPPKey) {
|
2019-04-30 19:10:18 +00:00
|
|
|
throw new Error('Failed to initialize Amplitude handler, no APP key');
|
2019-01-03 13:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._enabled = true;
|
|
|
|
|
2019-01-24 15:37:55 +00:00
|
|
|
this._amplitudeOptions = {
|
|
|
|
host
|
|
|
|
};
|
|
|
|
|
2019-11-29 15:01:46 +00:00
|
|
|
amplitude.getInstance(this._amplitudeOptions).init(amplitudeAPPKey, undefined, { includeReferrer: true });
|
2020-01-30 16:57:41 +00:00
|
|
|
fixDeviceID(amplitude.getInstance(this._amplitudeOptions));
|
2019-05-29 15:24:50 +00:00
|
|
|
|
|
|
|
if (user) {
|
|
|
|
amplitude.getInstance(this._amplitudeOptions).setUserId(user);
|
|
|
|
}
|
2019-01-03 13:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Amplitude user properties.
|
|
|
|
*
|
2019-01-03 14:48:41 +00:00
|
|
|
* @param {Object} userProps - The user portperties.
|
2019-01-03 13:54:02 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-01-03 14:48:41 +00:00
|
|
|
setUserProperties(userProps) {
|
2019-01-03 13:54:02 +00:00
|
|
|
if (this._enabled) {
|
2019-01-24 15:37:55 +00:00
|
|
|
amplitude.getInstance(this._amplitudeOptions)
|
|
|
|
.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}
|
|
|
|
*/
|
|
|
|
sendEvent(event) {
|
|
|
|
if (this._shouldIgnore(event)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-24 15:37:55 +00:00
|
|
|
amplitude.getInstance(this._amplitudeOptions).logEvent(
|
2019-01-03 13:54:02 +00:00
|
|
|
this._extractName(event),
|
|
|
|
event);
|
|
|
|
}
|
2020-07-15 15:22:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return amplitude identity information.
|
|
|
|
*
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
getIdentityProps() {
|
|
|
|
return {
|
|
|
|
sessionId: amplitude.getInstance(this._amplitudeOptions).getSessionId(),
|
|
|
|
deviceId: amplitude.getInstance(this._amplitudeOptions).options.deviceId,
|
|
|
|
userId: amplitude.getInstance(this._amplitudeOptions).options.userId
|
|
|
|
};
|
|
|
|
}
|
2019-01-03 13:54:02 +00:00
|
|
|
}
|