Use the whole Hotjar snippet (#21017)

GitOrigin-RevId: 3935d671bdf5299b720f319fd7d8c6879d1819b9
This commit is contained in:
Alf Eaton 2024-10-11 13:20:39 +01:00 committed by Copybot
parent 51b46f3909
commit 1327d536a3
2 changed files with 26 additions and 13 deletions

View file

@ -0,0 +1,15 @@
// tracking code from https://help.hotjar.com/hc/en-us/articles/115009336727-How-to-Install-Your-Hotjar-Tracking-Code
export const initializeHotjar = (hotjarId, hotjarVersion) =>
(function (h, o, t, j, a, r) {
h.hj =
h.hj ||
function () {
;(h.hj.q = h.hj.q || []).push(arguments)
}
h._hjSettings = { hjid: hotjarId, hjsv: hotjarVersion }
a = o.getElementsByTagName('head')[0]
r = o.createElement('script')
r.async = 1
r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv
a.appendChild(r)
})(window, document, 'https://static.hotjar.com/c/hotjar-', '.js?sv=')

View file

@ -1,9 +1,12 @@
import getMeta from '@/utils/meta'
import { isSplitTestEnabled } from '@/utils/splitTestUtils'
import { debugConsole } from '@/utils/debugging'
import { initializeHotjar } from '@/infrastructure/hotjar-snippet'
const { hotjarId, hotjarVersion } = getMeta('ol-ExposedSettings')
let hotjarInitialized = false
if (hotjarId && hotjarVersion && isSplitTestEnabled('hotjar')) {
const loadHotjar = () => {
// consent needed
@ -11,22 +14,17 @@ if (hotjarId && hotjarVersion && isSplitTestEnabled('hotjar')) {
return
}
// avoid inserting twice
if (document.getElementById('hotjar')) {
if (!/^\d+$/.test(hotjarId) || !/^\d+$/.test(hotjarVersion)) {
debugConsole.error('Invalid Hotjar id or version')
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)
// avoid inserting twice
if (!hotjarInitialized) {
debugConsole.log('Loading Hotjar')
hotjarInitialized = true
initializeHotjar(hotjarId, hotjarVersion)
}
}
// load when idle, if supported