@datalyr/web 1.7.1 → 1.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/attribution.d.ts +8 -0
- package/dist/attribution.d.ts.map +1 -1
- package/dist/auto-identify.d.ts +8 -0
- package/dist/auto-identify.d.ts.map +1 -1
- package/dist/container.d.ts +7 -0
- package/dist/container.d.ts.map +1 -1
- package/dist/datalyr.cjs.js +673 -225
- package/dist/datalyr.cjs.js.map +1 -1
- package/dist/datalyr.esm.js +673 -225
- package/dist/datalyr.esm.js.map +1 -1
- package/dist/datalyr.esm.min.js +1 -1
- package/dist/datalyr.esm.min.js.map +1 -1
- package/dist/datalyr.js +673 -225
- package/dist/datalyr.js.map +1 -1
- package/dist/datalyr.min.js +1 -1
- package/dist/datalyr.min.js.map +1 -1
- package/dist/identity.d.ts +22 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.test.d.ts +6 -0
- package/dist/identity.test.d.ts.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/queue.d.ts +36 -15
- package/dist/queue.d.ts.map +1 -1
- package/dist/storage.d.ts +11 -0
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.test.d.ts +6 -0
- package/dist/storage.test.d.ts.map +1 -0
- package/dist/utils.d.ts +10 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.test.d.ts +5 -0
- package/dist/utils.test.d.ts.map +1 -0
- package/package.json +2 -1
package/dist/datalyr.esm.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function t(t,e,i,s){return new(i||(i=Promise))(function(n,o){function r(t){try{c(s.next(t))}catch(t){o(t)}}function a(t){try{c(s.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i(function(t){t(e)})).then(r,a)}c((s=s.apply(t,e||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const e=new class{constructor(){this.key=null,this.salt=null}initialize(e,i){return t(this,void 0,void 0,function*(){if(!crypto.subtle){const t=new Error("Web Crypto API not available - encryption cannot be initialized");throw console.error("[Datalyr Encryption]",t.message),t}try{const t=new TextEncoder,s="dl_encryption_salt";let n=localStorage.getItem(s);n?this.salt=this.base64ToArrayBuffer(n):(this.salt=crypto.getRandomValues(new Uint8Array(32)),n=this.arrayBufferToBase64(this.salt),localStorage.setItem(s,n));const o=`datalyr:${e}:${i}`,r=t.encode(o),a=yield crypto.subtle.importKey("raw",r,"PBKDF2",!1,["deriveKey"]);this.key=yield crypto.subtle.deriveKey({name:"PBKDF2",salt:this.salt,iterations:1e5,hash:"SHA-256"},a,{name:"AES-GCM",length:256},!1,["encrypt","decrypt"])}catch(t){throw console.error("[Datalyr Encryption] Failed to initialize:",t),this.key=null,t}})}encrypt(e){return t(this,void 0,void 0,function*(){if(!this.key||!crypto.subtle)throw new Error("Encryption not initialized - cannot encrypt sensitive data");try{const t="string"==typeof e?e:JSON.stringify(e),i=(new TextEncoder).encode(t),s=crypto.getRandomValues(new Uint8Array(12)),n=yield crypto.subtle.encrypt({name:"AES-GCM",iv:s},this.key,i),o=new Uint8Array(s.length+n.byteLength);return o.set(s,0),o.set(new Uint8Array(n),s.length),this.arrayBufferToBase64(o)}catch(t){throw console.error("[Datalyr Encryption] Encryption failed:",t),t}})}decrypt(e){return t(this,void 0,void 0,function*(){if(!this.key||!crypto.subtle){console.warn("[Datalyr Encryption] Decryption not available - reading potentially unencrypted data");try{return JSON.parse(e)}catch(t){return e}}try{const t=this.base64ToArrayBuffer(e),i=t.slice(0,12),s=t.slice(12),n=yield crypto.subtle.decrypt({name:"AES-GCM",iv:i},this.key,s),o=(new TextDecoder).decode(n);try{return JSON.parse(o)}catch(t){return o}}catch(t){console.warn("[Datalyr Encryption] Decryption failed - attempting to read as unencrypted data (migration mode)");try{return JSON.parse(e)}catch(t){return e}}})}isAvailable(){return null!==this.key&&void 0!==crypto.subtle}arrayBufferToBase64(t){let e="";const i=new Uint8Array(t),s=i.byteLength;for(let t=0;t<s;t++)e+=String.fromCharCode(i[t]);return btoa(e)}base64ToArrayBuffer(t){const e=atob(t),i=e.length,s=new Uint8Array(i);for(let t=0;t<i;t++)s[t]=e.charCodeAt(t);return s}destroy(){this.key=null,this.salt=null}};class i{constructor(t){this.memory=new Map,this.prefix="dl_";try{if(!t)throw new Error("no storage");const e="dl_test__"+Math.random();t.setItem(e,"1"),t.removeItem(e),this.storage=t}catch(t){this.storage=null,"undefined"!=typeof window&&console.warn("[Datalyr] Storage not available, using memory fallback")}}get(t,e=null){const i=this.prefix+t;try{if(this.storage){const t=this.storage.getItem(i);if(null===t)return e;try{return JSON.parse(t)}catch(e){return t}}else{const t=this.memory.get(i);if(void 0===t)return e;try{return JSON.parse(t)}catch(e){return t}}}catch(t){return e}}set(t,e){const i=this.prefix+t,s="string"==typeof e?e:JSON.stringify(e);try{return this.storage?(this.storage.setItem(i,s),!0):(this.memory.set(i,s),!0)}catch(e){return console.warn("[Datalyr] Failed to store:",t,e),this.memory.set(i,s),!1}}remove(t){const e=this.prefix+t;try{return this.storage?(this.storage.removeItem(e),!0):(this.memory.delete(e),!0)}catch(t){return!1}}keys(){try{if(this.storage){const t=[];for(let e=0;e<this.storage.length;e++){const i=this.storage.key(e);i&&i.startsWith(this.prefix)&&t.push(i.slice(this.prefix.length))}return t}return Array.from(this.memory.keys()).filter(t=>t.startsWith(this.prefix)).map(t=>t.slice(this.prefix.length))}catch(t){return[]}}migrateFromLegacyPrefix(){if(!this.storage)return 0;let t=0;try{const e=[];for(let t=0;t<this.storage.length;t++){const i=this.storage.key(t);i&&i.startsWith("__dl_dl_")&&e.push(i)}e.forEach(e=>{try{const i=this.storage.getItem(e);if(i){const s=e.slice(5);this.storage.getItem(s)||(this.storage.setItem(s,i),t++),this.storage.removeItem(e)}}catch(t){console.warn(`[Datalyr Storage] Failed to migrate legacy key: ${e}`,t)}}),t>0&&console.log(`[Datalyr Storage] Migrated ${t} keys from legacy prefix`)}catch(t){console.warn("[Datalyr Storage] Error during legacy key migration:",t)}return t}getEncrypted(i){return t(this,arguments,void 0,function*(t,i=null){const s=this.prefix+t;try{let t=null;if(t=this.storage?this.storage.getItem(s):this.memory.get(s)||null,!t)return i;return yield e.decrypt(t)}catch(e){return console.warn("[Datalyr Storage] Failed to decrypt:",t,e),i}})}setEncrypted(i,s){return t(this,void 0,void 0,function*(){const t=this.prefix+i,n=yield e.encrypt(s);return this.storage?(this.storage.setItem(t,n),!0):(this.memory.set(t,n),!0)})}}class s{constructor(t={}){this.domain=t.domain||"auto",this.maxAge=t.maxAge||365,this.sameSite=t.sameSite||"Lax",this.secure=t.secure||"auto"}get(t){var e;const i=`; ${document.cookie}`.split(`; ${t}=`);if(2===i.length){const t=(null===(e=i.pop())||void 0===e?void 0:e.split(";").shift())||null;if(t)try{return decodeURIComponent(t)}catch(e){return t}}return null}set(t,e,i){try{const s=86400*(i||this.maxAge),n="auto"===this.secure?"https:"===location.protocol:this.secure;let o="";"auto"===this.domain?o=this.getAutoDomain():this.domain&&(o=`;domain=${this.domain}`);const r=[`${t}=${encodeURIComponent(e)}`,`max-age=${s}`,"path=/",`SameSite=${this.sameSite}`,n?"Secure":"",o].filter(Boolean).join(";");return document.cookie=r,!0}catch(e){return console.warn("[Datalyr] Failed to set cookie:",t,e),!1}}remove(t){try{const e=["",location.hostname],i=location.hostname.split(".");return i.length>2&&(e.push(`.${i.slice(-2).join(".")}`),e.push(`.${location.hostname}`)),e.forEach(e=>{const i=e?`;domain=${e}`:"";document.cookie=`${t}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/${i}`}),!0}catch(t){return!1}}getAutoDomain(){const t=location.hostname;if("localhost"===t||/^[\d.]+$/.test(t)||/^\[[\d:]+\]$/.test(t))return"";const e=t.split(".");for(let t=e.length-2;t>=0;t--){const i="."+e.slice(t).join("."),s="__dl_test_"+Math.random();if(document.cookie=`${s}=1;domain=${i};path=/`,-1!==document.cookie.indexOf(s))return document.cookie=`${s}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain=${i};path=/`,`;domain=${i}`}return""}}const n="undefined"!=typeof window?window.localStorage:void 0,o="undefined"!=typeof window?window.sessionStorage:void 0,r=new i(n);new i(o);const a=new s;function c(e){return t(this,void 0,void 0,function*(){if("undefined"==typeof crypto||!crypto.subtle)return null;if(!e)return null;try{const t=(new TextEncoder).encode(e.toLowerCase().trim()),i=yield crypto.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(i)).map(t=>t.toString(16).padStart(2,"0")).join("")}catch(t){return null}})}function l(){return"undefined"!=typeof crypto&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){const e=16*Math.random()|0;return("x"===t?e:3&e|8).toString(16)})}function d(t=window.location.search){const e={};try{if("URLSearchParams"in window){return new URLSearchParams(t).forEach((t,i)=>{e[i]=t}),e}}catch(t){}const i=(t||"").replace(/^\?/,"").split("&");for(const t of i){const[i,s=""]=t.split("=");try{const t=decodeURIComponent((i||"").replace(/\+/g," ")),n=decodeURIComponent((s||"").replace(/\+/g," "));t&&(e[t]=n)}catch(t){}}return e}function h(t,e=5,i=0){if(i>=e)return"[Max depth reached]";if(null==t)return t;if("undefined"!=typeof Element&&t instanceof Element||"undefined"!=typeof Document&&t instanceof Document||"function"==typeof t)return"[Removed]";if(Array.isArray(t))return t.map(t=>h(t,e,i+1));if("object"==typeof t){const s={},n=/pass|pwd|token|secret|auth|bearer|session|cookie|signature|api[-_]?key|private[-_]?key|access[-_]?token|refresh[-_]?token/i;for(const o in t)if(Object.prototype.hasOwnProperty.call(t,o)){if(n.test(o))continue;s[o]=h(t[o],e,i+1)}return s}return"string"==typeof t?t.length>1e3?t.slice(0,1e3)+"...[truncated]":t.match(/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/)||t.match(/^[a-f0-9]{32,}$/i)?"[Redacted]":t:t}function u(t,...e){if(!e.length)return t;const i=e.shift();if(g(t)&&g(i))for(const e in i)g(i[e])?(t[e]||Object.assign(t,{[e]:{}}),u(t[e],i[e])):Object.assign(t,{[e]:i[e]});return u(t,...e)}function g(t){return t&&"object"==typeof t&&!Array.isArray(t)}function f(t){const e={google:["google.com","google."],facebook:["facebook.com","fb.com"],twitter:["twitter.com","t.co","x.com"],linkedin:["linkedin.com","lnkd.in"],instagram:["instagram.com"],youtube:["youtube.com","youtu.be"],tiktok:["tiktok.com"],reddit:["reddit.com"],pinterest:["pinterest.com"],bing:["bing.com"],yahoo:["yahoo.com"],duckduckgo:["duckduckgo.com"],baidu:["baidu.com"]};for(const[i,s]of Object.entries(e))if(s.some(e=>t.includes(e)))return i;return"other"}class p{constructor(){this.userId=null,this.sessionId=null,this.anonymousId=this.getOrCreateAnonymousId(),this.userId=this.getStoredUserId()}getOrCreateAnonymousId(){try{const t=new URLSearchParams(window.location.search).get("_dl_vid");if(t&&t.startsWith("anon_"))return this.setRootDomainCookie("__dl_visitor_id",t),r.set("dl_anonymous_id",t),t}catch(t){console.warn("[Datalyr] Failed to parse URL for _dl_vid:",t)}let t=a.get("__dl_visitor_id");return t?(r.set("dl_anonymous_id",t),t):(t=r.get("dl_anonymous_id"),t?(this.setRootDomainCookie("__dl_visitor_id",t),t):(t=`anon_${l()}`,this.setRootDomainCookie("__dl_visitor_id",t),r.set("dl_anonymous_id",t),t))}setRootDomainCookie(t,e){try{const i=function(){const t=window.location.hostname;if("localhost"===t||t.match(/^[0-9]{1,3}\./)||t.match(/^\[?[0-9a-fA-F:]+\]?$/))return t;const e=t.split(".");if(e.length>=2){const t=e[e.length-1],i=`${e[e.length-2]}.${t}`;return["co.uk","org.uk","net.uk","ac.uk","gov.uk","me.uk","com.au","net.au","org.au","edu.au","gov.au","co.nz","net.nz","org.nz","govt.nz","co.jp","ne.jp","or.jp","ac.jp","go.jp","co.in","net.in","org.in","gov.in","ac.in","co.za","net.za","org.za","gov.za","com.br","net.br","org.br","gov.br","edu.br","co.kr","ne.kr","or.kr","go.kr","ac.kr","com.cn","net.cn","org.cn","gov.cn","edu.cn","co.id","co.th","com.sg","com.my","com.ph","com.vn","com.tw","com.hk","com.mx","com.ar","com.co","com.pe","com.cl","co.il","co.at","co.hu","co.pl"].includes(i)&&e.length>=3?"."+e.slice(-3).join("."):"."+e.slice(-2).join(".")}return t}(),s="https:"===location.protocol?"; Secure":"",n=encodeURIComponent(e);document.cookie=`${t}=${n}; domain=${i}; path=/; max-age=31536000; SameSite=Lax${s}`;a.get(t)!==e&&(document.cookie=`${t}=${n}; path=/; max-age=31536000; SameSite=Lax${s}`)}catch(i){console.error("[Datalyr] Error setting root domain cookie:",i);try{const i="https:"===location.protocol?"; Secure":"",s=encodeURIComponent(e);document.cookie=`${t}=${s}; path=/; max-age=31536000; SameSite=Lax${i}`}catch(t){console.error("[Datalyr] Failed to set cookie even without domain:",t)}}}getStoredUserId(){return r.get("dl_user_id")}getAnonymousId(){return this.anonymousId}getUserId(){return this.userId}getDistinctId(){return this.userId||this.anonymousId}getCanonicalId(){return this.getDistinctId()}setSessionId(t){this.sessionId=t}getSessionId(){return this.sessionId}identify(t,e={}){if(!t)return console.warn("[Datalyr] identify() called without userId"),{};const i=this.userId;return this.userId=t,r.set("dl_user_id",t),{anonymous_id:this.anonymousId,user_id:t,previous_id:i,traits:e,identified_at:(new Date).toISOString(),resolution_method:"identify_call"}}alias(t,e){const i={userId:t,previousId:e||this.anonymousId,aliased_at:(new Date).toISOString()};return e&&e!==this.anonymousId||(this.userId=t,r.set("dl_user_id",t)),i}reset(){this.userId=null,r.remove("dl_user_id"),r.remove("dl_user_traits"),this.anonymousId=`anon_${l()}`,r.set("dl_anonymous_id",this.anonymousId),this.setRootDomainCookie("__dl_visitor_id",this.anonymousId)}getIdentityFields(){return{distinct_id:this.getDistinctId(),anonymous_id:this.anonymousId,user_id:this.userId,visitor_id:this.anonymousId,visitorId:this.anonymousId,canonical_id:this.getCanonicalId(),session_id:this.sessionId,sessionId:this.sessionId,resolution_method:"browser_sdk",resolution_confidence:1}}}class m{constructor(t=36e5){this.sessionId=null,this.sessionData=null,this.lastActivity=Date.now(),this.SESSION_KEY="dl_session_data",this.activityCheckInterval=null,this.activityListeners=[],this.sessionCreationLock=!1,this.sessionTimeout=t,this.initSession(),this.setupActivityMonitor()}initSession(){const t=r.get(this.SESSION_KEY),e=Date.now();t&&this.isSessionValid(t,e)?(this.sessionData=t,this.sessionId=t.id,this.lastActivity=e):this.createNewSession()}isSessionValid(t,e){return e-t.lastActivity<this.sessionTimeout&&t.isActive}createNewSession(){if(this.sessionCreationLock)return console.log("[Datalyr Session] Session creation already in progress, returning existing ID"),this.sessionId||"";this.sessionCreationLock=!0;try{const t=Date.now();return this.sessionId=`sess_${l()}`,this.sessionData={id:this.sessionId,startTime:t,lastActivity:t,pageViews:0,events:0,duration:0,isActive:!0},this.saveSession(),this.incrementSessionCount(),this.sessionId}finally{this.sessionCreationLock=!1}}rotateSessionId(){const t=Date.now(),e=this.sessionId;return this.sessionId=`sess_${l()}`,this.sessionData?(this.sessionData.id=this.sessionId,this.sessionData.lastActivity=t,this.saveSession(),e&&r.remove(`dl_session_${e}_attribution`),console.log(`[Datalyr Session] Rotated session ID from ${e} to ${this.sessionId}`)):this.createNewSession(),this.sessionId}getSessionId(){return this.sessionId&&this.isSessionActive()||this.createNewSession(),this.sessionId}getSessionData(){return this.sessionData}updateActivity(t){const e=Date.now();this.sessionData&&this.isSessionValid(this.sessionData,e)?(this.lastActivity=e,this.sessionData.lastActivity=e,this.sessionData.duration=e-this.sessionData.startTime,"pageview"!==t&&"page_view"!==t||this.sessionData.pageViews++,this.sessionData.events++,this.saveSession()):this.createNewSession()}isSessionActive(){if(!this.sessionData)return!1;const t=Date.now();return this.isSessionValid(this.sessionData,t)}endSession(){this.sessionData&&(this.sessionData.isActive=!1,this.saveSession()),this.sessionId=null,this.sessionData=null,this.destroy()}saveSession(){this.sessionData&&r.set(this.SESSION_KEY,this.sessionData)}getTimeout(){return this.sessionTimeout}setTimeout(t){this.sessionTimeout=t}storeAttribution(t){r.set("dl_current_session_attribution",Object.assign(Object.assign({},t),{sessionId:this.sessionId,timestamp:Date.now()}))}getAttribution(){if(!this.sessionId)return null;if(!this.isSessionActive())return null;const t=r.get("dl_current_session_attribution");return t&&t.sessionId===this.sessionId?t:null}getMetrics(){return this.sessionData?{session_id:this.sessionId,session_duration:this.sessionData.duration,session_page_views:this.sessionData.pageViews,session_events:this.sessionData.events,session_start:this.sessionData.startTime,time_since_session_start:Date.now()-this.sessionData.startTime}:{}}setupActivityMonitor(){const t=()=>{const t=Date.now();this.sessionData&&t-this.lastActivity>1e3&&this.updateActivity()};["mousedown","keydown","scroll","touchstart"].forEach(e=>{window.addEventListener(e,t,{passive:!0,capture:!0}),this.activityListeners.push({event:e,handler:t})}),this.activityCheckInterval=setInterval(()=>{this.sessionData&&!this.isSessionActive()&&this.createNewSession()},6e4)}destroy(){this.activityListeners.forEach(({event:t,handler:e})=>{window.removeEventListener(t,e)}),this.activityListeners=[],this.activityCheckInterval&&(clearInterval(this.activityCheckInterval),this.activityCheckInterval=null)}getSessionNumber(){return r.get("dl_session_count",0)+1}incrementSessionCount(){const t=r.get("dl_session_count",0);r.set("dl_session_count",t+1)}}class y{constructor(t={}){this.queryParamsCache=null,this.UTM_PARAMS=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],this.CLICK_IDS=["fbclid","gclid","gbraid","wbraid","ttclid","msclkid","twclid","li_fat_id","sclid","dclid","epik","rdt_cid","obclid","irclid","ko_click_id"],this.DEFAULT_TRACKED_PARAMS=["lyr","ref","source","campaign","medium","gad_source"],this.attributionWindow=t.attributionWindow||7776e6,this.trackedParams=[...this.DEFAULT_TRACKED_PARAMS,...t.trackedParams||[]]}clearCache(){this.queryParamsCache=null}captureAttribution(){const t=this.queryParamsCache||d();this.queryParamsCache||(this.queryParamsCache=t);const e={timestamp:Date.now()};for(const i of this.UTM_PARAMS){const s=t[i];if(s){e[i.replace("utm_","")]=s}}for(const i of this.CLICK_IDS){const s=t[i];s&&(e.clickId||(e.clickId=s,e.clickIdType=i),e[i]=s)}for(const i of this.trackedParams){const s=t[i];s&&(e[i]=s)}return document.referrer&&(e.referrer=document.referrer,e.referrerHost=this.extractHostname(document.referrer)),e.landingPage=window.location.href,e.landingPath=window.location.pathname,e.source||(e.source=this.determineSource(e)),e.medium||(e.medium=this.determineMedium(e)),e}storeFirstTouch(t){const e=r.get("dl_first_touch");let i=!1;e?e.expires_at&&Date.now()>=e.expires_at&&(i=!0):i=!0,i&&r.set("dl_first_touch",Object.assign(Object.assign({},t),{captured_at:Date.now(),expires_at:Date.now()+this.attributionWindow}))}getFirstTouch(){const t=r.get("dl_first_touch");return t&&t.expires_at&&Date.now()>=t.expires_at?(r.remove("dl_first_touch"),null):t}storeLastTouch(t){r.set("dl_last_touch",Object.assign(Object.assign({},t),{captured_at:Date.now(),expires_at:Date.now()+this.attributionWindow}))}getLastTouch(){const t=r.get("dl_last_touch");return t&&t.expires_at&&Date.now()>=t.expires_at?(r.remove("dl_last_touch"),null):t}addTouchpoint(t,e){const i=this.getJourney(),s={timestamp:Date.now(),sessionId:t,source:e.source||void 0,medium:e.medium||void 0,campaign:e.campaign||void 0};i.push(s),i.length>30&&i.shift(),r.set("dl_journey",i)}getJourney(){return r.get("dl_journey",[])}captureAdCookies(){var t,e;const i={};if(i._fbp=a.get("_fbp"),i._fbc=a.get("_fbc"),i._gcl_aw=a.get("_gcl_aw"),i._gcl_dc=a.get("_gcl_dc"),i._gcl_gb=a.get("_gcl_gb"),i._gcl_ha=a.get("_gcl_ha"),i._gac=a.get("_gac"),i._ga=a.get("_ga"),i._gid=a.get("_gid"),i._ttp=a.get("_ttp"),i._ttc=a.get("_ttc"),!i._fbp&&(this.hasClickId("fbclid")||i._fbc)){const t=Date.now(),e=Math.floor(1e10*Math.random()).toString();i._fbp=`fb.1.${t}.${e}`,a.set("_fbp",i._fbp,90)}const s=this.getCurrentFbclid();s&&!a.get("_dl_fbclid_at")&&a.set("_dl_fbclid_at",String(Date.now()),90);if((this.hasClickId("gclid")&&null!==(e=null===(t=this.queryParamsCache)||void 0===t?void 0:t.gclid)&&void 0!==e?e:null)&&!a.get("_dl_gclid_at")&&a.set("_dl_gclid_at",String(Date.now()),90),s&&!i._fbc){const t=a.get("_dl_fbclid_at"),e=t?Number(t):NaN,n=Number.isFinite(e)&&e>0?e:Date.now();i._fbc=`fb.1.${n}.${s}`,a.set("_fbc",i._fbc,90)}return Object.fromEntries(Object.entries(i).filter(([t,e])=>null!==e))}hasClickId(t){return!!(this.queryParamsCache||d())[t]}getCurrentFbclid(){return(this.queryParamsCache||d()).fbclid||null}getAttributionData(){const t=this.getFirstTouch(),e=this.getLastTouch(),i=this.getJourney();let s=this.captureAttribution();const n=!!(s.clickId||s.campaign||s.source&&"direct"!==s.source);!n&&t&&(!t.expires_at||Date.now()<t.expires_at)&&(s=Object.assign(Object.assign({},t),{referrer:s.referrer,referrerHost:s.referrerHost,landingPage:s.landingPage,landingPath:s.landingPath}));const o=this.captureAdCookies();return!t&&n&&this.storeFirstTouch(s),n&&this.storeLastTouch(s),Object.assign(Object.assign(Object.assign({},s),o),{first_touch_source:null==t?void 0:t.source,first_touch_medium:null==t?void 0:t.medium,first_touch_campaign:null==t?void 0:t.campaign,first_touch_timestamp:null==t?void 0:t.timestamp,firstTouchSource:null==t?void 0:t.source,firstTouchMedium:null==t?void 0:t.medium,firstTouchCampaign:null==t?void 0:t.campaign,last_touch_source:null==e?void 0:e.source,last_touch_medium:null==e?void 0:e.medium,last_touch_campaign:null==e?void 0:e.campaign,last_touch_timestamp:null==e?void 0:e.timestamp,lastTouchSource:null==e?void 0:e.source,lastTouchMedium:null==e?void 0:e.medium,lastTouchCampaign:null==e?void 0:e.campaign,touchpoint_count:i.length,touchpointCount:i.length,days_since_first_touch:(null==t?void 0:t.timestamp)?Math.floor((Date.now()-t.timestamp)/864e5):0,daysSinceFirstTouch:(null==t?void 0:t.timestamp)?Math.floor((Date.now()-t.timestamp)/864e5):0})}determineSource(t){if(t.clickIdType){return{fbclid:"facebook",gclid:"google",ttclid:"tiktok",msclkid:"bing",twclid:"twitter",li_fat_id:"linkedin",sclid:"snapchat",dclid:"doubleclick",epik:"pinterest"}[t.clickIdType]||"paid"}if(t.referrerHost){const e=t.referrerHost.toLowerCase(),i=("undefined"!=typeof window?window.location.hostname:"").toLowerCase();return i&&(e===i||this.isSameRootDomain(e,i))?"direct":e.includes("facebook.com")||e.includes("fb.com")?"facebook":e.includes("twitter.com")||e.includes("t.co")||e.includes("x.com")?"twitter":e.includes("linkedin.com")||e.includes("lnkd.in")?"linkedin":e.includes("instagram.com")?"instagram":e.includes("youtube.com")||e.includes("youtu.be")?"youtube":e.includes("tiktok.com")?"tiktok":e.includes("reddit.com")?"reddit":e.includes("pinterest.com")?"pinterest":e.includes("google.")?"google":e.includes("bing.com")?"bing":e.includes("yahoo.com")?"yahoo":e.includes("duckduckgo.com")?"duckduckgo":e.includes("baidu.com")?"baidu":"referral"}return"direct"}determineMedium(t){if(t.clickId)return"cpc";const e=t.source;if(!e||"direct"===e)return"none";if(["facebook","twitter","linkedin","instagram","youtube","tiktok","reddit","pinterest"].includes(e))return"social";return["google","bing","yahoo","duckduckgo","baidu"].includes(e)?"organic":"referral"}isSameRootDomain(t,e){const i=t=>t.split(".").slice(-2).join(".");return i(t)===i(e)}extractHostname(t){try{return new URL(t).hostname}catch(t){return""}}isAttributionExpired(t){return!t.timestamp||Date.now()-t.timestamp>this.attributionWindow}clearExpiredAttribution(){const t=this.getFirstTouch(),e=this.getLastTouch();t&&this.isAttributionExpired(t)&&r.remove("dl_first_touch"),e&&this.isAttributionExpired(e)&&r.remove("dl_last_touch")}}const _=["purchase","signup","subscribe","lead","conversion"],v=["add_to_cart","begin_checkout","view_item","search"];class w extends Error{}class b{constructor(t){this.queue=[],this.offlineQueue=[],this.batchTimer=null,this.periodicFlushInterval=null,this.flushPromise=null,this.recentEventIds=new Set,this.MAX_RECENT_EVENT_IDS=1e3,this.OFFLINE_QUEUE_KEY="dl_offline_queue",this.flushLock=!1,this.offlineQueueLock=!1,this.offlineProcessing=!1,this.inFlight=[],this.enabled=!0,this.rateLimitedUntil=0;const e=(t,e,i,s)=>{const n=Number(t);return Number.isFinite(n)?Math.min(Math.max(Math.floor(n),i),s):e};this.config={batchSize:e(t.batchSize,10,1,1e3),flushInterval:e(t.flushInterval,5e3,250,36e5),maxRetries:e(t.maxRetries,5,0,20),retryDelay:e(t.retryDelay,1e3,0,6e4),endpoint:t.endpoint||"https://ingest.datalyr.com",fallbackEndpoints:Array.isArray(t.fallbackEndpoints)?t.fallbackEndpoints:[],workspaceId:t.workspaceId,debug:t.debug||!1,criticalEvents:t.criticalEvents||_,highPriorityEvents:t.highPriorityEvents||v,maxOfflineQueueSize:e(t.maxOfflineQueueSize,100,1,1e5)},this.networkStatus={isOnline:!1!==navigator.onLine,lastOfflineAt:null,lastOnlineAt:null},this.loadOfflineQueue(),this.setupNetworkListeners(),this.startPeriodicFlush(),this.networkStatus.isOnline&&this.offlineQueue.length>0&&setTimeout(()=>this.processOfflineQueue(),1e3)}enqueue(t){if(!this.enabled)return;const e=t.event_name;if(this.isDuplicateEvent(t))this.log("Duplicate event suppressed:",e);else{if(this.config.criticalEvents.includes(e))return this.log("Critical event, sending immediately:",e),void this.sendBatch([t]).catch(i=>{this.log("Critical event send failed, adding to offline queue:",e,i),this.moveToOfflineQueue([t])});this.queue.push(t),this.log("Event queued:",e),this.shouldFlush(e)&&this.flush()}}isDuplicateEvent(t){const e=this.createEventHash(t);if(this.recentEventIds.has(e))return!0;if(this.recentEventIds.add(e),this.recentEventIds.size>this.MAX_RECENT_EVENT_IDS){const t=this.recentEventIds.size-this.MAX_RECENT_EVENT_IDS,e=this.recentEventIds.values();for(let i=0;i<t;i++){const t=e.next();t.done||this.recentEventIds.delete(t.value)}}return!1}createEventHash(t){const e=[t.event_name,t.timestamp,JSON.stringify(t.event_data||{})].join("|");let i=0;for(let t=0;t<e.length;t++){i=(i<<5)-i+e.charCodeAt(t),i&=i}return i.toString(36)}shouldFlush(t){return this.queue.length>=this.config.batchSize||(t&&this.config.highPriorityEvents.includes(t)?(this.batchTimer&&clearTimeout(this.batchTimer),this.batchTimer=setTimeout(()=>this.flush(),1e3),!1):(this.batchTimer||(this.batchTimer=setTimeout(()=>this.flush(),this.config.flushInterval)),!1))}flush(){return t(this,void 0,void 0,function*(){if(this.enabled&&!(Date.now()<this.rateLimitedUntil)){if(this.flushPromise||this.flushLock)return this.flushPromise||Promise.resolve();this.flushLock=!0;try{this.flushPromise=this._flush(),yield this.flushPromise}finally{this.flushPromise=null,this.flushLock=!1}}})}_flush(){return t(this,void 0,void 0,function*(){if(this.batchTimer&&(clearTimeout(this.batchTimer),this.batchTimer=null),0===this.queue.length)return;if(!this.networkStatus.isOnline)return this.log("Network offline, queuing events"),void this.moveToOfflineQueue();const t=Math.min(this.config.batchSize,this.queue.length),e=this.queue.slice(0,t);this.inFlight=e;try{yield this.sendBatch(e),this.queue.splice(0,t),this.log(`Successfully sent and removed ${t} events from queue`)}catch(i){this.log("Failed to send batch:",i),this.queue.splice(0,t),this.moveToOfflineQueue(e)}finally{this.inFlight=[]}})}sendBatch(e){return t(this,arguments,void 0,function*(t,e=0,i=0){const s={events:t,batchId:l(),timestamp:(new Date).toISOString()},n=[this.config.endpoint,...this.config.fallbackEndpoints],o=n[i]||this.config.endpoint;try{const e=yield fetch(o,{method:"POST",headers:{"Content-Type":"application/json","X-Batch-Size":t.length.toString()},body:JSON.stringify(s),keepalive:!0});if(!e.ok){if(429===e.status){const t=parseInt(e.headers.get("Retry-After")||"60");throw this.rateLimitedUntil=Date.now()+1e3*Math.max(t,1),this.log(`Rate limited; backing off ${t}s`),new w("Rate limited (429)")}throw new Error(`HTTP ${e.status}: ${e.statusText}`)}this.log(`Batch sent successfully to ${o}: ${t.length} events`)}catch(s){if(s instanceof w)throw s;if(i<n.length-1)return this.log(`Failed on ${o}, trying fallback ${i+1}`),this.sendBatch(t,0,i+1);if(e<this.config.maxRetries){const s=function(t,e=1e3){const i=.1*Math.random();return Math.min(e*Math.pow(2,t)*(1+i),3e4)}(e,this.config.retryDelay);return this.log(`Retrying batch in ${s}ms (attempt ${e+1}/${this.config.maxRetries})`),yield new Promise(t=>setTimeout(t,s)),this.sendBatch(t,e+1,i)}throw s}})}setupNetworkListeners(){window.addEventListener("online",()=>{this.networkStatus.isOnline=!0,this.networkStatus.lastOnlineAt=Date.now(),this.log("Network connection restored"),setTimeout(()=>this.processOfflineQueue(),1e3)}),window.addEventListener("offline",()=>{this.networkStatus.isOnline=!1,this.networkStatus.lastOfflineAt=Date.now(),this.log("Network connection lost")})}startPeriodicFlush(){this.periodicFlushInterval=setInterval(()=>{this.queue.length>0&&this.flush(),this.networkStatus.isOnline&&this.offlineQueue.length>0&&this.processOfflineQueue()},this.config.flushInterval)}stopPeriodicFlush(){this.periodicFlushInterval&&(clearInterval(this.periodicFlushInterval),this.periodicFlushInterval=null)}moveToOfflineQueue(t){if(this.enabled)if(this.offlineQueueLock)console.warn("[Datalyr Queue] Offline queue operation already in progress");else{this.offlineQueueLock=!0;try{if(t?this.offlineQueue.push(...t):(this.offlineQueue.push(...this.queue),this.queue=[]),this.offlineQueue.length>this.config.maxOfflineQueueSize){const t=this.offlineQueue.length-this.config.maxOfflineQueueSize;this.offlineQueue.splice(0,t)}this.saveOfflineQueue()}finally{this.offlineQueueLock=!1}}}loadOfflineQueue(){const t=r.get(this.OFFLINE_QUEUE_KEY,[]);Array.isArray(t)&&(this.offlineQueue=t,this.log(`Loaded ${this.offlineQueue.length} offline events`))}saveOfflineQueue(){if(!this.enabled)return;const t=this.offlineQueue.slice(-this.config.maxOfflineQueueSize);r.set(this.OFFLINE_QUEUE_KEY,t)}processOfflineQueue(){return t(this,void 0,void 0,function*(){if(this.enabled&&!(Date.now()<this.rateLimitedUntil)&&!this.offlineProcessing&&0!==this.offlineQueue.length&&this.networkStatus.isOnline){this.offlineProcessing=!0;try{for(this.log(`Processing ${this.offlineQueue.length} offline events`);this.offlineQueue.length>0&&this.enabled;){const t=this.offlineQueue.splice(0,this.config.batchSize);try{yield this.sendBatch(t),this.saveOfflineQueue()}catch(e){this.log("Failed to send offline batch:",e),this.offlineQueue.unshift(...t),this.saveOfflineQueue();break}}0===this.offlineQueue.length&&r.remove(this.OFFLINE_QUEUE_KEY)}finally{this.offlineProcessing=!1}}})}getQueueSize(){return this.queue.length}getOfflineQueueSize(){return this.offlineQueue.length}getNetworkStatus(){return Object.assign({},this.networkStatus)}buildBatch(t){return{events:t,batchId:l(),timestamp:(new Date).toISOString()}}forceFlush(){return t(this,void 0,void 0,function*(){if(!this.enabled)return;if(!navigator.sendBeacon)return yield this.flush(),void(yield this.processOfflineQueue());const t=[...this.queue.slice(this.inFlight.length),...this.offlineQueue];if(this.queue=this.queue.slice(0,this.inFlight.length),this.offlineQueue=[],0===t.length)return;const e=[];let i=[],s=2;for(const n of t){const t=new Blob([JSON.stringify(n)]).size+1;i.length>0&&(i.length>=this.config.batchSize||s+t>6e4)&&(e.push(i),i=[],s=2),i.push(n),s+=t}i.length>0&&e.push(i);let n=-1;for(let t=0;t<e.length;t++){const i=new Blob([JSON.stringify(this.buildBatch(e[t]))],{type:"application/json"});if(!navigator.sendBeacon(this.config.endpoint,i)){n=t;break}}if(n>=0){const t=e.slice(n).reduce((t,e)=>t.concat(e),[]),i=t.slice(-this.config.maxOfflineQueueSize);t.length>i.length&&this.log(`Offline cap dropped ${t.length-i.length} oldest events at unload`),r.set(this.OFFLINE_QUEUE_KEY,i),this.log(`sendBeacon refused ${t.length} events; persisted ${i.length} for next load`)}else this.log("Events sent via sendBeacon"),r.remove(this.OFFLINE_QUEUE_KEY)})}clear(){this.queue=[],this.batchTimer&&(clearTimeout(this.batchTimer),this.batchTimer=null)}setEnabled(t){this.enabled=t}clearOffline(){this.offlineQueue=[],r.remove(this.OFFLINE_QUEUE_KEY)}log(...t){this.config.debug&&console.log("[Datalyr Queue]",...t)}destroy(){this.stopPeriodicFlush(),this.batchTimer&&(clearTimeout(this.batchTimer),this.batchTimer=null),this.queue.length>0&&this.moveToOfflineQueue()}}class k{constructor(t={}){this.privacyMode=t.privacyMode||"standard",this.enableFingerprinting=!1!==t.enableFingerprinting}collect(){return"strict"!==this.privacyMode&&this.enableFingerprinting?this.collectStandard():this.collectMinimal()}collectMinimal(){const t={timezone:this.getTimezone(),language:navigator.language||null,screen_bucket:this.getScreenBucket(),dnt:"1"===navigator.doNotTrack||!0===window.globalPrivacyControl||null,userAgent:navigator.userAgent||null};if("userAgentData"in navigator){const e=navigator.userAgentData;t.userAgentData={brands:e.brands||[],mobile:e.mobile||!1,platform:e.platform||null}}return t}collectStandard(){const t={};try{if(t.timezone=this.getTimezone(),t.language=navigator.language||null,t.screen_bucket=this.getScreenBucket(),t.dnt="1"===navigator.doNotTrack||!0===window.globalPrivacyControl||null,t.userAgent=navigator.userAgent||null,"userAgentData"in navigator){const e=navigator.userAgentData;t.userAgentData={brands:e.brands||[],mobile:e.mobile||!1,platform:e.platform||null}}}catch(t){console.warn("[Datalyr] Error collecting fingerprint:",t)}return t}getTimezone(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone||null}catch(t){return null}}getScreenBucket(){try{if(!window.screen)return null;const t=100*Math.round(screen.width/100);return`${t}x${100*Math.round(screen.height/100)}`}catch(t){return null}}generateHash(e){return t(this,void 0,void 0,function*(){try{const t=Object.keys(e).sort().reduce((t,i)=>(t[i]=e[i],t),{}),i=JSON.stringify(t);if(window.crypto&&window.crypto.subtle){const t=(new TextEncoder).encode(i),e=yield crypto.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(e)).map(t=>t.toString(16).padStart(2,"0")).join("")}let s=0;for(let t=0;t<i.length;t++){s=(s<<5)-s+i.charCodeAt(t),s&=s}return Math.abs(s).toString(16)}catch(t){return""}})}}class S{constructor(t){this.scripts=[],this.loadedScripts=new Set,this.sessionLoadedScripts=new Set,this.pixels=null,this.initialized=!1,this.sandboxedIframes=[],this.iframeCleanupTimeouts=new Map,this.messageHandler=null,this.workspaceId=t.workspaceId,this.endpoint=t.endpoint||"https://ingest.datalyr.com",this.debug=t.debug||!1,this.getIdentity=t.getIdentity;const e=r.get("dl_session_scripts",[]);this.sessionLoadedScripts=new Set(e),this.messageHandler=t=>{if(t.data&&"datalyr_script_complete"===t.data.type){const e=t.data.scriptId;this.log("Received script completion signal:",e);const i=this.sandboxedIframes.find(t=>t.dataset.datalyrScript===e);i&&this.cleanupIframe(i)}},window.addEventListener("message",this.messageHandler)}init(){return t(this,void 0,void 0,function*(){if(!this.initialized)try{const t=yield fetch(`${this.endpoint}/container-scripts`,{method:"POST",headers:{"Content-Type":"application/json","X-Container-Version":"1.0"},body:JSON.stringify({workspaceId:this.workspaceId})});if(!t.ok)throw new Error(`Failed to fetch container scripts: ${t.status}`);const e=yield t.json();this.scripts=e.scripts||[],this.pixels=e.pixels||null,this.remoteConfig=e.config&&"object"==typeof e.config?e.config:void 0,this.pixels&&(yield this.initializePixels()),this.loadScriptsByTrigger("page_load"),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",()=>{this.loadScriptsByTrigger("dom_ready")}):this.loadScriptsByTrigger("dom_ready"),window.addEventListener("load",()=>{this.loadScriptsByTrigger("window_load")}),this.initialized=!0,this.log("Container manager initialized with",this.scripts.length,"scripts")}catch(t){this.log("Error initializing container:",t)}})}getRemoteConfig(){return this.remoteConfig}loadScriptsByTrigger(t){this.scripts.filter(e=>e.enabled&&e.trigger===t&&this.shouldLoadScript(e)).forEach(t=>this.loadScript(t))}shouldLoadScript(t){return("once_per_page"!==t.frequency||!this.loadedScripts.has(t.id))&&(("once_per_session"!==t.frequency||!this.sessionLoadedScripts.has(t.id))&&(!(t.conditions&&t.conditions.length>0)||this.evaluateConditions(t.conditions)))}evaluateConditions(t){return t.every(t=>{try{const{type:e,operator:i,value:s}=t;switch(e){case"url_path":return this.evaluateStringCondition(window.location.pathname,i,s);case"url_host":return this.evaluateStringCondition(window.location.hostname,i,s);case"url_parameter":const e=new URLSearchParams(window.location.search);return this.evaluateStringCondition(e.get(t.parameter)||"",i,s);case"referrer":return this.evaluateStringCondition(document.referrer,i,s);case"device_type":const n=/Mobile|Android|iPhone|iPad/i.test(navigator.userAgent);return this.evaluateStringCondition(n?"mobile":"desktop",i,s);default:return!0}}catch(t){return!1}})}evaluateStringCondition(t,e,i){switch(e){case"equals":return t===i;case"not_equals":return t!==i;case"contains":return t.includes(i);case"not_contains":return!t.includes(i);case"starts_with":return t.startsWith(i);case"ends_with":return t.endsWith(i);case"matches_regex":try{return new RegExp(i).test(t)}catch(t){return!1}default:return!1}}loadScript(t){try{switch(t.type){case"inline":this.loadInlineScript(t);break;case"external":this.loadExternalScript(t);break;case"pixel":this.loadPixel(t)}this.loadedScripts.add(t.id),"once_per_session"===t.frequency&&(this.sessionLoadedScripts.add(t.id),r.set("dl_session_scripts",Array.from(this.sessionLoadedScripts))),this.log("Loaded script:",t.name)}catch(e){this.log("Error loading script:",t.name,e)}}loadInlineScript(t){const e=document.createElement("iframe");e.style.display="none",e.setAttribute("sandbox","allow-scripts"),e.dataset.datalyrScript=t.id,this.sandboxedIframes.push(e);const i=`\n <!DOCTYPE html>\n <html>\n <head>\n <meta charset="UTF-8">\n </head>\n <body>\n <script>\n // User-provided script runs here in isolation\n try {\n ${t.content}\n } catch (error) {\n console.error('[Datalyr Container] Script execution error:', error);\n }\n\n // FIXED (ISSUE-02): Signal completion for cleanup\n // Scripts have 5 seconds to execute before iframe is removed\n setTimeout(function() {\n try {\n parent.postMessage({ type: 'datalyr_script_complete', scriptId: '${t.id}' }, '*');\n } catch (e) {\n // Ignore postMessage errors from sandbox\n }\n }, 5000);\n <\/script>\n </body>\n </html>\n `;document.body.appendChild(e),e.contentDocument&&(e.contentDocument.open(),e.contentDocument.write(i),e.contentDocument.close());const s=window.setTimeout(()=>{this.cleanupIframe(e)},3e4);this.iframeCleanupTimeouts.set(e,s),this.log("Loaded inline script in sandbox:",t.id)}cleanupIframe(t){try{const e=this.iframeCleanupTimeouts.get(t);e&&(clearTimeout(e),this.iframeCleanupTimeouts.delete(t));const i=this.sandboxedIframes.indexOf(t);i>-1&&this.sandboxedIframes.splice(i,1),t.parentNode&&(t.parentNode.removeChild(t),this.log("Cleaned up sandboxed iframe:",t.dataset.datalyrScript))}catch(t){this.log("Error cleaning up iframe:",t)}}cleanupAllIframes(){const t=[...this.sandboxedIframes];t.forEach(t=>this.cleanupIframe(t)),this.messageHandler&&(window.removeEventListener("message",this.messageHandler),this.messageHandler=null),this.iframeCleanupTimeouts.forEach(t=>clearTimeout(t)),this.iframeCleanupTimeouts.clear(),this.log(`Cleaned up ${t.length} sandboxed iframes`)}loadExternalScript(t){var e;if(!this.isValidScriptUrl(t.content))return void this.log("Blocked invalid script URL:",t.content);if(!(null===(e=t.settings)||void 0===e?void 0:e.integrity))return void console.error(`[Datalyr Container] SECURITY: External script "${t.id}" blocked - missing SRI hash.\nAll external scripts MUST include an integrity hash to prevent CDN compromise attacks.\nGenerate SRI hash at: https://www.srihash.org/\nExample: { integrity: "sha384-..." }`);const i=document.createElement("script");i.src=t.content,i.dataset.datalyrScript=t.id,t.settings?(i.integrity=t.settings.integrity,i.crossOrigin=t.settings.crossorigin||"anonymous",!1!==t.settings.async&&(i.async=!0),t.settings.defer&&(i.defer=!0)):i.async=!0,document.head.appendChild(i)}loadPixel(t){const e=new Image;e.src=t.content,e.style.display="none",e.dataset.datalyrPixel=t.id,document.body.appendChild(e)}initializePixels(){return t(this,void 0,void 0,function*(){var t,e,i;this.pixels&&((null===(t=this.pixels.meta)||void 0===t?void 0:t.enabled)&&this.pixels.meta.pixel_id&&(yield this.initializeMetaPixel(this.pixels.meta)),(null===(e=this.pixels.google)||void 0===e?void 0:e.enabled)&&this.pixels.google.tag_id&&this.initializeGoogleTag(this.pixels.google),(null===(i=this.pixels.tiktok)||void 0===i?void 0:i.enabled)&&this.pixels.tiktok.pixel_id&&this.initializeTikTokPixel(this.pixels.tiktok))})}initializeMetaPixel(e){return t(this,void 0,void 0,function*(){var t,i,s,n,o,r,a;try{i=window,s=document,n="script",i.fbq||(o=i.fbq=function(){o.callMethod?o.callMethod.apply(o,arguments):o.queue.push(arguments)},i._fbq||(i._fbq=o),o.push=o,o.loaded=!0,o.version="2.0",o.queue=[],(r=s.createElement(n)).async=!0,r.src="https://connect.facebook.net/en_US/fbevents.js",(a=s.getElementsByTagName(n)[0]).parentNode.insertBefore(r,a));const l={},d=null===(t=this.getIdentity)||void 0===t?void 0:t.call(this);if(null==d?void 0:d.externalId){const t=yield c(String(d.externalId));t&&(l.external_id=t)}if(null==d?void 0:d.email){const t=yield c(String(d.email).toLowerCase().trim());t&&(l.em=t)}Object.keys(l).length>0?(window.fbq("init",e.pixel_id,l),this.log("Meta Pixel initialized with advanced matching:",e.pixel_id,Object.keys(l))):(window.fbq("init",e.pixel_id),this.log("Meta Pixel initialized (no advanced matching):",e.pixel_id))}catch(t){this.log("Error initializing Meta Pixel:",t)}})}initializeGoogleTag(t){try{const e=document.createElement("script");function i(...t){window.dataLayer.push(t)}e.async=!0,e.src=`https://www.googletagmanager.com/gtag/js?id=${t.tag_id}`,document.head.appendChild(e),window.dataLayer=window.dataLayer||[],window.gtag=i,i("js",new Date),i("config",t.tag_id,{allow_enhanced_conversions:!1!==t.enhanced_conversions}),this.log("Google Tag initialized:",t.tag_id)}catch(s){this.log("Error initializing Google Tag:",s)}}initializeTikTokPixel(t){try{!function(t,e,i){t.TiktokAnalyticsObject=i;var s=t[i]=t[i]||[];s.methods=["page","track","identify","instances","debug","on","off","once","ready","alias","group","enableCookie","disableCookie"],s.setAndDefer=function(t,e){t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}};for(var n=0;n<s.methods.length;n++)s.setAndDefer(s,s.methods[n]);s.instance=function(t){for(var e=s._i[t]||[],i=0;i<s.methods.length;i++)s.setAndDefer(e,s.methods[i]);return e},s.load=function(t,e){var n;s._i=s._i||{},s._i[t]=[],s._o=s._o||{},s._o[t]=e||{};var o=document.createElement("script");o.type="text/javascript",o.async=!0,o.src="https://analytics.tiktok.com/i18n/pixel/events.js?sdkid="+t+"&lib="+i;var r=document.getElementsByTagName("script")[0];null===(n=r.parentNode)||void 0===n||n.insertBefore(o,r)}}(window,document,"ttq"),window.ttq.load(t.pixel_id),window.ttq.page(),this.log("TikTok Pixel initialized:",t.pixel_id)}catch(t){this.log("Error initializing TikTok Pixel:",t)}}trackToPixels(t,e={},i){var s,n,o,r,a,c,l,d,h,u;if(t.startsWith("$"))return;const g=this.sanitizeEventName(t),f=this.sanitizeProperties(e);if((null===(n=null===(s=this.pixels)||void 0===s?void 0:s.meta)||void 0===n?void 0:n.enabled)&&window.fbq)try{const e={page_view:"PageView",pageview:"PageView",view_content:"ViewContent",product_viewed:"ViewContent",view_item:"ViewContent",add_to_cart:"AddToCart",product_added:"AddToCart",add_to_wishlist:"AddToWishlist",initiate_checkout:"InitiateCheckout",begin_checkout:"InitiateCheckout",checkout_started:"InitiateCheckout",add_payment_info:"AddPaymentInfo",purchase:"Purchase",order_completed:"Purchase",order_paid:"Purchase",lead:"Lead",complete_registration:"CompleteRegistration",sign_up:"CompleteRegistration",signup:"CompleteRegistration",search:"Search",subscribe:"Subscribe",subscription_created:"Subscribe",start_trial:"StartTrial",trial_started:"StartTrial",contact:"Contact",schedule:"Schedule"},s=null===(r=null===(o=this.pixels)||void 0===o?void 0:o.meta)||void 0===r?void 0:r.event_mappings,n=(null==s?void 0:s[t])||e[String(t).toLowerCase()]||g;i?window.fbq("track",n,f,{eventID:i}):window.fbq("track",n,f)}catch(t){this.log("Error tracking Meta Pixel event:",t)}if((null===(c=null===(a=this.pixels)||void 0===a?void 0:a.google)||void 0===c?void 0:c.enabled)&&window.gtag)try{window.gtag("event",g,f)}catch(t){this.log("Error tracking Google Tag event:",t)}if((null===(d=null===(l=this.pixels)||void 0===l?void 0:l.tiktok)||void 0===d?void 0:d.enabled)&&window.ttq)try{const e={view_content:"ViewContent",product_viewed:"ViewContent",view_item:"ViewContent",search:"Search",add_to_wishlist:"AddToWishlist",add_to_cart:"AddToCart",product_added:"AddToCart",initiate_checkout:"InitiateCheckout",begin_checkout:"InitiateCheckout",checkout_started:"InitiateCheckout",add_payment_info:"AddPaymentInfo",purchase:"CompletePayment",order_completed:"CompletePayment",order_paid:"CompletePayment",place_an_order:"PlaceAnOrder",contact:"Contact",download:"Download",lead:"SubmitForm",submit_form:"SubmitForm",complete_registration:"CompleteRegistration",sign_up:"CompleteRegistration",signup:"CompleteRegistration",subscribe:"Subscribe",subscription_created:"Subscribe"},i=null===(u=null===(h=this.pixels)||void 0===h?void 0:h.tiktok)||void 0===u?void 0:u.event_mappings,s=(null==i?void 0:i[t])||e[String(t).toLowerCase()]||g;window.ttq.track(s,f)}catch(t){this.log("Error tracking TikTok Pixel event:",t)}}triggerCustomScript(t){const e=this.scripts.find(e=>e.id===t&&"custom"===e.trigger);e&&this.shouldLoadScript(e)&&this.loadScript(e)}getLoadedScripts(){return Array.from(this.loadedScripts)}isValidScriptUrl(t){try{const e=new URL(t);return!("https:"!==e.protocol&&!e.hostname.includes("localhost"))&&!["data:","javascript:","file:"].includes(e.protocol)}catch(t){return!1}}sanitizeEventName(t){return"string"!=typeof t?"unknown_event":t.replace(/[^a-zA-Z0-9_$ ]/g,"").substring(0,100)}sanitizeProperties(t){if(null==t)return{};if("object"!=typeof t)return this.sanitizeValue(t);if(Array.isArray(t))return t.map(t=>this.sanitizeValue(t));const e={};for(const[i,s]of Object.entries(t)){const t=i.replace(/[^a-zA-Z0-9_]/g,"").substring(0,100);t&&(e[t]=this.sanitizeValue(s))}return e}sanitizeValue(t){return null==t?t:"string"==typeof t?t.replace(/<script[^>]*>.*?<\/script>/gi,"").replace(/<[^>]+>/g,"").replace(/javascript:/gi,"").replace(/on\w+\s*=/gi,"").substring(0,1e3):"number"==typeof t||"boolean"==typeof t?t:"object"==typeof t?this.sanitizeProperties(t):String(t).substring(0,1e3)}log(...t){this.debug&&console.log("[Datalyr Container]",...t)}}class I{constructor(t={}){this.formListeners=[],this.lastIdentifyTime=0,this.RATE_LIMIT_MS=5e3,this.destroyed=!1,this.config={enabled:!1!==t.enabled,captureFromForms:!1!==t.captureFromForms,captureFromAPI:!0===t.captureFromAPI,captureFromShopify:!1!==t.captureFromShopify,trustedDomains:t.trustedDomains||[],debug:t.debug||!1}}initialize(e){return t(this,void 0,void 0,function*(){if(!this.config.enabled)return void this.log("Auto-identify disabled");this.identifyCallback=e;const t=yield r.getEncrypted("dl_auto_identified_email");if(t){const e=r.get("dl_auto_identified_email");if("string"==typeof e&&e.includes("@"))try{yield r.setEncrypted("dl_auto_identified_email",t),this.log("Migrated legacy plaintext auto-identified email to encrypted storage")}catch(t){this.log("Failed to migrate legacy auto-identified email:",t)}return void this.log("User already auto-identified")}this.config.captureFromForms&&this.setupFormMonitoring(),this.config.captureFromAPI&&(this.setupFetchInterception(),this.setupXHRInterception()),this.config.captureFromShopify&&this.setupShopifyMonitoring(),this.log("Auto-identify initialized")})}setupFormMonitoring(){this.scanForEmailForms();new MutationObserver(()=>{this.scanForEmailForms()}).observe(document.body,{childList:!0,subtree:!0}),this.log("Form monitoring active")}scanForEmailForms(){document.querySelectorAll("form").forEach(t=>{if(this.formListeners.some(e=>e.element===t))return;if(t.querySelector('input[type="email"], input[name*="email" i], input[id*="email" i]')){const e=e=>this.handleFormSubmit(e,t);t.addEventListener("submit",e),this.formListeners.push({element:t,handler:e}),this.log("Monitoring form:",t)}})}handleFormSubmit(t,e){try{const t=e.querySelector('input[type="email"], input[name*="email" i], input[id*="email" i]');if(!t)return;const i=t.value.trim();this.isValidEmail(i)&&(this.log("Email captured from form:",i),this.triggerIdentify(i,"form"))}catch(t){this.log("Error handling form submit:",t)}}setupFetchInterception(){if("function"!=typeof window.fetch)return;this.originalFetch=window.fetch;const e=this;window.fetch=function(i,s){return t(this,void 0,void 0,function*(){const n="string"==typeof i?i:i instanceof URL?i.href:i.url;if(!e.isTrustedDomain(n))return e.originalFetch.call(window,i,s);try{(null==s?void 0:s.body)&&e.extractEmailFromData(s.body,"api-request");const n=e.originalFetch.call(window,i,s);return n.then(i=>t(this,void 0,void 0,function*(){var t;if(i.ok&&(null===(t=i.headers.get("content-type"))||void 0===t?void 0:t.includes("application/json")))try{const t=i.clone(),s=yield t.json();e.extractEmailFromData(s,"api-response")}catch(t){}})).catch(()=>{}),n}catch(t){return e.log("Error intercepting fetch:",t),e.originalFetch.call(window,i,s)}})},this.log("Fetch interception active")}setupXHRInterception(){if("undefined"==typeof XMLHttpRequest)return;const t=this;this.originalXHROpen=XMLHttpRequest.prototype.open,this.originalXHRSend=XMLHttpRequest.prototype.send,XMLHttpRequest.prototype.open=function(e,i,...s){return this._datalyrUrl="string"==typeof i?i:i.href,t.originalXHROpen.apply(this,[e,i,...s])},XMLHttpRequest.prototype.send=function(e){const i=this._datalyrUrl;return i&&t.isTrustedDomain(i)&&(e&&t.extractEmailFromData(e,"api-request"),this.addEventListener("load",function(){if(this.status>=200&&this.status<300)try{const e=this.getResponseHeader("content-type");if(null==e?void 0:e.includes("application/json")){const e=JSON.parse(this.responseText);t.extractEmailFromData(e,"api-response")}}catch(t){}})),t.originalXHRSend.call(this,e)},this.log("XHR interception active")}setupShopifyMonitoring(){if(!this.isShopify())return;this.log("Shopify detected, setting up monitoring"),this.checkShopifyCustomer();let t=0;this.shopifyCheckInterval=setInterval(()=>{t++,this.checkShopifyCustomer(),t>=12&&this.shopifyCheckInterval&&(clearInterval(this.shopifyCheckInterval),this.shopifyCheckInterval=void 0,this.log("Shopify monitoring stopped after max checks (no login detected)"))},1e4),this.log("Shopify monitoring active")}isShopify(){return!!(window.Shopify||document.querySelector('meta[name="shopify-checkout-api-token"]')||window.location.hostname.includes(".myshopify.com"))}checkShopifyCustomer(){return t(this,void 0,void 0,function*(){var t;try{const e=yield fetch("/account.json",{credentials:"same-origin"});if(e.ok){const i=yield e.json();(null===(t=null==i?void 0:i.customer)||void 0===t?void 0:t.email)&&(this.log("Email captured from Shopify:",i.customer.email),this.triggerIdentify(i.customer.email,"shopify"),this.shopifyCheckInterval&&(clearInterval(this.shopifyCheckInterval),this.shopifyCheckInterval=void 0))}}catch(t){this.log("Shopify customer check failed:",t)}})}extractEmailFromData(t,e){try{let i=[];if("string"==typeof t)try{const e=JSON.parse(t);i=this.findEmailsInObject(e)}catch(e){const s=/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g,n=t.match(s);n&&(i=n)}else t instanceof FormData?t.forEach(t=>{"string"==typeof t&&this.isValidEmail(t)&&i.push(t)}):"object"==typeof t&&null!==t&&(i=this.findEmailsInObject(t));if(i.length>0){const t=i[0];this.log(`Email found in ${e}:`,t),this.triggerIdentify(t,"api")}}catch(t){this.log("Error extracting email from data:",t)}}findEmailsInObject(t,e=0){if(e>5)return[];const i=[];for(const s in t){if(!t.hasOwnProperty(s))continue;const n=t[s];/email/i.test(s)&&"string"==typeof n&&this.isValidEmail(n)?i.push(n):"object"==typeof n&&null!==n&&i.push(...this.findEmailsInObject(n,e+1))}return i}isTrustedDomain(t){try{const e=new URL(t,window.location.origin);return e.origin===window.location.origin||0!==this.config.trustedDomains.length&&this.config.trustedDomains.some(t=>e.hostname===t||e.hostname.endsWith(`.${t}`))}catch(t){return!1}}isValidEmail(t){if(!t||"string"!=typeof t)return!1;if(!/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(t))return!1;return![/test@/i,/example@/i,/demo@/i,/fake@/i,/@test\./i,/@example\./i].some(e=>e.test(t))}triggerIdentify(e,i){return t(this,void 0,void 0,function*(){if(this.destroyed)return;const t=Date.now();if(t-this.lastIdentifyTime<this.RATE_LIMIT_MS)return void this.log("Rate limited, skipping identify");this.lastIdentifyTime=t;if((yield r.getEncrypted("dl_auto_identified_email"))!==e){if(!this.destroyed){try{yield r.setEncrypted("dl_auto_identified_email",e)}catch(t){this.log("Failed to persist auto-identified email:",t)}this.identifyCallback&&(this.log(`Auto-identifying user: ${e} (source: ${i})`),this.identifyCallback(e,i))}}else this.log("Already identified with this email")})}destroy(){this.destroyed=!0,this.originalFetch&&(window.fetch=this.originalFetch,this.originalFetch=void 0),this.originalXHROpen&&(XMLHttpRequest.prototype.open=this.originalXHROpen,this.originalXHROpen=void 0),this.originalXHRSend&&(XMLHttpRequest.prototype.send=this.originalXHRSend,this.originalXHRSend=void 0),this.formListeners.forEach(({element:t,handler:e})=>{t.removeEventListener("submit",e)}),this.formListeners=[],this.shopifyCheckInterval&&(clearInterval(this.shopifyCheckInterval),this.shopifyCheckInterval=void 0),this.log("Auto-identify destroyed")}log(...t){this.config.debug&&console.log("[Datalyr Auto-Identify]",...t)}}const E=["autoIdentify","autoIdentifyForms","autoIdentifyAPI","autoIdentifyShopify","shopifyCartAttributes","checkoutChampDomains","respectGlobalPrivacyControl","respectDoNotTrack","privacyMode"];const D=new class{constructor(){this.explicitConfigKeys=new Set,this.superProperties={},this.userProperties={},this.optedOut=!1,this.consent=null,this.initialized=!1,this.errors=[],this.MAX_ERRORS=50,this.lastSpaPath=null,this.initializationPromise=null}init(t){if(this.initialized)return void console.warn("[Datalyr] SDK already initialized");if(!t.workspaceId)throw new Error("[Datalyr] workspaceId is required");this.explicitConfigKeys=new Set(Object.keys(t)),this.config=Object.assign({endpoint:"https://ingest.datalyr.com",debug:!1,batchSize:10,flushInterval:5e3,flushAt:10,criticalEvents:void 0,highPriorityEvents:void 0,sessionTimeout:36e5,trackSessions:!0,attributionWindow:7776e6,trackedParams:[],respectDoNotTrack:!1,respectGlobalPrivacyControl:!0,privacyMode:"standard",cookieDomain:"auto",cookieExpires:365,secureCookie:"auto",sameSite:"Lax",cookiePrefix:"__dl_",enablePerformanceTracking:!0,enableFingerprinting:!0,maxRetries:5,retryDelay:1e3,maxOfflineQueueSize:100,trackSPA:!0,trackPageViews:!0,fallbackEndpoints:[],plugins:[]},t),"shopify"===this.config.platform&&void 0===this.config.shopifyCartAttributes&&(this.config.shopifyCartAttributes=!0),this.cookies=new s({domain:this.config.cookieDomain,maxAge:this.config.cookieExpires,sameSite:this.config.sameSite,secure:this.config.secureCookie}),r.migrateFromLegacyPrefix(),this.optedOut="true"===this.cookies.get("__dl_opt_out"),this.consent=r.get("dl_consent",null),"checkoutchamp"===this.config.platform&&this.restoreFromURL(),this.identity=new p,this.session=new m(this.config.sessionTimeout),this.attribution=new y({attributionWindow:this.config.attributionWindow,trackedParams:this.config.trackedParams}),this.queue=new b(this.config),this.fingerprint=new k({privacyMode:this.config.privacyMode,enableFingerprinting:this.config.enableFingerprinting});const e=this.session.getSessionId();if(this.identity.setSessionId(e),this.queue.setEnabled(this.shouldTrack()),this.initializeAsync(),this.setupUnloadHandler(),this.config.plugins)for(const t of this.config.plugins)try{t.initialize(this),this.log(`Plugin initialized: ${t.name}`)}catch(e){console.warn(`[Datalyr] Plugin '${t.name}' failed to initialize:`,e),this.trackError(e,{plugin:t.name})}this.initialized=!0,this.log("SDK initialized")}initializeAsync(){return t(this,void 0,void 0,function*(){return this.initializationPromise||(this.initializationPromise=(()=>t(this,void 0,void 0,function*(){var t;try{try{const t=this.identity.getAnonymousId();yield e.initialize(this.config.workspaceId,t),this.userProperties=yield r.getEncrypted("dl_user_traits",{}),this.log("Encryption initialized, user properties loaded")}catch(t){console.warn("[Datalyr] Encryption unavailable (non-secure context?); continuing WITHOUT PII-at-rest encryption:",t),this.userProperties=r.get("dl_user_traits",{})}this.config.trackSPA&&this.setupSPATracking();const i="strict"===this.config.privacyMode;!1!==this.config.enableContainer&&this.shouldTrack()&&!i&&this.consentAllowsMarketing()&&(this.container=new S({workspaceId:this.config.workspaceId,endpoint:this.config.endpoint,debug:this.config.debug,getIdentity:()=>{var t,e;return{externalId:null===(t=this.identity)||void 0===t?void 0:t.getDistinctId(),email:null===(e=this.userProperties)||void 0===e?void 0:e.email}}}),yield this.container.init().catch(t=>{this.log("Container initialization failed:",t)}),"checkoutchamp"===this.config.platform&&this.fireCheckoutChampPurchasePixel()),function(t,e,i){if(!e)return;const s=t;for(const t of E){const n=e[t];null!=n&&((null==i?void 0:i.has(t))||(s[t]=n))}}(this.config,null===(t=this.container)||void 0===t?void 0:t.getRemoteConfig(),this.explicitConfigKeys),"strict"===this.config.privacyMode&&(this.config.autoIdentify=!1);("checkoutchamp"===this.config.platform||Array.isArray(this.config.checkoutChampDomains)&&this.config.checkoutChampDomains.length>0)&&void 0===this.config.autoIdentify&&(this.config.autoIdentify=!0),!0===this.config.autoIdentify&&this.shouldTrack()&&(this.autoIdentify=new I({enabled:!0,captureFromForms:this.config.autoIdentifyForms,captureFromAPI:this.config.autoIdentifyAPI,captureFromShopify:this.config.autoIdentifyShopify,trustedDomains:this.config.autoIdentifyTrustedDomains,debug:this.config.debug}),this.autoIdentify.initialize((t,e)=>{this.log(`Auto-identified user: ${t} from ${e}`),this.track("$auto_identify",{email:t,source:e,timestamp:Date.now()}),this.identify(t,{email:t})})),!0===this.config.shopifyCartAttributes&&this.shouldTrack()&&this.syncShopifyCartAttributes().catch(t=>{this.log("Shopify cart attribute sync failed:",t)}),Array.isArray(this.config.checkoutChampDomains)&&this.config.checkoutChampDomains.length>0&&this.shouldTrack()&&this.syncOutboundLinkParams(this.config.checkoutChampDomains),this.config.trackPageViews&&this.page(),this.log("Async initialization complete")}catch(t){console.error("[Datalyr] Async initialization failed:",t),this.userProperties=r.get("dl_user_traits",{})}}))()),this.initializationPromise})}ready(){return t(this,void 0,void 0,function*(){if(!this.initialized)throw new Error("[Datalyr] SDK not initialized. Call init() first.");return this.initializationPromise||Promise.resolve()})}track(t,e={}){if(this.initialized){if(this.shouldTrack())try{this.session.updateActivity(t);const i=l(),s=this.createEventPayload(t,e,i);if(this.queue.enqueue(s),this.container&&this.container.trackToPixels(t,e,i),this.config.plugins)for(const i of this.config.plugins)if(i.track)try{i.track(t,e)}catch(e){this.trackError(e,{plugin:i.name,event:t})}this.log("Event tracked:",t)}catch(e){this.trackError(e,{event:t})}}else console.warn("[Datalyr] SDK not initialized. Call init() first.")}trackAppDownloadClick(e){return t(this,void 0,void 0,function*(){if(this.initialized){if(this.shouldTrack())if(this.track("$app_download_click",{target_platform:e.targetPlatform,app_store_url:e.appStoreUrl}),yield this.queue.forceFlush(),"android"===e.targetPlatform&&e.appStoreUrl.includes("play.google.com")){const t=this.attribution.getLastTouch(),i=new URLSearchParams;(null==t?void 0:t.clickId)&&i.set("dl_click_id",t.clickId),(null==t?void 0:t.clickIdType)&&i.set("dl_click_id_type",t.clickIdType),(null==t?void 0:t.source)&&i.set("utm_source",t.source),(null==t?void 0:t.medium)&&i.set("utm_medium",t.medium),(null==t?void 0:t.campaign)&&i.set("utm_campaign",t.campaign),(null==t?void 0:t.content)&&i.set("utm_content",t.content),(null==t?void 0:t.term)&&i.set("utm_term",t.term);try{const t=new URL(e.appStoreUrl);t.searchParams.set("referrer",i.toString()),window.location.href=t.toString()}catch(t){window.location.href=e.appStoreUrl}}else window.location.href=e.appStoreUrl}else console.warn("[Datalyr] SDK not initialized. Call init() first.")})}identify(t,e={}){if(this.initialized){if(this.shouldTrack())if(t)try{this.session&&this.session.rotateSessionId();const i=this.identity.identify(t,e);if(this.userProperties=Object.assign(Object.assign({},this.userProperties),e),r.setEncrypted("dl_user_traits",this.userProperties).catch(t=>{console.error("[Datalyr] Failed to encrypt user traits - NOT storing unencrypted PII:",t),console.error("[Datalyr] User traits will only persist in memory until page reload")}),this.track("$identify",Object.assign(Object.assign({},i),{traits:e})),this.config.plugins)for(const i of this.config.plugins)if(i.identify)try{i.identify(t,e)}catch(t){this.trackError(t,{plugin:i.name})}this.log("User identified:",t)}catch(e){this.trackError(e,{userId:t})}else console.warn("[Datalyr] identify() called without userId")}else console.warn("[Datalyr] SDK not initialized. Call init() first.")}page(t={}){if(!this.initialized)return void console.warn("[Datalyr] SDK not initialized. Call init() first.");if(!this.shouldTrack())return;const e=this.session.getSessionId(),i=this.attribution.getJourney(),s=i.length?i[i.length-1]:null;s&&s.sessionId===e||this.attribution.addTouchpoint(e,this.attribution.captureAttribution());const n=Object.assign({title:document.title,url:window.location.href,path:window.location.pathname,search:window.location.search,referrer:document.referrer},t),o=function(){const t=document.referrer;if(!t)return{};try{const e=new URL(t);return{referrer:t,referrer_host:e.hostname,referrer_path:e.pathname,referrer_search:e.search,referrer_source:f(e.hostname)}}catch(e){return{referrer:t}}}();if(Object.assign(n,o),this.config.enablePerformanceTracking){const t=this.getPerformanceMetrics();t&&(n.performance=t)}if(this.track("pageview",n),this.config.plugins)for(const t of this.config.plugins)if(t.page)try{t.page(n)}catch(e){this.trackError(e,{plugin:t.name})}}screen(t,e={}){this.initialized?this.shouldTrack()&&this.track("pageview",Object.assign({screen:t},e)):console.warn("[Datalyr] SDK not initialized. Call init() first.")}group(t,e={}){this.initialized?this.shouldTrack()&&this.track("$group",{group_id:t,traits:e}):console.warn("[Datalyr] SDK not initialized. Call init() first.")}alias(t,e){if(!this.initialized)return void console.warn("[Datalyr] SDK not initialized. Call init() first.");if(!t)return void console.warn("[Datalyr] alias() requires a non-empty userId");if(!this.shouldTrack())return;const i=this.identity.alias(t,e);this.track("$alias",i)}reset(){this.initialized?(this.identity.reset(),this.userProperties={},this.superProperties={},r.remove("dl_user_traits"),r.remove("dl_auto_identified_email"),r.remove("dl_journey"),this.session.createNewSession(),this.log("User reset")):console.warn("[Datalyr] SDK not initialized. Call init() first.")}getAnonymousId(){return this.identity.getAnonymousId()}getVisitorId(){return this.identity.getAnonymousId()}getStripeMetadata(){const t=this.identity.getAnonymousId();return{client_reference_id:t,metadata:{visitor_id:t}}}getWhopCheckoutMetadata(){return{visitor_id:this.identity.getAnonymousId()}}getUserId(){return this.identity.getUserId()}getDistinctId(){return this.identity.getDistinctId()}getSessionId(){return this.session.getSessionId()}startNewSession(){const t=this.session.createNewSession();return this.identity.setSessionId(t),t}getSessionData(){return this.session.getSessionData()}getAttribution(){return this.attribution.captureAttribution()}getJourney(){return this.attribution.getJourney()}setAttribution(t){const e=this.attribution.captureAttribution(),i=Object.assign(Object.assign({},e),t);this.attribution.storeLastTouch(i),this.attribution.storeFirstTouch(i),this.session.storeAttribution(i)}optOut(){var t;this.initialized?(this.optedOut=!0,this.cookies.set("__dl_opt_out","true",this.config.cookieExpires),this.queue.setEnabled(!1),this.queue.clear(),this.queue.clearOffline(),null===(t=this.autoIdentify)||void 0===t||t.destroy(),this.autoIdentify=void 0,this.container&&(this.container.cleanupAllIframes(),this.container=void 0),this.userProperties={},r.remove("dl_user_traits"),r.remove("dl_auto_identified_email"),r.remove("dl_journey"),this.log("User opted out")):console.warn("[Datalyr] SDK not initialized. Call init() first.")}optIn(){this.initialized?(this.optedOut=!1,this.cookies.set("__dl_opt_out","false",this.config.cookieExpires),this.queue.setEnabled(this.shouldTrack()),this.log("User opted in")):console.warn("[Datalyr] SDK not initialized. Call init() first.")}isOptedOut(){return this.optedOut}setConsent(t){this.consent=t,r.set("dl_consent",t);const e=this.shouldTrack();this.queue.setEnabled(e),e||(this.queue.clear(),this.queue.clearOffline()),!this.consentAllowsMarketing()&&this.container&&(this.container.cleanupAllIframes(),this.container=void 0),this.log("Consent updated:",t)}flush(){return t(this,void 0,void 0,function*(){yield this.queue.flush()})}setSuperProperties(t){this.superProperties=Object.assign(Object.assign({},this.superProperties),t),this.log("Super properties set:",t)}unsetSuperProperty(t){delete this.superProperties[t],this.log("Super property unset:",t)}getSuperProperties(){return Object.assign({},this.superProperties)}syncShopifyCartAttributes(){return t(this,void 0,void 0,function*(){if("undefined"==typeof window||"undefined"==typeof document)return;if(!!!(window.Shopify||document.querySelector('meta[name="shopify-checkout-api-token"]')||window.location.hostname.includes(".myshopify.com")))return;const t=this.attribution.getAttributionData(),e="fbclid"===t.clickIdType?t.clickId:null,i={},s=this.identity.getAnonymousId(),n=this.cookies.get("_fbc")||t._fbc,o=this.cookies.get("_fbp")||t._fbp,r=this.cookies.get("_dl_fbclid_at");if(s&&(i._datalyr_visitor_id=s),n&&(i._datalyr_fbc=String(n)),o&&(i._datalyr_fbp=String(o)),e&&(i._datalyr_fbclid=String(e)),r&&(i._datalyr_fbclid_at=String(r)),0!==Object.keys(i).length)try{yield fetch("/cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},credentials:"same-origin",body:JSON.stringify({attributes:i})}),this.log("Shopify cart attributes stamped:",Object.keys(i))}catch(t){this.log("Shopify /cart/update.js failed:",t)}})}restoreFromURL(){var t;if("undefined"!=typeof window&&"undefined"!=typeof document)try{const e=new URLSearchParams(window.location.search),i=t=>e.get(t),s=i("_dl_vid"),n=i("_dl_fbc"),o=i("_dl_fbp"),r=i("_dl_fbclid"),a=i("_dl_fbclid_at"),c=i("_dl_gclid"),l=i("_dl_gclid_at");s&&this.cookies.set("__dl_visitor_id",s,365);const d=(t,e)=>{e&&(this.cookies.get(t)||this.cookies.set(t,e,365))};d("_fbc",n),d("_fbp",o),d("_dl_fbclid_at",a),d("_dl_gclid_at",l);let h=!1;const u=[["fbclid",r],["gclid",c]];for(const[t,i]of u)i&&!e.get(t)&&(e.set(t,i),h=!0);for(const t of["_dl_vid","_dl_fbc","_dl_fbp","_dl_fbclid","_dl_fbclid_at","_dl_gclid","_dl_gclid_at"])e.has(t)&&(e.delete(t),h=!0);if(h&&"function"==typeof(null===(t=window.history)||void 0===t?void 0:t.replaceState)){const t=e.toString(),i=window.location.pathname+(t?"?"+t:"")+window.location.hash;window.history.replaceState(window.history.state,"",i)}this.log("Checkout Champ bridge restored:",{had_vid:!!s,had_fbc:!!n,had_fbp:!!o,had_fbclid:!!r,had_gclid:!!c})}catch(t){this.log("restoreFromURL failed:",t)}}fireCheckoutChampPurchasePixel(){var t;if("undefined"!=typeof window)try{if(!this.container)return;const e=null===(t=window.sessionStorage)||void 0===t?void 0:t.getItem("orderData");if(!e)return;const i=JSON.parse(e)||{},s=i.orderId;if(!s)return;const n=`__dl_cc_purchase_${s}`;if(window.sessionStorage.getItem(n))return;window.sessionStorage.setItem(n,"1");const o=`checkoutchamp_purchase_${s}`,r=Number(i.totalAmount),a={order_id:String(s),content_type:"product"};Number.isFinite(r)&&(a.value=r);const c=i.currencyCode||i.currency;c&&(a.currency=String(c)),i.productId&&(a.content_ids=[String(i.productId)]),this.container.trackToPixels("purchase",a,o),this.log("Checkout Champ Purchase Pixel co-fired (CAPI dedup):",{eventId:o,value:a.value,currency:a.currency})}catch(t){this.log("fireCheckoutChampPurchasePixel failed:",t)}}syncOutboundLinkParams(t){if("undefined"==typeof window||"undefined"==typeof document)return;const e=t.map(t=>t.toLowerCase()),i=t=>{try{const i=new URL(t,window.location.href).hostname.toLowerCase();return e.some(t=>i===t||i.endsWith("."+t))}catch(t){return!1}},s=()=>{const t=this.attribution.getAttributionData(),e={},i=this.identity.getAnonymousId(),s=this.cookies.get("_fbc")||t._fbc,n=this.cookies.get("_fbp")||t._fbp,o="fbclid"===t.clickIdType?t.clickId:null,r=this.cookies.get("_dl_fbclid_at"),a="gclid"===t.clickIdType?t.clickId:null,c=this.cookies.get("_dl_gclid_at");return i&&(e._dl_vid=i),s&&(e._dl_fbc=String(s)),n&&(e._dl_fbp=String(n)),o&&(e._dl_fbclid=String(o)),r&&(e._dl_fbclid_at=String(r)),a&&(e._dl_gclid=String(a)),c&&(e._dl_gclid_at=String(c)),e},n=t=>{if(t.href&&i(t.href))try{const e=new URL(t.href,window.location.href),i=s();let n=!1;for(const[t,s]of Object.entries(i))e.searchParams.get(t)||(e.searchParams.set(t,s),n=!0);n&&(t.href=e.toString())}catch(t){}},o=()=>{document.querySelectorAll("a[href]").forEach(t=>n(t))},r=t=>{var e,s;const o=null===(e=t.target)||void 0===e?void 0:e.closest("a[href]");if(!o)return;const r=o;if(i(r.href)){n(r);try{null===(s=this.queue)||void 0===s||s.flush()}catch(t){}}};o(),document.addEventListener("click",r,!0);try{let t=null;const e=new MutationObserver(()=>{null==t&&(t=setTimeout(()=>{t=null,o()},150))});e.observe(document.documentElement,{childList:!0,subtree:!0}),this.outboundDisposer=()=>{try{e.disconnect()}catch(t){}null!=t&&(clearTimeout(t),t=null),document.removeEventListener("click",r,!0)},window.addEventListener("pagehide",()=>{var t;return null===(t=this.outboundDisposer)||void 0===t?void 0:t.call(this)},{once:!0})}catch(t){this.log("MutationObserver setup failed (CC link sync):",t)}this.log("Checkout Champ outbound-link sync active for:",e)}createEventPayload(t,e,i){this.identity.setSessionId(this.session.getSessionId());const s=h(e),n=u({},this.superProperties,s),o=this.attribution.getAttributionData();Object.assign(n,o);const r=this.session.getMetrics();if(Object.assign(n,r),this.config.enableFingerprinting){const t=this.fingerprint.collect();Object.assign(n,{device_fingerprint:t})}Object.assign(n,{url:window.location.href,path:window.location.pathname,referrer:document.referrer,title:document.title,screen_width:screen.width,screen_height:screen.height,viewport_width:window.innerWidth,viewport_height:window.innerHeight});const a=this.identity.getIdentityFields(),c=null!=i?i:l(),d=a.distinct_id,g=a.anonymous_id,f=a.visitor_id||g,p=a.session_id;return{workspace_id:this.config.workspaceId,event_id:c,event_name:t,event_data:n,source:"web",timestamp:(new Date).toISOString(),distinct_id:d,anonymous_id:g,visitor_id:f,user_id:a.user_id,canonical_id:a.canonical_id,session_id:p,resolution_method:"browser_sdk",resolution_confidence:1,sdk_version:"1.7.1",sdk_name:"datalyr-web-sdk"}}shouldTrack(){return!this.optedOut&&((!this.consent||!1!==this.consent.analytics)&&((!this.config.respectDoNotTrack||"1"!==navigator.doNotTrack&&"1"!==window.doNotTrack&&"yes"!==navigator.doNotTrack)&&(!this.config.respectGlobalPrivacyControl||!0!==navigator.globalPrivacyControl&&!0!==window.globalPrivacyControl)))}consentAllowsMarketing(){return!this.consent||!1!==this.consent.marketing&&!1!==this.consent.sale}setupSPATracking(){this.originalPushState=history.pushState,this.originalReplaceState=history.replaceState;const t=this;this.lastSpaPath=window.location.pathname+window.location.search+window.location.hash,history.pushState=function(...e){t.originalPushState.apply(history,e),setTimeout(()=>t.handleSpaNavigation(),0)},history.replaceState=function(...e){t.originalReplaceState.apply(history,e),setTimeout(()=>t.handleSpaNavigation(),0)},this.popstateHandler=()=>{setTimeout(()=>t.handleSpaNavigation(),0)},window.addEventListener("popstate",this.popstateHandler),this.hashchangeHandler=()=>{t.handleSpaNavigation()},window.addEventListener("hashchange",this.hashchangeHandler)}handleSpaNavigation(){const t=window.location.pathname+window.location.search+window.location.hash;t!==this.lastSpaPath&&(this.lastSpaPath=t,this.attribution.clearCache(),this.page())}setupUnloadHandler(){this.unloadHandler=()=>{this.queue.forceFlush()},this.visibilityHandler=()=>{"hidden"===document.visibilityState&&this.queue.forceFlush()},window.addEventListener("beforeunload",this.unloadHandler),window.addEventListener("pagehide",this.unloadHandler),window.addEventListener("visibilitychange",this.visibilityHandler)}getPerformanceMetrics(){if(!this.config.enablePerformanceTracking)return null;const t={};try{if(performance&&"function"==typeof performance.getEntriesByType){const e=performance.getEntriesByType("navigation"),i=e&&e[0];i&&(t.pageLoadTime=Math.round(i.loadEventEnd),t.domReadyTime=Math.round(i.domContentLoadedEventEnd),t.firstByteTime=Math.round(i.responseStart),t.dnsTime=Math.round(i.domainLookupEnd-i.domainLookupStart),t.tcpTime=Math.round(i.connectEnd-i.connectStart),t.requestTime=Math.round(i.responseEnd-i.requestStart),t.timeOnPage=Math.round(performance.now()))}}catch(t){this.trackError(t,{context:"performance_metrics"})}return Object.keys(t).length>0?t:null}trackError(t,e){const i={message:t.message||String(t),stack:t.stack,context:e,timestamp:(new Date).toISOString(),url:window.location.href};this.errors.push(i),this.errors.length>this.MAX_ERRORS&&(this.errors=this.errors.slice(-this.MAX_ERRORS)),this.config.debug&&console.error("[Datalyr Error]",i)}getErrors(){return[...this.errors]}getNetworkStatus(){return this.queue.getNetworkStatus()}loadScript(t){this.container&&this.container.triggerCustomScript(t)}getLoadedScripts(){return this.container?this.container.getLoadedScripts():[]}log(...t){this.config.debug&&console.log("[Datalyr]",...t)}destroy(){this.originalPushState&&(history.pushState=this.originalPushState),this.originalReplaceState&&(history.replaceState=this.originalReplaceState),this.popstateHandler&&window.removeEventListener("popstate",this.popstateHandler),this.hashchangeHandler&&window.removeEventListener("hashchange",this.hashchangeHandler),this.unloadHandler&&(window.removeEventListener("beforeunload",this.unloadHandler),window.removeEventListener("pagehide",this.unloadHandler),this.unloadHandler=void 0),this.visibilityHandler&&(window.removeEventListener("visibilitychange",this.visibilityHandler),this.visibilityHandler=void 0),this.outboundDisposer&&(this.outboundDisposer(),this.outboundDisposer=void 0),this.queue&&this.queue.destroy(),this.session&&this.session.destroy(),this.container&&this.container.cleanupAllIframes(),this.autoIdentify&&this.autoIdentify.destroy(),e.destroy(),this.initializationPromise=null,this.container=void 0,this.autoIdentify=void 0,this.lastSpaPath=null,this.superProperties={},this.userProperties={},this.errors=[],this.initialized=!1,this.log("SDK destroyed")}};"undefined"!=typeof window&&(window.datalyr=D);export{D as datalyr,D as default};
|
|
1
|
+
function t(t,e,i,s){return new(i||(i=Promise))(function(n,o){function r(t){try{c(s.next(t))}catch(t){o(t)}}function a(t){try{c(s.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i(function(t){t(e)})).then(r,a)}c((s=s.apply(t,e||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const e=new class{constructor(){this.key=null,this.salt=null}initialize(e,i){return t(this,void 0,void 0,function*(){if(!crypto.subtle){const t=new Error("Web Crypto API not available - encryption cannot be initialized");throw console.error("[Datalyr Encryption]",t.message),t}try{const t=new TextEncoder,s="dl_encryption_salt";let n=localStorage.getItem(s);n?this.salt=this.base64ToArrayBuffer(n):(this.salt=crypto.getRandomValues(new Uint8Array(32)),n=this.arrayBufferToBase64(this.salt),localStorage.setItem(s,n));const o=`datalyr:${e}:${i}`,r=t.encode(o),a=yield crypto.subtle.importKey("raw",r,"PBKDF2",!1,["deriveKey"]);this.key=yield crypto.subtle.deriveKey({name:"PBKDF2",salt:this.salt,iterations:1e5,hash:"SHA-256"},a,{name:"AES-GCM",length:256},!1,["encrypt","decrypt"])}catch(t){throw console.error("[Datalyr Encryption] Failed to initialize:",t),this.key=null,t}})}encrypt(e){return t(this,void 0,void 0,function*(){if(!this.key||!crypto.subtle)throw new Error("Encryption not initialized - cannot encrypt sensitive data");try{const t="string"==typeof e?e:JSON.stringify(e),i=(new TextEncoder).encode(t),s=crypto.getRandomValues(new Uint8Array(12)),n=yield crypto.subtle.encrypt({name:"AES-GCM",iv:s},this.key,i),o=new Uint8Array(s.length+n.byteLength);return o.set(s,0),o.set(new Uint8Array(n),s.length),this.arrayBufferToBase64(o)}catch(t){throw console.error("[Datalyr Encryption] Encryption failed:",t),t}})}decrypt(e){return t(this,void 0,void 0,function*(){if(!this.key||!crypto.subtle){console.warn("[Datalyr Encryption] Decryption not available - reading potentially unencrypted data");try{return JSON.parse(e)}catch(t){return e}}try{const t=this.base64ToArrayBuffer(e),i=t.slice(0,12),s=t.slice(12),n=yield crypto.subtle.decrypt({name:"AES-GCM",iv:i},this.key,s),o=(new TextDecoder).decode(n);try{return JSON.parse(o)}catch(t){return o}}catch(t){console.warn("[Datalyr Encryption] Decryption failed - attempting to read as unencrypted data (migration mode)");try{return JSON.parse(e)}catch(t){return e}}})}isAvailable(){return null!==this.key&&void 0!==crypto.subtle}arrayBufferToBase64(t){let e="";const i=new Uint8Array(t),s=i.byteLength;for(let t=0;t<s;t++)e+=String.fromCharCode(i[t]);return btoa(e)}base64ToArrayBuffer(t){const e=atob(t),i=e.length,s=new Uint8Array(i);for(let t=0;t<i;t++)s[t]=e.charCodeAt(t);return s}destroy(){this.key=null,this.salt=null}};class i{constructor(t){this.memory=new Map,this.prefix="dl_";try{if(!t)throw new Error("no storage");const e="dl_test__"+Math.random();t.setItem(e,"1"),t.removeItem(e),this.storage=t}catch(t){this.storage=null,"undefined"!=typeof window&&console.warn("[Datalyr] Storage not available, using memory fallback")}}get(t,e=null){const i=this.prefix+t;try{if(this.storage){const t=this.storage.getItem(i);if(null===t)return e;try{return JSON.parse(t)}catch(e){return t}}else{const t=this.memory.get(i);if(void 0===t)return e;try{return JSON.parse(t)}catch(e){return t}}}catch(t){return e}}getString(t,e=null){const i=this.prefix+t;try{const t=this.storage?this.storage.getItem(i):this.memory.has(i)?this.memory.get(i):null;if(null==t)return e;if(t.length>=2&&'"'===t[0]&&'"'===t[t.length-1])try{const e=JSON.parse(t);if("string"==typeof e)return e}catch(t){}return t}catch(t){return e}}set(t,e){const i=this.prefix+t,s="string"==typeof e?e:JSON.stringify(e);try{return this.storage?(this.storage.setItem(i,s),!0):(this.memory.set(i,s),!0)}catch(e){return console.warn("[Datalyr] Failed to store:",t,e),this.memory.set(i,s),!1}}remove(t){const e=this.prefix+t;try{return this.storage?(this.storage.removeItem(e),!0):(this.memory.delete(e),!0)}catch(t){return!1}}keys(){try{if(this.storage){const t=[];for(let e=0;e<this.storage.length;e++){const i=this.storage.key(e);i&&i.startsWith(this.prefix)&&t.push(i.slice(this.prefix.length))}return t}return Array.from(this.memory.keys()).filter(t=>t.startsWith(this.prefix)).map(t=>t.slice(this.prefix.length))}catch(t){return[]}}migrateFromLegacyPrefix(){if(!this.storage)return 0;let t=0;try{const e=[];for(let t=0;t<this.storage.length;t++){const i=this.storage.key(t);i&&i.startsWith("__dl_dl_")&&e.push(i)}e.forEach(e=>{try{const i=this.storage.getItem(e);if(i){const s=e.slice(5);this.storage.getItem(s)||(this.storage.setItem(s,i),t++),this.storage.removeItem(e)}}catch(t){console.warn(`[Datalyr Storage] Failed to migrate legacy key: ${e}`,t)}}),t>0&&console.log(`[Datalyr Storage] Migrated ${t} keys from legacy prefix`)}catch(t){console.warn("[Datalyr Storage] Error during legacy key migration:",t)}return t}getEncrypted(i){return t(this,arguments,void 0,function*(t,i=null){const s=this.prefix+t;try{let t=null;if(t=this.storage?this.storage.getItem(s):this.memory.get(s)||null,!t)return i;return yield e.decrypt(t)}catch(e){return console.warn("[Datalyr Storage] Failed to decrypt:",t,e),i}})}setEncrypted(i,s){return t(this,void 0,void 0,function*(){const t=this.prefix+i,n=yield e.encrypt(s);return this.storage?(this.storage.setItem(t,n),!0):(this.memory.set(t,n),!0)})}}class s{constructor(t={}){this.domain=t.domain||"auto",this.maxAge=t.maxAge||365,this.sameSite=t.sameSite||"Lax",this.secure=t.secure||"auto"}get(t){const e="undefined"!=typeof document?document.cookie:"";if(!e)return null;for(const i of e.split(";")){const e=i.trim(),s=e.indexOf("=");if(-1===s)continue;if(e.slice(0,s)!==t)continue;const n=e.slice(s+1);if(!n)return null;try{return decodeURIComponent(n)}catch(t){return n}}return null}set(t,e,i){try{const s=86400*(i||this.maxAge),n="auto"===this.secure?"https:"===location.protocol:this.secure;let o="";"auto"===this.domain?o=this.getAutoDomain():this.domain&&(o=`;domain=${this.domain}`);const r=[`${t}=${encodeURIComponent(e)}`,`max-age=${s}`,"path=/",`SameSite=${this.sameSite}`,n?"Secure":"",o].filter(Boolean).join(";");return document.cookie=r,!0}catch(e){return console.warn("[Datalyr] Failed to set cookie:",t,e),!1}}remove(t){try{const e=["",location.hostname],i=location.hostname.split(".");return i.length>2&&(e.push(`.${i.slice(-2).join(".")}`),e.push(`.${location.hostname}`)),e.forEach(e=>{const i=e?`;domain=${e}`:"";document.cookie=`${t}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/${i}`}),!0}catch(t){return!1}}getAutoDomain(){const t=location.hostname;if("localhost"===t||/^[\d.]+$/.test(t)||/^\[[\d:]+\]$/.test(t))return"";const e=t.split(".");for(let t=e.length-2;t>=0;t--){const i="."+e.slice(t).join("."),s="__dl_test_"+Math.random();if(document.cookie=`${s}=1;domain=${i};path=/`,-1!==document.cookie.indexOf(s))return document.cookie=`${s}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain=${i};path=/`,`;domain=${i}`}return""}}const n="undefined"!=typeof window?window.localStorage:void 0,o="undefined"!=typeof window?window.sessionStorage:void 0,r=new i(n);new i(o);const a=new s;function c(e){return t(this,void 0,void 0,function*(){if("undefined"==typeof crypto||!crypto.subtle)return null;if(!e)return null;try{const t=(new TextEncoder).encode(e.toLowerCase().trim()),i=yield crypto.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(i)).map(t=>t.toString(16).padStart(2,"0")).join("")}catch(t){return null}})}function l(){return"undefined"!=typeof crypto&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){const e=16*Math.random()|0;return("x"===t?e:3&e|8).toString(16)})}function d(t=window.location.search){const e={};try{if("URLSearchParams"in window){return new URLSearchParams(t).forEach((t,i)=>{e[i]=t}),e}}catch(t){}const i=(t||"").replace(/^\?/,"").split("&");for(const t of i){const[i,s=""]=t.split("=");try{const t=decodeURIComponent((i||"").replace(/\+/g," ")),n=decodeURIComponent((s||"").replace(/\+/g," "));t&&(e[t]=n)}catch(t){}}return e}const h=new Set(["pass","password","passwd","pwd","secret","token","auth","authorization","bearer","signature","cvv","cvc","ssn"]),u=["apikey","accesskey","secretkey","privatekey","publickey","accesstoken","refreshtoken","idtoken","clientsecret","creditcard","cardnumber"];function f(t){if(t.replace(/([a-z0-9])([A-Z])/g,"$1 $2").split(/[\s._\-]+/).map(t=>t.toLowerCase()).filter(Boolean).some(t=>h.has(t)))return!0;const e=t.toLowerCase().replace(/[\s._\-]+/g,"");return u.some(t=>e.includes(t))}function g(t,e=5,i=0){if(i>=e)return"[Max depth reached]";if(null==t)return t;if("undefined"!=typeof Element&&t instanceof Element||"undefined"!=typeof Document&&t instanceof Document||"function"==typeof t)return"[Removed]";if(Array.isArray(t))return t.map(t=>g(t,e,i+1));if("object"==typeof t){const s={};for(const n in t)if(Object.prototype.hasOwnProperty.call(t,n)){if(f(n))continue;s[n]=g(t[n],e,i+1)}return s}return"string"==typeof t?t.length>1e3?t.slice(0,1e3)+"...[truncated]":/^eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(t)?"[Redacted]":t:t}function p(t,...e){if(!e.length)return t;const i=e.shift();if(m(t)&&m(i))for(const e in i)m(i[e])?(t[e]||Object.assign(t,{[e]:{}}),p(t[e],i[e])):Object.assign(t,{[e]:i[e]});return p(t,...e)}function m(t){return t&&"object"==typeof t&&!Array.isArray(t)}const y=new Set(["co.uk","org.uk","net.uk","ac.uk","gov.uk","me.uk","com.au","net.au","org.au","edu.au","gov.au","co.nz","net.nz","org.nz","govt.nz","co.jp","ne.jp","or.jp","ac.jp","go.jp","co.in","net.in","org.in","gov.in","ac.in","co.za","net.za","org.za","gov.za","com.br","net.br","org.br","gov.br","edu.br","co.kr","ne.kr","or.kr","go.kr","ac.kr","com.cn","net.cn","org.cn","gov.cn","edu.cn","co.id","co.th","com.sg","com.my","com.ph","com.vn","com.tw","com.hk","com.mx","com.ar","com.co","com.pe","com.cl","co.il","co.at","co.hu","co.pl"]);function _(t){if(!t)return t;const e=t.toLowerCase();if("localhost"===e||/^[0-9]{1,3}(\.[0-9]{1,3}){3}$/.test(e)||/^\[?[0-9a-fA-F:]+\]?$/.test(e))return t;const i=e.split(".");if(i.length<2)return t;const s=i.slice(-2).join(".");return y.has(s)&&i.length>=3?i.slice(-3).join("."):i.slice(-2).join(".")}function v(t){const e={google:["google.com","google."],facebook:["facebook.com","fb.com"],twitter:["twitter.com","t.co","x.com"],linkedin:["linkedin.com","lnkd.in"],instagram:["instagram.com"],youtube:["youtube.com","youtu.be"],tiktok:["tiktok.com"],reddit:["reddit.com"],pinterest:["pinterest.com"],bing:["bing.com"],yahoo:["yahoo.com"],duckduckgo:["duckduckgo.com"],baidu:["baidu.com"]};for(const[i,s]of Object.entries(e))if(s.some(e=>t.includes(e)))return i;return"other"}class w{constructor(t={}){this.userId=null,this.sessionId=null,this.persistNewId=!1!==t.persistNewId,this.anonymousId=this.getOrCreateAnonymousId(),this.userId=this.getStoredUserId()}getOrCreateAnonymousId(){let t=a.get("__dl_visitor_id");if(t)return r.set("dl_anonymous_id",t),t;if(t=r.getString("dl_anonymous_id"),t)return this.setRootDomainCookie("__dl_visitor_id",t),t;try{const t=new URLSearchParams(window.location.search).get("_dl_vid");if(t&&this.isValidAnonymousId(t))return this.stripUrlParam("_dl_vid"),this.persistAnonymousId(t),t}catch(t){console.warn("[Datalyr] Failed to parse URL for _dl_vid:",t)}return t=`anon_${l()}`,this.persistAnonymousId(t),t}persistAnonymousId(t){this.persistNewId&&(this.setRootDomainCookie("__dl_visitor_id",t),r.set("dl_anonymous_id",t))}isValidAnonymousId(t){return/^anon_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(t)}stripUrlParam(t){var e;try{if("undefined"==typeof window||"function"!=typeof(null===(e=window.history)||void 0===e?void 0:e.replaceState))return;const i=new URL(window.location.href);if(!i.searchParams.has(t))return;i.searchParams.delete(t);const s=i.searchParams.toString(),n=i.pathname+(s?"?"+s:"")+i.hash;window.history.replaceState(window.history.state,"",n)}catch(t){}}setRootDomainCookie(t,e){try{const i=function(){const t=window.location.hostname;return"localhost"===t||t.match(/^[0-9]{1,3}\./)||t.match(/^\[?[0-9a-fA-F:]+\]?$/)||t.split(".").length<2?t:"."+_(t)}(),s="https:"===location.protocol?"; Secure":"",n=encodeURIComponent(e);document.cookie=`${t}=${n}; domain=${i}; path=/; max-age=31536000; SameSite=Lax${s}`;a.get(t)!==e&&(document.cookie=`${t}=${n}; path=/; max-age=31536000; SameSite=Lax${s}`)}catch(i){console.error("[Datalyr] Error setting root domain cookie:",i);try{const i="https:"===location.protocol?"; Secure":"",s=encodeURIComponent(e);document.cookie=`${t}=${s}; path=/; max-age=31536000; SameSite=Lax${i}`}catch(t){console.error("[Datalyr] Failed to set cookie even without domain:",t)}}}getStoredUserId(){return r.getString("dl_user_id")}getAnonymousId(){return this.anonymousId}getUserId(){return this.userId}getDistinctId(){return this.userId||this.anonymousId}getCanonicalId(){return this.getDistinctId()}setSessionId(t){this.sessionId=t}getSessionId(){return this.sessionId}identify(t,e={}){if(!t)return console.warn("[Datalyr] identify() called without userId"),{};const i=this.userId;return this.userId=t,r.set("dl_user_id",t),{anonymous_id:this.anonymousId,user_id:t,previous_id:i,traits:e,identified_at:(new Date).toISOString(),resolution_method:"identify_call"}}alias(t,e){const i={userId:t,previousId:e||this.anonymousId,aliased_at:(new Date).toISOString()};return e&&e!==this.anonymousId||(this.userId=t,r.set("dl_user_id",t)),i}reset(){this.userId=null,r.remove("dl_user_id"),r.remove("dl_user_traits"),this.anonymousId=`anon_${l()}`,r.set("dl_anonymous_id",this.anonymousId),this.setRootDomainCookie("__dl_visitor_id",this.anonymousId)}getIdentityFields(){return{distinct_id:this.getDistinctId(),anonymous_id:this.anonymousId,user_id:this.userId,visitor_id:this.anonymousId,visitorId:this.anonymousId,canonical_id:this.getCanonicalId(),session_id:this.sessionId,sessionId:this.sessionId,resolution_method:"browser_sdk",resolution_confidence:1}}}class b{constructor(t=36e5){this.sessionId=null,this.sessionData=null,this.lastActivity=Date.now(),this.SESSION_KEY="dl_session_data",this.activityCheckInterval=null,this.activityListeners=[],this.sessionCreationLock=!1,this.sessionTimeout=t,this.initSession(),this.setupActivityMonitor()}initSession(){const t=r.get(this.SESSION_KEY),e=Date.now();t&&this.isSessionValid(t,e)?(this.sessionData=t,this.sessionId=t.id,this.lastActivity=e):this.createNewSession()}isSessionValid(t,e){return e-t.lastActivity<this.sessionTimeout&&t.isActive}createNewSession(){if(this.sessionCreationLock)return console.log("[Datalyr Session] Session creation already in progress, returning existing ID"),this.sessionId||"";this.sessionCreationLock=!0;try{const t=Date.now();return this.sessionId=`sess_${l()}`,this.sessionData={id:this.sessionId,startTime:t,lastActivity:t,pageViews:0,events:0,duration:0,isActive:!0},this.saveSession(),this.incrementSessionCount(),this.sessionId}finally{this.sessionCreationLock=!1}}rotateSessionId(){const t=Date.now(),e=this.sessionId;return this.sessionId=`sess_${l()}`,this.sessionData?(this.sessionData.id=this.sessionId,this.sessionData.lastActivity=t,this.saveSession(),e&&r.remove(`dl_session_${e}_attribution`),console.log(`[Datalyr Session] Rotated session ID from ${e} to ${this.sessionId}`)):this.createNewSession(),this.sessionId}getSessionId(){return this.sessionId&&this.isSessionActive()||this.createNewSession(),this.sessionId}getSessionData(){return this.sessionData}updateActivity(t){const e=Date.now();this.sessionData&&this.isSessionValid(this.sessionData,e)?(this.lastActivity=e,this.sessionData.lastActivity=e,this.sessionData.duration=e-this.sessionData.startTime,"pageview"!==t&&"page_view"!==t||this.sessionData.pageViews++,this.sessionData.events++,this.saveSession()):this.createNewSession()}isSessionActive(){if(!this.sessionData)return!1;const t=Date.now();return this.isSessionValid(this.sessionData,t)}endSession(){this.sessionData&&(this.sessionData.isActive=!1,this.saveSession()),this.sessionId=null,this.sessionData=null,this.destroy()}saveSession(){this.sessionData&&r.set(this.SESSION_KEY,this.sessionData)}getTimeout(){return this.sessionTimeout}setTimeout(t){this.sessionTimeout=t}storeAttribution(t){r.set("dl_current_session_attribution",Object.assign(Object.assign({},t),{sessionId:this.sessionId,timestamp:Date.now()}))}getAttribution(){if(!this.sessionId)return null;if(!this.isSessionActive())return null;const t=r.get("dl_current_session_attribution");return t&&t.sessionId===this.sessionId?t:null}getMetrics(){return this.sessionData?{session_id:this.sessionId,session_duration:this.sessionData.duration,session_page_views:this.sessionData.pageViews,session_events:this.sessionData.events,session_start:this.sessionData.startTime,time_since_session_start:Date.now()-this.sessionData.startTime}:{}}setupActivityMonitor(){const t=()=>{const t=Date.now();this.sessionData&&t-this.lastActivity>1e3&&this.updateActivity()};["mousedown","keydown","scroll","touchstart"].forEach(e=>{window.addEventListener(e,t,{passive:!0,capture:!0}),this.activityListeners.push({event:e,handler:t})}),this.activityCheckInterval=setInterval(()=>{this.sessionData&&!this.isSessionActive()&&this.createNewSession()},6e4)}destroy(){this.activityListeners.forEach(({event:t,handler:e})=>{window.removeEventListener(t,e)}),this.activityListeners=[],this.activityCheckInterval&&(clearInterval(this.activityCheckInterval),this.activityCheckInterval=null)}getSessionNumber(){return r.get("dl_session_count",0)+1}incrementSessionCount(){const t=r.get("dl_session_count",0);r.set("dl_session_count",t+1)}}class S{constructor(t={}){this.queryParamsCache=null,this.UTM_PARAMS=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],this.CLICK_IDS=["fbclid","gclid","gbraid","wbraid","ttclid","msclkid","twclid","li_fat_id","sclid","dclid","epik","rdt_cid","obclid","irclid","ko_click_id"],this.CLICK_ID_ALIASES={ScCid:"sclid",sccid:"sclid"},this.DEFAULT_TRACKED_PARAMS=["lyr","ref","source","campaign","medium","gad_source"],this.attributionWindow=t.attributionWindow||7776e6,this.trackedParams=[...this.DEFAULT_TRACKED_PARAMS,...t.trackedParams||[]]}clearCache(){this.queryParamsCache=null}captureAttribution(){const t=this.queryParamsCache||d();this.queryParamsCache||(this.queryParamsCache=t);const e={timestamp:Date.now()};for(const i of this.UTM_PARAMS){const s=t[i];if(s){e[i]=s;e[i.replace("utm_","")]=s}}for(const i of this.CLICK_IDS){const s=t[i];s&&(e.clickId||(e.clickId=s,e.clickIdType=i),e[i]=s)}for(const[i,s]of Object.entries(this.CLICK_ID_ALIASES)){const n=t[i];n&&!e[s]&&(e.clickId||(e.clickId=n,e.clickIdType=s),e[s]=n)}for(const i of this.trackedParams){const s=t[i];s&&(e[i]=s)}return document.referrer&&(e.referrer=document.referrer,e.referrerHost=this.extractHostname(document.referrer)),e.landingPage=window.location.href,e.landingPath=window.location.pathname,e.source||(e.source=this.determineSource(e)),e.medium||(e.medium=this.determineMedium(e)),e}storeFirstTouch(t){const e=r.get("dl_first_touch");let i=!1;e?e.expires_at&&Date.now()>=e.expires_at&&(i=!0):i=!0,i&&r.set("dl_first_touch",Object.assign(Object.assign({},t),{captured_at:Date.now(),expires_at:Date.now()+this.attributionWindow}))}getFirstTouch(){const t=r.get("dl_first_touch");return t&&t.expires_at&&Date.now()>=t.expires_at?(r.remove("dl_first_touch"),null):t}storeLastTouch(t){r.set("dl_last_touch",Object.assign(Object.assign({},t),{captured_at:Date.now(),expires_at:Date.now()+this.attributionWindow}))}getLastTouch(){const t=r.get("dl_last_touch");return t&&t.expires_at&&Date.now()>=t.expires_at?(r.remove("dl_last_touch"),null):t}addTouchpoint(t,e){const i=this.getJourney(),s={timestamp:Date.now(),sessionId:t,source:e.source||void 0,medium:e.medium||void 0,campaign:e.campaign||void 0};i.push(s),i.length>30&&i.shift(),r.set("dl_journey",i)}getJourney(){return r.get("dl_journey",[])}captureAdCookies(){var t,e;const i={};if(i._fbp=a.get("_fbp"),i._fbc=a.get("_fbc"),i._gcl_aw=a.get("_gcl_aw"),i._gcl_dc=a.get("_gcl_dc"),i._gcl_gb=a.get("_gcl_gb"),i._gcl_ha=a.get("_gcl_ha"),i._gac=a.get("_gac"),i._ga=a.get("_ga"),i._gid=a.get("_gid"),i._ttp=a.get("_ttp"),i._ttc=a.get("_ttc"),i._scid=a.get("_scid"),!i._fbp&&(this.hasClickId("fbclid")||i._fbc)){const t=Date.now(),e=Math.floor(1e10*Math.random()).toString();i._fbp=`fb.1.${t}.${e}`,a.set("_fbp",i._fbp,90)}const s=this.getCurrentFbclid();if(s){const t=i._fbc,e=this.extractFbclidFromFbc(t);if(t){if(e&&e!==s){const t=Date.now();i._fbc=`fb.1.${t}.${s}`,a.set("_fbc",i._fbc,90),a.set("_dl_fbclid_at",String(t),90)}}else{const t=a.get("_dl_fbclid_at"),e=t?Number(t):NaN,n=Number.isFinite(e)&&e>0?e:Date.now();t||a.set("_dl_fbclid_at",String(n),90),i._fbc=`fb.1.${n}.${s}`,a.set("_fbc",i._fbc,90)}}return(this.hasClickId("gclid")&&null!==(e=null===(t=this.queryParamsCache)||void 0===t?void 0:t.gclid)&&void 0!==e?e:null)&&!a.get("_dl_gclid_at")&&a.set("_dl_gclid_at",String(Date.now()),90),Object.fromEntries(Object.entries(i).filter(([t,e])=>null!==e))}hasClickId(t){return!!(this.queryParamsCache||d())[t]}getCurrentFbclid(){return(this.queryParamsCache||d()).fbclid||null}extractFbclidFromFbc(t){if(!t)return null;const e=t.split(".");return e.length<4||"fb"!==e[0]?null:e.slice(3).join(".")||null}getAttributionData(){const t=this.getFirstTouch(),e=this.getLastTouch(),i=this.getJourney();let s=this.captureAttribution();const n=!!(s.clickId||s.campaign||s.source&&"direct"!==s.source);if(!n){const i=e||t;i&&(s=Object.assign(Object.assign({},i),{referrer:s.referrer,referrerHost:s.referrerHost,landingPage:s.landingPage,landingPath:s.landingPath}))}const o=this.captureAdCookies();return!t&&n&&this.storeFirstTouch(s),n&&this.storeLastTouch(s),Object.assign(Object.assign(Object.assign({},s),o),{first_touch_source:null==t?void 0:t.source,first_touch_medium:null==t?void 0:t.medium,first_touch_campaign:null==t?void 0:t.campaign,first_touch_timestamp:null==t?void 0:t.timestamp,firstTouchSource:null==t?void 0:t.source,firstTouchMedium:null==t?void 0:t.medium,firstTouchCampaign:null==t?void 0:t.campaign,last_touch_source:null==e?void 0:e.source,last_touch_medium:null==e?void 0:e.medium,last_touch_campaign:null==e?void 0:e.campaign,last_touch_timestamp:null==e?void 0:e.timestamp,lastTouchSource:null==e?void 0:e.source,lastTouchMedium:null==e?void 0:e.medium,lastTouchCampaign:null==e?void 0:e.campaign,touchpoint_count:i.length,touchpointCount:i.length,days_since_first_touch:(null==t?void 0:t.timestamp)?Math.floor((Date.now()-t.timestamp)/864e5):0,daysSinceFirstTouch:(null==t?void 0:t.timestamp)?Math.floor((Date.now()-t.timestamp)/864e5):0})}determineSource(t){if(t.clickIdType){return{fbclid:"facebook",gclid:"google",ttclid:"tiktok",msclkid:"bing",twclid:"twitter",li_fat_id:"linkedin",sclid:"snapchat",dclid:"doubleclick",epik:"pinterest"}[t.clickIdType]||"paid"}if(t.referrerHost){const e=t.referrerHost.toLowerCase(),i=("undefined"!=typeof window?window.location.hostname:"").toLowerCase();return i&&(e===i||this.isSameRootDomain(e,i))?"direct":e.includes("facebook.com")||e.includes("fb.com")?"facebook":e.includes("twitter.com")||e.includes("t.co")||e.includes("x.com")?"twitter":e.includes("linkedin.com")||e.includes("lnkd.in")?"linkedin":e.includes("instagram.com")?"instagram":e.includes("youtube.com")||e.includes("youtu.be")?"youtube":e.includes("tiktok.com")?"tiktok":e.includes("reddit.com")?"reddit":e.includes("pinterest.com")?"pinterest":e.includes("google.")?"google":e.includes("bing.com")?"bing":e.includes("yahoo.com")?"yahoo":e.includes("duckduckgo.com")?"duckduckgo":e.includes("baidu.com")?"baidu":"referral"}return"direct"}determineMedium(t){if(t.clickId)return"cpc";const e=t.source;if(!e||"direct"===e)return"none";if(["facebook","twitter","linkedin","instagram","youtube","tiktok","reddit","pinterest"].includes(e))return"social";return["google","bing","yahoo","duckduckgo","baidu"].includes(e)?"organic":"referral"}isSameRootDomain(t,e){return _(t)===_(e)}extractHostname(t){try{return new URL(t).hostname}catch(t){return""}}isAttributionExpired(t){return!t.timestamp||Date.now()-t.timestamp>this.attributionWindow}clearExpiredAttribution(){const t=this.getFirstTouch(),e=this.getLastTouch();t&&this.isAttributionExpired(t)&&r.remove("dl_first_touch"),e&&this.isAttributionExpired(e)&&r.remove("dl_last_touch")}}const k=["purchase","signup","subscribe","lead","conversion"],I=["add_to_cart","begin_checkout","view_item","search"];class E extends Error{}class D extends Error{constructor(t,e){super(e||`HTTP ${t}`),this.status=t}}class C{constructor(t){this.queue=[],this.offlineQueue=[],this.batchTimer=null,this.periodicFlushInterval=null,this.flushPromise=null,this.recentEventIds=new Set,this.MAX_RECENT_EVENT_IDS=1e3,this.OFFLINE_QUEUE_KEY="dl_offline_queue",this.flushLock=!1,this.offlineQueueLock=!1,this.offlineProcessing=!1,this.inFlight=[],this.enabled=!0,this.rateLimitedUntil=0;const e=(t,e,i,s)=>{const n=Number(t);return Number.isFinite(n)?Math.min(Math.max(Math.floor(n),i),s):e};this.config={batchSize:e(t.batchSize,10,1,1e3),flushInterval:e(t.flushInterval,5e3,250,36e5),maxRetries:e(t.maxRetries,5,0,20),retryDelay:e(t.retryDelay,1e3,0,6e4),endpoint:t.endpoint||"https://ingest.datalyr.com",fallbackEndpoints:Array.isArray(t.fallbackEndpoints)?t.fallbackEndpoints:[],workspaceId:t.workspaceId,debug:t.debug||!1,criticalEvents:t.criticalEvents||k,highPriorityEvents:t.highPriorityEvents||I,maxOfflineQueueSize:e(t.maxOfflineQueueSize,100,1,1e5)},this.networkStatus={isOnline:!1!==navigator.onLine,lastOfflineAt:null,lastOnlineAt:null},this.loadOfflineQueue(),this.setupNetworkListeners(),this.startPeriodicFlush(),this.networkStatus.isOnline&&this.offlineQueue.length>0&&setTimeout(()=>this.processOfflineQueue(),1e3)}enqueue(t){if(!this.enabled)return;const e=t.event_name;if(this.isDuplicateEvent(t))this.log("Duplicate event suppressed:",e);else{if(this.config.criticalEvents.includes(e))return this.log("Critical event, sending immediately:",e),this.persistCriticalEvent(t),void this.sendBatch([t]).then(()=>{this.removeFromOfflineQueue(t.event_id)}).catch(i=>{if(i instanceof D)return this.log("Critical event permanently rejected, dropping:",e,i),void this.removeFromOfflineQueue(t.event_id);this.log("Critical event send failed, retained in offline queue:",e,i)});this.queue.push(t),this.log("Event queued:",e),this.shouldFlush(e)&&this.flush()}}isDuplicateEvent(t){const e=this.createEventHash(t);if(this.recentEventIds.has(e))return!0;if(this.recentEventIds.add(e),this.recentEventIds.size>this.MAX_RECENT_EVENT_IDS){const t=this.recentEventIds.size-this.MAX_RECENT_EVENT_IDS,e=this.recentEventIds.values();for(let i=0;i<t;i++){const t=e.next();t.done||this.recentEventIds.delete(t.value)}}return!1}createEventHash(t){const e=[t.event_name,t.timestamp,JSON.stringify(t.event_data||{})].join("|");let i=0;for(let t=0;t<e.length;t++){i=(i<<5)-i+e.charCodeAt(t),i&=i}return i.toString(36)}shouldFlush(t){return this.queue.length>=this.config.batchSize||(t&&this.config.highPriorityEvents.includes(t)?(this.batchTimer&&clearTimeout(this.batchTimer),this.batchTimer=setTimeout(()=>this.flush(),1e3),!1):(this.batchTimer||(this.batchTimer=setTimeout(()=>this.flush(),this.config.flushInterval)),!1))}flush(){return t(this,void 0,void 0,function*(){if(this.enabled&&!(Date.now()<this.rateLimitedUntil)){if(this.flushPromise||this.flushLock)return this.flushPromise||Promise.resolve();this.flushLock=!0;try{this.flushPromise=this._flush(),yield this.flushPromise}finally{this.flushPromise=null,this.flushLock=!1}}})}_flush(){return t(this,void 0,void 0,function*(){if(this.batchTimer&&(clearTimeout(this.batchTimer),this.batchTimer=null),0===this.queue.length)return;if(!this.networkStatus.isOnline)return this.log("Network offline, queuing events"),void this.moveToOfflineQueue();const t=Math.min(this.config.batchSize,this.queue.length),e=this.queue.slice(0,t);this.inFlight=e;try{yield this.sendBatch(e),this.queue.splice(0,t),this.log(`Successfully sent and removed ${t} events from queue`)}catch(i){this.log("Failed to send batch:",i),this.queue.splice(0,t),i instanceof D?this.log(`Dropping ${e.length} events — permanent error ${i.status}`):this.moveToOfflineQueue(e)}finally{this.inFlight=[]}})}sendBatch(e){return t(this,arguments,void 0,function*(t,e=0,i=0){const s={events:t,batchId:l(),timestamp:(new Date).toISOString()},n=[this.config.endpoint,...this.config.fallbackEndpoints],o=n[i]||this.config.endpoint,r=JSON.stringify(s),a=new Blob([r]).size<=6e4;try{const e=yield fetch(o,{method:"POST",headers:{"Content-Type":"application/json","X-Batch-Size":t.length.toString()},body:r,keepalive:a});if(!e.ok){if(429===e.status){const t=parseInt(e.headers.get("Retry-After")||"60");throw this.rateLimitedUntil=Date.now()+1e3*Math.max(t,1),this.log(`Rate limited; backing off ${t}s`),new E("Rate limited (429)")}if(e.status>=400&&e.status<500&&408!==e.status)throw new D(e.status,`HTTP ${e.status}: ${e.statusText}`);throw new Error(`HTTP ${e.status}: ${e.statusText}`)}this.log(`Batch sent successfully to ${o}: ${t.length} events`)}catch(s){if(s instanceof E||s instanceof D)throw s;if(i<n.length-1)return this.log(`Failed on ${o}, trying fallback ${i+1}`),this.sendBatch(t,0,i+1);if(e<this.config.maxRetries){const s=function(t,e=1e3){const i=.1*Math.random();return Math.min(e*Math.pow(2,t)*(1+i),3e4)}(e,this.config.retryDelay);return this.log(`Retrying batch in ${s}ms (attempt ${e+1}/${this.config.maxRetries})`),yield new Promise(t=>setTimeout(t,s)),this.sendBatch(t,e+1,i)}throw s}})}setupNetworkListeners(){window.addEventListener("online",()=>{this.networkStatus.isOnline=!0,this.networkStatus.lastOnlineAt=Date.now(),this.log("Network connection restored"),setTimeout(()=>this.processOfflineQueue(),1e3)}),window.addEventListener("offline",()=>{this.networkStatus.isOnline=!1,this.networkStatus.lastOfflineAt=Date.now(),this.log("Network connection lost")})}startPeriodicFlush(){this.periodicFlushInterval=setInterval(()=>{this.queue.length>0&&this.flush(),this.networkStatus.isOnline&&this.offlineQueue.length>0&&this.processOfflineQueue()},this.config.flushInterval)}stopPeriodicFlush(){this.periodicFlushInterval&&(clearInterval(this.periodicFlushInterval),this.periodicFlushInterval=null)}moveToOfflineQueue(t){if(this.enabled)if(this.offlineQueueLock)console.warn("[Datalyr Queue] Offline queue operation already in progress");else{this.offlineQueueLock=!0;try{if(t?this.offlineQueue.push(...t):(this.offlineQueue.push(...this.queue),this.queue=[]),this.offlineQueue.length>this.config.maxOfflineQueueSize){const t=this.offlineQueue.length-this.config.maxOfflineQueueSize;this.offlineQueue.splice(0,t)}this.saveOfflineQueue()}finally{this.offlineQueueLock=!1}}}persistCriticalEvent(t){this.enabled&&(this.offlineQueue.some(e=>e.event_id===t.event_id)||(this.offlineQueue.push(t),this.offlineQueue.length>this.config.maxOfflineQueueSize&&this.offlineQueue.splice(0,this.offlineQueue.length-this.config.maxOfflineQueueSize),this.saveOfflineQueue()))}removeFromOfflineQueue(t){const e=this.offlineQueue.length;this.offlineQueue=this.offlineQueue.filter(e=>e.event_id!==t),this.offlineQueue.length!==e&&(0===this.offlineQueue.length?r.remove(this.OFFLINE_QUEUE_KEY):this.saveOfflineQueue())}loadOfflineQueue(){const t=r.get(this.OFFLINE_QUEUE_KEY,[]);Array.isArray(t)&&(this.offlineQueue=t,this.log(`Loaded ${this.offlineQueue.length} offline events`))}saveOfflineQueue(){if(!this.enabled)return;const t=this.offlineQueue.slice(-this.config.maxOfflineQueueSize);r.set(this.OFFLINE_QUEUE_KEY,t)}processOfflineQueue(){return t(this,void 0,void 0,function*(){if(this.enabled&&!(Date.now()<this.rateLimitedUntil)&&!this.offlineProcessing&&0!==this.offlineQueue.length&&this.networkStatus.isOnline){this.offlineProcessing=!0;try{for(this.log(`Processing ${this.offlineQueue.length} offline events`);this.offlineQueue.length>0&&this.enabled;){const t=this.offlineQueue.splice(0,this.config.batchSize);try{yield this.sendBatch(t),this.saveOfflineQueue()}catch(e){if(e instanceof D){this.log(`Dropping poison offline batch (${t.length}) — permanent error ${e.status}`),this.saveOfflineQueue();continue}this.log("Failed to send offline batch:",e),this.offlineQueue.unshift(...t),this.saveOfflineQueue();break}}0===this.offlineQueue.length&&r.remove(this.OFFLINE_QUEUE_KEY)}finally{this.offlineProcessing=!1}}})}getQueueSize(){return this.queue.length}getOfflineQueueSize(){return this.offlineQueue.length}getNetworkStatus(){return Object.assign({},this.networkStatus)}buildBatch(t){return{events:t,batchId:l(),timestamp:(new Date).toISOString()}}forceFlush(){return t(this,arguments,void 0,function*(t=!1){if(!this.enabled)return;if(!t){if(Date.now()<this.rateLimitedUntil)return;return yield this.flush(),void(yield this.processOfflineQueue())}const e=[...this.queue.slice(this.inFlight.length),...this.offlineQueue];if(this.queue=this.queue.slice(0,this.inFlight.length),this.offlineQueue=[],0===e.length)return;if(this.enabled){const t=e.slice(-this.config.maxOfflineQueueSize);e.length>t.length&&this.log(`Offline cap dropped ${e.length-t.length} oldest events at unload`),r.set(this.OFFLINE_QUEUE_KEY,t)}if(Date.now()<this.rateLimitedUntil)return;if(!navigator.sendBeacon)return;const i=[];let s=[],n=2;for(const t of e){const e=new Blob([JSON.stringify(t)]).size+1;s.length>0&&(s.length>=this.config.batchSize||n+e>6e4)&&(i.push(s),s=[],n=2),s.push(t),n+=e}s.length>0&&i.push(s);let o=0;for(const t of i){const e=new Blob([JSON.stringify(this.buildBatch(t))],{type:"application/json"});if(!navigator.sendBeacon(this.config.endpoint,e))break;o+=t.length}this.log(`forceFlush(terminal): beaconed ${o}/${e.length} events; persisted copy retained for next-load drain`)})}clear(){this.queue=[],this.batchTimer&&(clearTimeout(this.batchTimer),this.batchTimer=null)}setEnabled(t){this.enabled=t}clearOffline(){this.offlineQueue=[],r.remove(this.OFFLINE_QUEUE_KEY)}log(...t){this.config.debug&&console.log("[Datalyr Queue]",...t)}destroy(){this.stopPeriodicFlush(),this.batchTimer&&(clearTimeout(this.batchTimer),this.batchTimer=null),this.queue.length>0&&this.moveToOfflineQueue()}}class T{constructor(t={}){this.privacyMode=t.privacyMode||"standard",this.enableFingerprinting=!1!==t.enableFingerprinting}collect(){return"strict"!==this.privacyMode&&this.enableFingerprinting?this.collectStandard():this.collectMinimal()}collectMinimal(){const t={timezone:this.getTimezone(),language:navigator.language||null,screen_bucket:this.getScreenBucket(),dnt:"1"===navigator.doNotTrack||!0===window.globalPrivacyControl||null,userAgent:navigator.userAgent||null};if("userAgentData"in navigator){const e=navigator.userAgentData;t.userAgentData={brands:e.brands||[],mobile:e.mobile||!1,platform:e.platform||null}}return t}collectStandard(){const t={};try{if(t.timezone=this.getTimezone(),t.language=navigator.language||null,t.screen_bucket=this.getScreenBucket(),t.dnt="1"===navigator.doNotTrack||!0===window.globalPrivacyControl||null,t.userAgent=navigator.userAgent||null,"userAgentData"in navigator){const e=navigator.userAgentData;t.userAgentData={brands:e.brands||[],mobile:e.mobile||!1,platform:e.platform||null}}}catch(t){console.warn("[Datalyr] Error collecting fingerprint:",t)}return t}getTimezone(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone||null}catch(t){return null}}getScreenBucket(){try{if(!window.screen)return null;const t=100*Math.round(screen.width/100);return`${t}x${100*Math.round(screen.height/100)}`}catch(t){return null}}generateHash(e){return t(this,void 0,void 0,function*(){try{const t=Object.keys(e).sort().reduce((t,i)=>(t[i]=e[i],t),{}),i=JSON.stringify(t);if(window.crypto&&window.crypto.subtle){const t=(new TextEncoder).encode(i),e=yield crypto.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(e)).map(t=>t.toString(16).padStart(2,"0")).join("")}let s=0;for(let t=0;t<i.length;t++){s=(s<<5)-s+i.charCodeAt(t),s&=s}return Math.abs(s).toString(16)}catch(t){return""}})}}class A{constructor(t){this.scripts=[],this.loadedScripts=new Set,this.sessionLoadedScripts=new Set,this.pixels=null,this.initialized=!1,this.sandboxedIframes=[],this.iframeCleanupTimeouts=new Map,this.messageHandler=null,this.workspaceId=t.workspaceId,this.endpoint=t.endpoint||"https://ingest.datalyr.com",this.debug=t.debug||!1,this.getIdentity=t.getIdentity;const e=r.get("dl_session_scripts",[]);this.sessionLoadedScripts=new Set(e),this.messageHandler=t=>{if(t.data&&"datalyr_script_complete"===t.data.type){const e=t.data.scriptId;this.log("Received script completion signal:",e);const i=this.sandboxedIframes.find(t=>t.dataset.datalyrScript===e);i&&this.cleanupIframe(i)}},window.addEventListener("message",this.messageHandler)}init(){return t(this,void 0,void 0,function*(){if(!this.initialized)try{const t="undefined"!=typeof AbortController?new AbortController:null,e=t?setTimeout(()=>t.abort(),3e3):null;let i;try{i=yield fetch(`${this.endpoint}/container-scripts`,{method:"POST",headers:{"Content-Type":"application/json","X-Container-Version":"1.0"},body:JSON.stringify({workspaceId:this.workspaceId}),signal:t?t.signal:void 0})}finally{e&&clearTimeout(e)}if(!i.ok)throw new Error(`Failed to fetch container scripts: ${i.status}`);const s=yield i.json();this.scripts=s.scripts||[],this.pixels=s.pixels||null,this.remoteConfig=s.config&&"object"==typeof s.config?s.config:void 0,this.pixels&&(yield this.initializePixels()),this.loadScriptsByTrigger("page_load"),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",()=>{this.loadScriptsByTrigger("dom_ready")}):this.loadScriptsByTrigger("dom_ready"),window.addEventListener("load",()=>{this.loadScriptsByTrigger("window_load")}),this.initialized=!0,this.log("Container manager initialized with",this.scripts.length,"scripts")}catch(t){this.log("Error initializing container:",t)}})}getRemoteConfig(){return this.remoteConfig}loadScriptsByTrigger(t){this.scripts.filter(e=>e.enabled&&e.trigger===t&&this.shouldLoadScript(e)).forEach(t=>this.loadScript(t))}shouldLoadScript(t){return("once_per_page"!==t.frequency||!this.loadedScripts.has(t.id))&&(("once_per_session"!==t.frequency||!this.sessionLoadedScripts.has(t.id))&&(!(t.conditions&&t.conditions.length>0)||this.evaluateConditions(t.conditions)))}evaluateConditions(t){return t.every(t=>{try{const{type:e,operator:i,value:s}=t;switch(e){case"url_path":return this.evaluateStringCondition(window.location.pathname,i,s);case"url_host":return this.evaluateStringCondition(window.location.hostname,i,s);case"url_parameter":const e=new URLSearchParams(window.location.search);return this.evaluateStringCondition(e.get(t.parameter)||"",i,s);case"referrer":return this.evaluateStringCondition(document.referrer,i,s);case"device_type":const n=/Mobile|Android|iPhone|iPad/i.test(navigator.userAgent);return this.evaluateStringCondition(n?"mobile":"desktop",i,s);default:return!0}}catch(t){return!1}})}evaluateStringCondition(t,e,i){switch(e){case"equals":return t===i;case"not_equals":return t!==i;case"contains":return t.includes(i);case"not_contains":return!t.includes(i);case"starts_with":return t.startsWith(i);case"ends_with":return t.endsWith(i);case"matches_regex":try{return new RegExp(i).test(t)}catch(t){return!1}default:return!1}}loadScript(t){try{switch(t.type){case"inline":this.loadInlineScript(t);break;case"external":this.loadExternalScript(t);break;case"pixel":this.loadPixel(t)}this.loadedScripts.add(t.id),"once_per_session"===t.frequency&&(this.sessionLoadedScripts.add(t.id),r.set("dl_session_scripts",Array.from(this.sessionLoadedScripts))),this.log("Loaded script:",t.name)}catch(e){this.log("Error loading script:",t.name,e)}}loadInlineScript(t){const e=document.createElement("iframe");e.style.display="none",e.setAttribute("sandbox","allow-scripts"),e.dataset.datalyrScript=t.id,this.sandboxedIframes.push(e);const i=`\n <!DOCTYPE html>\n <html>\n <head>\n <meta charset="UTF-8">\n </head>\n <body>\n <script>\n // User-provided script runs here in isolation\n try {\n ${t.content}\n } catch (error) {\n console.error('[Datalyr Container] Script execution error:', error);\n }\n\n // FIXED (ISSUE-02): Signal completion for cleanup\n // Scripts have 5 seconds to execute before iframe is removed\n setTimeout(function() {\n try {\n parent.postMessage({ type: 'datalyr_script_complete', scriptId: '${t.id}' }, '*');\n } catch (e) {\n // Ignore postMessage errors from sandbox\n }\n }, 5000);\n <\/script>\n </body>\n </html>\n `;document.body.appendChild(e),e.contentDocument&&(e.contentDocument.open(),e.contentDocument.write(i),e.contentDocument.close());const s=window.setTimeout(()=>{this.cleanupIframe(e)},3e4);this.iframeCleanupTimeouts.set(e,s),this.log("Loaded inline script in sandbox:",t.id)}cleanupIframe(t){try{const e=this.iframeCleanupTimeouts.get(t);e&&(clearTimeout(e),this.iframeCleanupTimeouts.delete(t));const i=this.sandboxedIframes.indexOf(t);i>-1&&this.sandboxedIframes.splice(i,1),t.parentNode&&(t.parentNode.removeChild(t),this.log("Cleaned up sandboxed iframe:",t.dataset.datalyrScript))}catch(t){this.log("Error cleaning up iframe:",t)}}cleanupAllIframes(){const t=[...this.sandboxedIframes];t.forEach(t=>this.cleanupIframe(t)),this.messageHandler&&(window.removeEventListener("message",this.messageHandler),this.messageHandler=null),this.iframeCleanupTimeouts.forEach(t=>clearTimeout(t)),this.iframeCleanupTimeouts.clear(),this.log(`Cleaned up ${t.length} sandboxed iframes`)}loadExternalScript(t){var e;if(!this.isValidScriptUrl(t.content))return void this.log("Blocked invalid script URL:",t.content);if(!(null===(e=t.settings)||void 0===e?void 0:e.integrity))return void console.error(`[Datalyr Container] SECURITY: External script "${t.id}" blocked - missing SRI hash.\nAll external scripts MUST include an integrity hash to prevent CDN compromise attacks.\nGenerate SRI hash at: https://www.srihash.org/\nExample: { integrity: "sha384-..." }`);const i=document.createElement("script");i.src=t.content,i.dataset.datalyrScript=t.id,t.settings?(i.integrity=t.settings.integrity,i.crossOrigin=t.settings.crossorigin||"anonymous",!1!==t.settings.async&&(i.async=!0),t.settings.defer&&(i.defer=!0)):i.async=!0,document.head.appendChild(i)}loadPixel(t){const e=new Image;e.src=t.content,e.style.display="none",e.dataset.datalyrPixel=t.id,document.body.appendChild(e)}initializePixels(){return t(this,void 0,void 0,function*(){var t,e,i;this.pixels&&((null===(t=this.pixels.meta)||void 0===t?void 0:t.enabled)&&this.pixels.meta.pixel_id&&(yield this.initializeMetaPixel(this.pixels.meta)),(null===(e=this.pixels.google)||void 0===e?void 0:e.enabled)&&this.pixels.google.tag_id&&this.initializeGoogleTag(this.pixels.google),(null===(i=this.pixels.tiktok)||void 0===i?void 0:i.enabled)&&this.pixels.tiktok.pixel_id&&this.initializeTikTokPixel(this.pixels.tiktok))})}initializeMetaPixel(e){return t(this,void 0,void 0,function*(){var t,i,s,n,o,r,a;try{i=window,s=document,n="script",i.fbq||(o=i.fbq=function(){o.callMethod?o.callMethod.apply(o,arguments):o.queue.push(arguments)},i._fbq||(i._fbq=o),o.push=o,o.loaded=!0,o.version="2.0",o.queue=[],(r=s.createElement(n)).async=!0,r.src="https://connect.facebook.net/en_US/fbevents.js",(a=s.getElementsByTagName(n)[0]).parentNode.insertBefore(r,a));const l={},d=null===(t=this.getIdentity)||void 0===t?void 0:t.call(this);if(null==d?void 0:d.externalId){const t=yield c(String(d.externalId));t&&(l.external_id=t)}if(null==d?void 0:d.email){const t=yield c(String(d.email).toLowerCase().trim());t&&(l.em=t)}Object.keys(l).length>0?(window.fbq("init",e.pixel_id,l),this.log("Meta Pixel initialized with advanced matching:",e.pixel_id,Object.keys(l))):(window.fbq("init",e.pixel_id),this.log("Meta Pixel initialized (no advanced matching):",e.pixel_id))}catch(t){this.log("Error initializing Meta Pixel:",t)}})}initializeGoogleTag(t){try{const e=document.createElement("script");function i(...t){window.dataLayer.push(t)}e.async=!0,e.src=`https://www.googletagmanager.com/gtag/js?id=${t.tag_id}`,document.head.appendChild(e),window.dataLayer=window.dataLayer||[],window.gtag=i,i("js",new Date),i("config",t.tag_id,{allow_enhanced_conversions:!1!==t.enhanced_conversions}),this.log("Google Tag initialized:",t.tag_id)}catch(s){this.log("Error initializing Google Tag:",s)}}initializeTikTokPixel(t){try{!function(t,e,i){t.TiktokAnalyticsObject=i;var s=t[i]=t[i]||[];s.methods=["page","track","identify","instances","debug","on","off","once","ready","alias","group","enableCookie","disableCookie"],s.setAndDefer=function(t,e){t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}};for(var n=0;n<s.methods.length;n++)s.setAndDefer(s,s.methods[n]);s.instance=function(t){for(var e=s._i[t]||[],i=0;i<s.methods.length;i++)s.setAndDefer(e,s.methods[i]);return e},s.load=function(t,e){var n;s._i=s._i||{},s._i[t]=[],s._o=s._o||{},s._o[t]=e||{};var o=document.createElement("script");o.type="text/javascript",o.async=!0,o.src="https://analytics.tiktok.com/i18n/pixel/events.js?sdkid="+t+"&lib="+i;var r=document.getElementsByTagName("script")[0];null===(n=r.parentNode)||void 0===n||n.insertBefore(o,r)}}(window,document,"ttq"),window.ttq.load(t.pixel_id),window.ttq.page(),this.log("TikTok Pixel initialized:",t.pixel_id)}catch(t){this.log("Error initializing TikTok Pixel:",t)}}hasMetaPixel(){var t,e;return!(!(null===(e=null===(t=this.pixels)||void 0===t?void 0:t.meta)||void 0===e?void 0:e.enabled)||!window.fbq)}trackToPixels(t,e={},i){var s,n,o,r,a,c,l,d,h,u;if(t.startsWith("$"))return;const f=this.sanitizeEventName(t),g=this.sanitizeProperties(e);if((null===(n=null===(s=this.pixels)||void 0===s?void 0:s.meta)||void 0===n?void 0:n.enabled)&&window.fbq)try{const e={page_view:"PageView",pageview:"PageView",view_content:"ViewContent",product_viewed:"ViewContent",view_item:"ViewContent",add_to_cart:"AddToCart",product_added:"AddToCart",add_to_wishlist:"AddToWishlist",initiate_checkout:"InitiateCheckout",begin_checkout:"InitiateCheckout",checkout_started:"InitiateCheckout",add_payment_info:"AddPaymentInfo",purchase:"Purchase",order_completed:"Purchase",order_paid:"Purchase",lead:"Lead",complete_registration:"CompleteRegistration",sign_up:"CompleteRegistration",signup:"CompleteRegistration",search:"Search",subscribe:"Subscribe",subscription_created:"Subscribe",start_trial:"StartTrial",trial_started:"StartTrial",contact:"Contact",schedule:"Schedule"},s=null===(r=null===(o=this.pixels)||void 0===o?void 0:o.meta)||void 0===r?void 0:r.event_mappings,n=(null==s?void 0:s[t])||e[String(t).toLowerCase()]||f;i?window.fbq("track",n,g,{eventID:i}):window.fbq("track",n,g)}catch(t){this.log("Error tracking Meta Pixel event:",t)}if((null===(c=null===(a=this.pixels)||void 0===a?void 0:a.google)||void 0===c?void 0:c.enabled)&&window.gtag)try{window.gtag("event",f,g)}catch(t){this.log("Error tracking Google Tag event:",t)}if((null===(d=null===(l=this.pixels)||void 0===l?void 0:l.tiktok)||void 0===d?void 0:d.enabled)&&window.ttq)try{const e={view_content:"ViewContent",product_viewed:"ViewContent",view_item:"ViewContent",search:"Search",add_to_wishlist:"AddToWishlist",add_to_cart:"AddToCart",product_added:"AddToCart",initiate_checkout:"InitiateCheckout",begin_checkout:"InitiateCheckout",checkout_started:"InitiateCheckout",add_payment_info:"AddPaymentInfo",purchase:"CompletePayment",order_completed:"CompletePayment",order_paid:"CompletePayment",place_an_order:"PlaceAnOrder",contact:"Contact",download:"Download",lead:"SubmitForm",submit_form:"SubmitForm",complete_registration:"CompleteRegistration",sign_up:"CompleteRegistration",signup:"CompleteRegistration",subscribe:"Subscribe",subscription_created:"Subscribe"},i=null===(u=null===(h=this.pixels)||void 0===h?void 0:h.tiktok)||void 0===u?void 0:u.event_mappings,s=(null==i?void 0:i[t])||e[String(t).toLowerCase()]||f;window.ttq.track(s,g)}catch(t){this.log("Error tracking TikTok Pixel event:",t)}}triggerCustomScript(t){const e=this.scripts.find(e=>e.id===t&&"custom"===e.trigger);e&&this.shouldLoadScript(e)&&this.loadScript(e)}getLoadedScripts(){return Array.from(this.loadedScripts)}isValidScriptUrl(t){try{const e=new URL(t);return!("https:"!==e.protocol&&!e.hostname.includes("localhost"))&&!["data:","javascript:","file:"].includes(e.protocol)}catch(t){return!1}}sanitizeEventName(t){return"string"!=typeof t?"unknown_event":t.replace(/[^a-zA-Z0-9_$ ]/g,"").substring(0,100)}sanitizeProperties(t){if(null==t)return{};if("object"!=typeof t)return this.sanitizeValue(t);if(Array.isArray(t))return t.map(t=>this.sanitizeValue(t));const e={};for(const[i,s]of Object.entries(t)){const t=i.replace(/[^a-zA-Z0-9_]/g,"").substring(0,100);t&&(e[t]=this.sanitizeValue(s))}return e}sanitizeValue(t){return null==t?t:"string"==typeof t?t.replace(/<script[^>]*>.*?<\/script>/gi,"").replace(/<[^>]+>/g,"").replace(/javascript:/gi,"").replace(/on\w+\s*=/gi,"").substring(0,1e3):"number"==typeof t||"boolean"==typeof t?t:"object"==typeof t?this.sanitizeProperties(t):String(t).substring(0,1e3)}log(...t){this.debug&&console.log("[Datalyr Container]",...t)}}class x{constructor(t={}){this.formListeners=[],this.lastIdentifyTime=0,this.RATE_LIMIT_MS=5e3,this.destroyed=!1,this.config={enabled:!1!==t.enabled,captureFromForms:!1!==t.captureFromForms,captureFromAPI:!0===t.captureFromAPI,captureFromShopify:!1!==t.captureFromShopify,trustedDomains:t.trustedDomains||[],debug:t.debug||!1}}initialize(e){return t(this,void 0,void 0,function*(){if(!this.config.enabled)return void this.log("Auto-identify disabled");this.identifyCallback=e;const t=yield r.getEncrypted("dl_auto_identified_email");if(t){const e=r.get("dl_auto_identified_email");if("string"==typeof e&&e.includes("@"))try{yield r.setEncrypted("dl_auto_identified_email",t),this.log("Migrated legacy plaintext auto-identified email to encrypted storage")}catch(t){this.log("Failed to migrate legacy auto-identified email:",t)}return void this.log("User already auto-identified")}this.config.captureFromForms&&this.setupFormMonitoring(),this.config.captureFromAPI&&(this.setupFetchInterception(),this.setupXHRInterception()),this.config.captureFromShopify&&this.setupShopifyMonitoring(),this.log("Auto-identify initialized")})}setupFormMonitoring(){this.scanForEmailForms(),this.formObserver=new MutationObserver(()=>{this.scanForEmailForms()}),this.formObserver.observe(document.body,{childList:!0,subtree:!0}),this.log("Form monitoring active")}findVisitorEmailInput(t){const e=Array.from(t.querySelectorAll('input[type="email"], input[name*="email" i], input[id*="email" i], input[autocomplete="email"]')).filter(t=>!this.isThirdPartyEmailField(t));if(0===e.length)return null;if(1===e.length)return e[0];const i=e.filter(t=>"email"===(t.getAttribute("autocomplete")||"").toLowerCase());return 1===i.length?i[0]:(this.log("Multiple email inputs in form; skipping auto-identify (ambiguous)"),null)}isThirdPartyEmailField(t){const e=[t.name,t.id,t.getAttribute("autocomplete"),t.getAttribute("aria-label"),t.placeholder].filter(Boolean).join(" ");return x.THIRD_PARTY_EMAIL_FIELD.test(e)}scanForEmailForms(){if(this.destroyed)return;document.querySelectorAll("form").forEach(t=>{if(this.formListeners.some(e=>e.element===t))return;if(this.findVisitorEmailInput(t)){const e=e=>this.handleFormSubmit(e,t);t.addEventListener("submit",e),this.formListeners.push({element:t,handler:e}),this.log("Monitoring form:",t)}})}handleFormSubmit(t,e){try{const t=this.findVisitorEmailInput(e);if(!t)return;const i=t.value.trim();this.isValidEmail(i)&&(this.log("Email captured from form:",i),this.triggerIdentify(i,"form"))}catch(t){this.log("Error handling form submit:",t)}}setupFetchInterception(){if("function"!=typeof window.fetch)return;this.originalFetch=window.fetch;const e=this;window.fetch=function(i,s){return t(this,void 0,void 0,function*(){const n="string"==typeof i?i:i instanceof URL?i.href:i.url;if(!e.isTrustedDomain(n))return e.originalFetch.call(window,i,s);try{(null==s?void 0:s.body)&&e.extractEmailFromData(s.body,"api-request");const n=e.originalFetch.call(window,i,s);return n.then(i=>t(this,void 0,void 0,function*(){var t;if(i.ok&&(null===(t=i.headers.get("content-type"))||void 0===t?void 0:t.includes("application/json")))try{const t=i.clone(),s=yield t.json();e.extractEmailFromData(s,"api-response")}catch(t){}})).catch(()=>{}),n}catch(t){return e.log("Error intercepting fetch:",t),e.originalFetch.call(window,i,s)}})},this.log("Fetch interception active")}setupXHRInterception(){if("undefined"==typeof XMLHttpRequest)return;const t=this;this.originalXHROpen=XMLHttpRequest.prototype.open,this.originalXHRSend=XMLHttpRequest.prototype.send,XMLHttpRequest.prototype.open=function(e,i,...s){return this._datalyrUrl="string"==typeof i?i:i.href,t.originalXHROpen.apply(this,[e,i,...s])},XMLHttpRequest.prototype.send=function(e){const i=this._datalyrUrl;return i&&t.isTrustedDomain(i)&&(e&&t.extractEmailFromData(e,"api-request"),this.addEventListener("load",function(){if(this.status>=200&&this.status<300)try{const e=this.getResponseHeader("content-type");if(null==e?void 0:e.includes("application/json")){const e=JSON.parse(this.responseText);t.extractEmailFromData(e,"api-response")}}catch(t){}})),t.originalXHRSend.call(this,e)},this.log("XHR interception active")}setupShopifyMonitoring(){if(!this.isShopify())return;this.log("Shopify detected, setting up monitoring"),this.checkShopifyCustomer();let t=0;this.shopifyCheckInterval=setInterval(()=>{t++,this.checkShopifyCustomer(),t>=12&&this.shopifyCheckInterval&&(clearInterval(this.shopifyCheckInterval),this.shopifyCheckInterval=void 0,this.log("Shopify monitoring stopped after max checks (no login detected)"))},1e4),this.log("Shopify monitoring active")}isShopify(){return!!(window.Shopify||document.querySelector('meta[name="shopify-checkout-api-token"]')||window.location.hostname.includes(".myshopify.com"))}checkShopifyCustomer(){return t(this,void 0,void 0,function*(){var t;try{const e=yield fetch("/account.json",{credentials:"same-origin"});if(e.ok){const i=yield e.json();(null===(t=null==i?void 0:i.customer)||void 0===t?void 0:t.email)&&(this.log("Email captured from Shopify:",i.customer.email),this.triggerIdentify(i.customer.email,"shopify"),this.shopifyCheckInterval&&(clearInterval(this.shopifyCheckInterval),this.shopifyCheckInterval=void 0))}}catch(t){this.log("Shopify customer check failed:",t)}})}extractEmailFromData(t,e){try{let i=[];if("string"==typeof t)try{const e=JSON.parse(t);i=this.findEmailsInObject(e)}catch(e){const s=/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g,n=t.match(s);n&&(i=n)}else t instanceof FormData?t.forEach(t=>{"string"==typeof t&&this.isValidEmail(t)&&i.push(t)}):"object"==typeof t&&null!==t&&(i=this.findEmailsInObject(t));if(i.length>0){const t=i[0];this.log(`Email found in ${e}:`,t),this.triggerIdentify(t,"api")}}catch(t){this.log("Error extracting email from data:",t)}}findEmailsInObject(t,e=0){if(e>5)return[];const i=[];for(const s in t){if(!t.hasOwnProperty(s))continue;const n=t[s];/email/i.test(s)&&"string"==typeof n&&this.isValidEmail(n)?i.push(n):"object"==typeof n&&null!==n&&i.push(...this.findEmailsInObject(n,e+1))}return i}isTrustedDomain(t){try{const e=new URL(t,window.location.origin);return e.origin===window.location.origin||0!==this.config.trustedDomains.length&&this.config.trustedDomains.some(t=>e.hostname===t||e.hostname.endsWith(`.${t}`))}catch(t){return!1}}isValidEmail(t){if(!t||"string"!=typeof t)return!1;if(!/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(t))return!1;return![/test@/i,/example@/i,/demo@/i,/fake@/i,/@test\./i,/@example\./i].some(e=>e.test(t))}triggerIdentify(e,i){return t(this,void 0,void 0,function*(){if(this.destroyed)return;const t=Date.now();if(t-this.lastIdentifyTime<this.RATE_LIMIT_MS)return void this.log("Rate limited, skipping identify");this.lastIdentifyTime=t;if((yield r.getEncrypted("dl_auto_identified_email"))!==e){if(!this.destroyed){try{yield r.setEncrypted("dl_auto_identified_email",e)}catch(t){this.log("Failed to persist auto-identified email:",t)}this.identifyCallback&&(this.log(`Auto-identifying user: ${e} (source: ${i})`),this.identifyCallback(e,i))}}else this.log("Already identified with this email")})}destroy(){this.destroyed=!0,this.originalFetch&&(window.fetch=this.originalFetch,this.originalFetch=void 0),this.originalXHROpen&&(XMLHttpRequest.prototype.open=this.originalXHROpen,this.originalXHROpen=void 0),this.originalXHRSend&&(XMLHttpRequest.prototype.send=this.originalXHRSend,this.originalXHRSend=void 0),this.formObserver&&(this.formObserver.disconnect(),this.formObserver=void 0),this.formListeners.forEach(({element:t,handler:e})=>{t.removeEventListener("submit",e)}),this.formListeners=[],this.shopifyCheckInterval&&(clearInterval(this.shopifyCheckInterval),this.shopifyCheckInterval=void 0),this.log("Auto-identify destroyed")}log(...t){this.config.debug&&console.log("[Datalyr Auto-Identify]",...t)}}x.THIRD_PARTY_EMAIL_FIELD=/(recipient|friend|gift|refer|invite|colleague|coworker|share|to[-_]?email|email[-_]?to|second(ary)?[-_]?email)/i;const P=["autoIdentify","autoIdentifyForms","autoIdentifyAPI","autoIdentifyShopify","shopifyCartAttributes","checkoutChampDomains","respectGlobalPrivacyControl","respectDoNotTrack","privacyMode"];const O=new class{constructor(){this.explicitConfigKeys=new Set,this.superProperties={},this.userProperties={},this.optedOut=!1,this.consent=null,this.initialized=!1,this.errors=[],this.MAX_ERRORS=50,this.lastSpaPath=null,this.initializationPromise=null}init(t){if(this.initialized)return void console.warn("[Datalyr] SDK already initialized");if(!t.workspaceId)throw new Error("[Datalyr] workspaceId is required");this.explicitConfigKeys=new Set(Object.keys(t)),this.config=Object.assign({endpoint:"https://ingest.datalyr.com",debug:!1,batchSize:10,flushInterval:5e3,flushAt:10,criticalEvents:void 0,highPriorityEvents:void 0,sessionTimeout:36e5,trackSessions:!0,attributionWindow:7776e6,trackedParams:[],respectDoNotTrack:!1,respectGlobalPrivacyControl:!0,privacyMode:"standard",cookieDomain:"auto",cookieExpires:365,secureCookie:"auto",sameSite:"Lax",cookiePrefix:"__dl_",enablePerformanceTracking:!0,enableFingerprinting:!0,maxRetries:5,retryDelay:1e3,maxOfflineQueueSize:100,trackSPA:!0,trackPageViews:!0,fallbackEndpoints:[],plugins:[]},t),"shopify"===this.config.platform&&void 0===this.config.shopifyCartAttributes&&(this.config.shopifyCartAttributes=!0),this.cookies=new s({domain:this.config.cookieDomain,maxAge:this.config.cookieExpires,sameSite:this.config.sameSite,secure:this.config.secureCookie}),r.migrateFromLegacyPrefix(),this.optedOut="true"===this.cookies.get("__dl_opt_out"),this.consent=r.get("dl_consent",null),"checkoutchamp"===this.config.platform&&this.restoreFromURL(),this.identity=new w({persistNewId:this.shouldTrack()}),this.session=new b(this.config.sessionTimeout),this.attribution=new S({attributionWindow:this.config.attributionWindow,trackedParams:this.config.trackedParams}),this.queue=new C(this.config),this.fingerprint=new T({privacyMode:this.config.privacyMode,enableFingerprinting:this.config.enableFingerprinting});const e=this.session.getSessionId();if(this.identity.setSessionId(e),this.queue.setEnabled(this.shouldTrack()),this.initializeAsync(),this.setupUnloadHandler(),this.config.plugins)for(const t of this.config.plugins)try{t.initialize(this),this.log(`Plugin initialized: ${t.name}`)}catch(e){console.warn(`[Datalyr] Plugin '${t.name}' failed to initialize:`,e),this.trackError(e,{plugin:t.name})}this.initialized=!0,this.log("SDK initialized")}initializeAsync(){return t(this,void 0,void 0,function*(){return this.initializationPromise||(this.initializationPromise=(()=>t(this,void 0,void 0,function*(){var t;try{try{const t=this.identity.getAnonymousId();yield e.initialize(this.config.workspaceId,t),this.userProperties=yield r.getEncrypted("dl_user_traits",{}),this.log("Encryption initialized, user properties loaded")}catch(t){console.warn("[Datalyr] Encryption unavailable (non-secure context?); continuing WITHOUT PII-at-rest encryption:",t),this.userProperties=r.get("dl_user_traits",{})}this.config.trackSPA&&this.setupSPATracking();const i="strict"===this.config.privacyMode;!1!==this.config.enableContainer&&this.shouldTrack()&&!i&&this.consentAllowsMarketing()&&(this.container=new A({workspaceId:this.config.workspaceId,endpoint:this.config.endpoint,debug:this.config.debug,getIdentity:()=>{var t,e;return{externalId:null===(t=this.identity)||void 0===t?void 0:t.getDistinctId(),email:null===(e=this.userProperties)||void 0===e?void 0:e.email}}}),yield this.container.init().catch(t=>{this.log("Container initialization failed:",t)}),"checkoutchamp"===this.config.platform&&this.fireCheckoutChampPurchasePixel()),function(t,e,i){if(!e)return;const s=t;for(const t of P){const n=e[t];null!=n&&((null==i?void 0:i.has(t))||(s[t]=n))}}(this.config,null===(t=this.container)||void 0===t?void 0:t.getRemoteConfig(),this.explicitConfigKeys),"strict"===this.config.privacyMode&&(this.config.autoIdentify=!1);("checkoutchamp"===this.config.platform||Array.isArray(this.config.checkoutChampDomains)&&this.config.checkoutChampDomains.length>0)&&void 0===this.config.autoIdentify&&(this.config.autoIdentify=!0),!0===this.config.autoIdentify&&this.shouldTrack()&&(this.autoIdentify=new x({enabled:!0,captureFromForms:this.config.autoIdentifyForms,captureFromAPI:this.config.autoIdentifyAPI,captureFromShopify:this.config.autoIdentifyShopify,trustedDomains:this.config.autoIdentifyTrustedDomains,debug:this.config.debug}),this.autoIdentify.initialize((t,e)=>{this.log(`Auto-identified user: ${t} from ${e}`),this.track("$auto_identify",{email:t,source:e,timestamp:Date.now()}),this.identify(t,{email:t})})),!0===this.config.shopifyCartAttributes&&this.shouldTrack()&&this.syncShopifyCartAttributes().catch(t=>{this.log("Shopify cart attribute sync failed:",t)}),Array.isArray(this.config.checkoutChampDomains)&&this.config.checkoutChampDomains.length>0&&this.shouldTrack()&&this.syncOutboundLinkParams(this.config.checkoutChampDomains),this.config.trackPageViews&&this.page(),this.log("Async initialization complete")}catch(t){console.error("[Datalyr] Async initialization failed:",t),this.userProperties=r.get("dl_user_traits",{})}}))()),this.initializationPromise})}ready(){return t(this,void 0,void 0,function*(){if(!this.initialized)throw new Error("[Datalyr] SDK not initialized. Call init() first.");return this.initializationPromise||Promise.resolve()})}track(t,e={}){if(this.initialized){if(this.shouldTrack())try{this.session.updateActivity(t);const i=l(),s=this.createEventPayload(t,e,i);if(this.queue.enqueue(s),this.container&&this.container.trackToPixels(t,e,i),this.config.plugins)for(const i of this.config.plugins)if(i.track)try{i.track(t,e)}catch(e){this.trackError(e,{plugin:i.name,event:t})}this.log("Event tracked:",t)}catch(e){this.trackError(e,{event:t})}}else console.warn("[Datalyr] SDK not initialized. Call init() first.")}trackAppDownloadClick(e){return t(this,void 0,void 0,function*(){if(this.initialized){if(this.shouldTrack())if(this.track("$app_download_click",{target_platform:e.targetPlatform,app_store_url:e.appStoreUrl}),yield this.queue.forceFlush(),"android"===e.targetPlatform&&e.appStoreUrl.includes("play.google.com")){const t=this.attribution.getLastTouch(),i=new URLSearchParams;(null==t?void 0:t.clickId)&&i.set("dl_click_id",t.clickId),(null==t?void 0:t.clickIdType)&&i.set("dl_click_id_type",t.clickIdType),(null==t?void 0:t.source)&&i.set("utm_source",t.source),(null==t?void 0:t.medium)&&i.set("utm_medium",t.medium),(null==t?void 0:t.campaign)&&i.set("utm_campaign",t.campaign),(null==t?void 0:t.content)&&i.set("utm_content",t.content),(null==t?void 0:t.term)&&i.set("utm_term",t.term);try{const t=new URL(e.appStoreUrl);t.searchParams.set("referrer",i.toString()),window.location.href=t.toString()}catch(t){window.location.href=e.appStoreUrl}}else window.location.href=e.appStoreUrl}else console.warn("[Datalyr] SDK not initialized. Call init() first.")})}identify(t,e={}){if(this.initialized){if(this.shouldTrack())if(t)try{const i=this.identity.identify(t,e);if(this.userProperties=Object.assign(Object.assign({},this.userProperties),e),r.setEncrypted("dl_user_traits",this.userProperties).catch(t=>{console.error("[Datalyr] Failed to encrypt user traits - NOT storing unencrypted PII:",t),console.error("[Datalyr] User traits will only persist in memory until page reload")}),this.track("$identify",Object.assign(Object.assign({},i),{traits:e})),this.config.plugins)for(const i of this.config.plugins)if(i.identify)try{i.identify(t,e)}catch(t){this.trackError(t,{plugin:i.name})}this.log("User identified:",t)}catch(e){this.trackError(e,{userId:t})}else console.warn("[Datalyr] identify() called without userId")}else console.warn("[Datalyr] SDK not initialized. Call init() first.")}page(t={}){if(!this.initialized)return void console.warn("[Datalyr] SDK not initialized. Call init() first.");if(!this.shouldTrack())return;const e=this.session.getSessionId(),i=this.attribution.getJourney(),s=i.length?i[i.length-1]:null;s&&s.sessionId===e||this.attribution.addTouchpoint(e,this.attribution.captureAttribution());const n=Object.assign({title:document.title,url:window.location.href,path:window.location.pathname,search:window.location.search,referrer:document.referrer},t),o=function(){const t=document.referrer;if(!t)return{};try{const e=new URL(t);return{referrer:t,referrer_host:e.hostname,referrer_path:e.pathname,referrer_search:e.search,referrer_source:v(e.hostname)}}catch(e){return{referrer:t}}}();if(Object.assign(n,o),this.config.enablePerformanceTracking){const t=this.getPerformanceMetrics();t&&(n.performance=t)}if(this.track("pageview",n),this.config.plugins)for(const t of this.config.plugins)if(t.page)try{t.page(n)}catch(e){this.trackError(e,{plugin:t.name})}}screen(t,e={}){this.initialized?this.shouldTrack()&&this.track("pageview",Object.assign({screen:t},e)):console.warn("[Datalyr] SDK not initialized. Call init() first.")}group(t,e={}){this.initialized?this.shouldTrack()&&this.track("$group",{group_id:t,traits:e}):console.warn("[Datalyr] SDK not initialized. Call init() first.")}alias(t,e){if(!this.initialized)return void console.warn("[Datalyr] SDK not initialized. Call init() first.");if(!t)return void console.warn("[Datalyr] alias() requires a non-empty userId");if(!this.shouldTrack())return;const i=this.identity.alias(t,e);this.track("$alias",i)}reset(){this.initialized?(this.identity.reset(),this.userProperties={},this.superProperties={},r.remove("dl_user_traits"),r.remove("dl_auto_identified_email"),r.remove("dl_journey"),r.remove("dl_first_touch"),r.remove("dl_last_touch"),this.session.createNewSession(),this.log("User reset")):console.warn("[Datalyr] SDK not initialized. Call init() first.")}getAnonymousId(){return this.identity.getAnonymousId()}getVisitorId(){return this.identity.getAnonymousId()}getStripeMetadata(){const t=this.identity.getAnonymousId();return{client_reference_id:t,metadata:{visitor_id:t}}}getWhopCheckoutMetadata(){return{visitor_id:this.identity.getAnonymousId()}}getUserId(){return this.identity.getUserId()}getDistinctId(){return this.identity.getDistinctId()}getSessionId(){return this.session.getSessionId()}startNewSession(){const t=this.session.createNewSession();return this.identity.setSessionId(t),t}getSessionData(){return this.session.getSessionData()}getAttribution(){return this.attribution.captureAttribution()}getJourney(){return this.attribution.getJourney()}setAttribution(t){const e=this.attribution.captureAttribution(),i=Object.assign(Object.assign({},e),t);this.attribution.storeLastTouch(i),this.attribution.storeFirstTouch(i),this.session.storeAttribution(i)}optOut(){var t;this.initialized?(this.optedOut=!0,this.cookies.set("__dl_opt_out","true",this.config.cookieExpires),this.queue.setEnabled(!1),this.queue.clear(),this.queue.clearOffline(),null===(t=this.autoIdentify)||void 0===t||t.destroy(),this.autoIdentify=void 0,this.container&&(this.container.cleanupAllIframes(),this.container=void 0),this.userProperties={},r.remove("dl_user_traits"),r.remove("dl_auto_identified_email"),r.remove("dl_journey"),this.log("User opted out")):console.warn("[Datalyr] SDK not initialized. Call init() first.")}optIn(){this.initialized?(this.optedOut=!1,this.cookies.set("__dl_opt_out","false",this.config.cookieExpires),this.queue.setEnabled(this.shouldTrack()),this.log("User opted in")):console.warn("[Datalyr] SDK not initialized. Call init() first.")}isOptedOut(){return this.optedOut}setConsent(t){if(this.consent=t,r.set("dl_consent",t),!this.initialized)return;const e=this.shouldTrack();this.queue.setEnabled(e),e||(this.queue.clear(),this.queue.clearOffline()),!this.consentAllowsMarketing()&&this.container&&(this.container.cleanupAllIframes(),this.container=void 0),this.log("Consent updated:",t)}flush(){return t(this,void 0,void 0,function*(){yield this.queue.flush()})}setSuperProperties(t){this.superProperties=Object.assign(Object.assign({},this.superProperties),t),this.log("Super properties set:",t)}unsetSuperProperty(t){delete this.superProperties[t],this.log("Super property unset:",t)}getSuperProperties(){return Object.assign({},this.superProperties)}syncShopifyCartAttributes(){return t(this,void 0,void 0,function*(){if("undefined"==typeof window||"undefined"==typeof document)return;if(!!!(window.Shopify||document.querySelector('meta[name="shopify-checkout-api-token"]')||window.location.hostname.includes(".myshopify.com")))return;const t=this.attribution.getAttributionData(),e="fbclid"===t.clickIdType?t.clickId:null,i={},s=this.identity.getAnonymousId(),n=this.cookies.get("_fbc")||t._fbc,o=this.cookies.get("_fbp")||t._fbp,r=this.cookies.get("_dl_fbclid_at");if(s&&(i._datalyr_visitor_id=s),n&&(i._datalyr_fbc=String(n)),o&&(i._datalyr_fbp=String(o)),e&&(i._datalyr_fbclid=String(e)),r&&(i._datalyr_fbclid_at=String(r)),0!==Object.keys(i).length)try{yield fetch("/cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},credentials:"same-origin",body:JSON.stringify({attributes:i})}),this.log("Shopify cart attributes stamped:",Object.keys(i))}catch(t){this.log("Shopify /cart/update.js failed:",t)}})}restoreFromURL(){var t;if("undefined"!=typeof window&&"undefined"!=typeof document)try{const e=new URLSearchParams(window.location.search),i=t=>e.get(t),s=i("_dl_vid"),n=i("_dl_fbc"),o=i("_dl_fbp"),r=i("_dl_fbclid"),a=i("_dl_fbclid_at"),c=i("_dl_gclid"),l=i("_dl_gclid_at");s&&this.cookies.set("__dl_visitor_id",s,365);const d=(t,e)=>{e&&(this.cookies.get(t)||this.cookies.set(t,e,365))};d("_fbc",n),d("_fbp",o),d("_dl_fbclid_at",a),d("_dl_gclid_at",l);let h=!1;const u=[["fbclid",r],["gclid",c]];for(const[t,i]of u)i&&!e.get(t)&&(e.set(t,i),h=!0);for(const t of["_dl_vid","_dl_fbc","_dl_fbp","_dl_fbclid","_dl_fbclid_at","_dl_gclid","_dl_gclid_at"])e.has(t)&&(e.delete(t),h=!0);if(h&&"function"==typeof(null===(t=window.history)||void 0===t?void 0:t.replaceState)){const t=e.toString(),i=window.location.pathname+(t?"?"+t:"")+window.location.hash;window.history.replaceState(window.history.state,"",i)}this.log("Checkout Champ bridge restored:",{had_vid:!!s,had_fbc:!!n,had_fbp:!!o,had_fbclid:!!r,had_gclid:!!c})}catch(t){this.log("restoreFromURL failed:",t)}}fireCheckoutChampPurchasePixel(){var t;if("undefined"!=typeof window)try{if(!this.container)return;const e=null===(t=window.sessionStorage)||void 0===t?void 0:t.getItem("orderData");if(!e)return;const i=JSON.parse(e)||{},s=i.orderId;if(!s)return;if(!this.container.hasMetaPixel())return void this.log("CC Purchase Pixel: Meta pixel not ready; will retry on next funnel page");const n=`__dl_cc_purchase_${s}`;if(window.sessionStorage.getItem(n))return;window.sessionStorage.setItem(n,"1");const o=`checkoutchamp_purchase_${s}`,r=Number(i.totalAmount),a={order_id:String(s),content_type:"product"};Number.isFinite(r)&&(a.value=r);const c=i.currencyCode||i.currency;c&&(a.currency=String(c)),i.productId&&(a.content_ids=[String(i.productId)]),this.container.trackToPixels("purchase",a,o),this.log("Checkout Champ Purchase Pixel co-fired (CAPI dedup):",{eventId:o,value:a.value,currency:a.currency})}catch(t){this.log("fireCheckoutChampPurchasePixel failed:",t)}}syncOutboundLinkParams(t){if("undefined"==typeof window||"undefined"==typeof document)return;const e=t.map(t=>t.toLowerCase()),i=t=>{try{const i=new URL(t,window.location.href).hostname.toLowerCase();return e.some(t=>i===t||i.endsWith("."+t))}catch(t){return!1}},s=()=>{var t,e;const i=this.attribution.getAttributionData(),s={},n=this.identity.getAnonymousId(),o=this.cookies.get("_fbc")||i._fbc,r=this.cookies.get("_fbp")||i._fbp,a=null!==(t=i.fbclid)&&void 0!==t?t:"fbclid"===i.clickIdType?i.clickId:null,c=this.cookies.get("_dl_fbclid_at"),l=null!==(e=i.gclid)&&void 0!==e?e:"gclid"===i.clickIdType?i.clickId:null,d=this.cookies.get("_dl_gclid_at");return n&&(s._dl_vid=n),o&&(s._dl_fbc=String(o)),r&&(s._dl_fbp=String(r)),a&&(s._dl_fbclid=String(a)),c&&(s._dl_fbclid_at=String(c)),l&&(s._dl_gclid=String(l)),d&&(s._dl_gclid_at=String(d)),s},n=t=>{if(t.href&&i(t.href))try{const e=new URL(t.href,window.location.href),i=s();let n=!1;for(const[t,s]of Object.entries(i))e.searchParams.get(t)||(e.searchParams.set(t,s),n=!0);n&&(t.href=e.toString())}catch(t){}},o=()=>{document.querySelectorAll("a[href]").forEach(t=>n(t))},r=t=>{var e,s;const o=null===(e=t.target)||void 0===e?void 0:e.closest("a[href]");if(!o)return;const r=o;if(i(r.href)){n(r);try{null===(s=this.queue)||void 0===s||s.flush()}catch(t){}}};o(),document.addEventListener("click",r,!0);try{let t=null;const e=new MutationObserver(()=>{null==t&&(t=setTimeout(()=>{t=null,o()},150))});e.observe(document.documentElement,{childList:!0,subtree:!0}),this.outboundDisposer=()=>{try{e.disconnect()}catch(t){}null!=t&&(clearTimeout(t),t=null),document.removeEventListener("click",r,!0)},window.addEventListener("pagehide",()=>{var t;return null===(t=this.outboundDisposer)||void 0===t?void 0:t.call(this)},{once:!0})}catch(t){this.log("MutationObserver setup failed (CC link sync):",t)}this.log("Checkout Champ outbound-link sync active for:",e)}createEventPayload(t,e,i){this.identity.setSessionId(this.session.getSessionId());const s=g(e),n=p({},this.superProperties,s),o=(t,e)=>{for(const i in e)Object.prototype.hasOwnProperty.call(e,i)&&!Object.prototype.hasOwnProperty.call(t,i)&&(t[i]=e[i])};o(n,this.attribution.getAttributionData());const r=this.session.getMetrics();if(Object.assign(n,r),this.config.enableFingerprinting){const t=this.fingerprint.collect();Object.assign(n,{device_fingerprint:t})}o(n,{url:window.location.href,path:window.location.pathname,referrer:document.referrer,title:document.title,screen_width:screen.width,screen_height:screen.height,viewport_width:window.innerWidth,viewport_height:window.innerHeight});const a=this.identity.getIdentityFields(),c=null!=i?i:l(),d=a.distinct_id,h=a.anonymous_id,u=a.visitor_id||h,f=a.session_id;return{workspace_id:this.config.workspaceId,event_id:c,event_name:t,event_data:n,source:"web",timestamp:(new Date).toISOString(),distinct_id:d,anonymous_id:h,visitor_id:u,user_id:a.user_id,canonical_id:a.canonical_id,session_id:f,resolution_method:"browser_sdk",resolution_confidence:1,sdk_version:"1.7.3",sdk_name:"datalyr-web-sdk"}}shouldTrack(){return!this.optedOut&&((!this.consent||!1!==this.consent.analytics)&&((!this.config.respectDoNotTrack||"1"!==navigator.doNotTrack&&"1"!==window.doNotTrack&&"yes"!==navigator.doNotTrack)&&(!this.config.respectGlobalPrivacyControl||!0!==navigator.globalPrivacyControl&&!0!==window.globalPrivacyControl)))}consentAllowsMarketing(){return!this.consent||!1!==this.consent.marketing&&!1!==this.consent.sale}setupSPATracking(){this.originalPushState=history.pushState,this.originalReplaceState=history.replaceState;const t=this;this.lastSpaPath=window.location.pathname+window.location.search+window.location.hash,history.pushState=function(...e){t.originalPushState.apply(history,e),setTimeout(()=>t.handleSpaNavigation(),0)},history.replaceState=function(...e){t.originalReplaceState.apply(history,e),setTimeout(()=>t.handleSpaNavigation(),0)},this.popstateHandler=()=>{setTimeout(()=>t.handleSpaNavigation(),0)},window.addEventListener("popstate",this.popstateHandler),this.hashchangeHandler=()=>{t.handleSpaNavigation()},window.addEventListener("hashchange",this.hashchangeHandler)}handleSpaNavigation(){const t=window.location.pathname+window.location.search+window.location.hash;t!==this.lastSpaPath&&(this.lastSpaPath=t,this.attribution.clearCache(),this.page())}setupUnloadHandler(){this.unloadHandler=()=>{this.queue.forceFlush(!0)},this.visibilityHandler=()=>{"hidden"===document.visibilityState&&this.queue.forceFlush(!1)},window.addEventListener("beforeunload",this.unloadHandler),window.addEventListener("pagehide",this.unloadHandler),window.addEventListener("visibilitychange",this.visibilityHandler)}getPerformanceMetrics(){if(!this.config.enablePerformanceTracking)return null;const t={};try{if(performance&&"function"==typeof performance.getEntriesByType){const e=performance.getEntriesByType("navigation"),i=e&&e[0];i&&(t.pageLoadTime=Math.round(i.loadEventEnd),t.domReadyTime=Math.round(i.domContentLoadedEventEnd),t.firstByteTime=Math.round(i.responseStart),t.dnsTime=Math.round(i.domainLookupEnd-i.domainLookupStart),t.tcpTime=Math.round(i.connectEnd-i.connectStart),t.requestTime=Math.round(i.responseEnd-i.requestStart),t.timeOnPage=Math.round(performance.now()))}}catch(t){this.trackError(t,{context:"performance_metrics"})}return Object.keys(t).length>0?t:null}trackError(t,e){const i={message:t.message||String(t),stack:t.stack,context:e,timestamp:(new Date).toISOString(),url:window.location.href};this.errors.push(i),this.errors.length>this.MAX_ERRORS&&(this.errors=this.errors.slice(-this.MAX_ERRORS)),this.config.debug&&console.error("[Datalyr Error]",i)}getErrors(){return[...this.errors]}getNetworkStatus(){return this.queue.getNetworkStatus()}loadScript(t){this.container&&this.container.triggerCustomScript(t)}getLoadedScripts(){return this.container?this.container.getLoadedScripts():[]}log(...t){var e;(null===(e=this.config)||void 0===e?void 0:e.debug)&&console.log("[Datalyr]",...t)}destroy(){this.originalPushState&&(history.pushState=this.originalPushState),this.originalReplaceState&&(history.replaceState=this.originalReplaceState),this.popstateHandler&&window.removeEventListener("popstate",this.popstateHandler),this.hashchangeHandler&&window.removeEventListener("hashchange",this.hashchangeHandler),this.unloadHandler&&(window.removeEventListener("beforeunload",this.unloadHandler),window.removeEventListener("pagehide",this.unloadHandler),this.unloadHandler=void 0),this.visibilityHandler&&(window.removeEventListener("visibilitychange",this.visibilityHandler),this.visibilityHandler=void 0),this.outboundDisposer&&(this.outboundDisposer(),this.outboundDisposer=void 0),this.queue&&this.queue.destroy(),this.session&&this.session.destroy(),this.container&&this.container.cleanupAllIframes(),this.autoIdentify&&this.autoIdentify.destroy(),e.destroy(),this.initializationPromise=null,this.container=void 0,this.autoIdentify=void 0,this.lastSpaPath=null,this.superProperties={},this.userProperties={},this.errors=[],this.initialized=!1,this.log("SDK destroyed")}};"undefined"!=typeof window&&(window.datalyr=O);export{O as datalyr,O as default};
|
|
2
2
|
//# sourceMappingURL=datalyr.esm.min.js.map
|