2021-09-16 11:18:16 -04:00
|
|
|
import * as eventTracking from '../../infrastructure/event-tracking'
|
2023-11-06 10:28:25 -05:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2021-09-16 11:18:16 -04:00
|
|
|
|
|
|
|
function setupEventTracking(el) {
|
|
|
|
const key = el.getAttribute('event-tracking')
|
|
|
|
const action = el.getAttribute('event-tracking-action') || key
|
|
|
|
const label = el.getAttribute('event-tracking-label') || ''
|
|
|
|
const gaCategory = el.getAttribute('event-tracking-ga')
|
|
|
|
const sendMB = el.getAttribute('event-tracking-mb')
|
|
|
|
const trigger = el.getAttribute('event-tracking-trigger')
|
|
|
|
const sendOnce = el.getAttribute('event-tracking-send-once')
|
2022-05-19 05:13:25 -04:00
|
|
|
const element = el.getAttribute('event-tracking-element')
|
2021-09-16 11:18:16 -04:00
|
|
|
|
|
|
|
function submit() {
|
2024-08-30 09:06:30 -04:00
|
|
|
const segmentation = JSON.parse(
|
|
|
|
el.getAttribute('event-segmentation') || '{}'
|
|
|
|
)
|
|
|
|
segmentation.page = window.location.pathname
|
|
|
|
|
2022-05-19 05:13:25 -04:00
|
|
|
if (element === 'checkbox') {
|
|
|
|
segmentation.checkbox = el.checked ? 'checked' : 'unchecked'
|
|
|
|
} else if (element === 'select') {
|
|
|
|
segmentation.selectValue = el.value
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:18:16 -04:00
|
|
|
if (sendMB) {
|
|
|
|
if (sendOnce) {
|
|
|
|
eventTracking.sendMBOnce(key, segmentation)
|
|
|
|
} else {
|
|
|
|
eventTracking.sendMB(key, segmentation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gaCategory) {
|
|
|
|
if (sendOnce) {
|
|
|
|
eventTracking.sendOnce(gaCategory, action, label)
|
|
|
|
} else {
|
|
|
|
eventTracking.send(gaCategory, action, label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let timer
|
|
|
|
let timeoutAmt = 500
|
|
|
|
switch (trigger) {
|
|
|
|
case 'click':
|
|
|
|
el.addEventListener('click', () => submit())
|
|
|
|
break
|
|
|
|
case 'hover':
|
|
|
|
if (el.getAttribute('event-hover-amt')) {
|
|
|
|
timeoutAmt = parseInt(el.getAttribute('event-hover-amt'), 10)
|
|
|
|
}
|
|
|
|
el.addEventListener('mouseenter', () => {
|
|
|
|
timer = setTimeout(() => submit(), timeoutAmt)
|
|
|
|
})
|
|
|
|
el.addEventListener('mouseleave', () => clearTimeout(timer))
|
|
|
|
break
|
2023-11-06 10:28:25 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
debugConsole.error(`unsupported event tracking action: ${trigger}`)
|
2021-09-16 11:18:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.querySelectorAll('[event-tracking]').forEach(setupEventTracking)
|