(function() { const apikey = 'jessetesting'; const sessionId = localStorage.getItem("session_id") || (()=>{ const newSession = Math.random().toString(36).substr(2, 9); localStorage.setItem("session_id", newSession); return newSession; })(); function sendAnalytics(eventType, eventData = {}) { const data = JSON.stringify({ API: apikey, // API key required for every request session_id: sessionId, user_id: null, // Replace with actual user ID if available page_url: window.location.href, referrer: document.referrer, screen_width: window.innerWidth, screen_height: window.innerHeight, event_type: eventType, event_data: eventData }); // Use sendBeacon for reliable async tracking (even when closing the page) if (navigator.sendBeacon) { navigator.sendBeacon("https://webpulse.arctechnologies.ca/analytics.php", data); } else { // Fallback to XMLHttpRequest for older browsers const xhr = new XMLHttpRequest(); xhr.open("POST", "https://webpulse.arctechnologies.ca/analytics.php", true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.send(data); } } // Track Page Views sendAnalytics("page_view"); // Track Click Events document.addEventListener("click", (e) => { sendAnalytics("click", { tag: e.target.tagName, id: e.target.id, class: e.target.className }); }); // Track Scroll Depth let lastScroll = 0; window.addEventListener("scroll", () => { const scrollDepth = Math.round(window.scrollY / document.body.scrollHeight * 100); if (Math.abs(scrollDepth - lastScroll) > 10) { sendAnalytics("scroll", { depth: scrollDepth }); lastScroll = scrollDepth; } }); })();