opnform/resources/js/plugins/amplitude.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-09-20 19:59:52 +00:00
// Log event function used to log event. Can be used when not using the directive.
2023-10-19 08:46:04 +00:00
const logEvent = function (eventName, eventData) {
2022-09-20 19:59:52 +00:00
if (!window.amplitude) return
if (eventData && typeof eventData !== 'object') {
throw new Error('Amplitude event value must be an object.')
}
2023-10-19 08:46:04 +00:00
console.log('in', window.config.production)
2022-09-20 19:59:52 +00:00
if (!window.config.production) {
console.log('[DEBUG] Amplitude logged event:', eventName, eventData)
} else {
window.amplitude.getInstance().logEvent(eventName, eventData)
}
}
// Hook function used by event listener
function hookLogEvent (binding) {
const modifiers = Object.keys(binding.modifiers)
if (modifiers.length !== 1) {
throw new Error('Amplitude directive takes only one modifier which is the event name.')
}
const eventName = modifiers[0]
2023-10-19 08:46:04 +00:00
logEvent(eventName, binding.value)
2022-09-20 19:59:52 +00:00
}
2023-10-19 08:46:04 +00:00
// Used in vue-plugins.js
export function registerLogEventOnApp (app) {
app.config.globalProperties.$logEvent = logEvent
// Register directive to log event
const registeredListeners = {}
app.directive('track', {
beforeMount (el, binding, vnode) {
registeredListeners[el] = () => {
hookLogEvent(binding)
}
el.addEventListener('click', registeredListeners[el])
2022-09-20 19:59:52 +00:00
}
2023-10-19 08:46:04 +00:00
})
}