Files
Verso/services/web/frontend/js/infrastructure/hotjar.ts
T
KristinaandCopybot da411733b6 Merge pull request #22119 from overleaf/kh-hotjar-on-plans-and-checkout
[web] add hotjar to plans pages

GitOrigin-RevId: eb582a85200747754923f28e7d710a9072577d75
2024-11-28 09:05:10 +00:00

44 lines
1.1 KiB
TypeScript

import getMeta from '@/utils/meta'
import { debugConsole } from '@/utils/debugging'
import { initializeHotjar } from '@/infrastructure/hotjar-snippet'
const { hotjarId, hotjarVersion } = getMeta('ol-ExposedSettings')
const shouldLoadHotjar = getMeta('ol-shouldLoadHotjar')
let hotjarInitialized = false
if (hotjarId && hotjarVersion && shouldLoadHotjar) {
const loadHotjar = () => {
// consent needed
if (!document.cookie.split('; ').some(item => item === 'oa=1')) {
return
}
if (!/^\d+$/.test(hotjarId) || !/^\d+$/.test(hotjarVersion)) {
debugConsole.error('Invalid Hotjar id or version')
return
}
// avoid inserting twice
if (!hotjarInitialized) {
debugConsole.log('Loading Hotjar')
hotjarInitialized = true
initializeHotjar(hotjarId, hotjarVersion)
}
}
// 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()
}
})
}