hotjar.ts 996 B

1234567891011121314151617181920212223242526272829303132333435
  1. import getMeta from '@/utils/meta'
  2. import { debugConsole } from '@/utils/debugging'
  3. import { initializeHotjar } from '@/infrastructure/hotjar-snippet'
  4. import { createTrackingLoader } from '@/infrastructure/tracking-loader'
  5. import grammarlyExtensionPresent from '@/shared/utils/grammarly'
  6. const { hotjarId, hotjarVersion } = getMeta('ol-ExposedSettings')
  7. const shouldLoadHotjar = getMeta('ol-shouldLoadHotjar')
  8. function attemptHotjarLoad() {
  9. if (!hotjarId || !hotjarVersion) {
  10. return
  11. }
  12. if (!/^\d+$/.test(hotjarId) || !/^\d+$/.test(hotjarVersion)) {
  13. debugConsole.error('Invalid Hotjar id or version')
  14. return
  15. }
  16. if (grammarlyExtensionPresent()) {
  17. return
  18. }
  19. createTrackingLoader(
  20. () => initializeHotjar(hotjarId, hotjarVersion),
  21. 'Hotjar'
  22. )
  23. }
  24. if (hotjarId && hotjarVersion && shouldLoadHotjar) {
  25. document.addEventListener('DOMContentLoaded', () => {
  26. // add delay to allow extensions to inject shadow DOM
  27. setTimeout(attemptHotjarLoad, 1000)
  28. })
  29. }