2024-10-11 04:35:08 -04:00
|
|
|
import getMeta from '@/utils/meta'
|
|
|
|
import { isSplitTestEnabled } from '@/utils/splitTestUtils'
|
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2024-10-11 08:20:39 -04:00
|
|
|
import { initializeHotjar } from '@/infrastructure/hotjar-snippet'
|
2024-10-11 04:35:08 -04:00
|
|
|
|
|
|
|
const { hotjarId, hotjarVersion } = getMeta('ol-ExposedSettings')
|
|
|
|
|
2024-10-11 08:20:39 -04:00
|
|
|
let hotjarInitialized = false
|
|
|
|
|
2024-10-11 04:35:08 -04:00
|
|
|
if (hotjarId && hotjarVersion && isSplitTestEnabled('hotjar')) {
|
|
|
|
const loadHotjar = () => {
|
|
|
|
// consent needed
|
|
|
|
if (!document.cookie.split('; ').some(item => item === 'oa=1')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-11 08:20:39 -04:00
|
|
|
if (!/^\d+$/.test(hotjarId) || !/^\d+$/.test(hotjarVersion)) {
|
|
|
|
debugConsole.error('Invalid Hotjar id or version')
|
2024-10-11 04:35:08 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-11 08:20:39 -04:00
|
|
|
// avoid inserting twice
|
|
|
|
if (!hotjarInitialized) {
|
|
|
|
debugConsole.log('Loading Hotjar')
|
|
|
|
hotjarInitialized = true
|
|
|
|
initializeHotjar(hotjarId, hotjarVersion)
|
|
|
|
}
|
2024-10-11 04:35:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// load when idle, if supported
|
|
|
|
if (typeof window.requestIdleCallback === 'function') {
|
|
|
|
window.requestIdleCallback(loadHotjar)
|
|
|
|
} else {
|
|
|
|
loadHotjar()
|
|
|
|
}
|
|
|
|
|
|
|
|
// listen for consent
|
|
|
|
window.addEventListener('cookie-consent', event => {
|
|
|
|
if ((event as CustomEvent<boolean>).detail) {
|
|
|
|
loadHotjar()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|