import type { CookieStorage } from '../types.js' import type { CrawlObservation, CrawlRequest } from './result.js' /** * Hosts whose requests before consent are a violation. Google's tag hosts are * separate: with Consent Mode `advanced` (the plugin's default) the Google tag * loads before consent with every signal denied, which is allowed; with * `basic` nothing may reach Google before consent. */ export const DEFAULT_TRACKER_HOSTS = [ 'analytics.tiktok.com', 'bat.bing.com', 'clarity.ms', 'connect.facebook.net', 'facebook.com', 'hotjar.com', 'licdn.com', 'linkedin.com', 'youtube.com', ] export const GOOGLE_TAG_HOSTS = [ 'doubleclick.net', 'google-analytics.com', 'googleadservices.com', 'googlesyndication.com', 'googletagmanager.com', ] export type CrawlViolation = { /** Consent category of the cookie or key (null = not classified yet). */ category?: null | string kind: 'request' | CookieStorage /** Cookie / key name, or the host for a request. */ name: string pages: string[] } const hostMatches = (host: string, list: string[]) => list.some((h) => host === h || host.endsWith(`.${h}`)) /** * Violations of one crawl: anything stored before consent whose category is not * `necessary` (unclassified counts — the editor decides), and requests before * consent to tracker hosts. `categoryOf` gives the review entry's category after * the scan merged it (an editor's category wins). */ export function findCrawlViolations({ categoryOf, consentMode = 'advanced', observations, requests, trackerHosts = DEFAULT_TRACKER_HOSTS, }: { categoryOf: (o: CrawlObservation) => null | string | undefined consentMode?: 'advanced' | 'basic' observations: CrawlObservation[] requests: CrawlRequest[] trackerHosts?: string[] }): CrawlViolation[] { const out: CrawlViolation[] = [] for (const o of observations) { if (!o.before) {continue} const category = categoryOf(o) ?? null if (category !== 'necessary') {out.push({ name: o.name, category, kind: o.storage, pages: o.pages })} } const hosts = consentMode === 'basic' ? [...trackerHosts, ...GOOGLE_TAG_HOSTS] : trackerHosts for (const r of requests) { if (r.before && hostMatches(r.host, hosts)) {out.push({ name: r.host, kind: 'request', pages: r.pages })} } return out }