export function installTracking() { if (navigator.globalPrivacyControl || navigator.doNotTrack === "1" || document.cookie.includes("dm-tracking=off") || location.hostname.startsWith("account.")) return; const sensitive = () => /\/(sso|auth|login|register|recover|reset|verify|payment|checkout)(\/|$)/i.test(location.pathname) || location.search || location.hash; let events = [], last = 0; function add(kind, extra = {}) { if (sensitive()) return; events.push({ kind, path: location.pathname, ...extra }); if (events.length >= 20) flush(); } function flush() { if (!events.length) return; const data = JSON.stringify({ events: events.splice(0, 40) }); fetch("/_dm/events", { method: "POST", headers: { "Content-Type": "application/json" }, body: data, keepalive: true }).catch(() => { }); } const point = (e) => ({ x: e.clientX / innerWidth, y: e.clientY / innerHeight, width: innerWidth, height: innerHeight }); add("view"); document.addEventListener("click", (e) => { const el = e.target.closest("a,button,[data-track]"); if (el) add("click", { target: el.dataset.track || el.getAttribute("name") || el.tagName.toLowerCase(), ...point(e) }); }); document.addEventListener("submit", () => add("action", { target: "submit" })); document.addEventListener("pointermove", (e) => { if (document.visibilityState === "visible" && Date.now() - last >= 500) { last = Date.now(); add("cursor", point(e)); } }, { passive: true }); window.addEventListener("popstate", () => add("view")); document.addEventListener("visibilitychange", () => { if (document.hidden) flush(); }); setInterval(flush, 1e4); window.dmTrack = (target) => add("action", { target }); }