overleaf/services/web/frontend/js/infrastructure/hotjar.ts
Alf Eaton 7a26d46d7c Add Hotjar script to the home page (#20758)
GitOrigin-RevId: b7fdb904702d84058c2e3519b17376083ee9cad7
2024-10-14 11:09:26 +00:00

45 lines
1.1 KiB
TypeScript

import getMeta from '@/utils/meta'
import { isSplitTestEnabled } from '@/utils/splitTestUtils'
import { debugConsole } from '@/utils/debugging'
const { hotjarId, hotjarVersion } = getMeta('ol-ExposedSettings')
if (hotjarId && hotjarVersion && isSplitTestEnabled('hotjar')) {
const loadHotjar = () => {
// consent needed
if (!document.cookie.split('; ').some(item => item === 'oa=1')) {
return
}
// avoid inserting twice
if (document.getElementById('hotjar')) {
return
}
debugConsole.log('Loading Hotjar')
const url = new URL(`https://static.hotjar.com/c/hotjar-${hotjarId}.js`)
url.searchParams.set('sv', hotjarVersion)
const script = document.createElement('script')
script.src = url.toString()
script.async = true
script.id = 'hotjar'
document.head.append(script)
}
// 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()
}
})
}