Repository URL to install this package:
|
Version:
0.6.0 ▾
|
import amplitude from 'amplitude-js/amplitude';
/**
* Initializes Amplitude
*
* @param {String} amplitudeApiKey Public API key that should be provided by the consuming frontend.
* @param {Function} callback If provided, will be called after successful initialization.
*/
export const initAmplitude = (amplitudeApiKey, callback = null) => {
// passing 2x null here since we don't pass the user ID with the init and don't need any aditional configuration for now
amplitude.getInstance().init(amplitudeApiKey, null, null, callback);
};
/**
* Logs an amplitude event
*
* @param {Object} trackingData The data that needs to be tracked.
* @param {Function} callback If provided, will be called after event has been successfully logged.
*/
export const logAmplitudeEvent = (trackingData, callback = null) => {
const { eventType, eventName, eventProperties } = trackingData;
const eventProps = {
...eventProperties,
'Event Type': eventType,
};
amplitude.getInstance().logEvent(eventName, eventProps, callback);
};
/**
* Sets the amplitude user ID
*
* @param {String} userId The doodle user ID that we save as the amplitude user ID
*/
export const onSetAmplitudeUserId = userId => {
amplitude.getInstance().setUserId(userId);
};
/**
* Sets amplitude user properties
*
* @param {Object} userProperties Proerties we want to save for the user.
* @param {Boolean} once If set to true, this will only be set once, initial calls will be ignored.
*/
export const setAmplitudeUserProperties = (userProperties, once = false) => {
Object.keys(userProperties).forEach(key => {
const identify = once
? new amplitude.Identify().setOnce(key, userProperties[key])
: new amplitude.Identify().set(key, userProperties[key]);
amplitude.getInstance().identify(identify);
});
};
/**
* Removes a property from an amplitude user.
*
* @param {String} userProperty Name of the property we want to remove.
*/
export const unsetAmplitudeUserProperty = userProperty => {
const identify = new amplitude.Identify().unset(userProperty);
amplitude.getInstance().identify(identify);
};