/** ReplyZen Pixel v1.0.0 */
(()=>{function s(i,...e){i.debug&&window.console&&typeof console.log=="function"&&console.log("[RZQ]",...e)}function E(){try{var i=new URLSearchParams(window.location.search);return i.has("__RZ_DEBUG__")}catch(e){return!1}}var q="https://track.replyzen.ai",h=["utm_source","utm_medium","utm_campaign","utm_term","utm_content","utm_source_platform","utm_marketing_tactic","utm_creative_format"],g="rzq_";function S(){var i=document.currentScript||function(){var e=document.getElementsByTagName("script");return e[e.length-1]}();return i.src||""}function I(i){return i&&i.split("?")[0].split("/").slice(0,3).join("/")||q}function P(){var i=S(),e=I(i),t=E();return{team_id:null,conversion_window:30,initialized:!1,debug:t,endpoint:e}}function u(i,e,t){try{var n={value:e,expires:t?new Date(Date.now()+t*864e5).getTime():null};return localStorage.setItem(g+i,JSON.stringify(n)),!0}catch(a){try{var r="";if(t){var o=new Date;o.setTime(o.getTime()+t*864e5),r="; expires="+o.toUTCString()}return document.cookie=g+i+"="+encodeURIComponent(e)+r+"; path=/; SameSite=Lax",!0}catch(c){return!1}}}function d(i){try{var e=localStorage.getItem(g+i);if(e){var t=JSON.parse(e);return t.expires&&t.expires<Date.now()?(localStorage.removeItem(g+i),null):t.value}}catch(c){try{for(var n=g+i+"=",r=document.cookie.split(";"),o=0;o<r.length;o++){var a=r[o].trim();if(a.indexOf(n)===0)return decodeURIComponent(a.substring(n.length,a.length))}}catch(_){}}return null}function b(i,e){var t=p(),n=!1;h.forEach(function(r){e[r]&&(t[r]=e[r],n=!0)}),(n||Object.keys(t).length>0)&&(u("utm_params",JSON.stringify(t),i.conversion_window),s(i,"Saved UTM parameters",t))}function p(i){try{var e=d("utm_params");return e?JSON.parse(e):{}}catch(t){return s(i,"Error getting UTM params",t),{}}}function f(i){var e={};try{var t=new URL(i||window.location.href);t.searchParams.forEach(function(r,o){e[o]=r})}catch(r){var n=(i||window.location.search).split("?")[1]||"";n.split("&").forEach(function(o){if(o!==""){var a=o.split("=");e[decodeURIComponent(a[0])]=a.length>1?decodeURIComponent(a[1]):""}})}return e}function U(){var i=f(),e={};return h.forEach(function(t){i[t]&&(e[t]=i[t])}),e}function w(i){var e=U();b(i,e)}function k(i){var e=p(i),t=U(),n=Object.assign({},e);return h.forEach(function(r){t[r]&&(n[r]=t[r])}),n}var m=class{constructor(e){this.config=e,this.pendingEvents=[],this.loadPendingEvents()}loadPendingEvents(){try{var e=d("pending_events");e&&(this.pendingEvents=JSON.parse(e),Array.isArray(this.pendingEvents)||(this.pendingEvents=[]))}catch(t){this.pendingEvents=[]}}savePendingEvents(){try{u("pending_events",JSON.stringify(this.pendingEvents),30)}catch(e){s(this.config,"Error saving pending events",e)}}sendEvent(e){if(!navigator.onLine)return s(this.config,"Browser offline, queueing event",e),this.pendingEvents.push(e),this.savePendingEvents(),Promise.resolve({success:!0,queued:!0});var t=f(),n=t.rzclid;n?u("click_id",n,this.config.conversion_window):n=d("click_id");var r=k(this.config),o=Object.assign({},e,r,{click_id:n,document_location:window.location.href,referer_location:document.referrer,team_id:this.config.team_id});return window.rzqData.hasBeacon&&typeof navigator.sendBeacon=="function"?this.sendWithBeacon(o):window.rzqData.hasXHR&&typeof XMLHttpRequest=="function"?this.sendWithXhr(o):this.sendWithImage(o)}sendWithBeacon(e){return new Promise(t=>{try{var n=this.config.endpoint+"/api/event",r=new Blob([JSON.stringify(e)],{type:"application/json"}),o=navigator.sendBeacon(n,r);o?(s(this.config,"Event sent with Beacon API",e),t({success:!0,method:"beacon"})):(s(this.config,"Beacon API failed, falling back to XHR",e),this.sendWithXhr(e).then(t))}catch(a){s(this.config,"Error using Beacon API",a),this.sendWithXhr(e).then(t)}})}sendWithXhr(e){return new Promise(t=>{try{var n=new XMLHttpRequest;n.open("POST",this.config.endpoint+"/api/event",!0),n.setRequestHeader("Content-Type","application/json"),n.onreadystatechange=()=>{n.readyState===4&&(n.status>=200&&n.status<300?(s(this.config,"Event sent with XHR",e),t({success:!0,method:"xhr"})):(s(this.config,"XHR failed with status "+n.status,e),t({success:!1,method:"xhr",error:"HTTP error "+n.status})))},n.send(JSON.stringify(e))}catch(r){s(this.config,"Error using XHR",r),this.sendWithImage(e).then(t)}})}sendWithImage(e){return s(this.config,"Image fallback used - redirecting to XHR method",e),this.sendWithXhr(e)}processPendingEvents(){if(this.pendingEvents.length!==0){s(this.config,"Processing "+this.pendingEvents.length+" pending events");var e=this.pendingEvents.splice(0,10);this.savePendingEvents();var t=e.map(n=>this.sendEvent(n));Promise.all(t).then(()=>{this.pendingEvents.length>0&&setTimeout(()=>{this.processPendingEvents()},1e3)})}}};var l=class{constructor(e){this.config=e,this.network=new m(e),this.queue=[]}init(e){if(this.config.initialized){s(this.config,"Already initialized");return}if(!e||!e.team_id){s(this.config,"Missing required team_id parameter");return}this.config.team_id=e.team_id,this.config.conversion_window=e.conversion_window||this.config.conversion_window,this.config.debug=e.debug!==void 0?e.debug:this.config.debug,this.config.endpoint=e.endpoint||this.config.endpoint,this.config.initialized=!0,s(this.config,"Initialized with options",e),w(this.config);var t=f();t.rzclid&&(u("click_id",t.rzclid,this.config.conversion_window),s(this.config,"Saved click ID from URL",t.rzclid)),e.auto_pageview!==!1&&this.trackEvent("pageview"),this.processQueue()}trackEvent(e,t={}){if(!this.config.initialized){s(this.config,"Not initialized, queueing event",e),this.queue.push(["track",e,t]);return}var n=d("click_id"),r=f(),o=r.rzclid;if(!n&&!o){s(this.config,"No click ID available for event, skipping tracking",e);return}o&&!n&&(u("click_id",o,this.config.conversion_window),s(this.config,"Saved click ID from URL for event tracking",o));var a={team_id:this.config.team_id,type:e,data:t,timestamp:new Date().toISOString()};s(this.config,"Tracking event",a),this.network.sendEvent(a).catch(function(c){s(this.config,"Error sending event",c)}.bind(this))}processQueue(){if(!this.queue||!Array.isArray(this.queue)){s(this.config,"Queue not properly initialized");return}let e=[...this.queue];this.queue=[],e.forEach(t=>{try{let[n,...r]=t;this.process(n,...r)}catch(n){s(this.config,"Error processing queued command:",n)}})}process(e,...t){try{switch(e){case"init":this.init(t[0]);break;case"track":if(!this.config.initialized){this.queue.push(["track",...t]);return}this.trackEvent(t[0],t[1]);break;case"debug":this.config.debug=t[0]===!0;break;default:s(this.config,"Unknown command:",e)}}catch(n){s(this.config,"Error processing command:",n)}}setupUnloadHandlers(){window.addEventListener&&window.addEventListener("beforeunload",()=>{if(this.network.pendingEvents.length>0){var e=this.network.pendingEvents[0];navigator.sendBeacon&&navigator.sendBeacon(this.config.endpoint+"/api/event",new Blob([JSON.stringify(e)],{type:"application/json"}))}})}};(function(i,e){"use strict";i.rzqData=i.rzqData||{},i.rzqData.hasBeacon=!!(navigator&&navigator.sendBeacon),i.rzqData.hasXHR=!!i.XMLHttpRequest;var t=P(),n=new l(t),r=[];Array.isArray(i.rzq&&i.rzq.queue)&&(r=i.rzq.queue);function o(...c){let[_,...y]=c;n.process(_,...y)}i.rzq,i.rzq=o,i.rzq.queue=n.queue,r.length>0&&r.forEach(c=>{i.rzq.queue.push(c)}),s(t,"Tracking library loaded");let a=i.rzq.queue.findIndex(c=>c[0]==="init");if(a!==-1){let c=i.rzq.queue[a];i.rzq.queue.splice(a,1),n.process(...c)}n.setupUnloadHandlers()})(window,document);})();
