!function(){"use strict";try{if("true"===localStorage.getItem("trackflow_ignore")){console.log("Tracking disabled via localStorage flag");return}let t=document.currentScript,e=t.getAttribute("data-site-id"),n=t.getAttribute("data-site-domain");if(!e||!n){console.warn("Missing website ID or domain");return}let r=t.src.includes("trackflow.dev")?new URL("/api/analytics",window.location.origin).href:"http://localhost:3000/api/analytics";function i(t,e,n){let r="";if(n){let i=new Date;i.setTime(i.getTime()+864e5*n),r="; expires="+i.toUTCString()}document.cookie=t+"="+(e||"")+r+"; path=/"}function o(t){let e=t+"=",n=document.cookie.split(";");for(let r=0;r2592e6}catch(r){return console.error("Error checking UTM expiration",r),!0}}function f(t){let e=new URLSearchParams(window.location.search),n=e.get(t);if(n)return n;try{let r=localStorage.getItem(s);if(!r)return null;if(c())return localStorage.removeItem(s),null;let i=JSON.parse(r);return i[t]||null}catch(o){return console.error("Error retrieving UTM from localStorage",o),null}}function m(){let t=window.location.href;if(!t){console.warn("Unable to collect href");return}let r;return{siteId:e,domain:n,page:t,utm_source:f("utm_source"),utm_medium:f("utm_medium"),utm_campaign:f("utm_campaign"),utm_content:f("utm_content"),utm_term:f("utm_term"),visitorId:((r=o("trackflow_visitor_id"))||(r=a(),i("trackflow_visitor_id",r,365)),r),sessionId:u(),userAgent:navigator.userAgent,referrer:document.referrer||null,timestamp:new Date().toISOString(),viewport:{width:window.innerWidth,height:window.innerHeight}}}function g(t,e,n){let r=m();r&&(r.event="CUSTOM_EVENT",r.type=t,r.extraData=e,h(r,n))}function d(t){if(t&&t.href){let e=t.href,n=window.location.hostname!==new URL(e,window.location.origin).hostname;n&&g("external_link",{url:e,text:t.textContent.trim()})}}function h(t,e){if("true"===localStorage.getItem("trackflow_ignore")){console.log("Tracking disabled via localStorage flag"),e&&e({status:200});return}let n=new XMLHttpRequest;n.open("POST",r,!0),n.setRequestHeader("Content-Type","application/json"),n.onreadystatechange=function(){n.readyState===XMLHttpRequest.DONE&&(200===n.status?(console.log("Event data sent successfully"),i("trackflow_visitor_id",u(),1/48)):console.error("Error sending event data:",n.status),e&&e({status:n.status}))},n.send(JSON.stringify(t))}window.trackflow=function(t,e){if(!t){console.warn("Missing event_name for custom event");return}if(t.length>=50){console.warn("Event name must be less than 50 characters");return}if(e&&Object.keys(e).length>4){console.warn("Event data must have 4 or fewer properties");return}if("purchase"===t&&e&&"number"==typeof e.amount){!function t(e,n){let r=m();r&&(r.event="PURCHASE",r.amount=e,h(r,void 0))}(e.amount);return}g(t,e)},document.addEventListener("click",function(t){d(t.target.closest("a"))}),document.addEventListener("keydown",function(t){("Enter"===t.key||" "===t.key)&&d(t.target.closest("a"))});let x=Date.now(),p=window.location.origin+window.location.pathname;function $(){let t=window.location.origin+window.location.pathname;if(t===p)return;let e=Math.round((Date.now()-x)/1e3);e<=0||(!function t(e,n,r){let i=m();i&&(i.url=e,i.timeSpent=n,i.event="SESSION_ACTIVITY",h(i,void 0))}(p,e),x=Date.now(),p=t)}!function t(e){!function t(){let e=new URLSearchParams(window.location.search),n={},r=!1;l.forEach(t=>{let i=e.get(t);i&&(n[t]=i,r=!0)}),r&&(n.timestamp=Date.now(),localStorage.setItem(s,JSON.stringify(n)))}(),c()&&localStorage.removeItem(s);let n=m();n&&(n.event="PAGE_VIEW",h(n,void 0))}();let w=window.history.pushState;window.history.pushState=function(){w.apply(this,arguments),$()},window.addEventListener("popstate",function(){$()}),console.log("Tracking script initialized.")}catch(v){console.error("Error initializing tracking script:",v)}}();