mirror of
https://github.com/OpenSquawk/OpenSquawk
synced 2026-08-12 04:15:39 +08:00
Ensure Hotjar tracking loads globally
This commit is contained in:
@@ -1,6 +1,3 @@
|
|||||||
import type { Ref } from 'vue';
|
|
||||||
import { watch } from 'vue';
|
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
hj?: ((...args: unknown[]) => void) & { q?: unknown[][] };
|
hj?: ((...args: unknown[]) => void) & { q?: unknown[][] };
|
||||||
@@ -45,49 +42,18 @@ const appendHotjarScript = () => {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const enableHotjar = (hasLoaded: Ref<boolean>) => {
|
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
window._hjOptOut = false;
|
|
||||||
|
|
||||||
if (hasLoaded.value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ensureHotjarStub();
|
|
||||||
|
|
||||||
if (appendHotjarScript()) {
|
|
||||||
hasLoaded.value = true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const disableHotjar = () => {
|
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
window._hjOptOut = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin(() => {
|
||||||
if (!import.meta.client) {
|
if (!import.meta.client) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { analyticsEnabled } = useCookieConsent();
|
|
||||||
const hasLoaded = useState('hotjar-loaded', () => false);
|
const hasLoaded = useState('hotjar-loaded', () => false);
|
||||||
|
|
||||||
watch(
|
if (!hasLoaded.value) {
|
||||||
() => analyticsEnabled.value,
|
ensureHotjarStub();
|
||||||
(allowed) => {
|
|
||||||
if (allowed) {
|
if (appendHotjarScript()) {
|
||||||
enableHotjar(hasLoaded);
|
hasLoaded.value = true;
|
||||||
} else {
|
}
|
||||||
disableHotjar();
|
}
|
||||||
}
|
|
||||||
},
|
|
||||||
{ immediate: true }
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user