opnform/client/composables/useAmplitude.js

41 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-12-09 14:47:03 +00:00
import amplitude from 'amplitude-js'
export const useAmplitude = () => {
const config = useRuntimeConfig()
const amplitudeCode = config.public.amplitudeCode
const amplitudeClient = amplitudeCode ? amplitude.getInstance() : null;
2023-12-16 18:21:03 +00:00
if (amplitudeClient) {
amplitudeClient.init(amplitudeCode)
2023-12-16 18:21:03 +00:00
}
2023-12-09 14:47:03 +00:00
const logEvent = function (eventName, eventData) {
if (!config.public.env === 'production' || !amplitudeClient) {
2023-12-09 14:47:03 +00:00
console.log('[DEBUG] Amplitude logged event:', eventName, eventData)
return
2023-12-09 14:47:03 +00:00
}
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
}
}