(function () { "use strict"; // Simple Analytics events queue window.sa_event = window.sa_event || function () { var a = [].slice.call(arguments); window.sa_event.q ? window.sa_event.q.push(a) : (window.sa_event.q = [a]); }; // Helper to convert sa_event to Promise-based function const sendEventAsync = (event, metadata) => { return new Promise((resolve) => { try { const timeout = setTimeout(() => { console.warn( `Simple Analytics event timeout: ${event}`, JSON.stringify(metadata, null, 2) ); resolve(); }, 2000); sa_event(event, metadata, () => { clearTimeout(timeout); resolve(); }); } catch (err) { console.warn( `Simple Analytics error: ${event}`, JSON.stringify({ error: err?.message, metadata }, null, 2) ); resolve(); } }); }; // track elements only once const bound = new WeakSet(); // extract metadata from data-simple-event-* attrs const getMeta = (el) => { const meta = {}; Array.from(el.attributes).forEach(({ name, value }) => { if (name.startsWith("data-simple-event-")) { // convert kebab-case to underscore_case const key = name.slice("data-simple-event-".length).replace(/-/g, "_"); meta[key] = value; } }); if (el.tagName) meta.tag = el.tagName.toLowerCase(); return meta; }; // bind click or submit to any [data-simple-event] const bindEl = (el) => { if (bound.has(el)) return; bound.add(el); const eventName = el.getAttribute("data-simple-event"); const meta = getMeta(el); if (el.tagName === "FORM") { el.addEventListener("submit", async (e) => { e.preventDefault(); const submitButton = document.activeElement; const buttonText = submitButton.tagName === "BUTTON" || (submitButton.tagName === "INPUT" && submitButton.type === "submit") ? submitButton.value || submitButton.textContent || "" : ""; await sendEventAsync(eventName, { ...meta, button_text: buttonText, }); el.submit(); }); } else { el.addEventListener("click", async (e) => { await sendEventAsync(eventName, meta); }); } }; // scan for new elements every 500ms setInterval(() => { document.querySelectorAll("[data-simple-event]").forEach(bindEl); }, 500); // 404 detector: if "404" on page, confirm via HEAD and send event const check404 = async () => { if (/[^0-9.,]404[^0-9.,]/.test(document.documentElement.innerHTML)) { try { const res = await fetch(window.location.href, { method: "HEAD" }); if (res.status === 404) { await sendEventAsync("page_404", { url: window.location.href, status: res.status, }); } } catch (_) {} } }; document.addEventListener("DOMContentLoaded", check404); })();