(function(){if(window._trackingInitialized)return;window._trackingInitialized=true;const API_ENDPOINT="https://api.iristech.dev/ir-grv-cfg";const urlParams=new URLSearchParams(window.location.search);const irclid=urlParams.get('irclid');const ircpid=urlParams.get('ircpid');const pubId=urlParams.get('pubId');if(!irclid)return;window.IRCLID=irclid;window.IRCPID=ircpid;window.PUB_ID=pubId;const SESSION_ID='nsess_'+Math.random().toString(36).substring(2,15);const CUSTOMER_ID='ncus_'+Math.random().toString(36).substring(2,15);async function trackEvent(eventType,eventMetadata={}){const payload={event_type:eventType,session_id:SESSION_ID,customer_id:CUSTOMER_ID,irclid:irclid,ircpid:ircpid,pub_id:pubId,page_url:window.location.href,page_title:document.title,event_metadata:eventMetadata};try{await fetch(API_ENDPOINT,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(payload),keepalive:true});}catch(err){console.error("[Pixel Event]: Tracking error",err);}} function getClickInfo(event){const element=event.target;if(element.tagName==='BODY'||element.tagName==='HTML')return null;return{element:element.tagName,id:element.id||null,class:element.className||null,text:(element.innerText||'').substring(0,200),href:element.href||null};} function addToLinks(){document.querySelectorAll('a').forEach(link=>{try{const url=new URL(link.href,window.location.origin);if(url.origin===window.location.origin&&!url.searchParams.has('irclid')){url.searchParams.set('irclid',irclid);link.href=url.toString();} if(url.origin===window.location.origin&&!url.searchParams.has('ircpid')){url.searchParams.set('ircpid',ircpid);link.href=url.toString();} if(url.origin===window.location.origin&&!url.searchParams.has('pubId')){url.searchParams.set('pubId',pubId);link.href=url.toString();}}catch(e){}});} function init(){trackEvent('page_view');document.addEventListener('click',(event)=>{const info=getClickInfo(event);if(info)trackEvent('click',info);},true);try{document.querySelectorAll('form[action*="/cart/add"]').forEach(form=>{if(!form.querySelector('input[name="properties[irclid]"]')){const input=document.createElement('input');input.type='hidden';input.name='properties[irclid]';input.value=irclid;form.appendChild(input);} if(!form.querySelector('input[name="properties[ircpid]"]')){const input=document.createElement('input');input.type='hidden';input.name='properties[ircpid]';input.value=ircpid;form.appendChild(input);} if(!form.querySelector('input[name="properties[pubId]"]')){const input=document.createElement('input');input.type='hidden';input.name='properties[pubId]';input.value=pubId;form.appendChild(input);}});}catch(e){} addToLinks();const observer=new MutationObserver(()=>{addToLinks();});observer.observe(document.body,{childList:true,subtree:true});} if(document.readyState==='loading'){document.addEventListener('DOMContentLoaded',init);}else{init();}})();