opnform/client/composables/useAmplitude.js

39 lines
993 B
JavaScript

import amplitude from 'amplitude-js'
import config from '~/opnform.config.js'
export default () => {
const amplitudeClient = config.amplitude_code ? amplitude.getInstance() : null;
if (amplitudeClient) {
amplitudeClient.init(config.amplitude_code)
}
const logEvent = function (eventName, eventData) {
if (!config.production || !amplitudeClient) {
console.log('[DEBUG] Amplitude logged event:', eventName, eventData)
}
if (eventData && typeof eventData !== 'object') {
throw new Error('Amplitude event value must be an object.')
}
amplitudeClient.logEvent(eventName, eventData)
}
const setUser = function (user) {
if (!amplitudeClient) return
amplitudeClient.setUserId(user.id)
amplitudeClient.setUserProperties({
email: user.email,
subscribed: user.is_subscribed,
enterprise_subscription: user.has_enterprise_subscription
})
}
return {
logEvent,
setUser,
amplitude: amplitudeClient
}
}