/* Project Moosed legal + analytics kit (pm-kit.js)
One file for every site: cookie consent banner and a first-party
analytics tracker that reports to our own collector. No third-party
trackers. Analytics cookies are set only after the visitor accepts.
Usage:
Cookie Settings: any element with [data-pm-cookie-settings] reopens
the banner. Conversions: window.PMKit.convert("name") or clicks on
elements with [data-pm-convert="name"]. */
(function () {
"use strict";
var script = document.currentScript || document.querySelector("script[data-site]");
var SITE = (script && script.getAttribute("data-site")) || location.hostname;
var COLLECT = "https://stats.projectmoosed.com/collect.php";
var COOKIE_PAGE = "/legal/cookies.html";
var YEAR = 365 * 24 * 60 * 60;
function getCookie(name) {
var m = document.cookie.match(new RegExp("(?:^|; )" + name + "=([^;]*)"));
return m ? decodeURIComponent(m[1]) : null;
}
function setCookie(name, value, seconds, sessionOnly) {
var c = name + "=" + encodeURIComponent(value) + "; path=/; SameSite=Lax";
if (!sessionOnly) c += "; max-age=" + seconds;
if (location.protocol === "https:") c += "; Secure";
document.cookie = c;
}
function dropCookie(name) {
document.cookie = name + "=; path=/; max-age=0; SameSite=Lax" +
(location.protocol === "https:" ? "; Secure" : "");
}
function rid(n) {
var s = "", a = "abcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < (n || 20); i++) s += a[Math.floor(Math.random() * a.length)];
return s;
}
var consent = getCookie("_pm_consent"); // "1" accepted, "0" declined, null unset
/* ---------------- consent banner ---------------- */
var banner = null;
function showBanner() {
if (banner) { banner.style.display = "block"; return; }
banner = document.createElement("div");
banner.setAttribute("role", "dialog");
banner.setAttribute("aria-label", "Cookie consent");
banner.style.cssText =
"position:fixed;left:0;right:0;bottom:0;z-index:99990;background:#101828;color:#f2f4f7;" +
"padding:14px 18px;display:flex;flex-wrap:wrap;gap:10px 16px;align-items:center;" +
"justify-content:center;font:14px/1.5 system-ui,-apple-system,Segoe UI,Roboto,sans-serif;" +
"box-shadow:0 -4px 24px rgba(0,0,0,.35)";
var txt = document.createElement("span");
txt.style.cssText = "max-width:640px";
txt.innerHTML = "We use cookies to run this site and, with your OK, to see how visitors use it so we can make it better. " +
'Read our Cookie Policy.';
var ok = document.createElement("button");
ok.textContent = "Accept cookies";
ok.style.cssText = "background:#f2b33d;color:#101828;border:0;border-radius:8px;padding:9px 16px;font-weight:600;cursor:pointer";
ok.addEventListener("click", function () { answer("1"); });
var no = document.createElement("button");
no.textContent = "Decline";
no.style.cssText = "background:transparent;color:#f2f4f7;border:1px solid #667085;border-radius:8px;padding:9px 16px;cursor:pointer";
no.addEventListener("click", function () { answer("0"); });
banner.appendChild(txt); banner.appendChild(ok); banner.appendChild(no);
document.body.appendChild(banner);
}
function hideBanner() { if (banner) banner.style.display = "none"; }
function answer(v) {
consent = v;
setCookie("_pm_consent", v, YEAR);
hideBanner();
if (v === "1") startTracker();
else { dropCookie("_pm_vid"); dropCookie("_pm_sid"); stopTracker(); }
}
document.addEventListener("click", function (e) {
var t = e.target.closest ? e.target.closest("[data-pm-cookie-settings]") : null;
if (t) { e.preventDefault(); showBanner(); }
});
/* ---------------- tracker ---------------- */
var started = false, visibleSec = 0, lastTick = null, maxScroll = 0;
var scrollMarks = { 25: 0, 50: 0, 75: 0, 100: 0 };
var vid = null, sid = null;
function send(ev, extra) {
if (!vid) return;
var p = {
site: SITE, vid: vid, sid: sid, ev: ev,
path: location.pathname, url: location.href, title: document.title,
ref: document.referrer || "",
ts: Date.now()
};
if (extra) for (var k in extra) p[k] = extra[k];
var body = JSON.stringify(p);
if (navigator.sendBeacon) {
navigator.sendBeacon(COLLECT, new Blob([body], { type: "text/plain" }));
} else {
fetch(COLLECT, { method: "POST", body: body, keepalive: true, mode: "no-cors" }).catch(function () {});
}
}
function dims() {
var ua = navigator.userAgent || "";
var device = /Mobi|Android|iPhone|iPad/i.test(ua) ? (/iPad|Tablet/i.test(ua) ? "tablet" : "mobile") : "desktop";
var browser = /Edg\//.test(ua) ? "Edge" : /OPR\//.test(ua) ? "Opera" : /Chrome\//.test(ua) ? "Chrome" :
/Firefox\//.test(ua) ? "Firefox" : /Safari\//.test(ua) ? "Safari" : "Other";
var os = /Windows/.test(ua) ? "Windows" : /Android/.test(ua) ? "Android" : /iPhone|iPad|iOS/.test(ua) ? "iOS" :
/Mac OS/.test(ua) ? "macOS" : /Linux/.test(ua) ? "Linux" : "Other";
var q = new URLSearchParams(location.search);
return {
device: device, browser: browser, os: os,
vp: window.innerWidth + "x" + window.innerHeight,
scr: (window.screen ? screen.width + "x" + screen.height : ""),
lang: navigator.language || "", tz: -new Date().getTimezoneOffset() / 60,
utm_source: q.get("utm_source") || "", utm_medium: q.get("utm_medium") || "",
utm_campaign: q.get("utm_campaign") || "", utm_term: q.get("utm_term") || "",
utm_content: q.get("utm_content") || ""
};
}
function onScroll() {
var h = document.documentElement;
var total = h.scrollHeight - h.clientHeight;
if (total <= 0) return;
var pct = Math.min(100, Math.round((h.scrollTop / total) * 100));
if (pct > maxScroll) maxScroll = pct;
[25, 50, 75, 100].forEach(function (m) {
if (pct >= m && !scrollMarks[m]) { scrollMarks[m] = 1; send("scroll", { depth: m }); }
});
}
function tick() {
var now = Date.now();
if (lastTick !== null && !document.hidden) visibleSec += Math.round((now - lastTick) / 1000);
lastTick = now;
}
function onClick(e) {
var a = e.target.closest ? e.target.closest("a,button,[data-pm-track],[data-pm-convert]") : null;
if (!a) return;
var conv = a.getAttribute && a.getAttribute("data-pm-convert");
if (conv) { send("convert", { name: conv }); return; }
var named = a.getAttribute && a.getAttribute("data-pm-track");
if (named) { send("click", { name: named, href: a.getAttribute("href") || "" }); return; }
if (a.tagName !== "A") return;
var href = a.getAttribute("href") || "";
if (href.indexOf("tel:") === 0) send("click", { name: "call", href: href });
else if (href.indexOf("mailto:") === 0) send("click", { name: "email", href: href });
else if (a.host && a.host !== location.host) send("click", { name: "outbound", href: a.href });
}
var formSeen = {};
function onFocusIn(e) {
var f = e.target.form;
if (!f || formSeen[f.id || f.name || "anon"]) return;
formSeen[f.id || f.name || "anon"] = 1;
send("form_start", { form: f.id || f.name || "" });
}
function onSubmit(e) {
var f = e.target;
var fid = f.id || f.name || "";
var qInput = f.querySelector('input[type="search"],input[name="q"],input[name="s"],input[name="query"],input[name="search"]');
if (qInput && qInput.value) send("search", { q: qInput.value.slice(0, 120), form: fid });
else send("form_submit", { form: fid });
}
function onHide() {
tick();
send("exit", { dur: visibleSec, depth: maxScroll });
}
function startTracker() {
if (started) return;
started = true;
vid = getCookie("_pm_vid");
if (!vid) { vid = rid(24); setCookie("_pm_vid", vid, 397 * 24 * 60 * 60); }
sid = getCookie("_pm_sid");
if (!sid) { sid = rid(16); setCookie("_pm_sid", sid, 0, true); }
var d = dims();
send("pageview", d);
lastTick = Date.now();
setInterval(tick, 5000);
setInterval(function () { if (visibleSec > 0) send("heartbeat", { dur: visibleSec }); }, 30000);
window.addEventListener("scroll", onScroll, { passive: true });
document.addEventListener("click", onClick, true);
document.addEventListener("focusin", onFocusIn, true);
document.addEventListener("submit", onSubmit, true);
window.addEventListener("pagehide", onHide);
}
function stopTracker() { started = false; vid = null; sid = null; }
window.PMKit = {
convert: function (name) { if (started) send("convert", { name: String(name) }); },
openSettings: showBanner
};
if (consent === "1") startTracker();
else if (consent === null) {
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", showBanner);
else showBanner();
}
})();