opnform/client/composables/useAmplitude.js

39 lines
993 B
JavaScript
Raw Normal View History

2023-12-09 14:47:03 +00:00
import amplitude from 'amplitude-js'
import config from '~/opnform.config.js'
export default () => {
2023-12-16 18:21:03 +00:00
const amplitudeClient = config.amplitude_code ? amplitude.getInstance() : null;
if (amplitudeClient) {
amplitudeClient.init(config.amplitude_code)
}
2023-12-09 14:47:03 +00:00
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) {
2023-12-16 18:21:03 +00:00
if (!amplitudeClient) return
2023-12-09 14:47:03 +00:00
amplitudeClient.setUserId(user.id)
amplitudeClient.setUserProperties({
2023-12-16 18:21:03 +00:00
email: user.email,
subscribed: user.is_subscribed,
enterprise_subscription: user.has_enterprise_subscription
2023-12-09 14:47:03 +00:00
})
}
return {
logEvent,
setUser,
amplitude: amplitudeClient
}
}