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