mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
7a26d46d7c
GitOrigin-RevId: b7fdb904702d84058c2e3519b17376083ee9cad7
45 lines
1.1 KiB
TypeScript
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()
|
|
}
|
|
})
|
|
}
|