@tracelog/lib 2.10.0 → 3.0.0-rc.115.6
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/README.md +160 -733
- package/dist/browser/tracelog.esm.js +1348 -2812
- package/dist/browser/tracelog.esm.js.map +1 -1
- package/dist/browser/tracelog.js +2 -2
- package/dist/browser/tracelog.js.map +1 -1
- package/dist/public-api.cjs +2 -2
- package/dist/public-api.cjs.map +1 -1
- package/dist/public-api.d.mts +91 -797
- package/dist/public-api.d.ts +91 -797
- package/dist/public-api.js +2 -2
- package/dist/public-api.js.map +1 -1
- package/package.json +3 -3
package/dist/browser/tracelog.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(f){"use strict";const L="data-tlog",Mt=["button","a",'input[type="button"]','input[type="submit"]','input[type="reset"]','input[type="checkbox"]','input[type="radio"]',"select","textarea",'[role="button"]','[role="link"]','[role="tab"]','[role="menuitem"]','[role="option"]','[role="checkbox"]','[role="radio"]','[role="switch"]',"[routerLink]","[ng-click]","[data-action]","[data-click]","[data-navigate]","[data-toggle]","[onclick]",".btn",".button",".clickable",".nav-link",".menu-item","[data-testid]",'[tabindex="0"]'],Ct=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],Rt=["token","auth","key","session","reset","password","api_key","apikey","secret","access_token","refresh_token","verification","code","otp"],T={INVALID_SESSION_TIMEOUT:"Session timeout must be between 30000ms (30 seconds) and 86400000ms (24 hours)",INVALID_SAMPLING_RATE:"Sampling rate must be between 0 and 1",INVALID_ERROR_SAMPLING_RATE:"Error sampling must be between 0 and 1",INVALID_TRACELOG_PROJECT_ID:"TraceLog project ID is required when integration is enabled",INVALID_CUSTOM_API_URL:"Custom API URL is required when integration is enabled",INVALID_GLOBAL_METADATA:"Global metadata must be an object",INVALID_SENSITIVE_QUERY_PARAMS:"Sensitive query params must be an array of strings",INVALID_PRIMARY_SCROLL_SELECTOR:"Primary scroll selector must be a non-empty string",INVALID_PRIMARY_SCROLL_SELECTOR_SYNTAX:"Invalid CSS selector syntax for primaryScrollSelector",INVALID_PAGE_VIEW_THROTTLE:"Page view throttle must be a non-negative number",INVALID_CLICK_THROTTLE:"Click throttle must be a non-negative number",INVALID_MAX_SAME_EVENT_PER_MINUTE:"Max same event per minute must be a positive number",INVALID_VIEWPORT_CONFIG:"Viewport config must be an object",INVALID_VIEWPORT_ELEMENTS:"Viewport elements must be a non-empty array",INVALID_VIEWPORT_ELEMENT:"Each viewport element must have a valid selector string",INVALID_VIEWPORT_ELEMENT_ID:"Viewport element id must be a non-empty string",INVALID_VIEWPORT_ELEMENT_NAME:"Viewport element name must be a non-empty string",INVALID_VIEWPORT_THRESHOLD:"Viewport threshold must be a number between 0 and 1",INVALID_VIEWPORT_MIN_DWELL_TIME:"Viewport minDwellTime must be a non-negative number",INVALID_VIEWPORT_COOLDOWN_PERIOD:"Viewport cooldownPeriod must be a non-negative number",INVALID_VIEWPORT_MAX_TRACKED_ELEMENTS:"Viewport maxTrackedElements must be a positive number",INVALID_SEND_INTERVAL:"Send interval must be between 1000ms (1 second) and 60000ms (60 seconds)"},Nt=[/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,/javascript:/gi,/on\w+\s*=/gi,/<iframe\b[^<]*(?:(?!<\/iframe>)<[^<]*)*<\/iframe>/gi,/<embed\b[^>]*>/gi,/<object\b[^<]*(?:(?!<\/object>)<[^<]*)*<\/object>/gi],I="tlog",X=`${I}:qa_mode`,me=`${I}:uid`,Be="tlog_mode",We="qa",Ge="qa_off",Ot=r=>r?`${I}:${r}:queue`:`${I}:queue`,Pt=r=>r?`${I}:${r}:rate_limit`:`${I}:rate_limit`,Dt=r=>r?`${I}:${r}:session`:`${I}:session`,kt=r=>r?`${I}:${r}:broadcast`:`${I}:broadcast`,Xe=(r,e)=>`${I}:${r}:session_counts:${e}`,je=10080*60*1e3,Ke=`${I}:session_counts_last_cleanup`,ze=3600*1e3,ge=r=>r?`${I}:${r}:identity`:`${I}:identity`,H=`${I}:pending_identity`;var F=(r=>(r.Localhost="localhost:8080",r.Fail="localhost:9999",r))(F||{}),A=(r=>(r.Mobile="mobile",r.Tablet="tablet",r.Desktop="desktop",r.Unknown="unknown",r))(A||{}),j=(r=>(r.EVENT="event",r.QUEUE="queue",r))(j||{});class R extends Error{constructor(e,t,s){super(e),this.statusCode=t,this.responseCode=s,this.name="PermanentError",Error.captureStackTrace&&Error.captureStackTrace(this,R)}statusCode;responseCode}class K extends Error{constructor(e){super(e),this.name="RateLimitError",Error.captureStackTrace&&Error.captureStackTrace(this,K)}}class z extends Error{constructor(e){super(e),this.name="TimeoutError",Error.captureStackTrace&&Error.captureStackTrace(this,z)}}var u=(r=>(r.PAGE_VIEW="page_view",r.CLICK="click",r.SCROLL="scroll",r.SESSION_START="session_start",r.CUSTOM="custom",r.WEB_VITALS="web_vitals",r.ERROR="error",r.VIEWPORT_VISIBLE="viewport_visible",r))(u||{}),Q=(r=>(r.UP="up",r.DOWN="down",r))(Q||{}),$=(r=>(r.JS_ERROR="js_error",r.PROMISE_REJECTION="promise_rejection",r))($||{}),Y=(r=>(r.QA="qa",r))(Y||{});const Vt=r=>r.type===u.SCROLL&&"scroll_data"in r&&r.scroll_data.is_primary===!0,Ut=r=>r.type===u.SCROLL&&"scroll_data"in r&&r.scroll_data.is_primary===!1;class x extends Error{constructor(e,t,s){super(e),this.errorCode=t,this.layer=s,this.name=this.constructor.name,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}errorCode;layer}class E extends x{constructor(e,t="config"){super(e,"APP_CONFIG_INVALID",t)}}class Qe extends x{constructor(e,t="config"){super(e,"SESSION_TIMEOUT_INVALID",t)}}class Ee extends x{constructor(e,t="config"){super(e,"SAMPLING_RATE_INVALID",t)}}class N extends x{constructor(e,t="config"){super(e,"INTEGRATION_INVALID",t)}}class Ht extends x{constructor(e,t,s="runtime"){super(e,"INITIALIZATION_TIMEOUT",s),this.timeoutMs=t}timeoutMs}const Ye="background: #ff9800; color: white; font-weight: bold; padding: 2px 8px; border-radius: 3px;",qe="background: #9e9e9e; color: white; font-weight: bold; padding: 2px 8px; border-radius: 3px;",Ft="background: #d32f2f; color: white; font-weight: bold; padding: 2px 8px; border-radius: 3px;",$t=(r,e)=>{if(e){if(e instanceof Error){const t=e.message.replace(/\s+at\s+.*$/gm,"").replace(/\s*\([^()]+:\d+:\d+\)/g,"");return`[TraceLog] ${r}: ${t}`}if(e instanceof Error)return`[TraceLog] ${r}: ${e.message}`;if(typeof e=="string")return`[TraceLog] ${r}: ${e}`;if(typeof e=="object")try{return`[TraceLog] ${r}: ${JSON.stringify(e)}`}catch{return`[TraceLog] ${r}: [Unable to serialize error]`}return`[TraceLog] ${r}: ${String(e)}`}return`[TraceLog] ${r}`},xt=()=>{if(typeof window>"u"||typeof sessionStorage>"u")return!1;try{return sessionStorage.getItem(X)==="true"}catch{return!1}},a=(r,e,t)=>{const{error:s,data:n,showToClient:i=!1,style:o,visibility:l}=t??{},d=s?$t(e,s):`[TraceLog] ${e}`,c=r==="error"?"error":r==="warn"?"warn":"log";if(!Bt(l,i))return;const S=Wt(l,o),g=n!==void 0?Se(n):void 0;Gt(c,d,S,g)},Bt=(r,e)=>r==="critical"?!0:r==="qa"||e?xt():!1,Wt=(r,e)=>e!==void 0&&e!==""?e:r==="critical"?Ft:"",Gt=(r,e,t,s)=>{const n=t!==void 0&&t!=="",i=n?`%c${e}`:e;s!==void 0?n?console[r](i,t,s):console[r](i,s):n?console[r](i,t):console[r](i)},Se=r=>{const e={},t=["token","password","secret","key","apikey","api_key","sessionid","session_id"];for(const[s,n]of Object.entries(r)){const i=s.toLowerCase();if(t.some(o=>i.includes(o))){e[s]="[REDACTED]";continue}n!==null&&typeof n=="object"&&!Array.isArray(n)?e[s]=Se(n):Array.isArray(n)?e[s]=n.map(o=>o!==null&&typeof o=="object"&&!Array.isArray(o)?Se(o):o):e[s]=n}return e};let pe,Je;const Xt=()=>{typeof window<"u"&&!pe&&(pe=window.matchMedia("(pointer: coarse)"),Je=window.matchMedia("(hover: none)"))},te="Unknown",jt=r=>{const e=r.userAgentData?.platform;if(e!=null&&e!==""){if(/windows/i.test(e))return"Windows";if(/macos/i.test(e))return"macOS";if(/android/i.test(e))return"Android";if(/linux/i.test(e))return"Linux";if(/chromeos/i.test(e))return"ChromeOS";if(/ios/i.test(e))return"iOS"}const t=navigator.userAgent;return/Windows/i.test(t)?"Windows":/iPhone|iPad|iPod/i.test(t)?"iOS":/Mac OS X|Macintosh/i.test(t)?"macOS":/Android/i.test(t)?"Android":/CrOS/i.test(t)?"ChromeOS":/Linux/i.test(t)?"Linux":te},Kt=r=>{const e=r.userAgentData?.brands;if(e!=null&&e.length>0){const n=e.filter(i=>!/not.?a.?brand|chromium/i.test(i.brand))[0];if(n!=null){const i=n.brand;return/google chrome/i.test(i)?"Chrome":/microsoft edge/i.test(i)?"Edge":/opera/i.test(i)?"Opera":i}}const t=navigator.userAgent;return/Edg\//i.test(t)?"Edge":/OPR\//i.test(t)?"Opera":/Chrome/i.test(t)?"Chrome":/Firefox/i.test(t)?"Firefox":/Safari/i.test(t)&&!/Chrome/i.test(t)?"Safari":te},zt=()=>{try{const r=navigator;if(r.userAgentData!=null&&typeof r.userAgentData.mobile=="boolean"){const d=r.userAgentData.platform;return d!=null&&d!==""&&/ipad|tablet/i.test(d)?A.Tablet:r.userAgentData.mobile?A.Mobile:A.Desktop}Xt();const e=window.innerWidth,t=pe?.matches??!1,s=Je?.matches??!1,n="ontouchstart"in window||navigator.maxTouchPoints>0,i=navigator.userAgent.toLowerCase(),o=/mobile|android|iphone|ipod|blackberry|iemobile|opera mini/.test(i),l=/tablet|ipad|android(?!.*mobile)/.test(i);return e<=767||o&&n?A.Mobile:e>=768&&e<=1024||l||t&&s&&n?A.Tablet:A.Desktop}catch(r){return a("debug","Device detection failed, defaulting to desktop",{error:r}),A.Desktop}},Qt=()=>{try{const r=navigator;return{type:zt(),os:jt(r),browser:Kt(r)}}catch(r){return a("debug","Device info detection failed, using defaults",{error:r}),{type:A.Desktop,os:te,browser:te}}},Te=[/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/gi,/\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/g,/\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b/g,/\b[A-Z]{2}\d{2}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b/gi,/\b[sp]k_(test|live)_[a-zA-Z0-9]{10,}\b/gi,/Bearer\s+[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)?(?:\.[A-Za-z0-9_-]+)?/gi,/:\/\/[^:/]+:([^@]+)@/gi,/[?&](token|password|passwd|auth|secret|secret_key|private_key|auth_key|api_key|apikey|access_token)=[^&\s]+/gi],Ze=500,et=2e3,tt=5e3,se=50,Yt=se*2,st=1,qt=1e3,Jt=10,rt=5e3,Zt=6e4,es=64,ts={LCP:2500,FCP:1800,CLS:.1,INP:200,TTFB:800,LONG_TASK:50},_e={LCP:2500,FCP:1800,CLS:.1,INP:200,TTFB:800,LONG_TASK:50},nt={LCP:4e3,FCP:3e3,CLS:.25,INP:500,TTFB:1800,LONG_TASK:50},re="needs-improvement",Ie=(r=re)=>{switch(r){case"all":return{LCP:0,FCP:0,CLS:0,INP:0,TTFB:0,LONG_TASK:0};case"needs-improvement":return _e;case"poor":return nt;default:return _e}},ss=1e3,rs=50,ns="2.9.0",it=()=>typeof window<"u"&&typeof sessionStorage<"u",is=()=>{try{const r=new URLSearchParams(window.location.search);r.delete(Be);const e=r.toString(),t=window.location.pathname+(e?"?"+e:"")+window.location.hash;window.history.replaceState({},"",t)}catch{}},os=()=>{if(!it())return!1;try{const e=new URLSearchParams(window.location.search).get(Be),t=sessionStorage.getItem(X);let s=null;return e===We?(s=!0,sessionStorage.setItem(X,"true"),a("info","QA Mode ACTIVE",{visibility:"qa",style:Ye})):e===Ge&&(s=!1,sessionStorage.setItem(X,"false"),a("info","QA Mode DISABLED",{visibility:"qa",style:qe})),(e===We||e===Ge)&&is(),s??t==="true"}catch{return!1}},as=r=>{if(it())try{sessionStorage.setItem(X,r?"true":"false"),a("info",r?"QA Mode ACTIVE":"QA Mode DISABLED",{visibility:"qa",style:r?Ye:qe})}catch{a("debug","Cannot set QA mode: sessionStorage unavailable")}},ls=["co.uk","org.uk","com.au","net.au","com.br","co.nz","co.jp","com.mx","co.in","com.cn","co.za"],ot=r=>{const e=r.toLowerCase().split(".");if(e.length<=2)return r.toLowerCase();const t=e.slice(-2).join(".");return ls.includes(t)?e.slice(-3).join("."):e.slice(-2).join(".")},cs=(r,e)=>r===e?!0:ot(r)===ot(e),ve=()=>{const r=document.referrer;if(!r)return"Direct";try{const e=new URL(r).hostname.toLowerCase(),t=window.location.hostname.toLowerCase();return cs(e,t)?"Direct":r}catch(e){return a("debug","Failed to parse referrer URL, using raw value",{error:e,data:{referrer:r}}),r}},ye=()=>{const r=new URLSearchParams(window.location.search),e={};return Ct.forEach(s=>{const n=r.get(s);if(n){const i=s.split("utm_")[1];e[i]=n}}),Object.keys(e).length?e:void 0},at=()=>typeof crypto<"u"&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,r=>{const e=Math.random()*16|0;return(r==="x"?e:e&3|8).toString(16)});let ne=0,ie=0;const ds=()=>{let r=Date.now();r<ie&&(r=ie),r===ie?ne=(ne+1)%1e3:ne=0,ie=r;const e=ne.toString().padStart(3,"0");let t="";try{if(typeof crypto<"u"&&crypto.getRandomValues){const s=crypto.getRandomValues(new Uint8Array(3));s&&(t=Array.from(s,n=>n.toString(16).padStart(2,"0")).join(""))}}catch{}return t||(t=Math.floor(Math.random()*16777215).toString(16).padStart(6,"0")),`${r}-${e}-${t}`},lt=(r,e=!1)=>{try{const t=new URL(r),s=t.protocol==="https:",n=t.protocol==="http:";return s||e&&n}catch{return!1}},us=r=>{try{const t=new URL(window.location.href).hostname;if(!t||typeof t!="string")throw new Error("Invalid hostname");if(t==="localhost"||t==="127.0.0.1"||/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(t))throw new Error("SaaS integration not supported on localhost or IP addresses. Use custom backend integration instead.");const s=t.split(".");if(!s||!Array.isArray(s)||s.length===0||s.length===1&&s[0]==="")throw new Error("Invalid hostname structure");if(s.length===1)throw new Error("Single-part domain not supported for SaaS integration");let n;if(s.length===2?n=s.join("."):n=s.slice(-2).join("."),!n||n.split(".").length<2)throw new Error("Invalid domain structure for SaaS");const i=`https://${r}.${n}/collect`;if(!lt(i))throw new Error("Generated URL failed validation");return i}catch(e){throw new Error(`Invalid SaaS URL configuration: ${e instanceof Error?e.message:String(e)}`)}},hs=r=>{const e={};r.integrations?.tracelog?.projectId&&(e.saas=us(r.integrations.tracelog.projectId));const t=r.integrations?.custom?.collectApiUrl;if(t){const s=r.integrations?.custom?.allowHttp??!1;if(!lt(t,s))throw new Error("Invalid custom API URL");e.custom=t}return e},we=(r,e=[])=>{if(!r||typeof r!="string")return a("warn","Invalid URL provided to normalizeUrl",{data:{type:typeof r}}),r||"";try{const t=new URL(r),s=t.searchParams,n=[...new Set([...Rt,...e])];let i=!1;const o=[];return n.forEach(d=>{s.has(d)&&(s.delete(d),i=!0,o.push(d))}),!i&&r.includes("?")?r:(t.search=s.toString(),t.toString())}catch(t){return a("warn","URL normalization failed, returning original",{error:t,data:{urlLength:r?.length}}),r}},ct=r=>{if(!r||typeof r!="string"||r.trim().length===0)return"";let e=r;r.length>1e3&&(e=r.slice(0,Math.max(0,1e3)));let t=0;for(const n of Nt){const i=e;e=e.replace(n,""),i!==e&&t++}return t>0&&a("warn","XSS patterns detected and removed",{data:{patternMatches:t,valueLength:r.length}}),e.trim()},be=(r,e=0)=>{if(r==null)return null;if(typeof r=="string")return ct(r);if(typeof r=="number")return!Number.isFinite(r)||r<-Number.MAX_SAFE_INTEGER||r>Number.MAX_SAFE_INTEGER?0:r;if(typeof r=="boolean")return r;if(e>10)return null;if(Array.isArray(r))return r.slice(0,1e3).map(n=>be(n,e+1)).filter(n=>n!==null);if(typeof r=="object"){const t={},n=Object.entries(r).slice(0,200);for(const[i,o]of n){const l=ct(i);if(l){const d=be(o,e+1);d!==null&&(t[l]=d)}}return t}return null},fs=r=>{if(typeof r!="object"||r===null)return{};try{const e=be(r);return typeof e=="object"&&e!==null?e:{}}catch(e){const t=e instanceof Error?e.message:String(e);throw new Error(`[TraceLog] Metadata sanitization failed: ${t}`)}},ms=r=>{if(r!==void 0&&(r===null||typeof r!="object"))throw new E("Configuration must be an object","config");if(r){if(r.sessionTimeout!==void 0&&(typeof r.sessionTimeout!="number"||r.sessionTimeout<3e4||r.sessionTimeout>864e5))throw new Qe(T.INVALID_SESSION_TIMEOUT,"config");if(r.globalMetadata!==void 0&&(typeof r.globalMetadata!="object"||r.globalMetadata===null))throw new E(T.INVALID_GLOBAL_METADATA,"config");if(r.integrations&&Es(r.integrations),r.sensitiveQueryParams!==void 0){if(!Array.isArray(r.sensitiveQueryParams))throw new E(T.INVALID_SENSITIVE_QUERY_PARAMS,"config");for(const e of r.sensitiveQueryParams)if(typeof e!="string")throw new E("All sensitive query params must be strings","config")}if(r.errorSampling!==void 0&&(typeof r.errorSampling!="number"||r.errorSampling<0||r.errorSampling>1))throw new Ee(T.INVALID_ERROR_SAMPLING_RATE,"config");if(r.samplingRate!==void 0&&(typeof r.samplingRate!="number"||r.samplingRate<0||r.samplingRate>1))throw new Ee(T.INVALID_SAMPLING_RATE,"config");if(r.primaryScrollSelector!==void 0){if(typeof r.primaryScrollSelector!="string"||!r.primaryScrollSelector.trim())throw new E(T.INVALID_PRIMARY_SCROLL_SELECTOR,"config");if(r.primaryScrollSelector!=="window")try{document.querySelector(r.primaryScrollSelector)}catch{throw new E(`${T.INVALID_PRIMARY_SCROLL_SELECTOR_SYNTAX}: "${r.primaryScrollSelector}"`,"config")}}if(r.pageViewThrottleMs!==void 0&&(typeof r.pageViewThrottleMs!="number"||r.pageViewThrottleMs<0))throw new E(T.INVALID_PAGE_VIEW_THROTTLE,"config");if(r.clickThrottleMs!==void 0&&(typeof r.clickThrottleMs!="number"||r.clickThrottleMs<0))throw new E(T.INVALID_CLICK_THROTTLE,"config");if(r.maxSameEventPerMinute!==void 0&&(typeof r.maxSameEventPerMinute!="number"||r.maxSameEventPerMinute<=0))throw new E(T.INVALID_MAX_SAME_EVENT_PER_MINUTE,"config");if(r.sendIntervalMs!==void 0&&(!Number.isFinite(r.sendIntervalMs)||r.sendIntervalMs<1e3||r.sendIntervalMs>6e4))throw new E(T.INVALID_SEND_INTERVAL,"config");if(r.flushOnSpaNavigation!==void 0&&typeof r.flushOnSpaNavigation!="boolean")throw new E(`Invalid flushOnSpaNavigation type: ${typeof r.flushOnSpaNavigation}. Must be a boolean`,"config");if(r.flushOnPageHidden!==void 0&&typeof r.flushOnPageHidden!="boolean")throw new E(`Invalid flushOnPageHidden type: ${typeof r.flushOnPageHidden}. Must be a boolean`,"config");if(r.viewport!==void 0&&gs(r.viewport),r.webVitalsMode!==void 0){if(typeof r.webVitalsMode!="string")throw new E(`Invalid webVitalsMode type: ${typeof r.webVitalsMode}. Must be a string`,"config");const e=["all","needs-improvement","poor"];if(!e.includes(r.webVitalsMode))throw new E(`Invalid webVitalsMode: "${r.webVitalsMode}". Must be one of: ${e.join(", ")}`,"config")}if(r.webVitalsThresholds!==void 0){if(typeof r.webVitalsThresholds!="object"||r.webVitalsThresholds===null||Array.isArray(r.webVitalsThresholds))throw new E("webVitalsThresholds must be an object","config");const e=["LCP","FCP","CLS","INP","TTFB","LONG_TASK"];for(const[t,s]of Object.entries(r.webVitalsThresholds)){if(!e.includes(t))throw new E(`Invalid Web Vitals threshold key: "${t}". Must be one of: ${e.join(", ")}`,"config");if(typeof s!="number"||!Number.isFinite(s)||s<0)throw new E(`Invalid Web Vitals threshold value for ${t}: ${s}. Must be a non-negative finite number`,"config")}}}},gs=r=>{if(typeof r!="object"||r===null)throw new E(T.INVALID_VIEWPORT_CONFIG,"config");if(!r.elements||!Array.isArray(r.elements))throw new E(T.INVALID_VIEWPORT_ELEMENTS,"config");if(r.elements.length===0)throw new E(T.INVALID_VIEWPORT_ELEMENTS,"config");const e=new Set;for(const t of r.elements){if(!t.selector||typeof t.selector!="string"||!t.selector.trim())throw new E(T.INVALID_VIEWPORT_ELEMENT,"config");const s=t.selector.trim();if(e.has(s))throw new E(`Duplicate viewport selector found: "${s}". Each selector should appear only once.`,"config");if(e.add(s),t.id!==void 0&&(typeof t.id!="string"||!t.id.trim()))throw new E(T.INVALID_VIEWPORT_ELEMENT_ID,"config");if(t.name!==void 0&&(typeof t.name!="string"||!t.name.trim()))throw new E(T.INVALID_VIEWPORT_ELEMENT_NAME,"config")}if(r.threshold!==void 0&&(typeof r.threshold!="number"||r.threshold<0||r.threshold>1))throw new E(T.INVALID_VIEWPORT_THRESHOLD,"config");if(r.minDwellTime!==void 0&&(typeof r.minDwellTime!="number"||r.minDwellTime<0))throw new E(T.INVALID_VIEWPORT_MIN_DWELL_TIME,"config");if(r.cooldownPeriod!==void 0&&(typeof r.cooldownPeriod!="number"||r.cooldownPeriod<0))throw new E(T.INVALID_VIEWPORT_COOLDOWN_PERIOD,"config");if(r.maxTrackedElements!==void 0&&(typeof r.maxTrackedElements!="number"||r.maxTrackedElements<=0))throw new E(T.INVALID_VIEWPORT_MAX_TRACKED_ELEMENTS,"config")},Es=r=>{if(r){if(r.tracelog&&(!r.tracelog.projectId||typeof r.tracelog.projectId!="string"||r.tracelog.projectId.trim()===""))throw new N(T.INVALID_TRACELOG_PROJECT_ID,"config");if(r.custom){if(!r.custom.collectApiUrl||typeof r.custom.collectApiUrl!="string"||r.custom.collectApiUrl.trim()==="")throw new N(T.INVALID_CUSTOM_API_URL,"config");if(r.custom.allowHttp!==void 0&&typeof r.custom.allowHttp!="boolean")throw new N("allowHttp must be a boolean","config");const e=r.custom.collectApiUrl.trim();if(!e.startsWith("http://")&&!e.startsWith("https://"))throw new N('Custom API URL must start with "http://" or "https://"',"config");if(!(r.custom.allowHttp??!1)&&e.startsWith("http://"))throw new N("Custom API URL must use HTTPS in production. Set allowHttp: true in integration config to allow HTTP (not recommended)","config");if(r.custom.fetchCredentials!==void 0&&!["include","same-origin","omit"].includes(r.custom.fetchCredentials))throw new N('fetchCredentials must be "include", "same-origin", or "omit"',"config")}if(r.tracelog?.shopify!==void 0&&typeof r.tracelog.shopify!="boolean")throw new N("tracelog.shopify must be a boolean","config")}},Ss=r=>{ms(r);const e={...r??{},sessionTimeout:r?.sessionTimeout??9e5,globalMetadata:r?.globalMetadata??{},sensitiveQueryParams:r?.sensitiveQueryParams??[],errorSampling:r?.errorSampling??st,samplingRate:r?.samplingRate??1,pageViewThrottleMs:r?.pageViewThrottleMs??1e3,clickThrottleMs:r?.clickThrottleMs??300,maxSameEventPerMinute:r?.maxSameEventPerMinute??60,sendIntervalMs:r?.sendIntervalMs??1e4,flushOnSpaNavigation:r?.flushOnSpaNavigation??!1,flushOnPageHidden:r?.flushOnPageHidden??!0};return e.integrations?.custom&&(e.integrations.custom={...e.integrations.custom,allowHttp:e.integrations.custom.allowHttp??!1}),e.viewport&&(e.viewport={...e.viewport,threshold:e.viewport.threshold??.5,minDwellTime:e.viewport.minDwellTime??2e3,cooldownPeriod:e.viewport.cooldownPeriod??6e4,maxTrackedElements:e.viewport.maxTrackedElements??100}),e},Le=(r,e=new Set)=>{if(r==null)return!0;const t=typeof r;return t==="string"||t==="number"||t==="boolean"?!0:t==="function"||t==="symbol"||t==="bigint"||e.has(r)?!1:(e.add(r),Array.isArray(r)?r.every(s=>Le(s,e)):t==="object"?Object.values(r).every(s=>Le(s,e)):!1)},ps=r=>typeof r!="object"||r===null?!1:Le(r),dt=r=>{if(typeof r!="object"||r===null||Array.isArray(r))return;const e={};for(const[t,s]of Object.entries(r))typeof s=="string"&&(e[t]=s);return Object.keys(e).length>0?e:void 0},Ts=r=>typeof r!="string"?{valid:!1,error:"Event name must be a string"}:r.length===0?{valid:!1,error:"Event name cannot be empty"}:r.length>120?{valid:!1,error:"Event name is too long (max 120 characters)"}:r.includes("<")||r.includes(">")||r.includes("&")?{valid:!1,error:"Event name contains invalid characters"}:["constructor","prototype","__proto__","eval","function","var","let","const"].includes(r.toLowerCase())?{valid:!1,error:"Event name cannot be a reserved word"}:{valid:!0},ut=(r,e,t)=>{const s=fs(e),n=t&&t==="customEvent"?`${t} "${r}" metadata error`:`${r} metadata error`;if(!ps(s))return{valid:!1,error:`${n}: object has invalid types. Valid types are string, number, boolean or string arrays.`};let i;try{i=JSON.stringify(s)}catch{return{valid:!1,error:`${n}: object contains circular references or cannot be serialized.`}}if(new TextEncoder().encode(i).byteLength>49152)return{valid:!1,error:`${n}: object is too large (max ${49152/1024} KB).`};if(Object.keys(s).length>100)return{valid:!1,error:`${n}: object has too many keys (max 100 keys).`};for(const[d,c]of Object.entries(s)){if(Array.isArray(c)){if(c.length>500)return{valid:!1,error:`${n}: array property "${d}" is too large (max 500 items).`};for(const m of c)if(typeof m=="string"&&m.length>500)return{valid:!1,error:`${n}: array property "${d}" contains strings that are too long (max 500 characters).`}}if(typeof c=="string"&&c.length>1e3)return{valid:!1,error:`${n}: property "${d}" is too long (max 1000 characters).`}}return{valid:!0,sanitizedMetadata:s}},ht=(r,e,t)=>{if(Array.isArray(e)){const s=[],n=t&&t==="customEvent"?`${t} "${r}" metadata error`:`${r} metadata error`;for(let i=0;i<e.length;i++){const o=e[i];if(typeof o!="object"||o===null||Array.isArray(o))return{valid:!1,error:`${n}: array item at index ${i} must be an object.`};const l=ut(r,o,t);if(!l.valid)return{valid:!1,error:`${n}: array item at index ${i} is invalid: ${l.error}`};l.sanitizedMetadata&&s.push(l.sanitizedMetadata)}return{valid:!0,sanitizedMetadata:s}}return ut(r,e,t)},_s=(r,e)=>{const t=Ts(r);if(!t.valid)return a("error","Event name validation failed",{data:{eventName:r,error:t.error}}),t;if(!e)return{valid:!0};const s=ht(r,e,"customEvent");return s.valid||a("error","Event metadata validation failed",{data:{eventName:r,error:s.error}}),s};class Is{listeners=new Map;on(e,t){this.listeners.has(e)||this.listeners.set(e,[]),this.listeners.get(e).push(t)}off(e,t){const s=this.listeners.get(e);if(s){const n=s.indexOf(t);n>-1&&s.splice(n,1)}}emit(e,t){const s=this.listeners.get(e);s&&s.forEach(n=>{n(t)})}removeAllListeners(){this.listeners.clear()}}function ft(r,e,t){try{const s=e(r);return s===null?null:typeof s=="object"&&s!==null&&"type"in s?s:(a("warn",`beforeSend transformer returned invalid data, using original [${t}]`),r)}catch(s){return a("error",`beforeSend transformer threw error, using original event [${t}]`,{error:s,visibility:"critical"}),r}}function vs(r,e,t){return r.map(s=>ft(s,e,t)).filter(s=>s!==null)}function mt(r,e,t){try{const s=e(r);return s===null?(a("debug",`Batch filtered by beforeBatch transformer [${t}]`,{data:{eventCount:r.events.length}}),null):typeof s=="object"&&s!==null&&Array.isArray(s.events)?s:(a("warn",`beforeBatch transformer returned invalid data, using original [${t}]`,{data:{eventCount:r.events.length}}),r)}catch(s){return a("error",`beforeBatch transformer threw error, using original batch [${t}]`,{error:s,data:{eventCount:r.events.length},visibility:"critical"}),r}}const Ae={config:{}};class y{get(e){return Ae[e]}set(e,t){Ae[e]=t}getState(){return{...Ae}}}class gt extends y{storeManager;integrationId;apiUrl;transformers;staticHeaders;customHeadersProvider;lastPermanentErrorLog=null;recoveryInProgress=!1;lastMetadataTimestamp=0;fetchCredentials;pendingControllers=new Set;consecutiveNetworkFailures=0;circuitOpenedAt=0;rateLimitedUntil=0;rateLimitStorageKeyAtArm=null;constructor(e,t,s,n={},i={},o,l="include"){if(super(),t&&!s||!t&&s)throw new Error("SenderManager: integrationId and apiUrl must either both be provided or both be undefined");this.storeManager=e,this.integrationId=t,this.apiUrl=s,this.transformers=n,this.staticHeaders=i,this.customHeadersProvider=o,this.fetchCredentials=l,this.rateLimitedUntil=this.loadRateLimitCooldown()}getIntegrationId(){return this.integrationId}setCustomHeadersProvider(e){this.customHeadersProvider=e}removeCustomHeadersProvider(){this.customHeadersProvider=void 0}getCustomHeaders(){if(this.integrationId!=="custom")return{};let e={};if(this.customHeadersProvider)try{const t=this.customHeadersProvider();typeof t=="object"&&t!==null&&!Array.isArray(t)?e=t:a("warn","Custom headers provider returned invalid value, expected object",{data:{received:typeof t}})}catch(t){a("warn","Custom headers provider threw an error, using static headers only",{error:t})}return{...this.staticHeaders,...e}}getQueueStorageKey(){const e=this.get("userId")||"anonymous",t=Ot(e);return this.integrationId?`${t}:${this.integrationId}`:t}getRateLimitStorageKey(){const e=this.get("userId")||"anonymous",t=Pt(e);return this.integrationId?`${t}:${this.integrationId}`:t}getActiveRateLimitKey(){return this.rateLimitStorageKeyAtArm??this.getRateLimitStorageKey()}armRateLimitCooldown(e){this.rateLimitedUntil=e,this.rateLimitStorageKeyAtArm=this.getRateLimitStorageKey(),this.persistRateLimitCooldown(e)}loadRateLimitCooldown(){const e=this.getRateLimitStorageKey();try{const t=this.storeManager.getItem(e);if(!t)return 0;const s=Number(t);return!Number.isFinite(s)||s<=Date.now()?(this.storeManager.removeItem(e),0):(this.rateLimitStorageKeyAtArm=e,s)}catch{return 0}}persistRateLimitCooldown(e){const t=this.getActiveRateLimitKey();try{const s=this.storeManager.getItem(t);if(s){const n=Number(s);if(Number.isFinite(n)&&n>=e)return}this.storeManager.setItem(t,String(e))}catch{}}clearRateLimitCooldown(){const e=this.getActiveRateLimitKey();try{const t=this.storeManager.getItem(e);if(t){const s=Number(t);if(Number.isFinite(s)&&s>Date.now()){this.rateLimitedUntil=s;return}}this.storeManager.removeItem(e)}catch{}this.rateLimitedUntil=0,this.rateLimitStorageKeyAtArm=null}isRateLimited(){return this.rateLimitedUntil===0&&(this.rateLimitedUntil=this.loadRateLimitCooldown()),!(this.rateLimitedUntil===0||Date.now()>=this.rateLimitedUntil&&(this.clearRateLimitCooldown(),this.rateLimitedUntil===0))}sendEventsQueueSync(e){if(this.shouldSkipSend())return!0;if(this.isRateLimited()){a("debug",`Rate-limit cooldown active, skipping sync send${this.integrationId?` [${this.integrationId}]`:""}`,{data:{cooldownRemainingMs:this.rateLimitedUntil-Date.now(),events:e.events.length}});const t=this.ensureBatchMetadata(e),s=this.getPersistedData(),n=typeof s?.recoveryFailures=="number"&&Number.isFinite(s.recoveryFailures)?s.recoveryFailures:0;return this.persistEventsWithFailureCount(t,n,!0),!1}return this.apiUrl?.includes(F.Fail)?(a("warn",`Fail mode: simulating network failure (sync)${this.integrationId?` [${this.integrationId}]`:""}`,{data:{events:e.events.length}}),!1):this.apiUrl?.includes(F.Localhost)?(a("debug",`Success mode: simulating successful send (sync)${this.integrationId?` [${this.integrationId}]`:""}`,{data:{events:e.events.length}}),!0):this.sendQueueSyncInternal(e)}async sendEventsQueue(e,t){const s=this.ensureBatchMetadata(e);try{const n=await this.send(s);return n?(this.clearPersistedEvents(),t?.onSuccess?.(s.events.length,s.events,s)):(this.persistEvents(s),t?.onFailure?.()),n}catch(n){return n instanceof R?(this.logPermanentError("Permanent error, not retrying",n),this.clearPersistedEvents(),t?.onFailure?.(),!1):(this.persistEvents(s),t?.onFailure?.(),!1)}}async recoverPersistedEvents(e){if(this.recoveryInProgress){a("debug","Recovery already in progress, skipping duplicate attempt");return}this.recoveryInProgress=!0;let t=null,s=0;try{const n=this.getPersistedData();if(!n||!this.isDataRecent(n)||n.events.length===0){this.clearPersistedEvents();return}const i=n.recoveryFailures;if(s=typeof i=="number"&&Number.isFinite(i)&&i>=0?i:0,s>=3){a("debug",`Discarding persisted events after ${s} failed recovery attempts${this.integrationId?` [${this.integrationId}]`:""}`),this.clearPersistedEvents(),e?.onFailure?.();return}if(this.isRateLimited()){a("debug",`Rate-limit cooldown active, deferring recovery${this.integrationId?` [${this.integrationId}]`:""}`,{data:{cooldownRemainingMs:this.rateLimitedUntil-Date.now()}}),e?.onFailure?.();return}t=this.ensureBatchMetadata(this.createRecoveryBody(n)),await this.send(t)?(this.clearPersistedEvents(),e?.onSuccess?.(n.events.length,n.events,t)):(this.persistEventsWithFailureCount(t,s+1,!0),e?.onFailure?.())}catch(n){if(n instanceof R){this.logPermanentError("Permanent error during recovery, clearing persisted events",n),this.clearPersistedEvents(),e?.onFailure?.();return}a("error","Failed to recover persisted events",{error:n}),t&&this.persistEventsWithFailureCount(t,s+1,!0),e?.onFailure?.()}finally{this.recoveryInProgress=!1}}stop(){}applyBeforeSendTransformer(e){if(this.integrationId==="saas")return e;const t=this.transformers.beforeSend;if(!t)return e;const s=vs(e.events,t,this.integrationId||"SenderManager");return s.length===0?null:{...e,events:s}}applyBeforeBatchTransformer(e){if(this.integrationId==="saas")return e;const t=this.transformers.beforeBatch;return t?mt(e,t,this.integrationId||"SenderManager"):e}async backoffDelay(e){const t=100*Math.pow(2,e),s=Math.random()*100,n=t+s;return new Promise(i=>setTimeout(i,n))}async send(e){if(this.shouldSkipSend())return this.simulateSuccessfulSend();const t=this.applyBeforeSendTransformer(e);if(!t)return!0;const s=this.applyBeforeBatchTransformer(t);if(!s)return!0;const n=this.ensureBatchMetadata(s,e._metadata?.idempotency_token);if(this.apiUrl?.includes(F.Fail))return a("debug",`Fail mode: simulating network failure${this.integrationId?` [${this.integrationId}]`:""}`,{data:{events:n.events.length}}),!1;if(this.apiUrl?.includes(F.Localhost))return a("debug",`Success mode: simulating successful send${this.integrationId?` [${this.integrationId}]`:""}`,{data:{events:n.events.length}}),!0;if(this.isRateLimited())return a("debug",`Rate-limit cooldown active, skipping send${this.integrationId?` [${this.integrationId}]`:""}`,{data:{cooldownRemainingMs:this.rateLimitedUntil-Date.now(),events:n.events.length}}),!1;if(this.consecutiveNetworkFailures>=3){const c=Date.now()-this.circuitOpenedAt;if(c<12e4)return a("debug",`Network circuit open, skipping send${this.integrationId?` [${this.integrationId}]`:""}`,{data:{consecutiveNetworkFailures:this.consecutiveNetworkFailures,cooldownRemainingMs:12e4-c}}),!1}const{url:i,payload:o}=this.prepareRequest(n);let l=!0,d=!1;for(let c=1;c<=3;c++)try{return(await this.sendWithTimeout(i,o)).ok?(c>1&&a("info",`Send succeeded after ${c-1} retry attempt(s)${this.integrationId?` [${this.integrationId}]`:""}`,{data:{events:n.events.length,attempt:c}}),this.consecutiveNetworkFailures=0,this.circuitOpenedAt=0,!0):!1}catch(m){const S=c===3;if(m instanceof R)throw this.consecutiveNetworkFailures=0,this.circuitOpenedAt=0,m;if(m instanceof K){this.consecutiveNetworkFailures=0,this.circuitOpenedAt=0,l=!1,d=!0,this.armRateLimitCooldown(Date.now()+6e4),a("warn",`Rate limited, skipping retries${this.integrationId?` [${this.integrationId}]`:""}`,{data:{events:e.events.length,attempt:c,cooldownMs:6e4}});break}if(m instanceof z||(l=!1),m instanceof TypeError||(d=!0),a(S?"error":"warn",`Send attempt ${c} failed${this.integrationId?` [${this.integrationId}]`:""}${S?" (all retries exhausted)":", will retry"}`,{error:m,data:{events:e.events.length,url:i.replace(/\/\/[^/]+/,"//[DOMAIN]"),attempt:c,maxAttempts:3}}),!S){await this.backoffDelay(c);continue}return l?(a("debug",`All retry attempts timed out, preserving batch for retry${this.integrationId?` [${this.integrationId}]`:""}`,{data:{events:n.events.length}}),!1):(d?(this.consecutiveNetworkFailures=0,this.circuitOpenedAt=0):(this.consecutiveNetworkFailures=Math.min(this.consecutiveNetworkFailures+1,3),this.consecutiveNetworkFailures>=3&&(this.circuitOpenedAt=Date.now())),!1)}return!1}async sendWithTimeout(e,t){const s=new AbortController;this.pendingControllers.add(s);let n=!1;const i=setTimeout(()=>{n=!0,s.abort()},15e3);try{const o=this.getCustomHeaders(),l=await fetch(e,{method:"POST",body:t,keepalive:!0,credentials:this.fetchCredentials,signal:s.signal,headers:{...o,"Content-Type":"application/json"}});if(!l.ok){if(l.status>=400&&l.status<500&&l.status!==408&&l.status!==429){const c=await this.readTraceLogErrorCode(l),m=c?`HTTP ${l.status}: ${l.statusText} (${c})`:`HTTP ${l.status}: ${l.statusText}`;throw new R(m,l.status,c)}throw l.status===429?new K(`HTTP 429: ${l.statusText}`):new Error(`HTTP ${l.status}: ${l.statusText}`)}return l}catch(o){throw o instanceof R?o:n?new z("Request timed out"):o}finally{clearTimeout(i),this.pendingControllers.delete(s)}}async readTraceLogErrorCode(e){try{const t=await e.clone().json();if(typeof t.code=="string"&&t.code.length>0&&t.code.length<=es)return t.code}catch{}}sendQueueSyncInternal(e){const t=this.ensureBatchMetadata(e),s=this.applyBeforeSendTransformer(t);if(!s)return!0;const n=this.applyBeforeBatchTransformer(s);if(!n)return!0;const i=this.ensureBatchMetadata(n,t._metadata?.idempotency_token),{url:o,payload:l}=this.prepareRequest(i);if(l.length>65536)return a("warn",`Payload exceeds sendBeacon limit, persisting for recovery${this.integrationId?` [${this.integrationId}]`:""}`,{data:{size:l.length,limit:65536,events:i.events.length}}),this.persistEvents(t),!1;const d=new Blob([l],{type:"application/json"});if(!this.isSendBeaconAvailable())return a("warn",`sendBeacon not available, persisting events for recovery${this.integrationId?` [${this.integrationId}]`:""}`),this.persistEvents(t),!1;const c=navigator.sendBeacon(o,d);return c||(a("warn",`sendBeacon rejected request, persisting events for recovery${this.integrationId?` [${this.integrationId}]`:""}`),this.persistEvents(t)),c}prepareRequest(e){let t=Date.now();t<this.lastMetadataTimestamp&&(t=this.lastMetadataTimestamp),this.lastMetadataTimestamp=t;const s={...e,_metadata:{...e._metadata,idempotency_token:e._metadata?.idempotency_token??this.computeContentToken(e),referer:typeof window<"u"?window.location.href:void 0,timestamp:t,client_version:ns}};return{url:this.apiUrl||"",payload:JSON.stringify(s)}}ensureBatchMetadata(e,t){const s=e._metadata?.idempotency_token??t??this.computeContentToken(e);return e._metadata?.idempotency_token===s?e:{...e,_metadata:{...e._metadata,idempotency_token:s}}}computeContentToken(e){const t=e.events.map(i=>i.id).sort().join(","),s=`${e.user_id}|${e.session_id}|${t}`;let n=2166136261;for(let i=0;i<s.length;i++)n^=s.charCodeAt(i),n=Math.imul(n,16777619)>>>0;return n.toString(16).padStart(8,"0")}getPersistedData(){try{const e=this.getQueueStorageKey(),t=this.storeManager.getItem(e);if(t)return JSON.parse(t)}catch(e){a("debug",`Failed to parse persisted data${this.integrationId?` [${this.integrationId}]`:""}`,{error:e}),this.clearPersistedEvents()}return null}isDataRecent(e){return!e.timestamp||typeof e.timestamp!="number"?!1:(Date.now()-e.timestamp)/(1e3*60*60)<2}createRecoveryBody(e){const{timestamp:t,recoveryFailures:s,...n}=e;return n}persistEvents(e){const t=this.getPersistedData(),s=typeof t?.recoveryFailures=="number"&&Number.isFinite(t.recoveryFailures)?t.recoveryFailures:0;return this.persistEventsWithFailureCount(e,s)}persistEventsWithFailureCount(e,t,s=!1){try{const n=this.getPersistedData();if(!s&&n&&n.timestamp){const l=Date.now()-n.timestamp;if(l<1e3)return a("debug",`Skipping persistence, another tab recently persisted events${this.integrationId?` [${this.integrationId}]`:""}`,{data:{timeSinceExisting:l}}),!0}const i={...e,timestamp:Date.now(),...t>0&&{recoveryFailures:t}},o=this.getQueueStorageKey();return this.storeManager.setItem(o,JSON.stringify(i)),!!this.storeManager.getItem(o)}catch(n){return a("debug",`Failed to persist events${this.integrationId?` [${this.integrationId}]`:""}`,{error:n}),!1}}clearPersistedEvents(){try{const e=this.getQueueStorageKey();this.storeManager.removeItem(e)}catch(e){a("debug",`Failed to clear persisted events${this.integrationId?` [${this.integrationId}]`:""}`,{error:e})}}shouldSkipSend(){return!this.apiUrl}async simulateSuccessfulSend(){const e=Math.random()*400+100;return await new Promise(t=>setTimeout(t,e)),!0}isSendBeaconAvailable(){return typeof navigator<"u"&&typeof navigator.sendBeacon=="function"}logPermanentError(e,t){const s=Date.now(),n=`${t.statusCode??"unknown"}:${t.responseCode??""}`;(!this.lastPermanentErrorLog||this.lastPermanentErrorLog.key!==n||s-this.lastPermanentErrorLog.timestamp>=Zt)&&(a("error",`${e}${this.integrationId?` [${this.integrationId}]`:""}`,{data:{status:t.statusCode,code:t.responseCode,message:t.message}}),this.lastPermanentErrorLog={key:n,timestamp:s})}}class ys extends y{bootTime;bootTimestamp;hasPerformanceNow;lastClockSkewCheck=0;detectedSkew=0;constructor(){if(super(),typeof window>"u"){this.hasPerformanceNow=!1,this.bootTime=0,this.bootTimestamp=0;return}this.hasPerformanceNow=typeof performance<"u"&&typeof performance.now=="function",this.hasPerformanceNow?(this.bootTime=performance.now(),this.bootTimestamp=Date.now(),a("debug","TimeManager initialized with monotonic clock",{data:{bootTime:this.bootTime.toFixed(3),bootTimestamp:this.bootTimestamp}})):(this.bootTime=0,this.bootTimestamp=Date.now(),a("debug","performance.now() not available, falling back to Date.now()"))}now(){if(!this.hasPerformanceNow)return Date.now();const e=performance.now()-this.bootTime;return Math.round(this.bootTimestamp+e)}getClockSkew(){if(!this.hasPerformanceNow)return 0;const e=Date.now();if(e-this.lastClockSkewCheck<5e3)return this.detectedSkew;this.lastClockSkewCheck=e;const t=this.now(),s=Date.now();return this.detectedSkew=s-t,Math.abs(this.detectedSkew)>3e4&&a("warn","Significant clock skew detected",{data:{skewMs:this.detectedSkew,skewMinutes:(this.detectedSkew/1e3/60).toFixed(2),monotonicTime:new Date(t).toISOString(),systemTime:new Date(s).toISOString()}}),this.detectedSkew}validateTimestamp(e){const s=this.now(),n=e-s;return n>12e4?{valid:!1,error:`Timestamp is ${(n/1e3/60).toFixed(2)} minutes in the future (max allowed: 2 minutes)`}:{valid:!0}}getBootInfo(){return{bootTime:this.bootTime,bootTimestamp:this.bootTimestamp,hasPerformanceNow:this.hasPerformanceNow,clockSkew:this.getClockSkew()}}}const ws=new Set(Object.values(u));class bs extends y{dataSenders;emitter;transformers;timeManager;recentEventFingerprints=new Map;perEventRateLimits=new Map;eventsQueue=[];pendingEventsBuffer=[];sendTimeoutId=null;sendInProgress=!1;consecutiveSendFailures=0;rateLimitCounter=0;rateLimitWindowStart=0;lastSessionId=null;pendingSyncFlush=!1;sessionEventCounts={total:0,[u.CLICK]:0,[u.PAGE_VIEW]:0,[u.CUSTOM]:0,[u.VIEWPORT_VISIBLE]:0,[u.SCROLL]:0};saveSessionCountsDebounced=null;constructor(e,t=null,s={},n={},i,o="include"){super(),this.emitter=t,this.transformers=s,this.timeManager=new ys,this.dataSenders=[];const l=this.get("collectApiUrls");l?.saas&&this.dataSenders.push(new gt(e,"saas",l.saas,s)),l?.custom&&this.dataSenders.push(new gt(e,"custom",l.custom,s,n,i,o)),this.saveSessionCountsDebounced=this.debounce(d=>{this.saveSessionCounts(d)},500),this.cleanupExpiredSessionCounts()}async recoverPersistedEvents(){const e=this.dataSenders.map(async t=>t.recoverPersistedEvents({onSuccess:(s,n,i)=>{if(n&&n.length>0){const o=n.map(l=>l.id);this.removeProcessedEvents(o),i&&this.emitEventsQueue(i)}},onFailure:()=>{a("debug","Failed to recover persisted events")}}));await Promise.allSettled(e)}track({type:e,page_url:t,from_page_url:s,scroll_data:n,click_data:i,custom_event:o,web_vitals:l,error_data:d,viewport_data:c,page_view:m}){if(!e){a("error","Event type is required - event will be ignored");return}if(!ws.has(e)){a("error","Invalid event type - event will be ignored",{data:{type:e}});return}const S=this.get("sessionId");if(!S){this.pendingEventsBuffer.length>=100&&(this.pendingEventsBuffer.shift(),a("debug","Pending events buffer full - dropping oldest event",{data:{maxBufferSize:100}})),this.pendingEventsBuffer.push({type:e,page_url:t,from_page_url:s,scroll_data:n,click_data:i,custom_event:o,web_vitals:l,error_data:d,viewport_data:c,page_view:m});return}this.lastSessionId!==S&&(this.lastSessionId=S,this.sessionEventCounts=this.loadSessionCounts(S));const g=e===u.SESSION_START;if(g&&a("debug","Processing SESSION_START event",{data:{sessionId:S}}),!g&&!this.checkRateLimit())return;const p=e;if(!g){if(this.sessionEventCounts.total>=1e3){a("warn","Session event limit reached",{data:{type:p,total:this.sessionEventCounts.total,limit:1e3}});return}const v=this.getTypeLimitForEvent(p);if(v){const xe=this.sessionEventCounts[p];if(xe!==void 0&&xe>=v){a("warn","Session event type limit reached",{data:{type:p,count:xe,limit:v}});return}}}if(p===u.CUSTOM&&o?.name){const v=this.get("config")?.maxSameEventPerMinute??60;if(!this.checkPerEventRateLimit(o.name,v))return}const $e=p===u.SESSION_START,fe=t||this.get("pageUrl"),G=this.buildEventPayload({type:p,page_url:fe,from_page_url:s,scroll_data:n,click_data:i,custom_event:o,web_vitals:l,error_data:d,viewport_data:c,page_view:m});if(G&&!(!g&&!this.shouldSample())){if($e){const v=this.get("sessionId");if(!v){a("error","Session start event requires sessionId - event will be ignored");return}if(this.get("hasStartSession")){a("debug","Duplicate session_start detected",{data:{sessionId:v}});return}this.set("hasStartSession",!0)}if(!this.isDuplicateEvent(G)){if(this.get("mode")===Y.QA){if(p===u.CUSTOM&&o){a("info",`Custom Event: ${o.name}`,{visibility:"qa",data:{name:o.name,...o.metadata&&{metadata:o.metadata}}}),this.emitEvent(G);return}if(p===u.VIEWPORT_VISIBLE&&c){const v=c.name||c.id||c.selector;a("info",`Viewport Visible: ${v}`,{visibility:"qa",data:{selector:c.selector,...c.name&&{name:c.name},...c.id&&{id:c.id},visibilityRatio:c.visibilityRatio,dwellTime:c.dwellTime}}),this.emitEvent(G);return}}if(this.addToQueue(G),!g){this.sessionEventCounts.total++,this.sessionEventCounts[p]!==void 0&&this.sessionEventCounts[p]++;const v=this.get("sessionId");v&&this.saveSessionCountsDebounced&&this.saveSessionCountsDebounced(v)}}}}stop(){this.clearSendTimeout(),this.sendInProgress=!1,this.pendingSyncFlush=!1,this.consecutiveSendFailures=0;const e=this.get("sessionId");e&&this.saveSessionCounts(e),this.eventsQueue=[],this.pendingEventsBuffer=[],this.recentEventFingerprints.clear(),this.rateLimitCounter=0,this.rateLimitWindowStart=0,this.perEventRateLimits.clear(),this.sessionEventCounts={total:0,[u.CLICK]:0,[u.PAGE_VIEW]:0,[u.CUSTOM]:0,[u.VIEWPORT_VISIBLE]:0,[u.SCROLL]:0},this.lastSessionId=null,this.set("hasStartSession",!1),this.dataSenders.forEach(t=>{t.stop()})}async flushImmediately(){return this.flushEvents(!1)}flushImmediatelySync(){return this.flushEvents(!0)}setCustomHeadersProvider(e){for(const t of this.dataSenders)t.getIntegrationId()==="custom"&&t.setCustomHeadersProvider(e)}removeCustomHeadersProvider(){for(const e of this.dataSenders)e.getIntegrationId()==="custom"&&e.removeCustomHeadersProvider()}getQueueLength(){return this.eventsQueue.length}getQueueEvents(){return this.eventsQueue.map(({_session_id:e,...t})=>t)}async flushQueue(){await this.flushImmediately()}clearQueue(){this.eventsQueue=[]}flushPendingEvents(){if(this.pendingEventsBuffer.length===0)return;if(!this.get("sessionId")){a("debug","Cannot flush pending events: session not initialized - keeping in buffer",{data:{bufferedEventCount:this.pendingEventsBuffer.length}});return}const t=[...this.pendingEventsBuffer];this.pendingEventsBuffer=[],t.forEach(s=>{this.track(s)})}clearSendTimeout(){this.sendTimeoutId!==null&&(clearTimeout(this.sendTimeoutId),this.sendTimeoutId=null)}isSuccessfulResult(e){return e.status==="fulfilled"&&e.value===!0}groupQueuedEventsBySession(){const e=new Map,t=[];for(const s of this.eventsQueue){if(!s._session_id){a("debug","Queued event missing _session_id, dropping",{data:{eventId:s.id,type:s.type}}),t.push(s.id);continue}const n=e.get(s._session_id);n?n.push(s):e.set(s._session_id,[s])}return t.length>0&&this.removeProcessedEvents(t),e}buildBatchesWithIds(){const e=this.groupQueuedEventsBySession();if(e.size===0)return[];const t=[];for(const[s,n]of e)t.push({batch:this.buildBatchFromGroup(s,n),eventIds:n.map(i=>i.id)});return t}flushEvents(e){if(this.eventsQueue.length===0)return e?!0:Promise.resolve(!0);if(!e&&this.sendInProgress)return a("debug","Async flush skipped: send already in progress"),Promise.resolve(!1);const t=this.buildBatchesWithIds();if(t.length===0)return e?!0:Promise.resolve(!0);if(this.dataSenders.length===0){for(const{batch:s,eventIds:n}of t)this.removeProcessedEvents(n),this.emitEventsQueue(s);return this.clearSendTimeout(),e?!0:Promise.resolve(!0)}if(e&&this.sendInProgress){const s=t.reduce((n,i)=>n+i.eventIds.length,0);return this.pendingSyncFlush=!0,a("debug","Sync flush deferred: async send in-flight, will retry on settle",{data:{eventCount:s}}),!1}if(e){const s=t.map(({batch:n,eventIds:i})=>this.sendBatchSync(n,i));return this.settleSendTimeout(),s.some(Boolean)}return this.sendInProgress=!0,(async()=>{try{const s=await Promise.all(t.map(async({batch:n,eventIds:i})=>this.sendBatchAsync(n,i)));return this.settleSendTimeout(),s.some(Boolean)}finally{this.sendInProgress=!1,this.drainPendingSyncFlush()}})()}settleSendTimeout(){this.eventsQueue.length===0?this.clearSendTimeout():this.scheduleSendTimeout()}drainPendingSyncFlush(){this.pendingSyncFlush&&(this.pendingSyncFlush=!1,this.flushImmediatelySync())}sendBatchSync(e,t){const n=this.dataSenders.map(i=>i.sendEventsQueueSync(e)).some(i=>i);return n?(this.removeProcessedEvents(t),this.emitEventsQueue(e)):a("debug","Sync send complete failure, events kept in queue for retry",{data:{eventCount:t.length,sessionId:e.session_id}}),n}async sendBatchAsync(e,t){const s=this.dataSenders.map(async o=>o.sendEventsQueue(e,{onSuccess:()=>{},onFailure:()=>{}})),n=await Promise.allSettled(s),i=n.some(o=>this.isSuccessfulResult(o));if(i){this.removeProcessedEvents(t),this.emitEventsQueue(e);const o=n.filter(l=>!this.isSuccessfulResult(l)).length;o>0&&a("debug","Async send completed with some failures, removed from queue and persisted per-integration",{data:{eventCount:t.length,failedCount:o,sessionId:e.session_id}})}else a("debug","Async send complete failure, events kept in queue for retry",{data:{eventCount:t.length,sessionId:e.session_id}});return i}async sendEventsQueue(){if(!(this.eventsQueue.length===0||this.sendInProgress)){this.sendInProgress=!0;try{const e=this.buildBatchesWithIds();if(e.length===0)return;if(this.dataSenders.length===0){for(const{batch:n}of e)this.emitEventsQueue(n);return}(await Promise.all(e.map(async({batch:n,eventIds:i})=>this.sendBatchAsync(n,i)))).some(Boolean)?this.consecutiveSendFailures=0:this.consecutiveSendFailures=Math.min(this.consecutiveSendFailures+1,5),this.eventsQueue.length===0?this.clearSendTimeout():this.scheduleSendTimeout()}finally{this.sendInProgress=!1,this.drainPendingSyncFlush()}}}buildBatchFromGroup(e,t){const s=new Map,n=[];for(const g of t){const p=this.createEventSignature(g);s.has(p)||n.push(p),s.set(p,g)}const i=n.map(g=>s.get(g)).filter(g=>!!g).sort((g,p)=>g.type===u.SESSION_START&&p.type!==u.SESSION_START?-1:p.type===u.SESSION_START&&g.type!==u.SESSION_START?1:g.timestamp-p.timestamp).map(({_session_id:g,...p})=>p),o=this.get("config")?.globalMetadata,l=this.get("identity");let d={user_id:this.get("userId"),session_id:e,device:this.get("device"),events:i,...o&&{global_metadata:o},...l&&{identify:l}};const c=this.get("collectApiUrls"),m=!!(c?.custom||c?.saas),S=this.transformers.beforeBatch;if(!m&&S){const g=mt(d,S,"EventManager");g!==null&&(d=g)}return d}buildEventPayload(e){const t=this.get("sessionId");if(!t)return a("error","buildEventPayload reached without sessionId — event dropped",{data:{type:e.type},visibility:"critical"}),null;const s=e.page_url??this.get("pageUrl"),n=typeof s=="string"&&s.length>0?s:"unknown",i=this.timeManager.now(),o=this.timeManager.validateTimestamp(i);o.valid||a("warn","Event timestamp validation failed",{data:{type:e.type,error:o.error}});const l=this.get("sessionReferrer"),d=this.get("sessionUtm");let c={id:ds(),type:e.type,page_url:n,timestamp:i,...l&&{referrer:l},...e.from_page_url&&{from_page_url:e.from_page_url},...e.scroll_data&&{scroll_data:e.scroll_data},...e.click_data&&{click_data:e.click_data},...e.custom_event&&{custom_event:e.custom_event},...e.web_vitals&&{web_vitals:e.web_vitals},...e.error_data&&{error_data:e.error_data},...e.viewport_data&&{viewport_data:e.viewport_data},...e.page_view&&{page_view:e.page_view},...d&&{utm:d}};const m=this.get("collectApiUrls"),S=!!m?.custom,g=!!m?.saas,p=S||g,$e=S&&g,fe=this.transformers.beforeSend;if(fe&&(!p||S&&!$e)){const v=ft(c,fe,"EventManager");if(v===null)return null;c=v}return{...c,_session_id:t}}isDuplicateEvent(e){const t=Date.now(),s=this.createEventFingerprint(e),n=this.recentEventFingerprints.get(s);return n&&t-n<1e3?(this.recentEventFingerprints.set(s,t),!0):(this.recentEventFingerprints.set(s,t),this.recentEventFingerprints.size>1500&&this.pruneOldFingerprints(),this.recentEventFingerprints.size>3e3&&(this.recentEventFingerprints.clear(),this.recentEventFingerprints.set(s,t),a("debug","Event fingerprint cache exceeded hard limit, cleared",{data:{hardLimit:3e3}})),!1)}pruneOldFingerprints(){const e=Date.now(),t=1e3*10;for(const[s,n]of this.recentEventFingerprints.entries())e-n>t&&this.recentEventFingerprints.delete(s);a("debug","Pruned old event fingerprints",{data:{remaining:this.recentEventFingerprints.size,cutoffMs:t}})}createEventFingerprint(e){let t=`${e.type}_${e.page_url}`;if(e.click_data){const s=Math.round((e.click_data.x||0)/10)*10,n=Math.round((e.click_data.y||0)/10)*10;t+=`_click_${s}_${n}`}return e.scroll_data&&(t+=`_scroll_${e.scroll_data.depth}_${e.scroll_data.direction}`),e.custom_event&&(t+=`_custom_${e.custom_event.name}`,e.custom_event.metadata&&(t+=`_${this.stableStringify(e.custom_event.metadata)}`)),e.web_vitals&&(t+=`_vitals_${e.web_vitals.type}`),e.error_data&&(t+=`_error_${e.error_data.type}_${e.error_data.message}`),t}createEventSignature(e){return this.createEventFingerprint(e)}stableStringify(e){return JSON.stringify(e,(t,s)=>s&&typeof s=="object"&&!Array.isArray(s)?Object.keys(s).sort().reduce((n,i)=>(n[i]=s[i],n),{}):s)}addToQueue(e){if(this.emitEvent(e),this.eventsQueue.push(e),this.eventsQueue.length>100){const t=this.eventsQueue.findIndex(n=>n.type!==u.SESSION_START),s=t>=0?this.eventsQueue.splice(t,1)[0]:this.eventsQueue.shift();a("warn","Event queue overflow, oldest non-critical event removed",{data:{maxLength:100,currentLength:this.eventsQueue.length,removedEventType:s?.type,wasCritical:s?.type===u.SESSION_START}})}this.scheduleSendTimeout(),this.eventsQueue.length>=50&&this.consecutiveSendFailures<5&&this.sendEventsQueue()}scheduleSendTimeout(){if(this.sendTimeoutId!==null)return;const e=this.calculateSendDelay();this.sendTimeoutId=window.setTimeout(()=>{this.sendTimeoutId=null,this.eventsQueue.length>0&&this.sendEventsQueue()},e)}calculateSendDelay(){const e=this.get("config")?.sendIntervalMs??1e4;if(this.consecutiveSendFailures===0)return e;const t=e*Math.pow(2,this.consecutiveSendFailures);return Math.min(t,12e4)}shouldSample(){const e=this.get("config")?.samplingRate??1;return Math.random()<e}checkRateLimit(){const e=Date.now();return e-this.rateLimitWindowStart>1e3&&(this.rateLimitCounter=0,this.rateLimitWindowStart=e),this.rateLimitCounter>=50?!1:(this.rateLimitCounter++,!0)}checkPerEventRateLimit(e,t){const s=Date.now(),i=(this.perEventRateLimits.get(e)??[]).filter(o=>s-o<6e4);return i.length>=t?(a("warn","Per-event rate limit exceeded for custom event",{data:{eventName:e,limit:t,window:`${6e4/1e3}s`}}),!1):(i.push(s),this.perEventRateLimits.set(e,i),!0)}getTypeLimitForEvent(e){return{[u.CLICK]:500,[u.PAGE_VIEW]:100,[u.CUSTOM]:500,[u.VIEWPORT_VISIBLE]:200,[u.SCROLL]:120}[e]??null}removeProcessedEvents(e){const t=new Set(e);this.eventsQueue=this.eventsQueue.filter(s=>!t.has(s.id))}emitEvent(e){if(this.emitter){const{_session_id:t,...s}=e;this.emitter.emit(j.EVENT,s)}}emitEventsQueue(e){this.emitter&&this.emitter.emit(j.QUEUE,e)}debounce(e,t){let s=null;return((...n)=>{s!==null&&clearTimeout(s),s=setTimeout(()=>{e(...n),s=null},t)})}getInitialCounts(){return{total:0,[u.CLICK]:0,[u.PAGE_VIEW]:0,[u.CUSTOM]:0,[u.VIEWPORT_VISIBLE]:0,[u.SCROLL]:0}}loadSessionCounts(e){if(typeof window>"u"||typeof localStorage>"u")return this.getInitialCounts();const t=this.get("userId")||"anonymous",s=Xe(t,e);try{const n=localStorage.getItem(s);if(!n)return this.getInitialCounts();const i=JSON.parse(n);return i._timestamp&&Date.now()-i._timestamp>je?(a("debug","Session counts expired, clearing",{data:{sessionId:e,age:Date.now()-i._timestamp}}),localStorage.removeItem(s),this.getInitialCounts()):typeof i.total=="number"&&typeof i[u.CLICK]=="number"&&typeof i[u.PAGE_VIEW]=="number"&&typeof i[u.CUSTOM]=="number"&&typeof i[u.VIEWPORT_VISIBLE]=="number"&&typeof i[u.SCROLL]=="number"?{total:i.total,[u.CLICK]:i[u.CLICK],[u.PAGE_VIEW]:i[u.PAGE_VIEW],[u.CUSTOM]:i[u.CUSTOM],[u.VIEWPORT_VISIBLE]:i[u.VIEWPORT_VISIBLE],[u.SCROLL]:i[u.SCROLL]}:(a("warn","Invalid session counts structure in localStorage, resetting",{data:{sessionId:e,parsed:i}}),localStorage.removeItem(s),a("debug","Session counts removed due to invalid/corrupted data",{data:{sessionId:e,parsed:i}}),this.getInitialCounts())}catch(n){return a("warn","Failed to load session counts from localStorage",{error:n,data:{sessionId:e}}),this.getInitialCounts()}}cleanupExpiredSessionCounts(){if(!(typeof window>"u"||typeof localStorage>"u"))try{const e=localStorage.getItem(Ke);if(e){const i=Date.now()-parseInt(e,10);if(i<ze){a("debug","Skipping session counts cleanup (throttled)",{data:{timeSinceLastCleanup:i,throttleMs:ze}});return}}const t=this.get("userId")||"anonymous",s=`${I}:${t}:session_counts:`,n=[];for(let i=0;i<localStorage.length;i++){const o=localStorage.key(i);if(o?.startsWith(s))try{const l=localStorage.getItem(o);if(l){const d=JSON.parse(l);d._timestamp&&Date.now()-d._timestamp>je&&n.push(o)}}catch{}}n.forEach(i=>{localStorage.removeItem(i),a("debug","Cleaned up expired session counts",{data:{key:i}})}),n.length>0&&a("info",`Cleaned up ${n.length} expired session counts entries`),localStorage.setItem(Ke,Date.now().toString())}catch(e){a("warn","Failed to cleanup expired session counts",{error:e})}}saveSessionCounts(e){const t=this.get("userId")||"anonymous",s=Xe(t,e);try{const n={...this.sessionEventCounts,_timestamp:Date.now(),_version:1};localStorage.setItem(s,JSON.stringify(n))}catch(n){a("warn","Failed to persist session counts to localStorage",{error:n,data:{sessionId:e}})}}}class Ls{static getId(e){const t=e.getItem(me);if(t)return t;const s=at();return e.setItem(me,s),s}}const As=/^\d{13}-[a-z0-9]{9}$/;class Ms extends y{storageManager;eventManager;projectId;activityHandler=null;visibilityChangeHandler=null;sessionTimeoutId=null;broadcastChannel=null;isTracking=!1;needsRenewal=!1;constructor(e,t,s){super(),this.storageManager=e,this.eventManager=t,this.projectId=s}initCrossTabSync(){if(typeof BroadcastChannel>"u"){a("debug","BroadcastChannel not supported");return}const e=this.getProjectId();this.broadcastChannel=new BroadcastChannel(kt(e)),this.broadcastChannel.onmessage=t=>{const{action:s,sessionId:n,timestamp:i,projectId:o}=t.data??{};o===e&&(s==="session_start"&&n&&typeof i=="number"&&i>Date.now()-5e3?(this.set("sessionId",n),this.persistSession(n,i),this.isTracking&&this.setupSessionTimeout()):s&&s!=="session_start"&&a("debug","Ignored BroadcastChannel message with unknown action",{data:{action:s}}))}}shareSession(e){this.broadcastChannel&&typeof this.broadcastChannel.postMessage=="function"&&this.broadcastChannel.postMessage({action:"session_start",projectId:this.getProjectId(),sessionId:e,timestamp:Date.now()})}cleanupCrossTabSync(){this.broadcastChannel&&(typeof this.broadcastChannel.close=="function"&&this.broadcastChannel.close(),this.broadcastChannel=null)}recoverSession(){const e=this.loadStoredSession();if(!e)return null;if(!As.test(e.id))return a("warn","Invalid session ID format recovered from storage, clearing",{data:{sessionId:e.id}}),this.clearStoredSession(),null;const t=this.get("config")?.sessionTimeout??9e5;return Date.now()-e.lastActivity>t?(this.clearStoredSession(),null):e.id}persistSession(e,t=Date.now(),s,n){this.saveStoredSession({id:e,lastActivity:t,...s&&{referrer:s},...n&&{utm:n}})}clearStoredSession(){const e=this.getSessionStorageKey();this.storageManager.removeItem(e)}loadStoredSession(){const e=this.getSessionStorageKey(),t=this.storageManager.getItem(e);if(t!==null)try{const n=JSON.parse(t);if(n.id&&typeof n.lastActivity=="number")return n}catch{this.storageManager.removeItem(e)}const s=this.storageManager.getSessionItem(e);if(s!==null)try{const n=JSON.parse(s);if(n.id&&typeof n.lastActivity=="number")return n}catch{this.storageManager.removeSessionItem(e)}return null}saveStoredSession(e){const t=this.getSessionStorageKey(),s=JSON.stringify(e);this.storageManager.setItem(t,s),this.storageManager.setSessionItem(t,s)}getSessionStorageKey(){return Dt(this.getProjectId())}getProjectId(){return this.projectId}startTracking(){if(this.isTracking){a("debug","Session tracking already active");return}const e=this.recoverSession(),t=e??this.generateSessionId();let s,n;if(e){const i=this.loadStoredSession();s=i?.referrer??ve(),n=i?.utm??ye()}else s=ve(),n=ye();a("debug","Session tracking initialized",{data:{sessionId:t,wasRecovered:!!e,willEmitSessionStart:!e,sessionReferrer:s,hasUtm:!!n}}),this.isTracking=!0;try{this.set("sessionId",t),this.set("sessionReferrer",s),this.set("sessionUtm",n),this.persistSession(t,Date.now(),s,n),this.initCrossTabSync(),this.shareSession(t),e?a("debug","Session recovered, skipping SESSION_START",{data:{sessionId:t}}):(a("debug","Emitting SESSION_START event",{data:{sessionId:t}}),this.eventManager.track({type:u.SESSION_START})),this.setupSessionTimeout(),this.setupActivityListeners(),this.setupLifecycleListeners()}catch(i){throw this.isTracking=!1,this.clearSessionTimeout(),this.cleanupActivityListeners(),this.cleanupLifecycleListeners(),this.cleanupCrossTabSync(),this.set("sessionId",null),i}}generateSessionId(){return`${Date.now()}-${Math.random().toString(36).substring(2,11)}`}setupSessionTimeout(){this.clearSessionTimeout();const e=this.get("config")?.sessionTimeout??9e5;this.sessionTimeoutId=setTimeout(()=>{this.enterRenewalMode()},e)}resetSessionTimeout(){this.setupSessionTimeout();const e=this.get("sessionId");e&&this.persistSession(e,Date.now(),this.get("sessionReferrer"),this.get("sessionUtm"))}clearSessionTimeout(){this.sessionTimeoutId&&(clearTimeout(this.sessionTimeoutId),this.sessionTimeoutId=null)}setupActivityListeners(){this.activityHandler=()=>{this.needsRenewal?this.renewSession():this.resetSessionTimeout()},document.addEventListener("click",this.activityHandler,{passive:!0}),document.addEventListener("keydown",this.activityHandler,{passive:!0}),document.addEventListener("scroll",this.activityHandler,{passive:!0})}renewSession(){this.needsRenewal=!1;const e=this.generateSessionId(),t=ve(),s=ye();a("debug","Renewing session after timeout",{data:{newSessionId:e}}),this.set("sessionId",e),this.set("sessionReferrer",t),this.set("sessionUtm",s),this.persistSession(e,Date.now(),t,s),this.cleanupCrossTabSync(),this.initCrossTabSync(),this.shareSession(e),this.eventManager.track({type:u.SESSION_START}),this.eventManager.flushPendingEvents(),this.setupSessionTimeout()}cleanupActivityListeners(){this.activityHandler&&(document.removeEventListener("click",this.activityHandler),document.removeEventListener("keydown",this.activityHandler),document.removeEventListener("scroll",this.activityHandler),this.activityHandler=null)}setupLifecycleListeners(){this.visibilityChangeHandler||(this.visibilityChangeHandler=()=>{if(document.hidden)this.clearSessionTimeout();else{if(this.isSessionStale()){a("debug","Session expired during suspend, entering renewal mode"),this.enterRenewalMode();return}this.get("sessionId")&&this.setupSessionTimeout()}},document.addEventListener("visibilitychange",this.visibilityChangeHandler))}isSessionStale(){if(this.needsRenewal||!this.get("sessionId"))return!1;const t=this.loadStoredSession();if(!t)return!1;const s=this.get("config")?.sessionTimeout??9e5;return Date.now()-t.lastActivity>s}cleanupLifecycleListeners(){this.visibilityChangeHandler&&(document.removeEventListener("visibilitychange",this.visibilityChangeHandler),this.visibilityChangeHandler=null)}enterRenewalMode(){this.clearSessionTimeout(),this.cleanupCrossTabSync(),this.clearStoredSession(),this.set("sessionId",null),this.set("hasStartSession",!1),this.set("sessionReferrer",void 0),this.set("sessionUtm",void 0),this.needsRenewal=!0,a("debug","Session timed out, entering renewal mode")}resetSessionState(){this.clearSessionTimeout(),this.cleanupActivityListeners(),this.cleanupLifecycleListeners(),this.cleanupCrossTabSync(),this.clearStoredSession(),this.set("sessionId",null),this.set("hasStartSession",!1),this.set("sessionReferrer",void 0),this.set("sessionUtm",void 0),this.needsRenewal=!1,this.isTracking=!1}stopTracking(){this.resetSessionState()}destroy(){this.clearSessionTimeout(),this.cleanupActivityListeners(),this.cleanupCrossTabSync(),this.cleanupLifecycleListeners(),this.isTracking=!1,this.needsRenewal=!1,this.set("hasStartSession",!1)}}class Cs extends y{eventManager;storageManager;sessionManager=null;destroyed=!1;constructor(e,t){super(),this.eventManager=t,this.storageManager=e}startTracking(){if(this.isActive())return;if(this.destroyed){a("debug","Cannot start tracking on destroyed handler");return}const t=this.get("config")?.integrations?.tracelog?.projectId??"custom";try{this.sessionManager=new Ms(this.storageManager,this.eventManager,t),this.sessionManager.startTracking(),this.eventManager.flushPendingEvents()}catch(s){if(this.sessionManager){try{this.sessionManager.destroy()}catch{}this.sessionManager=null}throw a("error","Failed to start session tracking",{error:s}),s}}isActive(){return this.sessionManager!==null&&!this.destroyed}cleanupSessionManager(){this.sessionManager&&(this.sessionManager.stopTracking(),this.sessionManager.destroy(),this.sessionManager=null)}stopTracking(){this.cleanupSessionManager()}destroy(){this.destroyed||(this.sessionManager&&(this.sessionManager.destroy(),this.sessionManager=null),this.destroyed=!0)}}class Rs extends y{eventManager;onTrack;originalPushState;originalReplaceState;lastPageViewTime=0;constructor(e,t){super(),this.eventManager=e,this.onTrack=t}startTracking(){this.trackInitialPageView(),window.addEventListener("popstate",this.trackCurrentPage,!0),window.addEventListener("hashchange",this.trackCurrentPage,!0),this.patchHistory("pushState"),this.patchHistory("replaceState")}stopTracking(){window.removeEventListener("popstate",this.trackCurrentPage,!0),window.removeEventListener("hashchange",this.trackCurrentPage,!0),this.originalPushState&&(window.history.pushState=this.originalPushState),this.originalReplaceState&&(window.history.replaceState=this.originalReplaceState),this.lastPageViewTime=0}patchHistory(e){const t=window.history[e];e==="pushState"&&!this.originalPushState?this.originalPushState=t:e==="replaceState"&&!this.originalReplaceState&&(this.originalReplaceState=t),window.history[e]=(...s)=>{t.apply(window.history,s),this.trackCurrentPage()}}trackCurrentPage=()=>{const e=window.location.href,t=we(e,this.get("config").sensitiveQueryParams);if(this.get("pageUrl")===t)return;const s=Date.now(),n=this.get("config").pageViewThrottleMs??1e3;if(s-this.lastPageViewTime<n)return;this.lastPageViewTime=s,this.onTrack();const i=this.get("pageUrl");this.set("pageUrl",t);const o=this.extractPageViewData();this.eventManager.track({type:u.PAGE_VIEW,page_url:this.get("pageUrl"),from_page_url:i,...o&&{page_view:o}}),this.get("config").flushOnSpaNavigation===!0&&this.eventManager.flushImmediately()};trackInitialPageView(){const e=we(window.location.href,this.get("config").sensitiveQueryParams),t=this.extractPageViewData();this.lastPageViewTime=Date.now(),this.eventManager.track({type:u.PAGE_VIEW,page_url:e,...t&&{page_view:t}}),this.onTrack()}extractPageViewData(){const{pathname:e,search:t,hash:s}=window.location,{referrer:n}=document,{title:i}=document;return!n&&!i&&!e&&!t&&!s?void 0:{...n&&{referrer:n},...i&&{title:i},...e&&{pathname:e},...t&&{search:t},...s&&{hash:s}}}}class Ns extends y{eventManager;lastClickTimes=new Map;clickHandler;lastPruneTime=0;constructor(e){super(),this.eventManager=e}startTracking(){this.clickHandler||(this.clickHandler=e=>{const t=e,s=t.target,n=typeof HTMLElement<"u"&&s instanceof HTMLElement?s:typeof HTMLElement<"u"&&s instanceof Node&&s.parentElement instanceof HTMLElement?s.parentElement:null;if(!n){a("debug","Click target not found or not an element");return}if(this.shouldIgnoreElement(n))return;const i=this.get("config")?.clickThrottleMs??300;if(i>0&&!this.checkClickThrottle(n,i))return;const o=this.findTrackingElement(n),l=this.getRelevantClickElement(n),d=this.calculateClickCoordinates(t,n);if(o){const m=this.extractTrackingData(o);if(m){const S=this.createCustomEventData(m);this.eventManager.track({type:u.CUSTOM,custom_event:{name:S.name,...S.value&&{metadata:{value:S.value}}}})}}if(!d){a("debug","Click skipped: invalid coordinates (likely synthetic)");return}const c=this.generateClickData(n,l,d);this.eventManager.track({type:u.CLICK,click_data:c})},window.addEventListener("click",this.clickHandler,!0))}stopTracking(){this.clickHandler&&(window.removeEventListener("click",this.clickHandler,!0),this.clickHandler=void 0),this.lastClickTimes.clear(),this.lastPruneTime=0}shouldIgnoreElement(e){return e.hasAttribute(`${L}-ignore`)?!0:e.closest(`[${L}-ignore]`)!==null}checkClickThrottle(e,t){const s=this.getElementSignature(e),n=Date.now();this.pruneThrottleCache(n);const i=this.lastClickTimes.get(s);return i!==void 0&&n-i<t?(a("debug","ClickHandler: Click suppressed by throttle",{data:{signature:s,throttleRemaining:t-(n-i)}}),!1):(this.lastClickTimes.set(s,n),!0)}pruneThrottleCache(e){if(e-this.lastPruneTime<3e4)return;this.lastPruneTime=e;const t=e-3e5;for(const[s,n]of this.lastClickTimes.entries())n<t&&this.lastClickTimes.delete(s);if(this.lastClickTimes.size>1e3){const s=Array.from(this.lastClickTimes.entries()).sort((o,l)=>o[1]-l[1]),n=this.lastClickTimes.size-1e3,i=s.slice(0,n);for(const[o]of i)this.lastClickTimes.delete(o);a("debug","ClickHandler: Pruned throttle cache",{data:{removed:i.length,remaining:this.lastClickTimes.size}})}}getElementSignature(e){if(e.id)return`#${e.id}`;const t=e.getAttribute("data-testid");if(t)return`[data-testid="${t}"]`;const s=e.getAttribute(`${L}-name`);return s?`[${L}-name="${s}"]`:this.getElementPath(e)}getElementPath(e){const t=[];let s=e;for(;s&&s!==document.body;){let n=s.tagName.toLowerCase();if(s.className){const i=s.className.split(" ")[0];i&&(n+=`.${i}`)}t.unshift(n),s=s.parentElement}return t.join(">")||"unknown"}findTrackingElement(e){return e.hasAttribute(`${L}-name`)?e:e.closest(`[${L}-name]`)}getRelevantClickElement(e){for(const t of Mt)try{if(e.matches(t))return e;const s=e.closest(t);if(s)return s}catch(s){a("debug","Invalid selector in element search",{error:s,data:{selector:t}});continue}return e}clamp(e){return Math.max(0,Math.min(1,Number(e.toFixed(3))))}calculateClickCoordinates(e,t){const s=e.clientX,n=e.clientY;if(typeof s!="number"||typeof n!="number"||!Number.isFinite(s)||!Number.isFinite(n)||s===0&&n===0&&!e.isTrusted)return null;const i=t.getBoundingClientRect(),o=i.width>0?this.clamp((s-i.left)/i.width):0,l=i.height>0?this.clamp((n-i.top)/i.height):0;return{x:s,y:n,relativeX:o,relativeY:l}}extractTrackingData(e){const t=e.getAttribute(`${L}-name`),s=e.getAttribute(`${L}-value`);if(t)return{element:e,name:t,...s&&{value:s}}}generateClickData(e,t,s){const{x:n,y:i,relativeX:o,relativeY:l}=s,d=this.getRelevantText(e,t),c=this.extractElementAttributes(t);return{x:n,y:i,relativeX:o,relativeY:l,tag:t.tagName.toLowerCase(),...t.id&&{id:t.id},...t.className&&{class:t.className},...d&&{text:d},...c.href&&{href:c.href},...c.title&&{title:c.title},...c.alt&&{alt:c.alt},...c.role&&{role:c.role},...c["aria-label"]&&{ariaLabel:c["aria-label"]},...Object.keys(c).length>0&&{dataAttributes:c}}}sanitizeText(e){let t=e;for(const s of Te){const n=new RegExp(s.source,s.flags);t=t.replace(n,"[REDACTED]")}return t}getRelevantText(e,t){const s=e.textContent?.trim()??"",n=t.textContent?.trim()??"";if(!s&&!n)return"";let i="";return s&&s.length<=255?i=s:n.length<=255?i=n:i=n.slice(0,252)+"...",this.sanitizeText(i)}extractElementAttributes(e){const t=["id","class","data-testid","aria-label","title","href","type","name","alt","role"],s={};for(const n of t){const i=e.getAttribute(n);i&&(s[n]=i)}return s}createCustomEventData(e){return{name:e.name,...e.value&&{value:e.value}}}}class Os extends y{eventManager;containers=[];limitWarningLogged=!1;minDepthChange=5;minIntervalMs=500;maxEventsPerSession=120;containerDiscoveryTimeoutId=null;constructor(e){super(),this.eventManager=e}startTracking(){this.limitWarningLogged=!1,this.applyConfigOverrides(),this.set("scrollEventCount",0),this.tryDetectScrollContainers(0)}stopTracking(){this.containerDiscoveryTimeoutId!==null&&(clearTimeout(this.containerDiscoveryTimeoutId),this.containerDiscoveryTimeoutId=null);for(const e of this.containers)this.clearContainerTimer(e),e.element===window?window.removeEventListener("scroll",e.listener):e.element.removeEventListener("scroll",e.listener);this.containers.length=0,this.set("scrollEventCount",0),this.limitWarningLogged=!1}tryDetectScrollContainers(e){const t=this.findScrollableElements();if(this.isWindowScrollable()&&this.setupScrollContainer(window,"window"),t.length>0){for(const s of t){const n=this.getElementSelector(s);this.setupScrollContainer(s,n)}this.applyPrimaryScrollSelectorIfConfigured();return}if(e<5){this.containerDiscoveryTimeoutId=window.setTimeout(()=>{this.containerDiscoveryTimeoutId=null,this.tryDetectScrollContainers(e+1)},200);return}this.containers.length===0&&this.setupScrollContainer(window,"window"),this.applyPrimaryScrollSelectorIfConfigured()}applyPrimaryScrollSelectorIfConfigured(){const e=this.get("config");e?.primaryScrollSelector&&this.applyPrimaryScrollSelector(e.primaryScrollSelector)}findScrollableElements(){if(!document.body)return[];const e=[],t=document.createTreeWalker(document.body,NodeFilter.SHOW_ELEMENT,{acceptNode:n=>{const i=n;if(!i.isConnected||!i.offsetParent)return NodeFilter.FILTER_SKIP;const o=getComputedStyle(i);return o.overflowY==="auto"||o.overflowY==="scroll"||o.overflow==="auto"||o.overflow==="scroll"?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}});let s;for(;(s=t.nextNode())&&e.length<10;){const n=s;this.isElementScrollable(n)&&e.push(n)}return e}getElementSelector(e){if(e===window)return"window";const t=e;if(t.id)return`#${t.id}`;if(t.className&&typeof t.className=="string"){const s=t.className.split(" ").filter(n=>n.trim())[0];if(s)return`.${s}`}return t.tagName.toLowerCase()}determineIfPrimary(e){return this.isWindowScrollable()?e===window:this.containers.length===0}setupScrollContainer(e,t){if(this.containers.some(c=>c.element===e)||e!==window&&!this.isElementScrollable(e))return;const n=this.getScrollTop(e),i=this.calculateScrollDepth(n,this.getScrollHeight(e),this.getViewportHeight(e)),o=this.determineIfPrimary(e),l={element:e,selector:t,isPrimary:o,lastScrollPos:n,lastDepth:i,lastDirection:Q.DOWN,lastEventTime:0,firstScrollEventTime:null,maxDepthReached:i,debounceTimer:null,listener:null},d=()=>{this.get("suppressNextScroll")||(l.firstScrollEventTime===null&&(l.firstScrollEventTime=Date.now()),this.clearContainerTimer(l),l.debounceTimer=window.setTimeout(()=>{const c=this.calculateScrollData(l);if(c){const m=Date.now();this.processScrollEvent(l,c,m)}l.debounceTimer=null},250))};l.listener=d,this.containers.push(l),e===window?window.addEventListener("scroll",d,{passive:!0}):e.addEventListener("scroll",d,{passive:!0})}processScrollEvent(e,t,s){if(!this.shouldEmitScrollEvent(e,t,s))return;e.lastEventTime=s,e.lastDepth=t.depth,e.lastDirection=t.direction;const n=this.get("scrollEventCount")??0;this.set("scrollEventCount",n+1),this.eventManager.track({type:u.SCROLL,scroll_data:{...t,container_selector:e.selector,is_primary:e.isPrimary}})}shouldEmitScrollEvent(e,t,s){return this.hasReachedSessionLimit()?(this.logLimitOnce(),!1):!(!this.hasElapsedMinimumInterval(e,s)||!this.hasSignificantDepthChange(e,t.depth))}hasReachedSessionLimit(){return(this.get("scrollEventCount")??0)>=this.maxEventsPerSession}hasElapsedMinimumInterval(e,t){return e.lastEventTime===0?!0:t-e.lastEventTime>=this.minIntervalMs}hasSignificantDepthChange(e,t){return Math.abs(t-e.lastDepth)>=this.minDepthChange}logLimitOnce(){this.limitWarningLogged||(this.limitWarningLogged=!0,a("debug","Max scroll events per session reached",{data:{limit:this.maxEventsPerSession}}))}applyConfigOverrides(){this.minDepthChange=5,this.minIntervalMs=500,this.maxEventsPerSession=120}isWindowScrollable(){return document.documentElement.scrollHeight>window.innerHeight}clearContainerTimer(e){e.debounceTimer!==null&&(clearTimeout(e.debounceTimer),e.debounceTimer=null)}getScrollDirection(e,t){return e>t?Q.DOWN:Q.UP}calculateScrollDepth(e,t,s){if(t<=s)return 0;const n=t-s;return Math.min(100,Math.max(0,Math.floor(e/n*100)))}calculateScrollData(e){const{element:t,lastScrollPos:s,lastEventTime:n}=e,i=this.getScrollTop(t),o=Date.now(),l=Math.abs(i-s);if(l<10||t===window&&!this.isWindowScrollable())return null;const d=this.getViewportHeight(t),c=this.getScrollHeight(t),m=this.getScrollDirection(i,s),S=this.calculateScrollDepth(i,c,d);let g;n>0?g=o-n:e.firstScrollEventTime!==null?g=o-e.firstScrollEventTime:g=250;const p=Math.round(l/g*1e3);return S>e.maxDepthReached&&(e.maxDepthReached=S),e.lastScrollPos=i,{depth:S,direction:m,velocity:p,max_depth_reached:e.maxDepthReached}}getScrollTop(e){return e===window?window.scrollY:e.scrollTop}getViewportHeight(e){return e===window?window.innerHeight:e.clientHeight}getScrollHeight(e){return e===window?document.documentElement.scrollHeight:e.scrollHeight}isElementScrollable(e){const t=getComputedStyle(e),s=t.overflowY==="auto"||t.overflowY==="scroll"||t.overflow==="auto"||t.overflow==="scroll",n=e.scrollHeight>e.clientHeight;return s&&n}applyPrimaryScrollSelector(e){let t;if(e==="window")t=window;else{const n=document.querySelector(e);if(!(n instanceof HTMLElement)){a("debug",`Selector "${e}" did not match an HTMLElement`);return}t=n}this.containers.forEach(n=>{this.updateContainerPrimary(n,n.element===t)}),!this.containers.some(n=>n.element===t)&&t instanceof HTMLElement&&this.isElementScrollable(t)&&this.setupScrollContainer(t,e)}updateContainerPrimary(e,t){e.isPrimary=t}}class Ps extends y{eventManager;trackedElements=new Map;observer=null;mutationObserver=null;mutationDebounceTimer=null;config=null;constructor(e){super(),this.eventManager=e}startTracking(){const e=this.get("config");if(this.config=e.viewport??null,!this.config?.elements||this.config.elements.length===0)return;const t=this.config.threshold??.5,s=this.config.minDwellTime??1e3;if(t<0||t>1){a("debug","ViewportHandler: Invalid threshold, must be between 0 and 1");return}if(s<0){a("debug","ViewportHandler: Invalid minDwellTime, must be non-negative");return}if(typeof IntersectionObserver>"u"){a("debug","ViewportHandler: IntersectionObserver not supported in this browser");return}this.observer=new IntersectionObserver(this.handleIntersection,{threshold:t}),this.observeElements(),this.setupMutationObserver()}stopTracking(){this.observer&&(this.observer.disconnect(),this.observer=null),this.mutationObserver&&(this.mutationObserver.disconnect(),this.mutationObserver=null),this.mutationDebounceTimer!==null&&(window.clearTimeout(this.mutationDebounceTimer),this.mutationDebounceTimer=null);for(const e of this.trackedElements.values())e.timeoutId!==null&&window.clearTimeout(e.timeoutId);this.trackedElements.clear()}observeElements(){if(!this.config||!this.observer)return;const e=this.config.maxTrackedElements??100;let t=this.trackedElements.size;for(const s of this.config.elements)try{const n=document.querySelectorAll(s.selector);for(const i of Array.from(n)){if(t>=e){a("debug","ViewportHandler: Maximum tracked elements reached",{data:{limit:e,selector:s.selector,message:"Some elements will not be tracked. Consider more specific selectors."}});return}i.hasAttribute(`${L}-ignore`)||this.trackedElements.has(i)||(this.trackedElements.set(i,{element:i,selector:s.selector,id:s.id,name:s.name,startTime:null,timeoutId:null,lastFiredTime:null}),this.observer?.observe(i),t++)}}catch(n){a("debug",`ViewportHandler: Invalid selector "${s.selector}"`,{error:n})}a("debug","ViewportHandler: Elements tracked",{data:{count:t,limit:e}})}handleIntersection=e=>{if(!this.config)return;const t=this.config.minDwellTime??1e3;for(const s of e){const n=this.trackedElements.get(s.target);n&&(s.isIntersecting?n.startTime===null&&(n.startTime=performance.now(),n.timeoutId=window.setTimeout(()=>{const i=Math.round(s.intersectionRatio*100)/100;this.fireViewportEvent(n,i)},t)):n.startTime!==null&&(n.timeoutId!==null&&(window.clearTimeout(n.timeoutId),n.timeoutId=null),n.startTime=null))}};fireViewportEvent(e,t){if(e.startTime===null)return;const s=Math.round(performance.now()-e.startTime);if(e.element.hasAttribute(`${L}-ignore`))return;const n=this.config?.cooldownPeriod??6e4,i=Date.now();if(e.lastFiredTime!==null&&i-e.lastFiredTime<n){a("debug","ViewportHandler: Event suppressed by cooldown period",{data:{selector:e.selector,cooldownRemaining:n-(i-e.lastFiredTime)}}),e.startTime=null,e.timeoutId=null;return}const o={selector:e.selector,dwellTime:s,visibilityRatio:t,...e.id!==void 0&&{id:e.id},...e.name!==void 0&&{name:e.name}};this.eventManager.track({type:u.VIEWPORT_VISIBLE,viewport_data:o}),e.startTime=null,e.timeoutId=null,e.lastFiredTime=i}setupMutationObserver(){if(!(!this.config||typeof MutationObserver>"u")){if(!document.body){a("debug","ViewportHandler: document.body not available, skipping MutationObserver setup");return}this.mutationObserver=new MutationObserver(e=>{let t=!1;for(const s of e)s.type==="childList"&&(s.addedNodes.length>0&&(t=!0),s.removedNodes.length>0&&this.cleanupRemovedNodes(s.removedNodes));t&&(this.mutationDebounceTimer!==null&&window.clearTimeout(this.mutationDebounceTimer),this.mutationDebounceTimer=window.setTimeout(()=>{this.observeElements(),this.mutationDebounceTimer=null},100))}),this.mutationObserver.observe(document.body,{childList:!0,subtree:!0})}}cleanupRemovedNodes(e){e.forEach(t=>{if(t.nodeType!==1)return;const s=t,n=this.trackedElements.get(s);n&&(n.timeoutId!==null&&window.clearTimeout(n.timeoutId),this.observer?.unobserve(s),this.trackedElements.delete(s)),Array.from(this.trackedElements.keys()).filter(o=>s.contains(o)).forEach(o=>{const l=this.trackedElements.get(o);l&&l.timeoutId!==null&&window.clearTimeout(l.timeoutId),this.observer?.unobserve(o),this.trackedElements.delete(o)})})}}const Ds="tracelog_session_id",ks="tracelog_user_id";class Vs extends y{visibilityHandler=null;pageshowHandler=null;lastSyncedKey=null;activate(){this.cleanupListeners(),this.syncCartAttribute(),this.setupListeners()}deactivate(){this.cleanupListeners(),this.lastSyncedKey=null}onSessionChange(){this.syncCartAttribute()}syncCartAttribute(){const e=this.get("sessionId");if(!e)return;const t=this.get("userId"),s=typeof t=="string"&&t.length>0?t:"",n=`${e}|${s}`;n!==this.lastSyncedKey&&(this.lastSyncedKey=n,this.postCartUpdate(e,s))}postCartUpdate(e,t){const s={[Ds]:e};t.length>0&&(s[ks]=t);try{fetch("/cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({attributes:s}),credentials:"same-origin"}).then(n=>{n.ok||(this.lastSyncedKey=null,a("debug","Shopify cart attribute update failed",{data:{status:n.status}}))}).catch(()=>{this.lastSyncedKey=null,a("debug","Shopify cart attribute update failed")})}catch{this.lastSyncedKey=null,a("debug","Shopify cart attribute update failed")}}setupListeners(){this.visibilityHandler=()=>{document.hidden||this.syncCartAttribute()},document.addEventListener("visibilitychange",this.visibilityHandler),this.pageshowHandler=e=>{e.persisted&&this.syncCartAttribute()},window.addEventListener("pageshow",this.pageshowHandler)}cleanupListeners(){this.visibilityHandler&&(document.removeEventListener("visibilitychange",this.visibilityHandler),this.visibilityHandler=null),this.pageshowHandler&&(window.removeEventListener("pageshow",this.pageshowHandler),this.pageshowHandler=null)}}class Us{storage;sessionStorageRef;fallbackStorage=new Map;fallbackSessionStorage=new Map;hasQuotaExceededError=!1;constructor(){this.storage=this.initializeStorage("localStorage"),this.sessionStorageRef=this.initializeStorage("sessionStorage"),this.storage||a("debug","localStorage not available, using memory fallback"),this.sessionStorageRef||a("debug","sessionStorage not available, using memory fallback")}getItem(e){try{return this.storage?this.storage.getItem(e):this.fallbackStorage.get(e)??null}catch{return this.fallbackStorage.get(e)??null}}setItem(e,t){this.fallbackStorage.set(e,t);try{if(this.storage){this.storage.setItem(e,t);return}}catch(s){if(s instanceof DOMException&&s.name==="QuotaExceededError"||s instanceof Error&&s.name==="QuotaExceededError")if(this.hasQuotaExceededError=!0,a("warn","localStorage quota exceeded, attempting cleanup",{data:{key:e,valueSize:t.length}}),this.cleanupOldData())try{if(this.storage){this.storage.setItem(e,t);return}}catch(o){a("error","localStorage quota exceeded even after cleanup - data will not persist",{error:o,data:{key:e,valueSize:t.length}})}else a("error","localStorage quota exceeded and no data to cleanup - data will not persist",{error:s,data:{key:e,valueSize:t.length}})}}removeItem(e){try{this.storage&&this.storage.removeItem(e)}catch{}this.fallbackStorage.delete(e)}clear(){if(!this.storage){this.fallbackStorage.clear();return}try{const e=[];for(let t=0;t<this.storage.length;t++){const s=this.storage.key(t);s?.startsWith("tracelog_")&&e.push(s)}e.forEach(t=>{this.storage.removeItem(t)}),this.fallbackStorage.clear()}catch(e){a("error","Failed to clear storage",{error:e}),this.fallbackStorage.clear()}}isAvailable(){return this.storage!==null}hasQuotaError(){return this.hasQuotaExceededError}cleanupOldData(){if(!this.storage)return!1;try{const e=[],t=[];for(let i=0;i<this.storage.length;i++){const o=this.storage.key(i);o?.startsWith("tracelog_")&&(e.push(o),o.startsWith("tracelog_persisted_events_")&&t.push(o))}if(t.length>0)return t.forEach(i=>{try{this.storage.removeItem(i)}catch{}}),!0;const s=["tracelog_session_","tracelog_user_id","tracelog_device_id","tracelog_config"],n=e.filter(i=>!s.some(o=>i.startsWith(o)));return n.length>0?(n.slice(0,5).forEach(o=>{try{this.storage.removeItem(o)}catch{}}),!0):!1}catch(e){return a("error","Failed to cleanup old data",{error:e}),!1}}initializeStorage(e){if(typeof window>"u")return null;try{const t=e==="localStorage"?window.localStorage:window.sessionStorage,s="__tracelog_test__";return t.setItem(s,"test"),t.removeItem(s),t}catch{return null}}getSessionItem(e){try{return this.sessionStorageRef?this.sessionStorageRef.getItem(e):this.fallbackSessionStorage.get(e)??null}catch{return this.fallbackSessionStorage.get(e)??null}}setSessionItem(e,t){this.fallbackSessionStorage.set(e,t);try{if(this.sessionStorageRef){this.sessionStorageRef.setItem(e,t);return}}catch(s){(s instanceof DOMException&&s.name==="QuotaExceededError"||s instanceof Error&&s.name==="QuotaExceededError")&&a("error","sessionStorage quota exceeded - data will not persist",{error:s,data:{key:e,valueSize:t.length}})}}removeSessionItem(e){try{this.sessionStorageRef&&this.sessionStorageRef.removeItem(e)}catch{}this.fallbackSessionStorage.delete(e)}}class Hs extends y{eventManager;reportedByNav=new Map;navigationHistory=[];observers=[];vitalThresholds;lastLongTaskSentAt=0;navigationCounter=0;constructor(e){super(),this.eventManager=e,this.vitalThresholds=Ie(re)}async startTracking(){const e=this.get("config"),t=e?.webVitalsMode??re;this.vitalThresholds=Ie(t),e?.webVitalsThresholds&&(this.vitalThresholds={...this.vitalThresholds,...e.webVitalsThresholds}),await this.initWebVitals(),this.observeLongTasks()}stopTracking(){this.observers.forEach((e,t)=>{try{e.disconnect()}catch(s){a("debug","Failed to disconnect performance observer",{error:s,data:{observerIndex:t}})}}),this.observers.length=0,this.reportedByNav.clear(),this.navigationHistory.length=0}observeWebVitalsFallback(){this.reportTTFB(),this.safeObserve("largest-contentful-paint",s=>{const n=s.getEntries(),i=n[n.length-1];i&&this.sendVital({type:"LCP",value:Number(i.startTime.toFixed(2))})},{type:"largest-contentful-paint",buffered:!0},!0);let e=0,t=this.getNavigationId();this.safeObserve("layout-shift",s=>{const n=this.getNavigationId();n!==t&&(e=0,t=n);const i=s.getEntries();for(const o of i){if(o.hadRecentInput===!0)continue;const l=typeof o.value=="number"?o.value:0;e+=l}this.sendVital({type:"CLS",value:Number(e.toFixed(2))})},{type:"layout-shift",buffered:!0}),this.safeObserve("paint",s=>{for(const n of s.getEntries())n.name==="first-contentful-paint"&&this.sendVital({type:"FCP",value:Number(n.startTime.toFixed(2))})},{type:"paint",buffered:!0},!0),this.safeObserve("event",s=>{let n=0;const i=s.getEntries();for(const o of i){const l=(o.processingEnd??0)-(o.startTime??0);n=Math.max(n,l)}n>0&&this.sendVital({type:"INP",value:Number(n.toFixed(2))})},{type:"event",buffered:!0})}async initWebVitals(){try{const{onLCP:e,onCLS:t,onFCP:s,onTTFB:n,onINP:i}=await Promise.resolve().then(()=>lr),o=l=>d=>{const c=Number(d.value.toFixed(2));this.sendVital({type:l,value:c})};e(o("LCP"),{reportAllChanges:!1}),t(o("CLS"),{reportAllChanges:!1}),s(o("FCP"),{reportAllChanges:!1}),n(o("TTFB"),{reportAllChanges:!1}),i(o("INP"),{reportAllChanges:!1})}catch(e){a("debug","Failed to load web-vitals library, using fallback",{error:e}),this.observeWebVitalsFallback()}}reportTTFB(){try{const e=performance.getEntriesByType("navigation")[0];if(!e)return;const t=e.responseStart;typeof t=="number"&&Number.isFinite(t)&&this.sendVital({type:"TTFB",value:Number(t.toFixed(2))})}catch(e){a("debug","Failed to report TTFB",{error:e})}}observeLongTasks(){this.safeObserve("longtask",e=>{const t=e.getEntries();for(const s of t){const n=Number(s.duration.toFixed(2)),i=Date.now();i-this.lastLongTaskSentAt>=ss&&(this.shouldSendVital("LONG_TASK",n)&&this.trackWebVital("LONG_TASK",n),this.lastLongTaskSentAt=i)}},{type:"longtask",buffered:!0})}sendVital(e){if(!this.shouldSendVital(e.type,e.value))return;const t=this.getNavigationId();if(t){const s=this.reportedByNav.get(t);if(s?.has(e.type))return;if(s)s.add(e.type);else if(this.reportedByNav.set(t,new Set([e.type])),this.navigationHistory.push(t),this.navigationHistory.length>rs){const i=this.navigationHistory.shift();i&&this.reportedByNav.delete(i)}}this.trackWebVital(e.type,e.value)}trackWebVital(e,t){if(!Number.isFinite(t)){a("debug","Invalid web vital value",{data:{type:e,value:t}});return}this.eventManager.track({type:u.WEB_VITALS,web_vitals:{type:e,value:t}})}getNavigationId(){try{const e=performance.getEntriesByType("navigation")[0];if(!e)return null;const t=e.startTime||performance.now(),s=++this.navigationCounter,n=`${t.toFixed(2)}_${window.location.pathname}`;return s>1?`${n}_${s}`:n}catch(e){return a("debug","Failed to get navigation ID",{error:e}),null}}isObserverSupported(e){if(typeof PerformanceObserver>"u")return!1;const t=PerformanceObserver.supportedEntryTypes;return!t||t.includes(e)}safeObserve(e,t,s,n=!1){try{if(!this.isObserverSupported(e))return!1;const i=new PerformanceObserver((o,l)=>{try{t(o,l)}catch(d){a("debug","Observer callback failed",{error:d,data:{type:e}})}if(n)try{l.disconnect()}catch{}});return i.observe(s??{type:e,buffered:!0}),n||this.observers.push(i),!0}catch(i){return a("debug","Failed to create performance observer",{error:i,data:{type:e}}),!1}}shouldSendVital(e,t){if(typeof t!="number"||!Number.isFinite(t))return a("debug","Invalid web vital value",{data:{type:e,value:t}}),!1;const s=this.vitalThresholds[e];return!(typeof s=="number"&&t<=s)}}class oe extends y{eventManager;recentErrors=new Map;errorBurstCounter=0;burstWindowStart=0;burstBackoffUntil=0;constructor(e){super(),this.eventManager=e}startTracking(){window.addEventListener("error",this.handleError),window.addEventListener("unhandledrejection",this.handleRejection)}stopTracking(){window.removeEventListener("error",this.handleError),window.removeEventListener("unhandledrejection",this.handleRejection),this.recentErrors.clear(),this.errorBurstCounter=0,this.burstWindowStart=0,this.burstBackoffUntil=0}shouldSample(){const e=Date.now();if(e<this.burstBackoffUntil)return!1;if(e-this.burstWindowStart>qt&&(this.errorBurstCounter=0,this.burstWindowStart=e),this.errorBurstCounter++,this.errorBurstCounter>Jt)return this.burstBackoffUntil=e+rt,a("debug","Error burst detected - entering cooldown",{data:{errorsInWindow:this.errorBurstCounter,cooldownMs:rt}}),!1;const s=this.get("config").errorSampling??st;return Math.random()<s}handleError=e=>{if(!this.shouldSample())return;const t=this.sanitize(e.message||"Unknown error");if(this.shouldSuppressError($.JS_ERROR,t))return;const s=typeof e.error?.stack=="string"?this.truncateStack(e.error.stack):void 0,n=typeof e.error?.name=="string"&&e.error.name!=="Error"?e.error.name:void 0;this.eventManager.track({type:u.ERROR,error_data:{type:$.JS_ERROR,message:t,...n!==void 0&&{name:n},...e.filename!==""&&{filename:e.filename},...e.lineno!==0&&{line:e.lineno},...e.colno!==0&&{column:e.colno},...s!==void 0&&{stack:s}}})};handleRejection=e=>{if(!this.shouldSample())return;const t=this.extractRejectionMessage(e.reason),s=this.sanitize(t);if(this.shouldSuppressError($.PROMISE_REJECTION,s))return;const n=e.reason instanceof Error&&typeof e.reason.stack=="string"?this.truncateStack(e.reason.stack):void 0,i=e.reason instanceof Error&&e.reason.name!=="Error"?e.reason.name:void 0;this.eventManager.track({type:u.ERROR,error_data:{type:$.PROMISE_REJECTION,message:s,...i!==void 0&&{name:i},...n!==void 0&&{stack:n}}})};extractRejectionMessage(e){if(e==null)return"Unknown rejection";if(typeof e=="string")return e;if(e instanceof Error)return e.message;if(typeof e=="object"&&"message"in e)return String(e.message);try{return JSON.stringify(e)}catch{return"Unserializable rejection"}}sanitize(e){const t=e.length>Ze?e.slice(0,Ze)+"...":e;return this.sanitizePii(t)}sanitizePii(e){let t=e;for(const s of Te){const n=new RegExp(s.source,s.flags);t=t.replace(n,"[REDACTED]")}return t}shouldSuppressError(e,t){const s=Date.now(),n=`${e}:${t}`,i=this.recentErrors.get(n);return i!==void 0&&s-i<tt?(this.recentErrors.set(n,s),!0):(this.recentErrors.set(n,s),this.recentErrors.size>Yt?(this.recentErrors.clear(),this.recentErrors.set(n,s),!1):(this.recentErrors.size>se&&this.pruneOldErrors(),!1))}static TRUNCATION_SUFFIX=`
|
|
2
|
-
...truncated`;truncateStack(e){if(e.length<=et)return this.sanitizePii(e);const t=et-oe.TRUNCATION_SUFFIX.length,s=e.slice(0,t)+oe.TRUNCATION_SUFFIX;return this.sanitizePii(s)}pruneOldErrors(){const e=Date.now();for(const[n,i]of this.recentErrors.entries())e-i>tt&&this.recentErrors.delete(n);if(this.recentErrors.size<=se)return;const t=Array.from(this.recentErrors.entries()).sort((n,i)=>n[1]-i[1]),s=this.recentErrors.size-se;for(let n=0;n<s;n+=1){const i=t[n];i&&this.recentErrors.delete(i[0])}}}class Fs extends y{isInitialized=!1;suppressNextScrollTimer=null;pageUnloadHandler=null;pageShowHandler=null;visibilityFlushHandler=null;emitter=new Is;transformers={};customHeadersProvider;managers={};handlers={};integrationInstances={};get initialized(){return this.isInitialized}async init(e={}){if(this.isInitialized)return{sessionId:this.get("sessionId")??""};this.managers.storage=new Us;try{this.setupState(e);const t=e.integrations?.custom?.headers??{},s=e.integrations?.custom?.fetchCredentials??"include";return this.managers.event=new bs(this.managers.storage,this.emitter,this.transformers,t,this.customHeadersProvider,s),this.loadPersistedIdentity(),this.initializeHandlers(),this.setupPageLifecycleListeners(),await this.managers.event.recoverPersistedEvents().catch(n=>{a("warn","Failed to recover persisted events",{error:n})}),this.isInitialized=!0,{sessionId:this.get("sessionId")??""}}catch(t){this.destroy(!0);const s=t instanceof Error?t.message:String(t);throw new Error(`[TraceLog] TraceLog initialization failed: ${s}`)}}async flushImmediately(){return await this.managers.event?.flushImmediately()??!1}flushImmediatelySync(){return this.managers.event?.flushImmediatelySync()??!1}sendCustomEvent(e,t,s){if(!this.managers.event){a("warn","Cannot send custom event: TraceLog not initialized",{data:{name:e}});return}let n=t;t&&typeof t=="object"&&!Array.isArray(t)&&Object.getPrototypeOf(t)!==Object.prototype&&(n=Object.assign({},t));const{valid:i,error:o,sanitizedMetadata:l}=_s(e,n);if(!i){if(this.get("mode")===Y.QA)throw new Error(`[TraceLog] Custom event "${e}" validation failed: ${o}`);a("warn",`Custom event "${e}" dropped: ${o}`);return}this.managers.event.track({type:u.CUSTOM,custom_event:{name:e,...l&&{metadata:l}}}),s?.critical===!0&&(this.managers.event.flushImmediatelySync()||a("debug","Critical event flush returned false (deferred to in-flight send or empty queue)",{data:{name:e}}))}on(e,t){this.emitter.on(e,t)}off(e,t){this.emitter.off(e,t)}setTransformer(e,t){if(typeof t!="function")throw new Error(`[TraceLog] Transformer must be a function, received: ${typeof t}`);this.transformers[e]=t}removeTransformer(e){delete this.transformers[e]}getTransformer(e){return this.transformers[e]}setCustomHeaders(e){if(typeof e!="function")throw new Error(`[TraceLog] Custom headers provider must be a function, received: ${typeof e}`);this.customHeadersProvider=e,this.managers.event&&this.managers.event.setCustomHeadersProvider(e)}removeCustomHeaders(){this.customHeadersProvider=void 0,this.managers.event&&this.managers.event.removeCustomHeadersProvider()}destroy(e=!1){!this.isInitialized&&!e||(Object.values(this.handlers).filter(Boolean).forEach(t=>{try{t.stopTracking()}catch(s){a("warn","Failed to stop tracking",{error:s})}}),this.suppressNextScrollTimer&&(clearTimeout(this.suppressNextScrollTimer),this.suppressNextScrollTimer=null),this.pageUnloadHandler&&(window.removeEventListener("pagehide",this.pageUnloadHandler),window.removeEventListener("beforeunload",this.pageUnloadHandler),this.pageUnloadHandler=null),this.pageShowHandler&&(window.removeEventListener("pageshow",this.pageShowHandler),this.pageShowHandler=null),this.visibilityFlushHandler&&(document.removeEventListener("visibilitychange",this.visibilityFlushHandler),this.visibilityFlushHandler=null),this.managers.event?.flushImmediatelySync(),this.managers.event?.stop(),this.emitter.removeAllListeners(),this.transformers.beforeSend=void 0,this.transformers.beforeBatch=void 0,this.customHeadersProvider=void 0,this.set("suppressNextScroll",!1),this.set("sessionId",null),this.set("identity",void 0),this.clearPersistedIdentity(),this.integrationInstances.shopifyCartLinker?.deactivate(),this.integrationInstances={},this.isInitialized=!1,this.handlers={},this.managers={})}setupState(e={}){this.set("config",e);const t=Ls.getId(this.managers.storage);this.set("userId",t);const s=hs(e);this.set("collectApiUrls",s);const n=Qt();this.set("device",n);const i=we(window.location.href,e.sensitiveQueryParams);this.set("pageUrl",i),os()&&this.set("mode",Y.QA)}getConfig(){return this.get("config")}getCollectApiUrls(){return this.get("collectApiUrls")}getEventManager(){return this.managers.event}getSessionId(){return this.get("sessionId")}getUserId(){return this.get("userId")}validateGlobalMetadata(e){if(typeof e!="object"||e===null||Array.isArray(e))return{valid:!1,error:"Global metadata must be a plain object"};const t=ht("Global",e,"globalMetadata");return t.valid?{valid:!0}:{valid:!1,error:t.error}}updateGlobalMetadata(e){const t=this.validateGlobalMetadata(e);if(!t.valid)throw new Error(`[TraceLog] Invalid global metadata: ${t.error}`);const n={...this.get("config"),globalMetadata:e};this.set("config",n),a("debug","Global metadata updated (replaced)",{data:{keys:Object.keys(e)}})}mergeGlobalMetadata(e){const t=this.validateGlobalMetadata(e);if(!t.valid)throw new Error(`[TraceLog] Invalid global metadata: ${t.error}`);const s=this.get("config"),i={...s.globalMetadata??{},...e},o={...s,globalMetadata:i};this.set("config",o),a("debug","Global metadata updated (merged)",{data:{keys:Object.keys(e)}})}identify(e,t){if(!e||typeof e!="string"||e.trim().length===0){a("warn","identify() called with invalid userId",{data:{type:typeof e,length:typeof e=="string"?e.trim().length:0}});return}if(e.trim().length>256){a("warn","identify() userId exceeds 256 characters",{data:{length:e.trim().length}});return}const s=e.trim(),n=dt(t),i={userId:s,...n?{traits:n}:{}};this.set("identity",i),this.persistIdentity(i),a("debug","Visitor identified",{data:{userIdLength:s.length,traitKeys:n?Object.keys(n):[]}})}async resetIdentity(){await this.managers.event?.flushImmediately(),this.set("identity",void 0),this.clearPersistedIdentity();const e=at();this.managers.storage.setItem(me,e),this.set("userId",e),this.set("hasStartSession",!1),this.set("sessionId",null),this.handlers.session?.stopTracking(),this.handlers.session?.startTracking(),a("debug","Identity reset, new UUID generated")}getProjectId(){return this.get("config")?.integrations?.tracelog?.projectId??"custom"}persistIdentity(e){try{const t=this.getProjectId(),s=ge(t);this.managers.storage.setItem(s,JSON.stringify(e))}catch{a("debug","Failed to persist identity to localStorage")}}loadPersistedIdentity(){const e=this.managers.storage,t=this.getProjectId(),s=ge(t);try{const n=e.getItem(H);if(n){const i=JSON.parse(n);if(e.removeItem(H),!this.isValidIdentityData(i)){a("debug","Invalid pending identity in localStorage, discarded");return}const o={...i,userId:i.userId.trim()};e.setItem(s,JSON.stringify(o)),this.set("identity",o),a("debug","Migrated pending identity to project-scoped key");return}}catch{e.removeItem(H)}try{const n=e.getItem(s);if(n){const i=JSON.parse(n);if(!this.isValidIdentityData(i)){e.removeItem(s),a("debug","Invalid persisted identity in localStorage, discarded");return}const o={...i,userId:i.userId.trim()};this.set("identity",o),a("debug","Loaded persisted identity")}}catch{a("debug","Failed to load persisted identity")}}isValidIdentityData(e){if(!e||typeof e!="object")return!1;const{userId:t,traits:s}=e;if(typeof t!="string"||t.trim().length===0||t.trim().length>256)return!1;if(s!==void 0){if(typeof s!="object"||s===null||Array.isArray(s))return!1;for(const n of Object.values(s))if(typeof n!="string")return!1}return!0}clearPersistedIdentity(){try{const e=this.managers.storage,t=this.getProjectId();e.removeItem(ge(t)),e.removeItem(H)}catch{a("debug","Failed to clear persisted identity")}}setupPageLifecycleListeners(){this.pageUnloadHandler=()=>{this.managers.event?.flushImmediatelySync()},this.pageShowHandler=e=>{e.persisted&&this.managers.event?.recoverPersistedEvents().catch(t=>{a("warn","Failed to recover persisted events on bfcache restore",{error:t})})},this.visibilityFlushHandler=()=>{typeof document>"u"||!document.hidden||this.get("config").flushOnPageHidden!==!1&&this.managers.event?.flushImmediatelySync()},window.addEventListener("pagehide",this.pageUnloadHandler),window.addEventListener("beforeunload",this.pageUnloadHandler),window.addEventListener("pageshow",this.pageShowHandler),document.addEventListener("visibilitychange",this.visibilityFlushHandler)}initializeHandlers(){const e=this.get("config");this.handlers.session=new Cs(this.managers.storage,this.managers.event),this.handlers.session.startTracking();const t=()=>{this.set("suppressNextScroll",!0),this.suppressNextScrollTimer&&clearTimeout(this.suppressNextScrollTimer),this.suppressNextScrollTimer=window.setTimeout(()=>{this.set("suppressNextScroll",!1)},500)};if(this.handlers.pageView=new Rs(this.managers.event,t),this.handlers.pageView.startTracking(),this.handlers.click=new Ns(this.managers.event),this.handlers.click.startTracking(),this.handlers.scroll=new Os(this.managers.event),this.handlers.scroll.startTracking(),this.handlers.performance=new Hs(this.managers.event),this.handlers.performance.startTracking().catch(s=>{a("warn","Failed to start performance tracking",{error:s})}),this.handlers.error=new oe(this.managers.event),this.handlers.error.startTracking(),e.viewport&&(this.handlers.viewport=new Ps(this.managers.event),this.handlers.viewport.startTracking()),e.integrations?.tracelog?.shopify){const s=new Vs;s.activate(),this.integrationInstances.shopifyCartLinker=s,this.emitter.on(j.EVENT,n=>{n.type===u.SESSION_START&&s.onSessionChange()})}}}const D=[],C=[];let k=null,h=null,O=!1,_=!1,V=null;const $s=async r=>typeof window>"u"||typeof document>"u"?{sessionId:""}:(_=!1,window.__traceLogDisabled===!0?{sessionId:""}:h?{sessionId:h.getSessionId()??""}:(O&&V||(O=!0,V=(async()=>{try{const e=Ss(r??{}),t=new Fs;try{D.forEach(({event:o,callback:l})=>{t.on(o,l)}),D.length=0,C.forEach(({hook:o,fn:l})=>{o==="beforeSend"?t.setTransformer("beforeSend",l):t.setTransformer("beforeBatch",l)}),C.length=0,k&&(t.setCustomHeaders(k),k=null);const s=t.init(e),n=new Promise((o,l)=>{setTimeout(()=>{l(new Error("[TraceLog] Initialization timeout after 10000ms"))},1e4)}),i=await Promise.race([s,n]);return h=t,i}catch(s){try{t.destroy(!0)}catch(n){a("error","Failed to cleanup partially initialized app",{error:n})}throw s}}catch(e){throw h=null,e}finally{O=!1,V=null}})()),V)),xs=(r,e,t)=>{if(!(typeof window>"u"||typeof document>"u")){if(!h)throw new Error("[TraceLog] TraceLog not initialized. Please call init() first.");if(_)throw new Error("[TraceLog] Cannot send events while TraceLog is being destroyed");h.sendCustomEvent(r,e,t)}},Bs=async()=>typeof window>"u"||typeof document>"u"||!h||_?!1:h.flushImmediately(),Ws=()=>typeof window>"u"||typeof document>"u"||!h||_?!1:h.flushImmediatelySync(),Gs=(r,e)=>{if(!(typeof window>"u"||typeof document>"u")){if(!h||O){D.push({event:r,callback:e});return}h.on(r,e)}},Xs=(r,e)=>{if(!(typeof window>"u"||typeof document>"u")){if(!h){const t=D.findIndex(s=>s.event===r&&s.callback===e);t!==-1&&D.splice(t,1);return}h.off(r,e)}};function js(r,e){if(!(typeof window>"u"||typeof document>"u")){if(typeof e!="function")throw new Error(`[TraceLog] Transformer must be a function, received: ${typeof e}`);if(!h||O){const t=C.findIndex(s=>s.hook===r);t!==-1&&C.splice(t,1),C.push({hook:r,fn:e});return}if(_)throw new Error("[TraceLog] Cannot set transformers while TraceLog is being destroyed");r==="beforeSend"?h.setTransformer("beforeSend",e):h.setTransformer("beforeBatch",e)}}const Ks={init:$s,event:xs,on:Gs,off:Xs,setTransformer:js,removeTransformer:r=>{if(!(typeof window>"u"||typeof document>"u")){if(!h){const e=C.findIndex(t=>t.hook===r);e!==-1&&C.splice(e,1);return}if(_)throw new Error("[TraceLog] Cannot remove transformers while TraceLog is being destroyed");h.removeTransformer(r)}},setCustomHeaders:r=>{if(!(typeof window>"u"||typeof document>"u")){if(typeof r!="function")throw new Error(`[TraceLog] Custom headers provider must be a function, received: ${typeof r}`);if(!h||O){k=r;return}if(_)throw new Error("[TraceLog] Cannot set custom headers while TraceLog is being destroyed");h.setCustomHeaders(r)}},removeCustomHeaders:()=>{if(!(typeof window>"u"||typeof document>"u")){if(!h){k=null;return}if(_)throw new Error("[TraceLog] Cannot remove custom headers while TraceLog is being destroyed");h.removeCustomHeaders()}},isInitialized:()=>typeof window>"u"||typeof document>"u"?!1:h!==null,getSessionId:()=>typeof window>"u"||typeof document>"u"||!h?null:h.getSessionId(),getUserId:()=>typeof window>"u"||typeof document>"u"||!h?null:h.getUserId(),destroy:()=>{if(!(typeof window>"u"||typeof document>"u")){if(_)throw new Error("[TraceLog] Destroy operation already in progress");if(!h){_=!1;return}_=!0;try{h.destroy(),h=null,O=!1,V=null,D.length=0,C.length=0,k=null,_=!1}catch(r){h=null,O=!1,V=null,D.length=0,C.length=0,k=null,_=!1,a("warn","Error during destroy, forced cleanup completed",{error:r})}}},setQaMode:r=>{typeof window>"u"||typeof document>"u"||as(r)},updateGlobalMetadata:r=>{if(!(typeof window>"u"||typeof document>"u")){if(!h)throw new Error("[TraceLog] TraceLog not initialized. Please call init() first.");if(_)throw new Error("[TraceLog] Cannot update metadata while TraceLog is being destroyed");h.updateGlobalMetadata(r)}},mergeGlobalMetadata:r=>{if(!(typeof window>"u"||typeof document>"u")){if(!h)throw new Error("[TraceLog] TraceLog not initialized. Please call init() first.");if(_)throw new Error("[TraceLog] Cannot update metadata while TraceLog is being destroyed");h.mergeGlobalMetadata(r)}},identify:(r,e)=>{if(!(typeof window>"u"||typeof document>"u")){if(!r||typeof r!="string"||r.trim().length===0){a("warn","identify() called with invalid userId");return}if(r.trim().length>256){a("warn","identify() userId exceeds 256 characters");return}if(_){a("warn","Cannot identify while TraceLog is being destroyed");return}if(h){h.identify(r,e);return}try{const t=dt(e),s={userId:r.trim(),...t?{traits:t}:{}};localStorage.setItem(H,JSON.stringify(s)),a("debug","Identity persisted pre-init (will be applied on init)")}catch{a("debug","Failed to persist pre-init identity")}}},resetIdentity:async()=>{if(!(typeof window>"u"||typeof document>"u")){if(!h){try{localStorage.removeItem(H)}catch{}return}if(_)throw new Error("[TraceLog] Cannot reset identity while TraceLog is being destroyed");await h.resetIdentity()}},flushImmediately:Bs,flushImmediatelySync:Ws};var Me,P,q,Et,ae,St=-1,U=function(r){addEventListener("pageshow",(function(e){e.persisted&&(St=e.timeStamp,r(e))}),!0)},Ce=function(){var r=self.performance&&performance.getEntriesByType&&performance.getEntriesByType("navigation")[0];if(r&&r.responseStart>0&&r.responseStart<performance.now())return r},le=function(){var r=Ce();return r&&r.activationStart||0},w=function(r,e){var t=Ce(),s="navigate";return St>=0?s="back-forward-cache":t&&(document.prerendering||le()>0?s="prerender":document.wasDiscarded?s="restore":t.type&&(s=t.type.replace(/_/g,"-"))),{name:r,value:e===void 0?-1:e,rating:"good",delta:0,entries:[],id:"v4-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:s}},B=function(r,e,t){try{if(PerformanceObserver.supportedEntryTypes.includes(r)){var s=new PerformanceObserver((function(n){Promise.resolve().then((function(){e(n.getEntries())}))}));return s.observe(Object.assign({type:r,buffered:!0},t||{})),s}}catch{}},b=function(r,e,t,s){var n,i;return function(o){e.value>=0&&(o||s)&&((i=e.value-(n||0))||n===void 0)&&(n=e.value,e.delta=i,e.rating=(function(l,d){return l>d[1]?"poor":l>d[0]?"needs-improvement":"good"})(e.value,t),r(e))}},Re=function(r){requestAnimationFrame((function(){return requestAnimationFrame((function(){return r()}))}))},J=function(r){document.addEventListener("visibilitychange",(function(){document.visibilityState==="hidden"&&r()}))},ce=function(r){var e=!1;return function(){e||(r(),e=!0)}},W=-1,pt=function(){return document.visibilityState!=="hidden"||document.prerendering?1/0:0},de=function(r){document.visibilityState==="hidden"&&W>-1&&(W=r.type==="visibilitychange"?r.timeStamp:0,zs())},Tt=function(){addEventListener("visibilitychange",de,!0),addEventListener("prerenderingchange",de,!0)},zs=function(){removeEventListener("visibilitychange",de,!0),removeEventListener("prerenderingchange",de,!0)},Ne=function(){return W<0&&(W=pt(),Tt(),U((function(){setTimeout((function(){W=pt(),Tt()}),0)}))),{get firstHiddenTime(){return W}}},Z=function(r){document.prerendering?addEventListener("prerenderingchange",(function(){return r()}),!0):r()},Oe=[1800,3e3],_t=function(r,e){e=e||{},Z((function(){var t,s=Ne(),n=w("FCP"),i=B("paint",(function(o){o.forEach((function(l){l.name==="first-contentful-paint"&&(i.disconnect(),l.startTime<s.firstHiddenTime&&(n.value=Math.max(l.startTime-le(),0),n.entries.push(l),t(!0)))}))}));i&&(t=b(r,n,Oe,e.reportAllChanges),U((function(o){n=w("FCP"),t=b(r,n,Oe,e.reportAllChanges),Re((function(){n.value=performance.now()-o.timeStamp,t(!0)}))})))}))},Pe=[.1,.25],Qs=function(r,e){e=e||{},_t(ce((function(){var t,s=w("CLS",0),n=0,i=[],o=function(d){d.forEach((function(c){if(!c.hadRecentInput){var m=i[0],S=i[i.length-1];n&&c.startTime-S.startTime<1e3&&c.startTime-m.startTime<5e3?(n+=c.value,i.push(c)):(n=c.value,i=[c])}})),n>s.value&&(s.value=n,s.entries=i,t())},l=B("layout-shift",o);l&&(t=b(r,s,Pe,e.reportAllChanges),J((function(){o(l.takeRecords()),t(!0)})),U((function(){n=0,s=w("CLS",0),t=b(r,s,Pe,e.reportAllChanges),Re((function(){return t()}))})),setTimeout(t,0))})))},It=0,De=1/0,ue=0,Ys=function(r){r.forEach((function(e){e.interactionId&&(De=Math.min(De,e.interactionId),ue=Math.max(ue,e.interactionId),It=ue?(ue-De)/7+1:0)}))},vt=function(){return Me?It:performance.interactionCount||0},qs=function(){"interactionCount"in performance||Me||(Me=B("event",Ys,{type:"event",buffered:!0,durationThreshold:0}))},M=[],he=new Map,yt=0,Js=function(){var r=Math.min(M.length-1,Math.floor((vt()-yt)/50));return M[r]},Zs=[],er=function(r){if(Zs.forEach((function(n){return n(r)})),r.interactionId||r.entryType==="first-input"){var e=M[M.length-1],t=he.get(r.interactionId);if(t||M.length<10||r.duration>e.latency){if(t)r.duration>t.latency?(t.entries=[r],t.latency=r.duration):r.duration===t.latency&&r.startTime===t.entries[0].startTime&&t.entries.push(r);else{var s={id:r.interactionId,latency:r.duration,entries:[r]};he.set(s.id,s),M.push(s)}M.sort((function(n,i){return i.latency-n.latency})),M.length>10&&M.splice(10).forEach((function(n){return he.delete(n.id)}))}}},wt=function(r){var e=self.requestIdleCallback||self.setTimeout,t=-1;return r=ce(r),document.visibilityState==="hidden"?r():(t=e(r),J(r)),t},ke=[200,500],tr=function(r,e){"PerformanceEventTiming"in self&&"interactionId"in PerformanceEventTiming.prototype&&(e=e||{},Z((function(){var t;qs();var s,n=w("INP"),i=function(l){wt((function(){l.forEach(er);var d=Js();d&&d.latency!==n.value&&(n.value=d.latency,n.entries=d.entries,s())}))},o=B("event",i,{durationThreshold:(t=e.durationThreshold)!==null&&t!==void 0?t:40});s=b(r,n,ke,e.reportAllChanges),o&&(o.observe({type:"first-input",buffered:!0}),J((function(){i(o.takeRecords()),s(!0)})),U((function(){yt=vt(),M.length=0,he.clear(),n=w("INP"),s=b(r,n,ke,e.reportAllChanges)})))})))},Ve=[2500,4e3],Ue={},sr=function(r,e){e=e||{},Z((function(){var t,s=Ne(),n=w("LCP"),i=function(d){e.reportAllChanges||(d=d.slice(-1)),d.forEach((function(c){c.startTime<s.firstHiddenTime&&(n.value=Math.max(c.startTime-le(),0),n.entries=[c],t())}))},o=B("largest-contentful-paint",i);if(o){t=b(r,n,Ve,e.reportAllChanges);var l=ce((function(){Ue[n.id]||(i(o.takeRecords()),o.disconnect(),Ue[n.id]=!0,t(!0))}));["keydown","click"].forEach((function(d){addEventListener(d,(function(){return wt(l)}),{once:!0,capture:!0})})),J(l),U((function(d){n=w("LCP"),t=b(r,n,Ve,e.reportAllChanges),Re((function(){n.value=performance.now()-d.timeStamp,Ue[n.id]=!0,t(!0)}))}))}}))},He=[800,1800],rr=function r(e){document.prerendering?Z((function(){return r(e)})):document.readyState!=="complete"?addEventListener("load",(function(){return r(e)}),!0):setTimeout(e,0)},nr=function(r,e){e=e||{};var t=w("TTFB"),s=b(r,t,He,e.reportAllChanges);rr((function(){var n=Ce();n&&(t.value=Math.max(n.responseStart-le(),0),t.entries=[n],s(!0),U((function(){t=w("TTFB",0),(s=b(r,t,He,e.reportAllChanges))(!0)})))}))},ee={passive:!0,capture:!0},ir=new Date,bt=function(r,e){P||(P=e,q=r,Et=new Date,At(removeEventListener),Lt())},Lt=function(){if(q>=0&&q<Et-ir){var r={entryType:"first-input",name:P.type,target:P.target,cancelable:P.cancelable,startTime:P.timeStamp,processingStart:P.timeStamp+q};ae.forEach((function(e){e(r)})),ae=[]}},or=function(r){if(r.cancelable){var e=(r.timeStamp>1e12?new Date:performance.now())-r.timeStamp;r.type=="pointerdown"?(function(t,s){var n=function(){bt(t,s),o()},i=function(){o()},o=function(){removeEventListener("pointerup",n,ee),removeEventListener("pointercancel",i,ee)};addEventListener("pointerup",n,ee),addEventListener("pointercancel",i,ee)})(e,r):bt(e,r)}},At=function(r){["mousedown","keydown","touchstart","pointerdown"].forEach((function(e){return r(e,or,ee)}))},Fe=[100,300],ar=function(r,e){e=e||{},Z((function(){var t,s=Ne(),n=w("FID"),i=function(d){d.startTime<s.firstHiddenTime&&(n.value=d.processingStart-d.startTime,n.entries.push(d),t(!0))},o=function(d){d.forEach(i)},l=B("first-input",o);t=b(r,n,Fe,e.reportAllChanges),l&&(J(ce((function(){o(l.takeRecords()),l.disconnect()}))),U((function(){var d;n=w("FID"),t=b(r,n,Fe,e.reportAllChanges),ae=[],q=-1,P=null,At(addEventListener),d=i,ae.push(d),Lt()})))}))};const lr=Object.freeze(Object.defineProperty({__proto__:null,CLSThresholds:Pe,FCPThresholds:Oe,FIDThresholds:Fe,INPThresholds:ke,LCPThresholds:Ve,TTFBThresholds:He,onCLS:Qs,onFCP:_t,onFID:ar,onINP:tr,onLCP:sr,onTTFB:nr},Symbol.toStringTag,{value:"Module"}));f.AppConfigValidationError=E,f.DEFAULT_SESSION_TIMEOUT=9e5,f.DEFAULT_WEB_VITALS_MODE=re,f.DeviceType=A,f.EmitterEvent=j,f.ErrorType=$,f.EventType=u,f.InitializationTimeoutError=Ht,f.IntegrationValidationError=N,f.MAX_ARRAY_LENGTH=1e3,f.MAX_CUSTOM_EVENT_ARRAY_SIZE=500,f.MAX_CUSTOM_EVENT_KEYS=100,f.MAX_CUSTOM_EVENT_NAME_LENGTH=120,f.MAX_CUSTOM_EVENT_STRING_SIZE=49152,f.MAX_NESTED_OBJECT_KEYS=200,f.MAX_STRING_LENGTH=1e3,f.MAX_STRING_LENGTH_IN_ARRAY=500,f.Mode=Y,f.PII_PATTERNS=Te,f.PermanentError=R,f.RateLimitError=K,f.SamplingRateValidationError=Ee,f.ScrollDirection=Q,f.SessionTimeoutValidationError=Qe,f.SpecialApiUrl=F,f.TimeoutError=z,f.TraceLogValidationError=x,f.WEB_VITALS_GOOD_THRESHOLDS=ts,f.WEB_VITALS_NEEDS_IMPROVEMENT_THRESHOLDS=_e,f.WEB_VITALS_POOR_THRESHOLDS=nt,f.getWebVitalsThresholds=Ie,f.isPrimaryScrollEvent=Vt,f.isSecondaryScrollEvent=Ut,f.tracelog=Ks,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})})(this.TraceLog=this.TraceLog||{});typeof window<"u"&&window.TraceLog?.tracelog&&(window.tracelog=window.TraceLog.tracelog);
|
|
1
|
+
(function(h){"use strict";const L="data-tlog",_t=["button","a",'input[type="button"]','input[type="submit"]','input[type="reset"]','input[type="checkbox"]','input[type="radio"]',"select","textarea",'[role="button"]','[role="link"]','[role="tab"]','[role="menuitem"]','[role="option"]','[role="checkbox"]','[role="radio"]','[role="switch"]',"[routerLink]","[ng-click]","[data-action]","[data-click]","[data-navigate]","[data-toggle]","[onclick]",".btn",".button",".clickable",".nav-link",".menu-item","[data-testid]",'[tabindex="0"]'],vt=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],yt=["token","auth","key","session","reset","password","api_key","apikey","secret","access_token","refresh_token","verification","code","otp"],I={INVALID_SESSION_TIMEOUT:"Session timeout must be between 30000ms (30 seconds) and 86400000ms (24 hours)",INVALID_SAMPLING_RATE:"Sampling rate must be between 0 and 1",INVALID_ERROR_SAMPLING_RATE:"Error sampling must be between 0 and 1",INVALID_TRACELOG_PROJECT_ID:"TraceLog project ID is required when integration is enabled",INVALID_GLOBAL_METADATA:"Global metadata must be an object",INVALID_SENSITIVE_QUERY_PARAMS:"Sensitive query params must be an array of strings",INVALID_PAGE_VIEW_THROTTLE:"Page view throttle must be a non-negative number",INVALID_CLICK_THROTTLE:"Click throttle must be a non-negative number",INVALID_MAX_SAME_EVENT_PER_MINUTE:"Max same event per minute must be a positive number",INVALID_SEND_INTERVAL:"Send interval must be between 1000ms (1 second) and 60000ms (60 seconds)"},It=[/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,/javascript:/gi,/on\w+\s*=/gi,/<iframe\b[^<]*(?:(?!<\/iframe>)<[^<]*)*<\/iframe>/gi,/<embed\b[^>]*>/gi,/<object\b[^<]*(?:(?!<\/object>)<[^<]*)*<\/object>/gi],m="tlog",z=`${m}:qa_mode`,ue=`${m}:uid`,$e="tlog_mode",Be="qa",Xe="qa_off",de=n=>n?`${m}:${n}:queue`:`${m}:queue`,he=n=>n?`${m}:${n}:rate_limit`:`${m}:rate_limit`,wt=n=>n?`${m}:${n}:session`:`${m}:session`,At=n=>n?`${m}:${n}:broadcast`:`${m}:broadcast`,Ge=(n,e)=>`${m}:${n}:session_counts:${e}`,We=10080*60*1e3,Ke=`${m}:session_counts_last_cleanup`,je=3600*1e3,fe=n=>n?`${m}:${n}:identity`:`${m}:identity`,D=`${m}:pending_identity`;var k=(n=>(n.Localhost="localhost:8080",n.Fail="localhost:9999",n))(k||{}),w=(n=>(n.Mobile="mobile",n.Tablet="tablet",n.Desktop="desktop",n.Unknown="unknown",n))(w||{}),C=(n=>(n.EVENT="event",n.QUEUE="queue",n))(C||{});class b extends Error{constructor(e,t,s){super(e),this.statusCode=t,this.responseCode=s,this.name="PermanentError",Error.captureStackTrace&&Error.captureStackTrace(this,b)}statusCode;responseCode}class $ extends Error{constructor(e){super(e),this.name="RateLimitError",Error.captureStackTrace&&Error.captureStackTrace(this,$)}}class B extends Error{constructor(e){super(e),this.name="TimeoutError",Error.captureStackTrace&&Error.captureStackTrace(this,B)}}var d=(n=>(n.PAGE_VIEW="page_view",n.CLICK="click",n.SCROLL="scroll",n.SESSION_START="session_start",n.CUSTOM="custom",n.WEB_VITALS="web_vitals",n.ERROR="error",n))(d||{}),Q=(n=>(n.UP="up",n.DOWN="down",n))(Q||{}),U=(n=>(n.JS_ERROR="js_error",n.PROMISE_REJECTION="promise_rejection",n))(U||{}),X=(n=>(n.QA="qa",n))(X||{});class F extends Error{constructor(e,t,s){super(e),this.errorCode=t,this.layer=s,this.name=this.constructor.name,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}errorCode;layer}class S extends F{constructor(e,t="config"){super(e,"APP_CONFIG_INVALID",t)}}class ze extends F{constructor(e,t="config"){super(e,"SESSION_TIMEOUT_INVALID",t)}}class ge extends F{constructor(e,t="config"){super(e,"SAMPLING_RATE_INVALID",t)}}class me extends F{constructor(e,t="config"){super(e,"INTEGRATION_INVALID",t)}}class Mt extends F{constructor(e,t,s="runtime"){super(e,"INITIALIZATION_TIMEOUT",s),this.timeoutMs=t}timeoutMs}const Lt=["gclid","gbraid","wbraid","fbclid","ttclid"],Se=()=>{const n=new URLSearchParams(window.location.search),e={};return Lt.forEach(s=>{const r=n.get(s);r&&(e[s]=r)}),Object.keys(e).length?e:void 0},bt="background: #ff9800; color: white; font-weight: bold; padding: 2px 8px; border-radius: 3px;",Rt="background: #9e9e9e; color: white; font-weight: bold; padding: 2px 8px; border-radius: 3px;",Ct="background: #d32f2f; color: white; font-weight: bold; padding: 2px 8px; border-radius: 3px;",Nt=(n,e)=>{if(e){if(e instanceof Error){const t=e.message.replace(/\s+at\s+.*$/gm,"").replace(/\s*\([^()]+:\d+:\d+\)/g,"");return`[TraceLog] ${n}: ${t}`}if(e instanceof Error)return`[TraceLog] ${n}: ${e.message}`;if(typeof e=="string")return`[TraceLog] ${n}: ${e}`;if(typeof e=="object")try{return`[TraceLog] ${n}: ${JSON.stringify(e)}`}catch{return`[TraceLog] ${n}: [Unable to serialize error]`}return`[TraceLog] ${n}: ${String(e)}`}return`[TraceLog] ${n}`},Ot=()=>{if(typeof window>"u"||typeof sessionStorage>"u")return!1;try{return sessionStorage.getItem(z)==="true"}catch{return!1}},a=(n,e,t)=>{const{error:s,data:r,showToClient:i=!1,style:o,visibility:c}=t??{},l=s?Nt(e,s):`[TraceLog] ${e}`,u=n==="error"?"error":n==="warn"?"warn":"log";if(!Pt(c,i))return;const p=Dt(c,o),v=r!==void 0?pe(r):void 0;kt(u,l,p,v)},Pt=(n,e)=>n==="critical"?!0:n==="qa"||e?Ot():!1,Dt=(n,e)=>e!==void 0&&e!==""?e:n==="critical"?Ct:"",kt=(n,e,t,s)=>{const r=t!==void 0&&t!=="",i=r?`%c${e}`:e;s!==void 0?r?console[n](i,t,s):console[n](i,s):r?console[n](i,t):console[n](i)},pe=n=>{const e={},t=["token","password","secret","key","apikey","api_key","sessionid","session_id"];for(const[s,r]of Object.entries(n)){const i=s.toLowerCase();if(t.some(o=>i.includes(o))){e[s]="[REDACTED]";continue}r!==null&&typeof r=="object"&&!Array.isArray(r)?e[s]=pe(r):Array.isArray(r)?e[s]=r.map(o=>o!==null&&typeof o=="object"&&!Array.isArray(o)?pe(o):o):e[s]=r}return e};let Ee,Qe;const Ut=()=>{typeof window<"u"&&!Ee&&(Ee=window.matchMedia("(pointer: coarse)"),Qe=window.matchMedia("(hover: none)"))},Y="Unknown",Ft=n=>{const e=n.userAgentData?.platform;if(e!=null&&e!==""){if(/windows/i.test(e))return"Windows";if(/macos/i.test(e))return"macOS";if(/android/i.test(e))return"Android";if(/linux/i.test(e))return"Linux";if(/chromeos/i.test(e))return"ChromeOS";if(/ios/i.test(e))return"iOS"}const t=navigator.userAgent;return/Windows/i.test(t)?"Windows":/iPhone|iPad|iPod/i.test(t)?"iOS":/Mac OS X|Macintosh/i.test(t)?"macOS":/Android/i.test(t)?"Android":/CrOS/i.test(t)?"ChromeOS":/Linux/i.test(t)?"Linux":Y},Vt=n=>{const e=n.userAgentData?.brands;if(e!=null&&e.length>0){const r=e.filter(i=>!/not.?a.?brand|chromium/i.test(i.brand))[0];if(r!=null){const i=r.brand;return/google chrome/i.test(i)?"Chrome":/microsoft edge/i.test(i)?"Edge":/opera/i.test(i)?"Opera":i}}const t=navigator.userAgent;return/Edg\//i.test(t)?"Edge":/OPR\//i.test(t)?"Opera":/Chrome/i.test(t)?"Chrome":/Firefox/i.test(t)?"Firefox":/Safari/i.test(t)&&!/Chrome/i.test(t)?"Safari":Y},Ht=()=>{try{const n=navigator;if(n.userAgentData!=null&&typeof n.userAgentData.mobile=="boolean"){const l=n.userAgentData.platform;return l!=null&&l!==""&&/ipad|tablet/i.test(l)?w.Tablet:n.userAgentData.mobile?w.Mobile:w.Desktop}Ut();const e=window.innerWidth,t=Ee?.matches??!1,s=Qe?.matches??!1,r="ontouchstart"in window||navigator.maxTouchPoints>0,i=navigator.userAgent.toLowerCase(),o=/mobile|android|iphone|ipod|blackberry|iemobile|opera mini/.test(i),c=/tablet|ipad|android(?!.*mobile)/.test(i);return e<=767||o&&r?w.Mobile:e>=768&&e<=1024||c||t&&s&&r?w.Tablet:w.Desktop}catch(n){return a("debug","Device detection failed, defaulting to desktop",{error:n}),w.Desktop}},xt=()=>{try{const n=navigator;return{type:Ht(),os:Ft(n),browser:Vt(n)}}catch(n){return a("debug","Device info detection failed, using defaults",{error:n}),{type:w.Desktop,os:Y,browser:Y}}},Ye=500,qe=2e3,Je=5e3,q=50,$t=q*2,Ze=1,Bt=1e3,Xt=10,et=5e3,Gt=3,Wt=200,Kt=6e4,jt=64,zt={LCP:2500,FCP:1800,CLS:.1,INP:200,TTFB:800},Te={LCP:2500,FCP:1800,CLS:.1,INP:200,TTFB:800},tt={LCP:4e3,FCP:3e3,CLS:.25,INP:500,TTFB:1800},J="needs-improvement",_e=(n=J)=>{switch(n){case"all":return{LCP:0,FCP:0,CLS:0,INP:0,TTFB:0};case"needs-improvement":return Te;case"poor":return tt;default:return Te}},Qt=50,Yt="3.0.0",qt=()=>typeof window<"u"&&typeof sessionStorage<"u",Jt=()=>{try{const n=new URLSearchParams(window.location.search);n.delete($e);const e=n.toString(),t=window.location.pathname+(e?"?"+e:"")+window.location.hash;window.history.replaceState({},"",t)}catch{}},Zt=()=>{if(!qt())return!1;try{const e=new URLSearchParams(window.location.search).get($e),t=sessionStorage.getItem(z);let s=null;return e===Be?(s=!0,sessionStorage.setItem(z,"true"),a("info","QA Mode ACTIVE",{visibility:"qa",style:bt})):e===Xe&&(s=!1,sessionStorage.setItem(z,"false"),a("info","QA Mode DISABLED",{visibility:"qa",style:Rt})),(e===Be||e===Xe)&&Jt(),s??t==="true"}catch{return!1}},st=()=>typeof document<"u"&&document.prerendering===!0,es=["co.uk","org.uk","com.au","net.au","com.br","co.nz","co.jp","com.mx","co.in","com.cn","co.za"],nt=n=>{const e=n.toLowerCase().split(".");if(e.length<=2)return n.toLowerCase();const t=e.slice(-2).join(".");return es.includes(t)?e.slice(-3).join("."):e.slice(-2).join(".")},ts=(n,e)=>n===e?!0:nt(n)===nt(e),ve=()=>{const n=document.referrer;if(!n)return"Direct";try{const e=new URL(n).hostname.toLowerCase(),t=window.location.hostname.toLowerCase();return ts(e,t)?"Direct":n}catch(e){return a("debug","Failed to parse referrer URL, using raw value",{error:e,data:{referrer:n}}),n}},ye=()=>{const n=new URLSearchParams(window.location.search),e={};return vt.forEach(s=>{const r=n.get(s);if(r){const i=s.split("utm_")[1];e[i]=r}}),Object.keys(e).length?e:void 0},rt=()=>typeof crypto<"u"&&crypto.randomUUID?crypto.randomUUID():"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,n=>{const e=Math.random()*16|0;return(n==="x"?e:e&3|8).toString(16)});let Z=0,ee=0;const ss=()=>{let n=Date.now();n<ee&&(n=ee),n===ee?Z=(Z+1)%1e3:Z=0,ee=n;const e=Z.toString().padStart(3,"0");let t="";try{if(typeof crypto<"u"&&crypto.getRandomValues){const s=crypto.getRandomValues(new Uint8Array(3));s&&(t=Array.from(s,r=>r.toString(16).padStart(2,"0")).join(""))}}catch{}return t||(t=Math.floor(Math.random()*16777215).toString(16).padStart(6,"0")),`${n}-${e}-${t}`},ns=n=>{try{return new URL(n).protocol==="https:"}catch{return!1}},rs=n=>{try{const t=new URL(window.location.href).hostname;if(!t||typeof t!="string")throw new Error("Invalid hostname");if(t==="localhost"||t==="127.0.0.1"||/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(t))throw new Error("SaaS integration requires a domain hostname; localhost and IP addresses are not supported. For local development, omit `integrations.tracelog` to run in standalone mode (events emitted locally, no network requests), or test against a staging domain that resolves to your dev machine via /etc/hosts.");const s=t.split(".");if(!s||!Array.isArray(s)||s.length===0||s.length===1&&s[0]==="")throw new Error("Invalid hostname structure");if(s.length===1)throw new Error("Single-part domain not supported for SaaS integration");const r=s.length===2?s.join("."):s.slice(-2).join(".");if(!r||r.split(".").length<2)throw new Error("Invalid domain structure for SaaS");const i=`https://${n}.${r}/collect`;if(!ns(i))throw new Error("Generated URL failed validation");return i}catch(e){throw new Error(`Invalid SaaS URL configuration: ${e instanceof Error?e.message:String(e)}`)}},is=n=>{const e={};return n.integrations?.tracelog?.projectId&&(e.saas=rs(n.integrations.tracelog.projectId)),e},Ie=(n,e=[])=>{if(!n||typeof n!="string")return a("warn","Invalid URL provided to normalizeUrl",{data:{type:typeof n}}),n||"";try{const t=new URL(n),s=t.searchParams,r=[...new Set([...yt,...e])];let i=!1;const o=[];return r.forEach(c=>{s.has(c)&&(s.delete(c),i=!0,o.push(c))}),!i&&n.includes("?")?n:(t.search=s.toString(),t.toString())}catch(t){return a("warn","URL normalization failed, returning original",{error:t,data:{urlLength:n?.length}}),n}},it=n=>{if(!n||typeof n!="string"||n.trim().length===0)return"";let e=n;n.length>1e3&&(e=n.slice(0,Math.max(0,1e3)));let t=0;for(const r of It){const i=e;e=e.replace(r,""),i!==e&&t++}return t>0&&a("warn","XSS patterns detected and removed",{data:{patternMatches:t,valueLength:n.length}}),e.trim()},we=(n,e=0)=>{if(n==null)return null;if(typeof n=="string")return it(n);if(typeof n=="number")return!Number.isFinite(n)||n<-Number.MAX_SAFE_INTEGER||n>Number.MAX_SAFE_INTEGER?0:n;if(typeof n=="boolean")return n;if(e>10)return null;if(Array.isArray(n))return n.slice(0,1e3).map(r=>we(r,e+1)).filter(r=>r!==null);if(typeof n=="object"){const t={},r=Object.entries(n).slice(0,200);for(const[i,o]of r){const c=it(i);if(c){const l=we(o,e+1);l!==null&&(t[c]=l)}}return t}return null},os=n=>{if(typeof n!="object"||n===null)return{};try{const e=we(n);return typeof e=="object"&&e!==null?e:{}}catch(e){const t=e instanceof Error?e.message:String(e);throw new Error(`[TraceLog] Metadata sanitization failed: ${t}`)}},ot=[/\b[A-Za-z0-9._%+-]{1,64}@(?:[A-Za-z0-9-]{1,63}\.)+[A-Za-z]{2,63}\b/gi,/\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/g,/\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b/g,/\b[A-Z]{2}\d{2}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b/gi,/\b[sp]k_(test|live)_[a-zA-Z0-9]{10,}\b/gi,/Bearer\s+[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)?(?:\.[A-Za-z0-9_-]+)?/gi,/:\/\/[^:/]+:([^@]+)@/gi,/[?&](token|password|passwd|auth|secret|secret_key|private_key|auth_key|api_key|apikey|access_token)=[^&\s]+/gi],te=n=>{let e=n;for(const t of ot)e=e.replace(t,"[REDACTED]");return e},as=n=>{if(n!==void 0&&(n===null||typeof n!="object"))throw new S("Configuration must be an object","config");if(n){if(n.sessionTimeout!==void 0&&(typeof n.sessionTimeout!="number"||n.sessionTimeout<3e4||n.sessionTimeout>864e5))throw new ze(I.INVALID_SESSION_TIMEOUT,"config");if(n.globalMetadata!==void 0&&(typeof n.globalMetadata!="object"||n.globalMetadata===null))throw new S(I.INVALID_GLOBAL_METADATA,"config");if(n.integrations&&cs(n.integrations),n.sensitiveQueryParams!==void 0){if(!Array.isArray(n.sensitiveQueryParams))throw new S(I.INVALID_SENSITIVE_QUERY_PARAMS,"config");for(const e of n.sensitiveQueryParams)if(typeof e!="string")throw new S("All sensitive query params must be strings","config")}if(n.errorSampling!==void 0&&(typeof n.errorSampling!="number"||n.errorSampling<0||n.errorSampling>1))throw new ge(I.INVALID_ERROR_SAMPLING_RATE,"config");if(n.samplingRate!==void 0&&(typeof n.samplingRate!="number"||n.samplingRate<0||n.samplingRate>1))throw new ge(I.INVALID_SAMPLING_RATE,"config");if(n.pageViewThrottleMs!==void 0&&(typeof n.pageViewThrottleMs!="number"||n.pageViewThrottleMs<0))throw new S(I.INVALID_PAGE_VIEW_THROTTLE,"config");if(n.clickThrottleMs!==void 0&&(typeof n.clickThrottleMs!="number"||n.clickThrottleMs<0))throw new S(I.INVALID_CLICK_THROTTLE,"config");if(n.maxSameEventPerMinute!==void 0&&(typeof n.maxSameEventPerMinute!="number"||n.maxSameEventPerMinute<=0))throw new S(I.INVALID_MAX_SAME_EVENT_PER_MINUTE,"config");if(n.sendIntervalMs!==void 0&&(!Number.isFinite(n.sendIntervalMs)||n.sendIntervalMs<1e3||n.sendIntervalMs>6e4))throw new S(I.INVALID_SEND_INTERVAL,"config");if(n.flushOnSpaNavigation!==void 0&&typeof n.flushOnSpaNavigation!="boolean")throw new S(`Invalid flushOnSpaNavigation type: ${typeof n.flushOnSpaNavigation}. Must be a boolean`,"config");if(n.flushOnPageHidden!==void 0&&typeof n.flushOnPageHidden!="boolean")throw new S(`Invalid flushOnPageHidden type: ${typeof n.flushOnPageHidden}. Must be a boolean`,"config");if(n.webVitalsMode!==void 0){if(typeof n.webVitalsMode!="string")throw new S(`Invalid webVitalsMode type: ${typeof n.webVitalsMode}. Must be a string`,"config");const e=["all","needs-improvement","poor"];if(!e.includes(n.webVitalsMode))throw new S(`Invalid webVitalsMode: "${n.webVitalsMode}". Must be one of: ${e.join(", ")}`,"config")}if(n.webVitalsThresholds!==void 0){if(typeof n.webVitalsThresholds!="object"||n.webVitalsThresholds===null||Array.isArray(n.webVitalsThresholds))throw new S("webVitalsThresholds must be an object","config");const e=["LCP","FCP","CLS","INP","TTFB"];for(const[t,s]of Object.entries(n.webVitalsThresholds)){if(!e.includes(t))throw new S(`Invalid Web Vitals threshold key: "${t}". Must be one of: ${e.join(", ")}`,"config");if(typeof s!="number"||!Number.isFinite(s)||s<0)throw new S(`Invalid Web Vitals threshold value for ${t}: ${s}. Must be a non-negative finite number`,"config")}}}},cs=n=>{if(n&&n.tracelog){if(!n.tracelog.projectId||typeof n.tracelog.projectId!="string"||n.tracelog.projectId.trim()==="")throw new me(I.INVALID_TRACELOG_PROJECT_ID,"config");if(n.tracelog.shopify!==void 0&&typeof n.tracelog.shopify!="boolean")throw new me("tracelog.shopify must be a boolean","config")}},ls=n=>(as(n),{...n??{},sessionTimeout:n?.sessionTimeout??9e5,globalMetadata:n?.globalMetadata??{},sensitiveQueryParams:n?.sensitiveQueryParams??[],errorSampling:n?.errorSampling??Ze,samplingRate:n?.samplingRate??1,pageViewThrottleMs:n?.pageViewThrottleMs??1e3,clickThrottleMs:n?.clickThrottleMs??300,maxSameEventPerMinute:n?.maxSameEventPerMinute??60,sendIntervalMs:n?.sendIntervalMs??1e4,flushOnSpaNavigation:n?.flushOnSpaNavigation??!1,flushOnPageHidden:n?.flushOnPageHidden??!0}),Ae=(n,e=new Set)=>{if(n==null)return!0;const t=typeof n;return t==="string"||t==="number"||t==="boolean"?!0:t==="function"||t==="symbol"||t==="bigint"||e.has(n)?!1:(e.add(n),Array.isArray(n)?n.every(s=>Ae(s,e)):t==="object"?Object.values(n).every(s=>Ae(s,e)):!1)},us=n=>typeof n!="object"||n===null?!1:Ae(n),Me=n=>{if(typeof n!="object"||n===null||Array.isArray(n))return;const e={};for(const[t,s]of Object.entries(n))typeof s=="string"&&(e[t]=s);return Object.keys(e).length>0?e:void 0},ds=n=>typeof n!="string"?{valid:!1,error:"Event name must be a string"}:n.length===0?{valid:!1,error:"Event name cannot be empty"}:n.length>120?{valid:!1,error:"Event name is too long (max 120 characters)"}:n.includes("<")||n.includes(">")||n.includes("&")?{valid:!1,error:"Event name contains invalid characters"}:["constructor","prototype","__proto__","eval","function","var","let","const"].includes(n.toLowerCase())?{valid:!1,error:"Event name cannot be a reserved word"}:{valid:!0},at=(n,e,t)=>{const s=os(e),r=`${t} "${n}" metadata error`;if(!us(s))return{valid:!1,error:`${r}: object has invalid types. Valid types are string, number, boolean or string arrays.`};let i;try{i=JSON.stringify(s)}catch{return{valid:!1,error:`${r}: object contains circular references or cannot be serialized.`}}if(new TextEncoder().encode(i).byteLength>49152)return{valid:!1,error:`${r}: object is too large (max ${49152/1024} KB).`};if(Object.keys(s).length>100)return{valid:!1,error:`${r}: object has too many keys (max 100 keys).`};for(const[l,u]of Object.entries(s)){if(Array.isArray(u)){if(u.length>500)return{valid:!1,error:`${r}: array property "${l}" is too large (max 500 items).`};for(const f of u)if(typeof f=="string"&&f.length>500)return{valid:!1,error:`${r}: array property "${l}" contains strings that are too long (max 500 characters).`}}if(typeof u=="string"&&u.length>1e3)return{valid:!1,error:`${r}: property "${l}" is too long (max 1000 characters).`}}return{valid:!0,sanitizedMetadata:s}},hs=(n,e,t)=>{if(Array.isArray(e)){const s=[],r=`${t} "${n}" metadata error`;for(let i=0;i<e.length;i++){const o=e[i];if(typeof o!="object"||o===null||Array.isArray(o))return{valid:!1,error:`${r}: array item at index ${i} must be an object.`};const c=at(n,o,t);if(!c.valid)return{valid:!1,error:`${r}: array item at index ${i} is invalid: ${c.error}`};c.sanitizedMetadata&&s.push(c.sanitizedMetadata)}return{valid:!0,sanitizedMetadata:s}}return at(n,e,t)},fs=(n,e)=>{const t=ds(n);if(!t.valid)return a("error","Event name validation failed",{data:{eventName:n,error:t.error}}),t;if(!e)return{valid:!0};const s=hs(n,e,"customEvent");return s.valid||a("error","Event metadata validation failed",{data:{eventName:n,error:s.error}}),s};class gs{listeners=new Map;on(e,t){this.listeners.has(e)||this.listeners.set(e,[]),this.listeners.get(e).push(t)}off(e,t){const s=this.listeners.get(e);if(s){const r=s.indexOf(t);r>-1&&s.splice(r,1)}}emit(e,t){const s=this.listeners.get(e);s&&s.forEach(r=>{r(t)})}removeAllListeners(){this.listeners.clear()}}const ms=/https?:\/\/\S+/g,Ss=/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,ps=/0x[0-9a-fA-F]{4,}/g,Es=/(?<!\d)\d{4,}(?!\d)/g,Ts=/(['"])[^'"]{20,}\1/g;function _s(n){return n.replace(ms,"[URL]").replace(Ss,"[ID]").replace(ps,"[ADDR]").replace(Es,"[N]").replace(Ts,"$1[VAR]$1").toLowerCase().trim()}function vs(n){const e=_s(n.message),t=(n.filename??"").trim(),s=t.search(/[?#]/),r=s===-1?t:t.slice(0,s),i=n.line==null?"":String(n.line);return`${e}|${r}|${i}`}const Le={config:{}};class E{get(e){return Le[e]}set(e,t){Le[e]=t}getState(){return{...Le}}}class ys extends E{storeManager;apiUrl;lastPermanentErrorLog=null;recoveryInProgress=!1;lastMetadataTimestamp=0;pendingControllers=new Set;consecutiveNetworkFailures=0;circuitOpenedAt=0;rateLimitedUntil=0;rateLimitStorageKeyAtArm=null;constructor(e,t){super(),this.storeManager=e,this.apiUrl=t,this.migrateLegacyV2Keys(),this.rateLimitedUntil=this.loadRateLimitCooldown()}migrateLegacyV2Keys(){const e=this.get("userId")||"anonymous",t=`${de(e)}:saas`,s=`${de(e)}:custom`,r=`${he(e)}:saas`,i=`${he(e)}:custom`;try{const o=this.storeManager.getItem(t);if(o){const c=this.getQueueStorageKey(),l=this.storeManager.getItem(c);l?this.mergeLegacyIntoCurrent(c,o,l):(this.storeManager.setItem(c,o),a("debug","Migrated v2 SaaS queue to v3 unscoped key")),this.storeManager.removeItem(t)}}catch(o){a("debug","Failed to migrate v2 SaaS queue, discarding legacy key",{error:o});try{this.storeManager.removeItem(t)}catch{}}[s,r,i].forEach(o=>{try{this.storeManager.getItem(o)!==null&&this.storeManager.removeItem(o)}catch{}})}mergeLegacyIntoCurrent(e,t,s){try{const r=JSON.parse(t),i=JSON.parse(s);if(!Array.isArray(r?.events)||!Array.isArray(i?.events)){a("debug","Legacy or current queue malformed, keeping current only");return}const o=new Set(i.events.map(u=>u.id)),c=[...i.events,...r.events.filter(u=>typeof u.id=="string"&&!o.has(u.id))],l={...i,events:c,timestamp:typeof i.timestamp=="number"&&typeof r.timestamp=="number"?Math.min(i.timestamp,r.timestamp):i.timestamp??r.timestamp??Date.now(),recoveryFailures:Math.max(i.recoveryFailures??0,r.recoveryFailures??0)||void 0};this.storeManager.setItem(e,JSON.stringify(l)),a("debug","Merged v2 SaaS queue into existing v3 queue",{data:{added:c.length-i.events.length,total:c.length}})}catch(r){a("debug","Failed to merge legacy queue, keeping current",{error:r})}}getQueueStorageKey(){const e=this.get("userId")||"anonymous";return de(e)}getRateLimitStorageKey(){const e=this.get("userId")||"anonymous";return he(e)}getActiveRateLimitKey(){return this.rateLimitStorageKeyAtArm??this.getRateLimitStorageKey()}armRateLimitCooldown(e){this.rateLimitedUntil=e,this.rateLimitStorageKeyAtArm=this.getRateLimitStorageKey(),this.persistRateLimitCooldown(e)}loadRateLimitCooldown(){const e=this.getRateLimitStorageKey();try{const t=this.storeManager.getItem(e);if(!t)return 0;const s=Number(t);return!Number.isFinite(s)||s<=Date.now()?(this.storeManager.removeItem(e),0):(this.rateLimitStorageKeyAtArm=e,s)}catch{return 0}}persistRateLimitCooldown(e){const t=this.getActiveRateLimitKey();try{const s=this.storeManager.getItem(t);if(s){const r=Number(s);if(Number.isFinite(r)&&r>=e)return}this.storeManager.setItem(t,String(e))}catch{}}clearRateLimitCooldown(){const e=this.getActiveRateLimitKey();try{const t=this.storeManager.getItem(e);if(t){const s=Number(t);if(Number.isFinite(s)&&s>Date.now()){this.rateLimitedUntil=s;return}}this.storeManager.removeItem(e)}catch{}this.rateLimitedUntil=0,this.rateLimitStorageKeyAtArm=null}isRateLimited(){return this.rateLimitedUntil===0&&(this.rateLimitedUntil=this.loadRateLimitCooldown()),!(this.rateLimitedUntil===0||Date.now()>=this.rateLimitedUntil&&(this.clearRateLimitCooldown(),this.rateLimitedUntil===0))}sendEventsQueueSync(e){if(this.isRateLimited()){a("debug","Rate-limit cooldown active, skipping sync send",{data:{cooldownRemainingMs:this.rateLimitedUntil-Date.now(),events:e.events.length}});const t=this.ensureBatchMetadata(e),s=this.getPersistedData(),r=typeof s?.recoveryFailures=="number"&&Number.isFinite(s.recoveryFailures)?s.recoveryFailures:0;return this.persistEventsWithFailureCount(t,r,!0),!1}return this.apiUrl.includes(k.Fail)?(a("warn","Fail mode: simulating network failure (sync)",{data:{events:e.events.length}}),!1):this.apiUrl.includes(k.Localhost)?(a("debug","Success mode: simulating successful send (sync)",{data:{events:e.events.length}}),!0):this.sendQueueSyncInternal(e)}async sendEventsQueue(e,t){const s=this.ensureBatchMetadata(e);try{const r=await this.send(s);return r?(this.clearPersistedEvents(),t?.onSuccess?.(s.events.length,s.events,s)):(this.persistEvents(s),t?.onFailure?.()),r}catch(r){return r instanceof b?(this.logPermanentError("Permanent error, not retrying",r),this.clearPersistedEvents(),t?.onFailure?.(),!1):(this.persistEvents(s),t?.onFailure?.(),!1)}}async recoverPersistedEvents(e){if(this.recoveryInProgress){a("debug","Recovery already in progress, skipping duplicate attempt");return}this.recoveryInProgress=!0;let t=null,s=0;try{const r=this.getPersistedData();if(!r||!this.isDataRecent(r)||r.events.length===0){this.clearPersistedEvents();return}const i=r.recoveryFailures;if(s=typeof i=="number"&&Number.isFinite(i)&&i>=0?i:0,s>=3){a("debug",`Discarding persisted events after ${s} failed recovery attempts`),this.clearPersistedEvents(),e?.onFailure?.();return}if(this.isRateLimited()){a("debug","Rate-limit cooldown active, deferring recovery",{data:{cooldownRemainingMs:this.rateLimitedUntil-Date.now()}}),e?.onFailure?.();return}if(t=this.ensureBatchMetadata(this.createRecoveryBody(r)),t.events.length===0){a("debug","All persisted events exceeded the recovery age cutoff; discarding batch"),this.clearPersistedEvents();return}await this.send(t)?(this.clearPersistedEvents(),e?.onSuccess?.(r.events.length,r.events,t)):(this.persistEventsWithFailureCount(t,s+1,!0),e?.onFailure?.())}catch(r){if(r instanceof b){this.logPermanentError("Permanent error during recovery, clearing persisted events",r),this.clearPersistedEvents(),e?.onFailure?.();return}a("error","Failed to recover persisted events",{error:r}),t&&this.persistEventsWithFailureCount(t,s+1,!0),e?.onFailure?.()}finally{this.recoveryInProgress=!1}}stop(){}async backoffDelay(e){const t=100*Math.pow(2,e),s=Math.random()*100;return new Promise(r=>setTimeout(r,t+s))}async send(e){const t=this.ensureBatchMetadata(e,e._metadata?.idempotency_token);if(this.apiUrl.includes(k.Fail))return a("debug","Fail mode: simulating network failure",{data:{events:t.events.length}}),!1;if(this.apiUrl.includes(k.Localhost))return a("debug","Success mode: simulating successful send",{data:{events:t.events.length}}),!0;if(this.isRateLimited())return a("debug","Rate-limit cooldown active, skipping send",{data:{cooldownRemainingMs:this.rateLimitedUntil-Date.now(),events:t.events.length}}),!1;if(this.consecutiveNetworkFailures>=3){const c=Date.now()-this.circuitOpenedAt;if(c<12e4)return a("debug","Network circuit open, skipping send",{data:{consecutiveNetworkFailures:this.consecutiveNetworkFailures,cooldownRemainingMs:12e4-c}}),!1}const{url:s,payload:r}=this.prepareRequest(t);let i=!0,o=!1;for(let c=1;c<=3;c++)try{return(await this.sendWithTimeout(s,r)).ok?(c>1&&a("info",`Send succeeded after ${c-1} retry attempt(s)`,{data:{events:t.events.length,attempt:c}}),this.consecutiveNetworkFailures=0,this.circuitOpenedAt=0,!0):!1}catch(l){const u=c===3;if(l instanceof b)throw this.consecutiveNetworkFailures=0,this.circuitOpenedAt=0,l;if(l instanceof $){this.consecutiveNetworkFailures=0,this.circuitOpenedAt=0,i=!1,o=!0,this.armRateLimitCooldown(Date.now()+6e4),a("warn","Rate limited, skipping retries",{data:{events:e.events.length,attempt:c,cooldownMs:6e4}});break}if(l instanceof B||(i=!1),l instanceof TypeError||(o=!0),a(u?"error":"warn",`Send attempt ${c} failed${u?" (all retries exhausted)":", will retry"}`,{error:l,data:{events:e.events.length,url:s.replace(/\/\/[^/]+/,"//[DOMAIN]"),attempt:c,maxAttempts:3}}),!u){await this.backoffDelay(c);continue}return i?(a("debug","All retry attempts timed out, preserving batch for retry",{data:{events:t.events.length}}),!1):(o?(this.consecutiveNetworkFailures=0,this.circuitOpenedAt=0):(this.consecutiveNetworkFailures=Math.min(this.consecutiveNetworkFailures+1,3),this.consecutiveNetworkFailures>=3&&(this.circuitOpenedAt=Date.now())),!1)}return!1}async sendWithTimeout(e,t){const s=new AbortController;this.pendingControllers.add(s);let r=!1;const i=setTimeout(()=>{r=!0,s.abort()},15e3);try{const o=await fetch(e,{method:"POST",body:t,keepalive:!0,credentials:"include",signal:s.signal,headers:{"Content-Type":"application/json"}});if(!o.ok){if(o.status>=400&&o.status<500&&o.status!==408&&o.status!==429){const l=await this.readTraceLogErrorCode(o),u=l?`HTTP ${o.status}: ${o.statusText} (${l})`:`HTTP ${o.status}: ${o.statusText}`;throw new b(u,o.status,l)}throw o.status===429?new $(`HTTP 429: ${o.statusText}`):new Error(`HTTP ${o.status}: ${o.statusText}`)}return o}catch(o){throw o instanceof b?o:r?new B("Request timed out"):o}finally{clearTimeout(i),this.pendingControllers.delete(s)}}async readTraceLogErrorCode(e){try{const t=await e.clone().json();if(typeof t.code=="string"&&t.code.length>0&&t.code.length<=jt)return t.code}catch{}}sendQueueSyncInternal(e){const t=this.ensureBatchMetadata(e),s=this.ensureBatchMetadata(t,t._metadata?.idempotency_token),{url:r,payload:i}=this.prepareRequest(s);if(i.length>65536)return a("warn","Payload exceeds sendBeacon limit, persisting for recovery",{data:{size:i.length,limit:65536,events:s.events.length}}),this.persistEvents(t),!1;const o=new Blob([i],{type:"application/json"});if(!this.isSendBeaconAvailable())return a("warn","sendBeacon not available, persisting events for recovery"),this.persistEvents(t),!1;const c=navigator.sendBeacon(r,o);return c||(a("warn","sendBeacon rejected request, persisting events for recovery"),this.persistEvents(t)),c}prepareRequest(e){let t=Date.now();t<this.lastMetadataTimestamp&&(t=this.lastMetadataTimestamp),this.lastMetadataTimestamp=t;const s={...e,_metadata:{...e._metadata,idempotency_token:e._metadata?.idempotency_token??this.computeContentToken(e),referer:typeof window<"u"?window.location.href:void 0,timestamp:t,client_version:Yt}};return{url:this.apiUrl,payload:JSON.stringify(s)}}ensureBatchMetadata(e,t){const s=e._metadata?.idempotency_token??t??this.computeContentToken(e);return e._metadata?.idempotency_token===s?e:{...e,_metadata:{...e._metadata,idempotency_token:s}}}computeContentToken(e){const t=e.events.map(i=>i.id).sort().join(","),s=`${e.user_id}|${e.session_id}|${t}`;let r=2166136261;for(let i=0;i<s.length;i++)r^=s.charCodeAt(i),r=Math.imul(r,16777619)>>>0;return r.toString(16).padStart(8,"0")}getPersistedData(){try{const e=this.getQueueStorageKey(),t=this.storeManager.getItem(e);if(t)return JSON.parse(t)}catch(e){a("debug","Failed to parse persisted data",{error:e}),this.clearPersistedEvents()}return null}isDataRecent(e){return!e.timestamp||typeof e.timestamp!="number"?!1:(Date.now()-e.timestamp)/(1e3*60*60)<2}createRecoveryBody(e){const{timestamp:t,recoveryFailures:s,...r}=e,i=r.events??[],o=Date.now()-5184e5,c=i.filter(l=>{const u=typeof l.timestamp=="number"?l.timestamp:new Date(l.timestamp).getTime();return Number.isFinite(u)&&u>=o});return c.length<i.length&&a("debug","Recovery dropped stale events",{data:{dropped:i.length-c.length,kept:c.length}}),{...r,events:c}}persistEvents(e){const t=this.getPersistedData(),s=typeof t?.recoveryFailures=="number"&&Number.isFinite(t.recoveryFailures)?t.recoveryFailures:0;return this.persistEventsWithFailureCount(e,s)}persistEventsWithFailureCount(e,t,s=!1){try{const r=this.getPersistedData();if(!s&&r&&r.timestamp){const c=Date.now()-r.timestamp;if(c<1e3)return a("debug","Skipping persistence, another tab recently persisted events",{data:{timeSinceExisting:c}}),!0}const i={...e,timestamp:Date.now(),...t>0&&{recoveryFailures:t}},o=this.getQueueStorageKey();return this.storeManager.setItem(o,JSON.stringify(i)),!!this.storeManager.getItem(o)}catch(r){return a("debug","Failed to persist events",{error:r}),!1}}clearPersistedEvents(){try{const e=this.getQueueStorageKey();this.storeManager.removeItem(e)}catch(e){a("debug","Failed to clear persisted events",{error:e})}}isSendBeaconAvailable(){return typeof navigator<"u"&&typeof navigator.sendBeacon=="function"}logPermanentError(e,t){const s=Date.now(),r=`${t.statusCode??"unknown"}:${t.responseCode??""}`;(!this.lastPermanentErrorLog||this.lastPermanentErrorLog.key!==r||s-this.lastPermanentErrorLog.timestamp>=Kt)&&(a("error",e,{data:{status:t.statusCode,code:t.responseCode,message:t.message}}),this.lastPermanentErrorLog={key:r,timestamp:s})}}class Is extends E{bootTime;bootTimestamp;hasPerformanceNow;constructor(){if(super(),typeof window>"u"){this.hasPerformanceNow=!1,this.bootTime=0,this.bootTimestamp=0;return}this.hasPerformanceNow=typeof performance<"u"&&typeof performance.now=="function",this.hasPerformanceNow?(this.bootTime=performance.now(),this.bootTimestamp=Date.now()):(this.bootTime=0,this.bootTimestamp=Date.now(),a("debug","performance.now() not available, falling back to Date.now()"))}now(){if(!this.hasPerformanceNow)return Date.now();const e=performance.now()-this.bootTime;return Math.round(this.bootTimestamp+e)}validateTimestamp(e){const s=e-this.now();return s>12e4?{valid:!1,error:`Timestamp is ${(s/1e3/60).toFixed(2)} minutes in the future (max allowed: 2 minutes)`}:{valid:!0}}}const ws=new Set(Object.values(d));class As extends E{dataSenders;emitter;timeManager;recentEventFingerprints=new Map;perEventRateLimits=new Map;eventsQueue=[];pendingEventsBuffer=[];sendTimeoutId=null;sendInProgress=!1;consecutiveSendFailures=0;rateLimitCounter=0;rateLimitWindowStart=0;lastSessionId=null;pendingSyncFlush=!1;sessionEventCounts={total:0,[d.CLICK]:0,[d.PAGE_VIEW]:0,[d.CUSTOM]:0,[d.SCROLL]:0};saveSessionCountsDebounced=null;constructor(e,t=null){super(),this.emitter=t,this.timeManager=new Is,this.dataSenders=[];const s=this.get("collectApiUrls");s?.saas&&this.dataSenders.push(new ys(e,s.saas)),this.saveSessionCountsDebounced=this.debounce(r=>{this.saveSessionCounts(r)},500),this.cleanupExpiredSessionCounts()}async recoverPersistedEvents(){const e=this.dataSenders.map(async t=>t.recoverPersistedEvents({onSuccess:(s,r,i)=>{if(r&&r.length>0){const o=r.map(c=>c.id);this.removeProcessedEvents(o),i&&this.emitEventsQueue(i)}},onFailure:()=>{a("debug","Failed to recover persisted events")}}));await Promise.allSettled(e)}track({type:e,page_url:t,from_page_url:s,scroll_data:r,click_data:i,custom_event:o,web_vitals:c,error_data:l,page_view:u}){if(!e){a("error","Event type is required - event will be ignored");return}if(!ws.has(e)){a("error","Invalid event type - event will be ignored",{data:{type:e}});return}const f=this.get("sessionId");if(!f){this.pendingEventsBuffer.length>=100&&(this.pendingEventsBuffer.shift(),a("debug","Pending events buffer full - dropping oldest event",{data:{maxBufferSize:100}})),this.pendingEventsBuffer.push({type:e,page_url:t,from_page_url:s,scroll_data:r,click_data:i,custom_event:o,web_vitals:c,error_data:l,page_view:u});return}this.lastSessionId!==f&&(this.lastSessionId=f,this.sessionEventCounts=this.loadSessionCounts(f));const p=e===d.SESSION_START;if(p&&a("debug","Processing SESSION_START event",{data:{sessionId:f}}),!p&&!this.checkRateLimit())return;const v=e;if(!p){if(this.sessionEventCounts.total>=1e3){a("warn","Session event limit reached",{data:{type:v,total:this.sessionEventCounts.total,limit:1e3}});return}const y=this.getTypeLimitForEvent(v);if(y){const xe=this.sessionEventCounts[v];if(xe!==void 0&&xe>=y){a("warn","Session event type limit reached",{data:{type:v,count:xe,limit:y}});return}}}if(v===d.CUSTOM&&o?.name){const y=this.get("config")?.maxSameEventPerMinute??60;if(!this.checkPerEventRateLimit(o.name,y))return}const tn=v===d.SESSION_START,sn=t||this.get("pageUrl"),le=this.buildEventPayload({type:v,page_url:sn,from_page_url:s,scroll_data:r,click_data:i,custom_event:o,web_vitals:c,error_data:l,page_view:u});if(le&&!(!p&&!this.shouldSample())){if(tn){const y=this.get("sessionId");if(!y){a("error","Session start event requires sessionId - event will be ignored");return}if(this.get("hasStartSession")){a("debug","Duplicate session_start detected",{data:{sessionId:y}});return}this.set("hasStartSession",!0)}if(!this.isDuplicateEvent(le)){if(this.get("mode")===X.QA&&v===d.CUSTOM&&o){a("info",`Custom Event: ${o.name}`,{visibility:"qa",data:{name:o.name,...o.metadata&&{metadata:o.metadata}}}),this.emitEvent(le);return}if(this.addToQueue(le),!p){this.sessionEventCounts.total++,this.sessionEventCounts[v]!==void 0&&this.sessionEventCounts[v]++;const y=this.get("sessionId");y&&this.saveSessionCountsDebounced&&this.saveSessionCountsDebounced(y)}}}}stop(){this.clearSendTimeout(),this.sendInProgress=!1,this.pendingSyncFlush=!1,this.consecutiveSendFailures=0;const e=this.get("sessionId");e&&this.saveSessionCounts(e),this.eventsQueue=[],this.pendingEventsBuffer=[],this.recentEventFingerprints.clear(),this.rateLimitCounter=0,this.rateLimitWindowStart=0,this.perEventRateLimits.clear(),this.sessionEventCounts={total:0,[d.CLICK]:0,[d.PAGE_VIEW]:0,[d.CUSTOM]:0,[d.SCROLL]:0},this.lastSessionId=null,this.set("hasStartSession",!1),this.dataSenders.forEach(t=>{t.stop()})}async flushImmediately(){return this.flushEvents(!1)}flushImmediatelySync(){return this.flushEvents(!0)}getQueueLength(){return this.eventsQueue.length}getQueueEvents(){return this.eventsQueue.map(({_session_id:e,...t})=>t)}async flushQueue(){await this.flushImmediately()}clearQueue(){this.eventsQueue=[]}flushPendingEvents(){if(this.pendingEventsBuffer.length===0)return;if(!this.get("sessionId")){a("debug","Cannot flush pending events: session not initialized - keeping in buffer",{data:{bufferedEventCount:this.pendingEventsBuffer.length}});return}const t=[...this.pendingEventsBuffer];this.pendingEventsBuffer=[],t.forEach(s=>{this.track(s)})}clearSendTimeout(){this.sendTimeoutId!==null&&(clearTimeout(this.sendTimeoutId),this.sendTimeoutId=null)}isSuccessfulResult(e){return e.status==="fulfilled"&&e.value===!0}groupQueuedEventsBySession(){const e=new Map,t=[];for(const s of this.eventsQueue){if(!s._session_id){a("debug","Queued event missing _session_id, dropping",{data:{eventId:s.id,type:s.type}}),t.push(s.id);continue}const r=e.get(s._session_id);r?r.push(s):e.set(s._session_id,[s])}return t.length>0&&this.removeProcessedEvents(t),e}buildBatchesWithIds(){const e=this.groupQueuedEventsBySession();if(e.size===0)return[];const t=[];for(const[s,r]of e)t.push({batch:this.buildBatchFromGroup(s,r),eventIds:r.map(i=>i.id)});return t}flushEvents(e){if(this.eventsQueue.length===0)return e?!0:Promise.resolve(!0);if(!e&&this.sendInProgress)return a("debug","Async flush skipped: send already in progress"),Promise.resolve(!1);const t=this.buildBatchesWithIds();if(t.length===0)return e?!0:Promise.resolve(!0);if(this.dataSenders.length===0){for(const{batch:s,eventIds:r}of t)this.removeProcessedEvents(r),this.emitEventsQueue(s);return this.clearSendTimeout(),e?!0:Promise.resolve(!0)}if(e&&this.sendInProgress){const s=t.reduce((r,i)=>r+i.eventIds.length,0);return this.pendingSyncFlush=!0,a("debug","Sync flush deferred: async send in-flight, will retry on settle",{data:{eventCount:s}}),!1}if(e){const s=t.map(({batch:r,eventIds:i})=>this.sendBatchSync(r,i));return this.settleSendTimeout(),s.some(Boolean)}return this.sendInProgress=!0,(async()=>{try{const s=await Promise.all(t.map(async({batch:r,eventIds:i})=>this.sendBatchAsync(r,i)));return this.settleSendTimeout(),s.some(Boolean)}finally{this.sendInProgress=!1,this.drainPendingSyncFlush()}})()}settleSendTimeout(){this.eventsQueue.length===0?this.clearSendTimeout():this.scheduleSendTimeout()}drainPendingSyncFlush(){this.pendingSyncFlush&&(this.pendingSyncFlush=!1,this.flushImmediatelySync())}sendBatchSync(e,t){const r=this.dataSenders.map(i=>i.sendEventsQueueSync(e)).some(i=>i);return r?(this.removeProcessedEvents(t),this.emitEventsQueue(e)):a("debug","Sync send complete failure, events kept in queue for retry",{data:{eventCount:t.length,sessionId:e.session_id}}),r}async sendBatchAsync(e,t){const s=this.dataSenders.map(async o=>o.sendEventsQueue(e,{onSuccess:()=>{},onFailure:()=>{}})),r=await Promise.allSettled(s),i=r.some(o=>this.isSuccessfulResult(o));if(i){this.removeProcessedEvents(t),this.emitEventsQueue(e);const o=r.filter(c=>!this.isSuccessfulResult(c)).length;o>0&&a("debug","Async send completed with some failures, removed from queue and persisted per-integration",{data:{eventCount:t.length,failedCount:o,sessionId:e.session_id}})}else a("debug","Async send complete failure, events kept in queue for retry",{data:{eventCount:t.length,sessionId:e.session_id}});return i}async sendEventsQueue(){if(!(this.eventsQueue.length===0||this.sendInProgress)){this.sendInProgress=!0;try{const e=this.buildBatchesWithIds();if(e.length===0)return;if(this.dataSenders.length===0){for(const{batch:r}of e)this.emitEventsQueue(r);return}(await Promise.all(e.map(async({batch:r,eventIds:i})=>this.sendBatchAsync(r,i)))).some(Boolean)?this.consecutiveSendFailures=0:this.consecutiveSendFailures=Math.min(this.consecutiveSendFailures+1,5),this.eventsQueue.length===0?this.clearSendTimeout():this.scheduleSendTimeout()}finally{this.sendInProgress=!1,this.drainPendingSyncFlush()}}}buildBatchFromGroup(e,t){const s=new Map,r=[];for(const u of t){const f=this.createEventSignature(u);s.has(f)||r.push(f),s.set(f,u)}const i=r.map(u=>s.get(u)).filter(u=>!!u).sort((u,f)=>u.type===d.SESSION_START&&f.type!==d.SESSION_START?-1:f.type===d.SESSION_START&&u.type!==d.SESSION_START?1:u.timestamp-f.timestamp).map(({_session_id:u,...f})=>f),o=this.get("config")?.globalMetadata,c=this.get("identity");return{user_id:this.get("userId"),session_id:e,device:this.get("device"),events:i,...o&&{global_metadata:o},...c&&{identify:c}}}buildEventPayload(e){const t=this.get("sessionId");if(!t)return a("error","buildEventPayload reached without sessionId — event dropped",{data:{type:e.type},visibility:"critical"}),null;const s=e.page_url??this.get("pageUrl"),r=typeof s=="string"&&s.length>0?s:"unknown",i=this.timeManager.now(),o=this.timeManager.validateTimestamp(i);o.valid||a("warn","Event timestamp validation failed",{data:{type:e.type,error:o.error}});const c=this.get("sessionReferrer"),l=this.get("sessionUtm"),u=this.get("sessionClickIds");return{...{id:ss(),type:e.type,page_url:r,timestamp:i,...c&&{referrer:c},...e.from_page_url&&{from_page_url:e.from_page_url},...e.scroll_data&&{scroll_data:e.scroll_data},...e.click_data&&{click_data:e.click_data},...e.custom_event&&{custom_event:e.custom_event},...e.web_vitals&&{web_vitals:e.web_vitals},...e.error_data&&{error_data:e.error_data},...e.page_view&&{page_view:e.page_view},...l&&{utm:l},...u&&{click_ids:u}},_session_id:t}}isDuplicateEvent(e){const t=Date.now(),s=this.createEventFingerprint(e),r=this.recentEventFingerprints.get(s);return r&&t-r<1e3?(this.recentEventFingerprints.set(s,t),!0):(this.recentEventFingerprints.set(s,t),this.recentEventFingerprints.size>1500&&this.pruneOldFingerprints(),this.recentEventFingerprints.size>3e3&&(this.recentEventFingerprints.clear(),this.recentEventFingerprints.set(s,t),a("debug","Event fingerprint cache exceeded hard limit, cleared",{data:{hardLimit:3e3}})),!1)}pruneOldFingerprints(){const e=Date.now(),t=1e3*10;for(const[s,r]of this.recentEventFingerprints.entries())e-r>t&&this.recentEventFingerprints.delete(s);a("debug","Pruned old event fingerprints",{data:{remaining:this.recentEventFingerprints.size,cutoffMs:t}})}createEventFingerprint(e){let t=`${e.type}_${e.page_url}`;if(e.click_data){const s=Math.round((e.click_data.x||0)/10)*10,r=Math.round((e.click_data.y||0)/10)*10;t+=`_click_${s}_${r}`}return e.scroll_data&&(t+=`_scroll_${e.scroll_data.depth}_${e.scroll_data.direction}`),e.custom_event&&(t+=`_custom_${e.custom_event.name}`,e.custom_event.metadata&&(t+=`_${this.stableStringify(e.custom_event.metadata)}`)),e.web_vitals&&(t+=`_vitals_${e.web_vitals.type}`),e.error_data&&(t+=`_error_${e.error_data.type}_${e.error_data.message}`),t}createEventSignature(e){return this.createEventFingerprint(e)}stableStringify(e){return JSON.stringify(e,(t,s)=>s&&typeof s=="object"&&!Array.isArray(s)?Object.keys(s).sort().reduce((r,i)=>(r[i]=s[i],r),{}):s)}addToQueue(e){if(this.emitEvent(e),this.eventsQueue.push(e),this.eventsQueue.length>100){const t=this.eventsQueue.findIndex(r=>r.type!==d.SESSION_START),s=t>=0?this.eventsQueue.splice(t,1)[0]:this.eventsQueue.shift();a("warn","Event queue overflow, oldest non-critical event removed",{data:{maxLength:100,currentLength:this.eventsQueue.length,removedEventType:s?.type,wasCritical:s?.type===d.SESSION_START}})}this.scheduleSendTimeout(),this.eventsQueue.length>=50&&this.consecutiveSendFailures<5&&this.sendEventsQueue()}scheduleSendTimeout(){if(this.sendTimeoutId!==null)return;const e=this.calculateSendDelay();this.sendTimeoutId=window.setTimeout(()=>{this.sendTimeoutId=null,this.eventsQueue.length>0&&this.sendEventsQueue()},e)}calculateSendDelay(){const e=this.get("config")?.sendIntervalMs??1e4;if(this.consecutiveSendFailures===0)return e;const t=e*Math.pow(2,this.consecutiveSendFailures);return Math.min(t,12e4)}shouldSample(){const e=this.get("config")?.samplingRate??1;return Math.random()<e}checkRateLimit(){const e=Date.now();return e-this.rateLimitWindowStart>1e3&&(this.rateLimitCounter=0,this.rateLimitWindowStart=e),this.rateLimitCounter>=50?!1:(this.rateLimitCounter++,!0)}checkPerEventRateLimit(e,t){const s=Date.now(),i=(this.perEventRateLimits.get(e)??[]).filter(o=>s-o<6e4);return i.length>=t?(a("warn","Per-event rate limit exceeded for custom event",{data:{eventName:e,limit:t,window:`${6e4/1e3}s`}}),!1):(i.push(s),this.perEventRateLimits.set(e,i),!0)}getTypeLimitForEvent(e){return{[d.CLICK]:500,[d.PAGE_VIEW]:100,[d.CUSTOM]:500,[d.SCROLL]:120}[e]??null}removeProcessedEvents(e){const t=new Set(e);this.eventsQueue=this.eventsQueue.filter(s=>!t.has(s.id))}emitEvent(e){if(this.emitter){const{_session_id:t,...s}=e;this.emitter.emit(C.EVENT,s)}}emitEventsQueue(e){this.emitter&&this.emitter.emit(C.QUEUE,e)}debounce(e,t){let s=null;return((...r)=>{s!==null&&clearTimeout(s),s=setTimeout(()=>{e(...r),s=null},t)})}getInitialCounts(){return{total:0,[d.CLICK]:0,[d.PAGE_VIEW]:0,[d.CUSTOM]:0,[d.SCROLL]:0}}loadSessionCounts(e){if(typeof window>"u"||typeof localStorage>"u")return this.getInitialCounts();const t=this.get("userId")||"anonymous",s=Ge(t,e);try{const r=localStorage.getItem(s);if(!r)return this.getInitialCounts();const i=JSON.parse(r);return i._timestamp&&Date.now()-i._timestamp>We?(a("debug","Session counts expired, clearing",{data:{sessionId:e,age:Date.now()-i._timestamp}}),localStorage.removeItem(s),this.getInitialCounts()):typeof i.total=="number"&&typeof i[d.CLICK]=="number"&&typeof i[d.PAGE_VIEW]=="number"&&typeof i[d.CUSTOM]=="number"&&typeof i[d.SCROLL]=="number"?{total:i.total,[d.CLICK]:i[d.CLICK],[d.PAGE_VIEW]:i[d.PAGE_VIEW],[d.CUSTOM]:i[d.CUSTOM],[d.SCROLL]:i[d.SCROLL]}:(a("warn","Invalid session counts structure in localStorage, resetting",{data:{sessionId:e,parsed:i}}),localStorage.removeItem(s),a("debug","Session counts removed due to invalid/corrupted data",{data:{sessionId:e,parsed:i}}),this.getInitialCounts())}catch(r){return a("warn","Failed to load session counts from localStorage",{error:r,data:{sessionId:e}}),this.getInitialCounts()}}cleanupExpiredSessionCounts(){if(!(typeof window>"u"||typeof localStorage>"u"))try{const e=localStorage.getItem(Ke);if(e){const i=Date.now()-parseInt(e,10);if(i<je){a("debug","Skipping session counts cleanup (throttled)",{data:{timeSinceLastCleanup:i,throttleMs:je}});return}}const t=this.get("userId")||"anonymous",s=`${m}:${t}:session_counts:`,r=[];for(let i=0;i<localStorage.length;i++){const o=localStorage.key(i);if(o?.startsWith(s))try{const c=localStorage.getItem(o);if(c){const l=JSON.parse(c);l._timestamp&&Date.now()-l._timestamp>We&&r.push(o)}}catch{}}r.forEach(i=>{localStorage.removeItem(i),a("debug","Cleaned up expired session counts",{data:{key:i}})}),r.length>0&&a("info",`Cleaned up ${r.length} expired session counts entries`),localStorage.setItem(Ke,Date.now().toString())}catch(e){a("warn","Failed to cleanup expired session counts",{error:e})}}saveSessionCounts(e){const t=this.get("userId")||"anonymous",s=Ge(t,e);try{const r={...this.sessionEventCounts,_timestamp:Date.now(),_version:1};localStorage.setItem(s,JSON.stringify(r))}catch(r){a("warn","Failed to persist session counts to localStorage",{error:r,data:{sessionId:e}})}}}class Ms{static getId(e){const t=e.getItem(ue);if(t)return t;const s=rt();return e.setItem(ue,s),s}}const Ls=/^\d{13}-[a-z0-9]{9}$/;class bs extends E{storageManager;eventManager;projectId;activityHandler=null;visibilityChangeHandler=null;sessionTimeoutId=null;broadcastChannel=null;isTracking=!1;needsRenewal=!1;prerenderActivationHandler=null;constructor(e,t,s){super(),this.storageManager=e,this.eventManager=t,this.projectId=s}initCrossTabSync(){if(typeof BroadcastChannel>"u"){a("debug","BroadcastChannel not supported");return}const e=this.getProjectId();this.broadcastChannel=new BroadcastChannel(At(e)),this.broadcastChannel.onmessage=t=>{const{action:s,sessionId:r,timestamp:i,projectId:o}=t.data??{};if(o===e)if(s==="session_start"&&r&&typeof i=="number"&&i>Date.now()-5e3){this.set("sessionId",r);const c=this.loadStoredSession();this.set("sessionReferrer",c?.referrer),this.set("sessionUtm",c?.utm),this.set("sessionClickIds",c?.clickIds),this.persistSession(r,i,c?.referrer,c?.utm,c?.clickIds),this.isTracking&&this.setupSessionTimeout()}else s&&s!=="session_start"&&a("debug","Ignored BroadcastChannel message with unknown action",{data:{action:s}})}}shareSession(e){this.broadcastChannel&&typeof this.broadcastChannel.postMessage=="function"&&this.broadcastChannel.postMessage({action:"session_start",projectId:this.getProjectId(),sessionId:e,timestamp:Date.now()})}cleanupCrossTabSync(){this.broadcastChannel&&(typeof this.broadcastChannel.close=="function"&&this.broadcastChannel.close(),this.broadcastChannel=null)}recoverSession(){const e=this.loadStoredSession();if(!e)return null;if(!Ls.test(e.id))return a("warn","Invalid session ID format recovered from storage, clearing",{data:{sessionId:e.id}}),this.clearStoredSession(),null;const t=this.get("config")?.sessionTimeout??9e5;return Date.now()-e.lastActivity>t?(this.clearStoredSession(),null):e.id}persistSession(e,t=Date.now(),s,r,i){this.saveStoredSession({id:e,lastActivity:t,...s&&{referrer:s},...r&&{utm:r},...i&&{clickIds:i}})}clearStoredSession(){const e=this.getSessionStorageKey();this.storageManager.removeItem(e)}loadStoredSession(){const e=this.getSessionStorageKey(),t=this.storageManager.getItem(e);if(t!==null)try{const r=JSON.parse(t);if(r.id&&typeof r.lastActivity=="number")return r}catch{this.storageManager.removeItem(e)}const s=this.storageManager.getSessionItem(e);if(s!==null)try{const r=JSON.parse(s);if(r.id&&typeof r.lastActivity=="number")return r}catch{this.storageManager.removeSessionItem(e)}return null}saveStoredSession(e){const t=this.getSessionStorageKey(),s=JSON.stringify(e);this.storageManager.setItem(t,s),this.storageManager.setSessionItem(t,s)}getSessionStorageKey(){return wt(this.getProjectId())}getProjectId(){return this.projectId}startTracking(){if(this.isTracking){a("debug","Session tracking already active");return}const e=this.recoverSession(),t=e??this.generateSessionId();let s,r,i;if(e){const o=this.loadStoredSession();s=o?.referrer??ve(),r=o?.utm??ye(),i=o?.clickIds??Se()}else s=ve(),r=ye(),i=Se();a("debug","Session tracking initialized",{data:{sessionId:t,wasRecovered:!!e,willEmitSessionStart:!e,sessionReferrer:s,hasUtm:!!r,hasClickIds:!!i}}),this.isTracking=!0;try{if(this.set("sessionId",t),this.set("sessionReferrer",s),this.set("sessionUtm",r),this.set("sessionClickIds",i),st()){this.prerenderActivationHandler=()=>{this.prerenderActivationHandler=null,this.activateSession(t,e,s,r,i)},document.addEventListener("prerenderingchange",this.prerenderActivationHandler,{once:!0});return}this.activateSession(t,e,s,r,i)}catch(o){throw this.isTracking=!1,this.clearSessionTimeout(),this.cleanupActivityListeners(),this.cleanupLifecycleListeners(),this.cleanupCrossTabSync(),this.set("sessionId",null),o}}activateSession(e,t,s,r,i){this.persistSession(e,Date.now(),s,r,i),this.initCrossTabSync(),this.shareSession(e),t?a("debug","Session recovered, skipping SESSION_START",{data:{sessionId:e}}):(a("debug","Emitting SESSION_START event",{data:{sessionId:e}}),this.eventManager.track({type:d.SESSION_START})),this.setupSessionTimeout(),this.setupActivityListeners(),this.setupLifecycleListeners()}generateSessionId(){return`${Date.now()}-${Math.random().toString(36).substring(2,11)}`}setupSessionTimeout(){this.clearSessionTimeout();const e=this.get("config")?.sessionTimeout??9e5;this.sessionTimeoutId=setTimeout(()=>{this.enterRenewalMode()},e)}resetSessionTimeout(){this.setupSessionTimeout();const e=this.get("sessionId");e&&this.persistSession(e,Date.now(),this.get("sessionReferrer"),this.get("sessionUtm"),this.get("sessionClickIds"))}clearSessionTimeout(){this.sessionTimeoutId&&(clearTimeout(this.sessionTimeoutId),this.sessionTimeoutId=null)}setupActivityListeners(){this.activityHandler=()=>{this.needsRenewal?this.renewSession():this.resetSessionTimeout()},document.addEventListener("click",this.activityHandler,{passive:!0}),document.addEventListener("keydown",this.activityHandler,{passive:!0}),document.addEventListener("scroll",this.activityHandler,{passive:!0})}renewSession(){this.needsRenewal=!1;const e=this.generateSessionId(),t=ve(),s=ye(),r=Se();a("debug","Renewing session after timeout",{data:{newSessionId:e}}),this.set("sessionId",e),this.set("sessionReferrer",t),this.set("sessionUtm",s),this.set("sessionClickIds",r),this.persistSession(e,Date.now(),t,s,r),this.cleanupCrossTabSync(),this.initCrossTabSync(),this.shareSession(e),this.eventManager.track({type:d.SESSION_START}),this.eventManager.flushPendingEvents(),this.setupSessionTimeout()}cleanupActivityListeners(){this.activityHandler&&(document.removeEventListener("click",this.activityHandler),document.removeEventListener("keydown",this.activityHandler),document.removeEventListener("scroll",this.activityHandler),this.activityHandler=null)}setupLifecycleListeners(){this.visibilityChangeHandler||(this.visibilityChangeHandler=()=>{if(document.hidden)this.clearSessionTimeout();else{if(this.isSessionStale()){a("debug","Session expired during suspend, entering renewal mode"),this.enterRenewalMode();return}this.get("sessionId")&&this.setupSessionTimeout()}},document.addEventListener("visibilitychange",this.visibilityChangeHandler))}isSessionStale(){if(this.needsRenewal||!this.get("sessionId"))return!1;const t=this.loadStoredSession();if(!t)return!1;const s=this.get("config")?.sessionTimeout??9e5;return Date.now()-t.lastActivity>s}cleanupLifecycleListeners(){this.visibilityChangeHandler&&(document.removeEventListener("visibilitychange",this.visibilityChangeHandler),this.visibilityChangeHandler=null)}enterRenewalMode(){this.clearSessionTimeout(),this.cleanupCrossTabSync(),this.clearStoredSession(),this.set("sessionId",null),this.set("hasStartSession",!1),this.set("sessionReferrer",void 0),this.set("sessionUtm",void 0),this.set("sessionClickIds",void 0),this.needsRenewal=!0,a("debug","Session timed out, entering renewal mode")}resetSessionState(){this.clearSessionTimeout(),this.cleanupActivityListeners(),this.cleanupLifecycleListeners(),this.cleanupCrossTabSync(),this.cleanupPrerenderActivation(),this.clearStoredSession(),this.set("sessionId",null),this.set("hasStartSession",!1),this.set("sessionReferrer",void 0),this.set("sessionUtm",void 0),this.set("sessionClickIds",void 0),this.needsRenewal=!1,this.isTracking=!1}stopTracking(){this.resetSessionState()}destroy(){this.clearSessionTimeout(),this.cleanupActivityListeners(),this.cleanupCrossTabSync(),this.cleanupLifecycleListeners(),this.cleanupPrerenderActivation(),this.isTracking=!1,this.needsRenewal=!1,this.set("hasStartSession",!1)}cleanupPrerenderActivation(){this.prerenderActivationHandler&&(document.removeEventListener("prerenderingchange",this.prerenderActivationHandler),this.prerenderActivationHandler=null)}}class Rs extends E{eventManager;storageManager;sessionManager=null;destroyed=!1;constructor(e,t){super(),this.eventManager=t,this.storageManager=e}startTracking(){if(this.isActive())return;if(this.destroyed){a("debug","Cannot start tracking on destroyed handler");return}const t=this.get("config")?.integrations?.tracelog?.projectId??"custom";try{this.sessionManager=new bs(this.storageManager,this.eventManager,t),this.sessionManager.startTracking(),this.eventManager.flushPendingEvents()}catch(s){if(this.sessionManager){try{this.sessionManager.destroy()}catch{}this.sessionManager=null}throw a("error","Failed to start session tracking",{error:s}),s}}isActive(){return this.sessionManager!==null&&!this.destroyed}cleanupSessionManager(){this.sessionManager&&(this.sessionManager.stopTracking(),this.sessionManager.destroy(),this.sessionManager=null)}stopTracking(){this.cleanupSessionManager()}destroy(){this.destroyed||(this.sessionManager&&(this.sessionManager.destroy(),this.sessionManager=null),this.destroyed=!0)}}class Cs extends E{eventManager;onTrack;originalPushState;originalReplaceState;lastPageViewTime=0;constructor(e,t){super(),this.eventManager=e,this.onTrack=t}startTracking(){this.trackInitialPageView(),window.addEventListener("popstate",this.trackCurrentPage,!0),window.addEventListener("hashchange",this.trackCurrentPage,!0),this.patchHistory("pushState"),this.patchHistory("replaceState")}stopTracking(){window.removeEventListener("popstate",this.trackCurrentPage,!0),window.removeEventListener("hashchange",this.trackCurrentPage,!0),this.originalPushState&&(window.history.pushState=this.originalPushState),this.originalReplaceState&&(window.history.replaceState=this.originalReplaceState),this.lastPageViewTime=0}patchHistory(e){const t=window.history[e];e==="pushState"&&!this.originalPushState?this.originalPushState=t:e==="replaceState"&&!this.originalReplaceState&&(this.originalReplaceState=t),window.history[e]=(...s)=>{t.apply(window.history,s),this.trackCurrentPage()}}trackCurrentPage=()=>{const e=window.location.href,t=Ie(e,this.get("config").sensitiveQueryParams);if(this.get("pageUrl")===t)return;const s=Date.now(),r=this.get("config").pageViewThrottleMs??1e3;if(s-this.lastPageViewTime<r)return;this.lastPageViewTime=s,this.onTrack();const i=this.get("pageUrl");this.set("pageUrl",t);const o=this.extractPageViewData();this.eventManager.track({type:d.PAGE_VIEW,page_url:this.get("pageUrl"),from_page_url:i,...o&&{page_view:o}}),this.get("config").flushOnSpaNavigation===!0&&this.eventManager.flushImmediately()};trackInitialPageView(){const e=Ie(window.location.href,this.get("config").sensitiveQueryParams),t=this.extractPageViewData();this.lastPageViewTime=Date.now(),this.eventManager.track({type:d.PAGE_VIEW,page_url:e,...t&&{page_view:t}}),this.onTrack()}extractPageViewData(){const{referrer:e}=document,{title:t}=document;if(!(!e&&!t))return{...e&&{referrer:e},...t&&{title:t}}}}class Ns extends E{eventManager;lastClickTimes=new Map;clickHandler;lastPruneTime=0;constructor(e){super(),this.eventManager=e}startTracking(){this.clickHandler||(this.clickHandler=e=>{const t=e,s=t.target,r=typeof HTMLElement<"u"&&s instanceof HTMLElement?s:typeof HTMLElement<"u"&&s instanceof Node&&s.parentElement instanceof HTMLElement?s.parentElement:null;if(!r){a("debug","Click target not found or not an element");return}if(this.shouldIgnoreElement(r))return;const i=this.get("config")?.clickThrottleMs??300;if(i>0&&!this.checkClickThrottle(r,i))return;const o=this.findTrackingElement(r),c=this.getRelevantClickElement(r),l=this.calculateClickCoordinates(t);if(o){const f=this.extractTrackingData(o);if(f){const p=this.createCustomEventData(f);this.eventManager.track({type:d.CUSTOM,custom_event:{name:p.name,...p.value&&{metadata:{value:p.value}}}})}}if(!l){a("debug","Click skipped: invalid coordinates (likely synthetic)");return}const u=this.generateClickData(r,c,l);this.eventManager.track({type:d.CLICK,click_data:u})},window.addEventListener("click",this.clickHandler,!0))}stopTracking(){this.clickHandler&&(window.removeEventListener("click",this.clickHandler,!0),this.clickHandler=void 0),this.lastClickTimes.clear(),this.lastPruneTime=0}shouldIgnoreElement(e){return e.hasAttribute(`${L}-ignore`)?!0:e.closest(`[${L}-ignore]`)!==null}checkClickThrottle(e,t){const s=this.getElementSignature(e),r=Date.now();this.pruneThrottleCache(r);const i=this.lastClickTimes.get(s);return i!==void 0&&r-i<t?(a("debug","ClickHandler: Click suppressed by throttle",{data:{signature:s,throttleRemaining:t-(r-i)}}),!1):(this.lastClickTimes.set(s,r),!0)}pruneThrottleCache(e){if(e-this.lastPruneTime<3e4)return;this.lastPruneTime=e;const t=e-3e5;for(const[s,r]of this.lastClickTimes.entries())r<t&&this.lastClickTimes.delete(s);if(this.lastClickTimes.size>1e3){const s=Array.from(this.lastClickTimes.entries()).sort((o,c)=>o[1]-c[1]),r=this.lastClickTimes.size-1e3,i=s.slice(0,r);for(const[o]of i)this.lastClickTimes.delete(o);a("debug","ClickHandler: Pruned throttle cache",{data:{removed:i.length,remaining:this.lastClickTimes.size}})}}getElementSignature(e){if(e.id)return`#${e.id}`;const t=e.getAttribute("data-testid");if(t)return`[data-testid="${t}"]`;const s=e.getAttribute(`${L}-name`);return s?`[${L}-name="${s}"]`:this.getElementPath(e)}getElementPath(e){const t=[];let s=e;for(;s&&s!==document.body;){let r=s.tagName.toLowerCase();if(s.className){const i=s.className.split(" ")[0];i&&(r+=`.${i}`)}t.unshift(r),s=s.parentElement}return t.join(">")||"unknown"}findTrackingElement(e){return e.hasAttribute(`${L}-name`)?e:e.closest(`[${L}-name]`)}getRelevantClickElement(e){for(const t of _t)try{if(e.matches(t))return e;const s=e.closest(t);if(s)return s}catch(s){a("debug","Invalid selector in element search",{error:s,data:{selector:t}});continue}return e}calculateClickCoordinates(e){const t=e.clientX,s=e.clientY;return typeof t!="number"||typeof s!="number"||!Number.isFinite(t)||!Number.isFinite(s)||t===0&&s===0&&!e.isTrusted?null:{x:t,y:s}}extractTrackingData(e){const t=e.getAttribute(`${L}-name`),s=e.getAttribute(`${L}-value`);if(t)return{element:e,name:t,...s&&{value:s}}}generateClickData(e,t,s){const{x:r,y:i}=s,o=this.getRelevantText(e,t),c=t.getAttribute("href")??void 0;return{x:r,y:i,tag:t.tagName.toLowerCase(),...t.id&&{id:t.id},...t.className&&{class:t.className},...o&&{text:o},...c&&{href:c}}}getRelevantText(e,t){const s=e.textContent?.trim()??"",r=t.textContent?.trim()??"";if(!s&&!r)return"";let i="";return s&&s.length<=255?i=s:r.length<=255?i=r:i=r.slice(0,252)+"...",te(i)}createCustomEventData(e){return{name:e.name,...e.value&&{value:e.value}}}}class Os extends E{eventManager;containers=[];limitWarningLogged=!1;containerDiscoveryTimeoutId=null;constructor(e){super(),this.eventManager=e}startTracking(){this.limitWarningLogged=!1,this.set("scrollEventCount",0),this.tryDetectScrollContainers(0)}stopTracking(){this.containerDiscoveryTimeoutId!==null&&(clearTimeout(this.containerDiscoveryTimeoutId),this.containerDiscoveryTimeoutId=null);for(const e of this.containers)this.clearContainerTimer(e),e.element===window?window.removeEventListener("scroll",e.listener):e.element.removeEventListener("scroll",e.listener);this.containers.length=0,this.set("scrollEventCount",0),this.limitWarningLogged=!1}tryDetectScrollContainers(e){const t=this.findScrollableElements();if(this.isWindowScrollable()&&this.setupScrollContainer(window,"window"),t.length>0){for(const s of t){const r=this.getElementSelector(s);this.setupScrollContainer(s,r)}return}if(e<5){this.containerDiscoveryTimeoutId=window.setTimeout(()=>{this.containerDiscoveryTimeoutId=null,this.tryDetectScrollContainers(e+1)},200);return}this.containers.length===0&&this.setupScrollContainer(window,"window")}findScrollableElements(){if(!document.body)return[];const e=[],t=document.createTreeWalker(document.body,NodeFilter.SHOW_ELEMENT,{acceptNode:r=>{const i=r;if(!i.isConnected||!i.offsetParent)return NodeFilter.FILTER_SKIP;const o=getComputedStyle(i);return o.overflowY==="auto"||o.overflowY==="scroll"||o.overflow==="auto"||o.overflow==="scroll"?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}});let s;for(;(s=t.nextNode())&&e.length<10;){const r=s;this.isElementScrollable(r)&&e.push(r)}return e}getElementSelector(e){if(e===window)return"window";const t=e;if(t.id)return`#${t.id}`;if(t.className&&typeof t.className=="string"){const s=t.className.split(" ").filter(r=>r.trim())[0];if(s)return`.${s}`}return t.tagName.toLowerCase()}setupScrollContainer(e,t){if(this.containers.some(l=>l.element===e)||e!==window&&!this.isElementScrollable(e))return;const r=this.getScrollTop(e),i=this.calculateScrollDepth(r,this.getScrollHeight(e),this.getViewportHeight(e)),o={element:e,selector:t,lastScrollPos:r,lastDepth:i,lastEventTime:0,debounceTimer:null,listener:null},c=()=>{this.get("suppressNextScroll")||(this.clearContainerTimer(o),o.debounceTimer=window.setTimeout(()=>{const l=this.calculateScrollData(o);l&&this.processScrollEvent(o,l,Date.now()),o.debounceTimer=null},250))};o.listener=c,this.containers.push(o),e===window?window.addEventListener("scroll",c,{passive:!0}):e.addEventListener("scroll",c,{passive:!0})}processScrollEvent(e,t,s){if(!this.shouldEmitScrollEvent(e,t,s))return;e.lastEventTime=s,e.lastDepth=t.depth;const r=this.get("scrollEventCount")??0;this.set("scrollEventCount",r+1),this.eventManager.track({type:d.SCROLL,scroll_data:{...t,container_selector:e.selector}})}shouldEmitScrollEvent(e,t,s){return this.hasReachedSessionLimit()?(this.logLimitOnce(),!1):!(!this.hasElapsedMinimumInterval(e,s)||!this.hasSignificantDepthChange(e,t.depth))}hasReachedSessionLimit(){return(this.get("scrollEventCount")??0)>=120}hasElapsedMinimumInterval(e,t){return e.lastEventTime===0?!0:t-e.lastEventTime>=500}hasSignificantDepthChange(e,t){return Math.abs(t-e.lastDepth)>=5}logLimitOnce(){this.limitWarningLogged||(this.limitWarningLogged=!0,a("debug","Max scroll events per session reached",{data:{limit:120}}))}isWindowScrollable(){return document.documentElement.scrollHeight>window.innerHeight}clearContainerTimer(e){e.debounceTimer!==null&&(clearTimeout(e.debounceTimer),e.debounceTimer=null)}getScrollDirection(e,t){return e>t?Q.DOWN:Q.UP}calculateScrollDepth(e,t,s){if(t<=s)return 0;const r=t-s;return Math.min(100,Math.max(0,Math.floor(e/r*100)))}calculateScrollData(e){const{element:t,lastScrollPos:s}=e,r=this.getScrollTop(t);if(Math.abs(r-s)<10||t===window&&!this.isWindowScrollable())return null;const o=this.getViewportHeight(t),c=this.getScrollHeight(t),l=this.getScrollDirection(r,s),u=this.calculateScrollDepth(r,c,o);return e.lastScrollPos=r,{depth:u,direction:l}}getScrollTop(e){return e===window?window.scrollY:e.scrollTop}getViewportHeight(e){return e===window?window.innerHeight:e.clientHeight}getScrollHeight(e){return e===window?document.documentElement.scrollHeight:e.scrollHeight}isElementScrollable(e){const t=getComputedStyle(e),s=t.overflowY==="auto"||t.overflowY==="scroll"||t.overflow==="auto"||t.overflow==="scroll",r=e.scrollHeight>e.clientHeight;return s&&r}}const Ps="tracelog_session_id",Ds="tracelog_user_id";class ks extends E{visibilityHandler=null;pageshowHandler=null;lastSyncedKey=null;activate(){this.cleanupListeners(),this.syncCartAttribute(),this.setupListeners()}deactivate(){this.cleanupListeners(),this.lastSyncedKey=null}onSessionChange(){this.syncCartAttribute()}syncCartAttribute(){const e=this.get("sessionId");if(!e)return;const t=this.get("userId"),s=typeof t=="string"&&t.length>0?t:"",r=`${e}|${s}`;r!==this.lastSyncedKey&&(this.lastSyncedKey=r,this.postCartUpdate(e,s))}postCartUpdate(e,t){const s={[Ps]:e};t.length>0&&(s[Ds]=t);try{fetch("/cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({attributes:s}),credentials:"same-origin"}).then(r=>{r.ok||(this.lastSyncedKey=null,a("debug","Shopify cart attribute update failed",{data:{status:r.status}}))}).catch(()=>{this.lastSyncedKey=null,a("debug","Shopify cart attribute update failed")})}catch{this.lastSyncedKey=null,a("debug","Shopify cart attribute update failed")}}setupListeners(){this.visibilityHandler=()=>{document.hidden||this.syncCartAttribute()},document.addEventListener("visibilitychange",this.visibilityHandler),this.pageshowHandler=e=>{e.persisted&&this.syncCartAttribute()},window.addEventListener("pageshow",this.pageshowHandler)}cleanupListeners(){this.visibilityHandler&&(document.removeEventListener("visibilitychange",this.visibilityHandler),this.visibilityHandler=null),this.pageshowHandler&&(window.removeEventListener("pageshow",this.pageshowHandler),this.pageshowHandler=null)}}class Us{storage;sessionStorageRef;fallbackStorage=new Map;fallbackSessionStorage=new Map;constructor(){this.storage=this.initializeStorage("localStorage"),this.sessionStorageRef=this.initializeStorage("sessionStorage"),this.storage||a("debug","localStorage not available, using memory fallback"),this.sessionStorageRef||a("debug","sessionStorage not available, using memory fallback")}getItem(e){try{return this.storage?this.storage.getItem(e):this.fallbackStorage.get(e)??null}catch{return this.fallbackStorage.get(e)??null}}setItem(e,t){if(this.fallbackStorage.set(e,t),!!this.storage)try{this.storage.setItem(e,t);return}catch(s){if(!(s instanceof DOMException&&s.name==="QuotaExceededError"||s instanceof Error&&s.name==="QuotaExceededError"))return;if(a("warn","localStorage quota exceeded, attempting cleanup",{data:{key:e,valueSize:t.length}}),!this.cleanupOldData()){a("error","localStorage quota exceeded and no data to cleanup - data will not persist",{error:s,data:{key:e,valueSize:t.length}});return}try{this.storage.setItem(e,t)}catch(i){a("error","localStorage quota exceeded even after cleanup - data will not persist",{error:i,data:{key:e,valueSize:t.length}})}}}removeItem(e){try{this.storage&&this.storage.removeItem(e)}catch{}this.fallbackStorage.delete(e)}cleanupOldData(){if(!this.storage)return!1;try{const e=["tracelog_session_","tracelog_user_id","tracelog_device_id","tracelog_config"],t=[],s=[];for(let i=0;i<this.storage.length;i++){const o=this.storage.key(i);o?.startsWith("tracelog_")&&(o.startsWith("tracelog_persisted_events_")?t.push(o):e.some(c=>o.startsWith(c))||s.push(o))}const r=[...t,...s.slice(0,5)];return r.length===0?!1:(r.forEach(i=>{try{this.storage.removeItem(i)}catch{}}),!0)}catch(e){return a("error","Failed to cleanup old data",{error:e}),!1}}initializeStorage(e){if(typeof window>"u")return null;try{const t=e==="localStorage"?window.localStorage:window.sessionStorage,s="__tracelog_test__";return t.setItem(s,"test"),t.removeItem(s),t}catch{return null}}getSessionItem(e){try{return this.sessionStorageRef?this.sessionStorageRef.getItem(e):this.fallbackSessionStorage.get(e)??null}catch{return this.fallbackSessionStorage.get(e)??null}}setSessionItem(e,t){this.fallbackSessionStorage.set(e,t);try{if(this.sessionStorageRef){this.sessionStorageRef.setItem(e,t);return}}catch(s){(s instanceof DOMException&&s.name==="QuotaExceededError"||s instanceof Error&&s.name==="QuotaExceededError")&&a("error","sessionStorage quota exceeded - data will not persist",{error:s,data:{key:e,valueSize:t.length}})}}removeSessionItem(e){try{this.sessionStorageRef&&this.sessionStorageRef.removeItem(e)}catch{}this.fallbackSessionStorage.delete(e)}}class Fs extends E{eventManager;reportedByNav=new Map;navigationHistory=[];observers=[];vitalThresholds;navigationCounter=0;constructor(e){super(),this.eventManager=e,this.vitalThresholds=_e(J)}async startTracking(){const e=this.get("config"),t=e?.webVitalsMode??J;this.vitalThresholds=_e(t),e?.webVitalsThresholds&&(this.vitalThresholds={...this.vitalThresholds,...e.webVitalsThresholds}),await this.initWebVitals()}stopTracking(){this.observers.forEach((e,t)=>{try{e.disconnect()}catch(s){a("debug","Failed to disconnect performance observer",{error:s,data:{observerIndex:t}})}}),this.observers.length=0,this.reportedByNav.clear(),this.navigationHistory.length=0}observeWebVitalsFallback(){this.reportTTFB(),this.safeObserve("largest-contentful-paint",s=>{const r=s.getEntries(),i=r[r.length-1];i&&this.sendVital({type:"LCP",value:Number(i.startTime.toFixed(2))})},{type:"largest-contentful-paint",buffered:!0},!0);let e=0,t=this.getNavigationId();this.safeObserve("layout-shift",s=>{const r=this.getNavigationId();r!==t&&(e=0,t=r);const i=s.getEntries();for(const o of i){if(o.hadRecentInput===!0)continue;const c=typeof o.value=="number"?o.value:0;e+=c}this.sendVital({type:"CLS",value:Number(e.toFixed(2))})},{type:"layout-shift",buffered:!0}),this.safeObserve("paint",s=>{for(const r of s.getEntries())r.name==="first-contentful-paint"&&this.sendVital({type:"FCP",value:Number(r.startTime.toFixed(2))})},{type:"paint",buffered:!0},!0),this.safeObserve("event",s=>{let r=0;const i=s.getEntries();for(const o of i){const c=(o.processingEnd??0)-(o.startTime??0);r=Math.max(r,c)}r>0&&this.sendVital({type:"INP",value:Number(r.toFixed(2))})},{type:"event",buffered:!0})}async initWebVitals(){try{const{onLCP:e,onCLS:t,onFCP:s,onTTFB:r,onINP:i}=await Promise.resolve().then(()=>en),o=c=>l=>{const u=Number(l.value.toFixed(2));this.sendVital({type:c,value:u})};e(o("LCP"),{reportAllChanges:!1}),t(o("CLS"),{reportAllChanges:!1}),s(o("FCP"),{reportAllChanges:!1}),r(o("TTFB"),{reportAllChanges:!1}),i(o("INP"),{reportAllChanges:!1})}catch(e){a("debug","Failed to load web-vitals library, using fallback",{error:e}),this.observeWebVitalsFallback()}}reportTTFB(){try{const e=performance.getEntriesByType("navigation")[0];if(!e)return;const t=e.responseStart;typeof t=="number"&&Number.isFinite(t)&&this.sendVital({type:"TTFB",value:Number(t.toFixed(2))})}catch(e){a("debug","Failed to report TTFB",{error:e})}}sendVital(e){if(!this.shouldSendVital(e.type,e.value))return;const t=this.getNavigationId();if(t){const s=this.reportedByNav.get(t);if(s?.has(e.type))return;if(s)s.add(e.type);else if(this.reportedByNav.set(t,new Set([e.type])),this.navigationHistory.push(t),this.navigationHistory.length>Qt){const i=this.navigationHistory.shift();i&&this.reportedByNav.delete(i)}}this.trackWebVital(e.type,e.value)}trackWebVital(e,t){if(!Number.isFinite(t)){a("debug","Invalid web vital value",{data:{type:e,value:t}});return}this.eventManager.track({type:d.WEB_VITALS,web_vitals:{type:e,value:t}})}getNavigationId(){try{const e=performance.getEntriesByType("navigation")[0];if(!e)return null;const t=e.startTime||performance.now(),s=++this.navigationCounter,r=`${t.toFixed(2)}_${window.location.pathname}`;return s>1?`${r}_${s}`:r}catch(e){return a("debug","Failed to get navigation ID",{error:e}),null}}isObserverSupported(e){if(typeof PerformanceObserver>"u")return!1;const t=PerformanceObserver.supportedEntryTypes;return!t||t.includes(e)}safeObserve(e,t,s,r=!1){try{if(!this.isObserverSupported(e))return!1;const i=new PerformanceObserver((o,c)=>{try{t(o,c)}catch(l){a("debug","Observer callback failed",{error:l,data:{type:e}})}if(r)try{c.disconnect()}catch{}});return i.observe(s??{type:e,buffered:!0}),r||this.observers.push(i),!0}catch(i){return a("debug","Failed to create performance observer",{error:i,data:{type:e}}),!1}}shouldSendVital(e,t){if(typeof t!="number"||!Number.isFinite(t))return a("debug","Invalid web vital value",{data:{type:e,value:t}}),!1;const s=this.vitalThresholds[e];return!(typeof s=="number"&&t<=s)}}class se extends E{eventManager;emitter;recentErrors=new Map;pageviewSignatureCounts=new Map;errorBurstCounter=0;burstWindowStart=0;burstBackoffUntil=0;pagehideHandler=null;pageviewResetListener=null;constructor(e,t){super(),this.eventManager=e,this.emitter=t}startTracking(){window.addEventListener("error",this.handleError),window.addEventListener("unhandledrejection",this.handleRejection),this.pagehideHandler=()=>{this.resetPageviewCounter()},window.addEventListener("pagehide",this.pagehideHandler,{passive:!0}),this.emitter&&(this.pageviewResetListener=e=>{(e.type===d.SESSION_START||e.type===d.PAGE_VIEW)&&this.resetPageviewCounter()},this.emitter.on(C.EVENT,this.pageviewResetListener))}stopTracking(){window.removeEventListener("error",this.handleError),window.removeEventListener("unhandledrejection",this.handleRejection),this.pagehideHandler&&(window.removeEventListener("pagehide",this.pagehideHandler),this.pagehideHandler=null),this.emitter&&this.pageviewResetListener&&(this.emitter.off(C.EVENT,this.pageviewResetListener),this.pageviewResetListener=null),this.recentErrors.clear(),this.pageviewSignatureCounts.clear(),this.errorBurstCounter=0,this.burstWindowStart=0,this.burstBackoffUntil=0}resetPageviewCounter(){this.pageviewSignatureCounts.clear()}shouldSample(){const e=Date.now();if(e<this.burstBackoffUntil)return!1;if(e-this.burstWindowStart>Bt&&(this.errorBurstCounter=0,this.burstWindowStart=e),this.errorBurstCounter++,this.errorBurstCounter>Xt)return this.burstBackoffUntil=e+et,a("debug","Error burst detected - entering cooldown",{data:{errorsInWindow:this.errorBurstCounter,cooldownMs:et}}),!1;const s=this.get("config").errorSampling??Ze;return Math.random()<s}shouldThrottleBySignature(e){const t=vs({message:e.message,filename:e.filename,line:e.line}),s=this.pageviewSignatureCounts.get(t)??0;if(s>=Gt)return a("debug","Error throttled (pageview cap)",{data:{signature:t,count:s}}),!0;const r=s+1;return this.pageviewSignatureCounts.set(t,r),this.pageviewSignatureCounts.size>Wt&&(this.pageviewSignatureCounts.clear(),this.pageviewSignatureCounts.set(t,r)),!1}handleError=e=>{if(!this.shouldSample())return;const t=this.sanitize(e.message||"Unknown error");if(this.shouldSuppressError(U.JS_ERROR,t)||this.shouldThrottleBySignature({message:t,filename:e.filename,line:e.lineno}))return;const s=typeof e.error?.stack=="string"?this.truncateStack(e.error.stack):void 0,r=typeof e.error?.name=="string"&&e.error.name!=="Error"?e.error.name:void 0;this.eventManager.track({type:d.ERROR,error_data:{type:U.JS_ERROR,message:t,...r!==void 0&&{name:r},...e.filename!==""&&{filename:e.filename},...e.lineno!==0&&{line:e.lineno},...e.colno!==0&&{column:e.colno},...s!==void 0&&{stack:s}}})};handleRejection=e=>{if(!this.shouldSample())return;const t=this.extractRejectionMessage(e.reason),s=this.sanitize(t);if(this.shouldSuppressError(U.PROMISE_REJECTION,s)||this.shouldThrottleBySignature({message:s}))return;const r=e.reason instanceof Error&&typeof e.reason.stack=="string"?this.truncateStack(e.reason.stack):void 0,i=e.reason instanceof Error&&e.reason.name!=="Error"?e.reason.name:void 0;this.eventManager.track({type:d.ERROR,error_data:{type:U.PROMISE_REJECTION,message:s,...i!==void 0&&{name:i},...r!==void 0&&{stack:r}}})};extractRejectionMessage(e){if(e==null)return"Unknown rejection";if(typeof e=="string")return e;if(e instanceof Error)return e.message;if(typeof e=="object"&&"message"in e)return String(e.message);try{return JSON.stringify(e)}catch{return"Unserializable rejection"}}sanitize(e){const t=e.length>Ye?e.slice(0,Ye)+"...":e;return te(t)}shouldSuppressError(e,t){const s=Date.now(),r=`${e}:${t}`,i=this.recentErrors.get(r);return i!==void 0&&s-i<Je?(this.recentErrors.set(r,s),!0):(this.recentErrors.set(r,s),this.recentErrors.size>$t?(this.recentErrors.clear(),this.recentErrors.set(r,s),!1):(this.recentErrors.size>q&&this.pruneOldErrors(),!1))}static TRUNCATION_SUFFIX=`
|
|
2
|
+
...truncated`;truncateStack(e){if(e.length<=qe)return te(e);const t=qe-se.TRUNCATION_SUFFIX.length,s=e.slice(0,t)+se.TRUNCATION_SUFFIX;return te(s)}pruneOldErrors(){const e=Date.now();for(const[r,i]of this.recentErrors.entries())e-i>Je&&this.recentErrors.delete(r);if(this.recentErrors.size<=q)return;const t=Array.from(this.recentErrors.entries()).sort((r,i)=>r[1]-i[1]),s=this.recentErrors.size-q;for(let r=0;r<s;r+=1){const i=t[r];i&&this.recentErrors.delete(i[0])}}}class Vs extends E{isInitialized=!1;suppressNextScrollTimer=null;pageUnloadHandler=null;pageShowHandler=null;visibilityFlushHandler=null;prerenderActivationHandler=null;emitter=new gs;managers={};handlers={};integrationInstances={};get initialized(){return this.isInitialized}async init(e={}){if(this.isInitialized)return{sessionId:this.get("sessionId")??""};this.managers.storage=new Us;try{return this.setupState(e),this.managers.event=new As(this.managers.storage,this.emitter),this.loadPersistedIdentity(),this.initializeHandlers(),this.setupPageLifecycleListeners(),await this.managers.event.recoverPersistedEvents().catch(t=>{a("warn","Failed to recover persisted events",{error:t})}),this.isInitialized=!0,{sessionId:this.get("sessionId")??""}}catch(t){this.destroy(!0);const s=t instanceof Error?t.message:String(t);throw new Error(`[TraceLog] TraceLog initialization failed: ${s}`)}}sendCustomEvent(e,t,s){if(!this.managers.event){a("warn","Cannot send custom event: TraceLog not initialized",{data:{name:e}});return}let r=t;t&&typeof t=="object"&&!Array.isArray(t)&&Object.getPrototypeOf(t)!==Object.prototype&&(r=Object.assign({},t));const{valid:i,error:o,sanitizedMetadata:c}=fs(e,r);if(!i){if(this.get("mode")===X.QA)throw new Error(`[TraceLog] Custom event "${e}" validation failed: ${o}`);a("warn",`Custom event "${e}" dropped: ${o}`);return}this.managers.event.track({type:d.CUSTOM,custom_event:{name:e,...c&&{metadata:c}}}),s?.critical===!0&&(this.managers.event.flushImmediatelySync()||a("debug","Critical event flush returned false (deferred to in-flight send or empty queue)",{data:{name:e}}))}on(e,t){this.emitter.on(e,t)}off(e,t){this.emitter.off(e,t)}destroy(e=!1){!this.isInitialized&&!e||(Object.values(this.handlers).filter(Boolean).forEach(t=>{try{t.stopTracking()}catch(s){a("warn","Failed to stop tracking",{error:s})}}),this.suppressNextScrollTimer&&(clearTimeout(this.suppressNextScrollTimer),this.suppressNextScrollTimer=null),this.pageUnloadHandler&&(window.removeEventListener("pagehide",this.pageUnloadHandler),window.removeEventListener("beforeunload",this.pageUnloadHandler),this.pageUnloadHandler=null),this.pageShowHandler&&(window.removeEventListener("pageshow",this.pageShowHandler),this.pageShowHandler=null),this.visibilityFlushHandler&&(document.removeEventListener("visibilitychange",this.visibilityFlushHandler),this.visibilityFlushHandler=null),this.prerenderActivationHandler&&(document.removeEventListener("prerenderingchange",this.prerenderActivationHandler),this.prerenderActivationHandler=null),this.managers.event?.flushImmediatelySync(),this.managers.event?.stop(),this.emitter.removeAllListeners(),this.set("suppressNextScroll",!1),this.set("sessionId",null),this.set("identity",void 0),this.clearPersistedIdentity(),this.integrationInstances.shopifyCartLinker?.deactivate(),this.integrationInstances={},this.isInitialized=!1,this.handlers={},this.managers={})}setupState(e={}){this.set("config",e);const t=Ms.getId(this.managers.storage);this.set("userId",t);const s=is(e);this.set("collectApiUrls",s);const r=xt();this.set("device",r);const i=Ie(window.location.href,e.sensitiveQueryParams);this.set("pageUrl",i),Zt()&&this.set("mode",X.QA)}getConfig(){return this.get("config")}getCollectApiUrls(){return this.get("collectApiUrls")}getEventManager(){return this.managers.event}getSessionId(){return this.get("sessionId")}getUserId(){return this.get("userId")}identify(e,t){if(!e||typeof e!="string"||e.trim().length===0){a("warn","identify() called with invalid userId",{data:{type:typeof e,length:typeof e=="string"?e.trim().length:0}});return}if(e.trim().length>256){a("warn","identify() userId exceeds 256 characters",{data:{length:e.trim().length}});return}const s=e.trim(),r=Me(t),i={userId:s,...r?{traits:r}:{}};this.set("identity",i),this.persistIdentity(i),a("debug","Visitor identified",{data:{userIdLength:s.length,traitKeys:r?Object.keys(r):[]}})}async resetIdentity(){await this.managers.event?.flushImmediately().catch(t=>(a("debug","Failed to flush before identity reset",{error:t}),!1)),this.set("identity",void 0),this.clearPersistedIdentity();const e=rt();this.managers.storage.setItem(ue,e),this.set("userId",e),this.set("hasStartSession",!1),this.set("sessionId",null),this.handlers.session?.stopTracking(),this.handlers.session?.startTracking(),a("debug","Identity reset, new UUID generated")}getProjectId(){return this.get("config")?.integrations?.tracelog?.projectId??"custom"}persistIdentity(e){try{const t=this.getProjectId(),s=fe(t);this.managers.storage.setItem(s,JSON.stringify(e))}catch{a("debug","Failed to persist identity to localStorage")}}loadPersistedIdentity(){const e=this.managers.storage,t=this.getProjectId(),s=fe(t);try{const r=e.getItem(D);if(r){const i=JSON.parse(r);if(e.removeItem(D),!this.isValidIdentityData(i)){a("debug","Invalid pending identity in localStorage, discarded");return}const o=this.normalizePersistedIdentity(i);e.setItem(s,JSON.stringify(o)),this.set("identity",o),a("debug","Migrated pending identity to project-scoped key");return}}catch{e.removeItem(D)}try{const r=e.getItem(s);if(r){const i=JSON.parse(r);if(!this.isValidIdentityData(i)){e.removeItem(s),a("debug","Invalid persisted identity in localStorage, discarded");return}const o=this.normalizePersistedIdentity(i);this.set("identity",o),a("debug","Loaded persisted identity")}}catch{a("debug","Failed to load persisted identity")}}isValidIdentityData(e){if(!e||typeof e!="object")return!1;const{userId:t}=e;return!(typeof t!="string"||t.trim().length===0||t.trim().length>256)}normalizePersistedIdentity(e){const t=Me(e.traits);return{userId:e.userId.trim(),...t?{traits:t}:{}}}clearPersistedIdentity(){try{const e=this.managers.storage,t=this.getProjectId();e.removeItem(fe(t)),e.removeItem(D)}catch{a("debug","Failed to clear persisted identity")}}setupPageLifecycleListeners(){this.pageUnloadHandler=()=>{this.managers.event?.flushImmediatelySync()},this.pageShowHandler=e=>{e.persisted&&this.managers.event?.recoverPersistedEvents().catch(t=>{a("warn","Failed to recover persisted events on bfcache restore",{error:t})})},this.visibilityFlushHandler=()=>{typeof document>"u"||!document.hidden||this.get("config").flushOnPageHidden!==!1&&this.managers.event?.flushImmediatelySync()},window.addEventListener("pagehide",this.pageUnloadHandler),window.addEventListener("beforeunload",this.pageUnloadHandler),window.addEventListener("pageshow",this.pageShowHandler),document.addEventListener("visibilitychange",this.visibilityFlushHandler)}initializeHandlers(){const e=this.get("config");this.handlers.session=new Rs(this.managers.storage,this.managers.event),this.handlers.session.startTracking();const t=()=>{this.set("suppressNextScroll",!0),this.suppressNextScrollTimer&&clearTimeout(this.suppressNextScrollTimer),this.suppressNextScrollTimer=window.setTimeout(()=>{this.set("suppressNextScroll",!1)},500)};this.handlers.pageView=new Cs(this.managers.event,t),this.handlers.click=new Ns(this.managers.event),this.handlers.scroll=new Os(this.managers.event),this.handlers.performance=new Fs(this.managers.event),this.handlers.error=new se(this.managers.event,this.emitter);const s=()=>{if(this.handlers.pageView?.startTracking(),this.handlers.click?.startTracking(),this.handlers.scroll?.startTracking(),this.handlers.performance?.startTracking().catch(r=>{a("warn","Failed to start performance tracking",{error:r})}),this.handlers.error?.startTracking(),e.integrations?.tracelog?.shopify){const r=new ks;r.activate(),this.integrationInstances.shopifyCartLinker=r,this.emitter.on(C.EVENT,i=>{i.type===d.SESSION_START&&r.onSessionChange()})}};st()?(this.prerenderActivationHandler=()=>{this.prerenderActivationHandler=null,s()},document.addEventListener("prerenderingchange",this.prerenderActivationHandler,{once:!0})):s()}}const N=[];let g=null,V=!1,M=!1,O=null;const Hs={init:async n=>typeof window>"u"||typeof document>"u"?{sessionId:""}:(M=!1,window.__traceLogDisabled===!0?{sessionId:""}:g?{sessionId:g.getSessionId()??""}:(V&&O||(V=!0,O=(async()=>{try{const e=ls(n??{}),t=new Vs;try{N.forEach(({event:o,callback:c})=>{t.on(o,c)}),N.length=0;const s=t.init(e),r=new Promise((o,c)=>{setTimeout(()=>{c(new Error("[TraceLog] Initialization timeout after 10000ms"))},1e4)}),i=await Promise.race([s,r]);return g=t,i}catch(s){try{t.destroy(!0)}catch(r){a("error","Failed to cleanup partially initialized app",{error:r})}throw s}}catch(e){throw g=null,e}finally{V=!1,O=null}})()),O)),event:(n,e,t)=>{if(!(typeof window>"u"||typeof document>"u")){if(!g)throw new Error("[TraceLog] TraceLog not initialized. Please call init() first.");if(M)throw new Error("[TraceLog] Cannot send events while TraceLog is being destroyed");g.sendCustomEvent(n,e,t)}},on:(n,e)=>{if(!(typeof window>"u"||typeof document>"u")){if(!g||V){N.push({event:n,callback:e});return}g.on(n,e)}},off:(n,e)=>{if(!(typeof window>"u"||typeof document>"u")){if(!g){const t=N.findIndex(s=>s.event===n&&s.callback===e);t!==-1&&N.splice(t,1);return}g.off(n,e)}},isInitialized:()=>typeof window>"u"||typeof document>"u"?!1:g!==null,getSessionId:()=>typeof window>"u"||typeof document>"u"||!g?null:g.getSessionId(),getUserId:()=>typeof window>"u"||typeof document>"u"||!g?null:g.getUserId(),destroy:()=>{if(!(typeof window>"u"||typeof document>"u")){if(M)throw new Error("[TraceLog] Destroy operation already in progress");if(!g){M=!1;return}M=!0;try{g.destroy(),g=null,V=!1,O=null,N.length=0,M=!1}catch(n){g=null,V=!1,O=null,N.length=0,M=!1,a("warn","Error during destroy, forced cleanup completed",{error:n})}}},identify:(n,e)=>{if(!(typeof window>"u"||typeof document>"u")){if(!n||typeof n!="string"||n.trim().length===0){a("warn","identify() called with invalid userId");return}if(n.trim().length>256){a("warn","identify() userId exceeds 256 characters");return}if(M){a("warn","Cannot identify while TraceLog is being destroyed");return}if(g){g.identify(n,e);return}try{const t=Me(e),s={userId:n.trim(),...t?{traits:t}:{}};localStorage.setItem(D,JSON.stringify(s)),a("debug","Identity persisted pre-init (will be applied on init)")}catch{a("debug","Failed to persist pre-init identity")}}},resetIdentity:async()=>{if(!(typeof window>"u"||typeof document>"u")){if(!g){try{localStorage.removeItem(D)}catch{}return}if(M)throw new Error("[TraceLog] Cannot reset identity while TraceLog is being destroyed");await g.resetIdentity()}}};var be,R,G,ct,ne,lt=-1,P=function(n){addEventListener("pageshow",(function(e){e.persisted&&(lt=e.timeStamp,n(e))}),!0)},Re=function(){var n=self.performance&&performance.getEntriesByType&&performance.getEntriesByType("navigation")[0];if(n&&n.responseStart>0&&n.responseStart<performance.now())return n},re=function(){var n=Re();return n&&n.activationStart||0},T=function(n,e){var t=Re(),s="navigate";return lt>=0?s="back-forward-cache":t&&(document.prerendering||re()>0?s="prerender":document.wasDiscarded?s="restore":t.type&&(s=t.type.replace(/_/g,"-"))),{name:n,value:e===void 0?-1:e,rating:"good",delta:0,entries:[],id:"v4-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:s}},H=function(n,e,t){try{if(PerformanceObserver.supportedEntryTypes.includes(n)){var s=new PerformanceObserver((function(r){Promise.resolve().then((function(){e(r.getEntries())}))}));return s.observe(Object.assign({type:n,buffered:!0},t||{})),s}}catch{}},_=function(n,e,t,s){var r,i;return function(o){e.value>=0&&(o||s)&&((i=e.value-(r||0))||r===void 0)&&(r=e.value,e.delta=i,e.rating=(function(c,l){return c>l[1]?"poor":c>l[0]?"needs-improvement":"good"})(e.value,t),n(e))}},Ce=function(n){requestAnimationFrame((function(){return requestAnimationFrame((function(){return n()}))}))},W=function(n){document.addEventListener("visibilitychange",(function(){document.visibilityState==="hidden"&&n()}))},ie=function(n){var e=!1;return function(){e||(n(),e=!0)}},x=-1,ut=function(){return document.visibilityState!=="hidden"||document.prerendering?1/0:0},oe=function(n){document.visibilityState==="hidden"&&x>-1&&(x=n.type==="visibilitychange"?n.timeStamp:0,xs())},dt=function(){addEventListener("visibilitychange",oe,!0),addEventListener("prerenderingchange",oe,!0)},xs=function(){removeEventListener("visibilitychange",oe,!0),removeEventListener("prerenderingchange",oe,!0)},Ne=function(){return x<0&&(x=ut(),dt(),P((function(){setTimeout((function(){x=ut(),dt()}),0)}))),{get firstHiddenTime(){return x}}},K=function(n){document.prerendering?addEventListener("prerenderingchange",(function(){return n()}),!0):n()},Oe=[1800,3e3],ht=function(n,e){e=e||{},K((function(){var t,s=Ne(),r=T("FCP"),i=H("paint",(function(o){o.forEach((function(c){c.name==="first-contentful-paint"&&(i.disconnect(),c.startTime<s.firstHiddenTime&&(r.value=Math.max(c.startTime-re(),0),r.entries.push(c),t(!0)))}))}));i&&(t=_(n,r,Oe,e.reportAllChanges),P((function(o){r=T("FCP"),t=_(n,r,Oe,e.reportAllChanges),Ce((function(){r.value=performance.now()-o.timeStamp,t(!0)}))})))}))},Pe=[.1,.25],$s=function(n,e){e=e||{},ht(ie((function(){var t,s=T("CLS",0),r=0,i=[],o=function(l){l.forEach((function(u){if(!u.hadRecentInput){var f=i[0],p=i[i.length-1];r&&u.startTime-p.startTime<1e3&&u.startTime-f.startTime<5e3?(r+=u.value,i.push(u)):(r=u.value,i=[u])}})),r>s.value&&(s.value=r,s.entries=i,t())},c=H("layout-shift",o);c&&(t=_(n,s,Pe,e.reportAllChanges),W((function(){o(c.takeRecords()),t(!0)})),P((function(){r=0,s=T("CLS",0),t=_(n,s,Pe,e.reportAllChanges),Ce((function(){return t()}))})),setTimeout(t,0))})))},ft=0,De=1/0,ae=0,Bs=function(n){n.forEach((function(e){e.interactionId&&(De=Math.min(De,e.interactionId),ae=Math.max(ae,e.interactionId),ft=ae?(ae-De)/7+1:0)}))},gt=function(){return be?ft:performance.interactionCount||0},Xs=function(){"interactionCount"in performance||be||(be=H("event",Bs,{type:"event",buffered:!0,durationThreshold:0}))},A=[],ce=new Map,mt=0,Gs=function(){var n=Math.min(A.length-1,Math.floor((gt()-mt)/50));return A[n]},Ws=[],Ks=function(n){if(Ws.forEach((function(r){return r(n)})),n.interactionId||n.entryType==="first-input"){var e=A[A.length-1],t=ce.get(n.interactionId);if(t||A.length<10||n.duration>e.latency){if(t)n.duration>t.latency?(t.entries=[n],t.latency=n.duration):n.duration===t.latency&&n.startTime===t.entries[0].startTime&&t.entries.push(n);else{var s={id:n.interactionId,latency:n.duration,entries:[n]};ce.set(s.id,s),A.push(s)}A.sort((function(r,i){return i.latency-r.latency})),A.length>10&&A.splice(10).forEach((function(r){return ce.delete(r.id)}))}}},St=function(n){var e=self.requestIdleCallback||self.setTimeout,t=-1;return n=ie(n),document.visibilityState==="hidden"?n():(t=e(n),W(n)),t},ke=[200,500],js=function(n,e){"PerformanceEventTiming"in self&&"interactionId"in PerformanceEventTiming.prototype&&(e=e||{},K((function(){var t;Xs();var s,r=T("INP"),i=function(c){St((function(){c.forEach(Ks);var l=Gs();l&&l.latency!==r.value&&(r.value=l.latency,r.entries=l.entries,s())}))},o=H("event",i,{durationThreshold:(t=e.durationThreshold)!==null&&t!==void 0?t:40});s=_(n,r,ke,e.reportAllChanges),o&&(o.observe({type:"first-input",buffered:!0}),W((function(){i(o.takeRecords()),s(!0)})),P((function(){mt=gt(),A.length=0,ce.clear(),r=T("INP"),s=_(n,r,ke,e.reportAllChanges)})))})))},Ue=[2500,4e3],Fe={},zs=function(n,e){e=e||{},K((function(){var t,s=Ne(),r=T("LCP"),i=function(l){e.reportAllChanges||(l=l.slice(-1)),l.forEach((function(u){u.startTime<s.firstHiddenTime&&(r.value=Math.max(u.startTime-re(),0),r.entries=[u],t())}))},o=H("largest-contentful-paint",i);if(o){t=_(n,r,Ue,e.reportAllChanges);var c=ie((function(){Fe[r.id]||(i(o.takeRecords()),o.disconnect(),Fe[r.id]=!0,t(!0))}));["keydown","click"].forEach((function(l){addEventListener(l,(function(){return St(c)}),{once:!0,capture:!0})})),W(c),P((function(l){r=T("LCP"),t=_(n,r,Ue,e.reportAllChanges),Ce((function(){r.value=performance.now()-l.timeStamp,Fe[r.id]=!0,t(!0)}))}))}}))},Ve=[800,1800],Qs=function n(e){document.prerendering?K((function(){return n(e)})):document.readyState!=="complete"?addEventListener("load",(function(){return n(e)}),!0):setTimeout(e,0)},Ys=function(n,e){e=e||{};var t=T("TTFB"),s=_(n,t,Ve,e.reportAllChanges);Qs((function(){var r=Re();r&&(t.value=Math.max(r.responseStart-re(),0),t.entries=[r],s(!0),P((function(){t=T("TTFB",0),(s=_(n,t,Ve,e.reportAllChanges))(!0)})))}))},j={passive:!0,capture:!0},qs=new Date,pt=function(n,e){R||(R=e,G=n,ct=new Date,Tt(removeEventListener),Et())},Et=function(){if(G>=0&&G<ct-qs){var n={entryType:"first-input",name:R.type,target:R.target,cancelable:R.cancelable,startTime:R.timeStamp,processingStart:R.timeStamp+G};ne.forEach((function(e){e(n)})),ne=[]}},Js=function(n){if(n.cancelable){var e=(n.timeStamp>1e12?new Date:performance.now())-n.timeStamp;n.type=="pointerdown"?(function(t,s){var r=function(){pt(t,s),o()},i=function(){o()},o=function(){removeEventListener("pointerup",r,j),removeEventListener("pointercancel",i,j)};addEventListener("pointerup",r,j),addEventListener("pointercancel",i,j)})(e,n):pt(e,n)}},Tt=function(n){["mousedown","keydown","touchstart","pointerdown"].forEach((function(e){return n(e,Js,j)}))},He=[100,300],Zs=function(n,e){e=e||{},K((function(){var t,s=Ne(),r=T("FID"),i=function(l){l.startTime<s.firstHiddenTime&&(r.value=l.processingStart-l.startTime,r.entries.push(l),t(!0))},o=function(l){l.forEach(i)},c=H("first-input",o);t=_(n,r,He,e.reportAllChanges),c&&(W(ie((function(){o(c.takeRecords()),c.disconnect()}))),P((function(){var l;r=T("FID"),t=_(n,r,He,e.reportAllChanges),ne=[],G=-1,R=null,Tt(addEventListener),l=i,ne.push(l),Et()})))}))};const en=Object.freeze(Object.defineProperty({__proto__:null,CLSThresholds:Pe,FCPThresholds:Oe,FIDThresholds:He,INPThresholds:ke,LCPThresholds:Ue,TTFBThresholds:Ve,onCLS:$s,onFCP:ht,onFID:Zs,onINP:js,onLCP:zs,onTTFB:Ys},Symbol.toStringTag,{value:"Module"}));h.AppConfigValidationError=S,h.DEFAULT_SESSION_TIMEOUT=9e5,h.DEFAULT_WEB_VITALS_MODE=J,h.DeviceType=w,h.EmitterEvent=C,h.ErrorType=U,h.EventType=d,h.InitializationTimeoutError=Mt,h.IntegrationValidationError=me,h.MAX_ARRAY_LENGTH=1e3,h.MAX_CUSTOM_EVENT_ARRAY_SIZE=500,h.MAX_CUSTOM_EVENT_KEYS=100,h.MAX_CUSTOM_EVENT_NAME_LENGTH=120,h.MAX_CUSTOM_EVENT_STRING_SIZE=49152,h.MAX_NESTED_OBJECT_KEYS=200,h.MAX_STRING_LENGTH=1e3,h.MAX_STRING_LENGTH_IN_ARRAY=500,h.Mode=X,h.PII_PATTERNS=ot,h.PermanentError=b,h.RateLimitError=$,h.SamplingRateValidationError=ge,h.ScrollDirection=Q,h.SessionTimeoutValidationError=ze,h.SpecialApiUrl=k,h.TimeoutError=B,h.TraceLogValidationError=F,h.WEB_VITALS_GOOD_THRESHOLDS=zt,h.WEB_VITALS_NEEDS_IMPROVEMENT_THRESHOLDS=Te,h.WEB_VITALS_POOR_THRESHOLDS=tt,h.getWebVitalsThresholds=_e,h.tracelog=Hs,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})})(this.TraceLog=this.TraceLog||{});typeof window<"u"&&window.TraceLog?.tracelog&&(window.tracelog=window.TraceLog.tracelog);
|