All files / apps/huella-legal/layers/tracking/app/plugins trackers.client.ts

100% Statements 19/19
100% Branches 6/6
100% Functions 5/5
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31        44x 44x   44x   5x 5x   44x 12x     44x 44x   44x   5x 6x 5x   5x 3x        
import type { CookieValue } from "vanilla-cookieconsent";
import type { TrackerKey } from "../trackers";
 
export default defineNuxtPlugin(async (nuxtApp) => {
    const { trackers } = useRuntimeConfig().public;
    const active = (Object.keys(trackers) as TrackerKey[]).filter((key) => trackers[key].id);
 
    if (!active.length) return;
 
    const stored = useCookie<{ categories?: string[] } | null>("cc_cookie", { readonly: true });
    const accepted = ref<string[]>(stored.value?.categories ?? []);
 
    const sync = (event: Event) => {
        accepted.value = (event as CustomEvent<{ cookie: CookieValue }>).detail.cookie.categories;
    };
 
    window.addEventListener("cc:onConsent", sync);
    window.addEventListener("cc:onChange", sync);
 
    const { TRACKERS } = await import("../trackers");
 
    for (const key of active) {
        const consent = computed(() => accepted.value.includes(TRACKERS[key].category));
        const send = TRACKERS[key].load(trackers[key].id, useScriptTriggerConsent({ consent }));
 
        nuxtApp.hook("analytics:event", (event) => {
            if (consent.value) send(event);
        });
    }
});