opnform-host-nginx/client/plugins/amplitude.js

26 lines
690 B
JavaScript
Raw Normal View History

2023-12-09 15:47:03 +01:00
// Hook function used by event listener
function hookLogEvent(binding) {
2023-12-09 15:47:03 +01:00
const modifiers = Object.keys(binding.modifiers)
if (modifiers.length !== 1) {
throw new Error(
"Amplitude directive takes only one modifier which is the event name.",
)
2023-12-09 15:47:03 +01:00
}
const eventName = modifiers[0]
useAmplitude().logEvent(eventName, binding.value)
2023-12-09 15:47:03 +01:00
}
export default defineNuxtPlugin((nuxtApp) => {
2023-12-09 15:47:03 +01:00
// Doing something with nuxtApp
const registeredListeners = {}
nuxtApp.vueApp.directive("track", {
beforeMount(el, binding) {
2023-12-09 15:47:03 +01:00
registeredListeners[el] = () => {
hookLogEvent(binding)
}
el.addEventListener("click", registeredListeners[el])
},
2023-12-09 15:47:03 +01:00
})
})