(function(o,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(o=typeof globalThis<"u"?globalThis:o||self,s(o.AltchaAnalyticsTracker={}))})(this,function(o){"use strict";var q=Object.defineProperty;var D=o=>{throw TypeError(o)};var W=(o,s,h)=>s in o?q(o,s,{enumerable:!0,configurable:!0,writable:!0,value:h}):o[s]=h;var r=(o,s,h)=>W(o,typeof s!="symbol"?s+"":s,h),U=(o,s,h)=>s.has(o)||D("Cannot "+h);var n=(o,s,h)=>(U(o,s,"read from private field"),h?h.call(o):s.get(o)),l=(o,s,h)=>s.has(o)?D("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(o):s.set(o,h),d=(o,s,h,R)=>(U(o,s,"write to private field"),R?R.call(o,h):s.set(o,h),h),H=(o,s,h)=>(U(o,s,"access private method"),h);var m,f,b,k,y,x,L,w,P,S,p,T,O,A,N,E,g,C,I;class s{constructor(i,t={disable:!1}){r(this,"lastEvent",null);this.tracker=i,this.options=t}destroy(){}getExitReason(i=!1){}isLastEventRecent(i=1e4,t=this.lastEvent){return!!t&&performance.now()-t.timeStampa.includes("*")?new RegExp(a.replace(/\*/g,"[^.]+")):a))}checkFn(t){if(this.options.checkFn)return this.options.checkFn(t)}destroy(){}isBot(){return u.CRAWLER_REG_EXP.test(this.tracker.getUserAgent())}isPrivateHostname(){const t=this.tracker.getHostname();return n(this,y).some(e=>e instanceof RegExp&&e.test(t)?!0:t===e)}shouldTrackEvent(t){if(n(this,x))return this.tracker.log(`ignoring event: ${u.LOCAL_STORAGE_KEY}`),!1;const e=this.checkFn(t);return e!==void 0?e:this.isPrivateHostname()?(this.tracker.log("ignoring event: private hostname"),!1):this.options.allowBots!==!0&&this.isBot()?(this.tracker.log("ignoring event: bot"),!1):!0}};y=new WeakMap,x=new WeakMap,r(u,"CRAWLER_REG_EXP",/(?:bot|spider|crawler|facebookexternalhit|simplepie|yahooseeker|embedly|quora link preview|outbrain|vkshare|monit|Pingability|Monitoring|WinHttpRequest|Apache-HttpClient|getprismatic.com|python-requests|Twurly|yandex|browserproxy|Qwantify|Yahoo! Slurp|pinterest|Tumblr|WhatsApp|Google-Structured-Data-Testing-Tool|Google-InspectionTool|GPTBot|Applebot)/i),r(u,"DEFAULT_HOSTNAMES",["127.0.0.1","localhost","*.local"]),r(u,"LOCAL_STORAGE_KEY","altcha_ignore");let _=u;class X extends s{constructor(t,e){super(t);l(this,L,this.onHashChange.bind(this));addEventListener("hashchange",n(this,L))}destroy(){removeEventListener("hashchange",n(this,L))}onHashChange(){this.tracker.trackPageview()}}L=new WeakMap;class B extends s{constructor(t,e){super(t);l(this,w,this.onKeyDown.bind(this));addEventListener("keydown",n(this,w))}destroy(){removeEventListener("keydown",n(this,w))}isLastKeyboardEventCtrl(){return!!this.lastEvent&&(this.lastEvent.ctrlKey||this.lastEvent.metaKey)}getExitReason(t=!1){if(t&&this.isLastEventRecent(100)&&this.isLastKeyboardEventCtrl())return""}onKeyDown(t){this.lastEvent=t}}w=new WeakMap;class V extends s{constructor(t,e){super(t);l(this,P,this.onMouseEnter.bind(this));l(this,S,this.onMouseLeave.bind(this));r(this,"isMouseOut",!1);r(this,"offsetX",-1);r(this,"offsetY",-1);document.body.addEventListener("mouseleave",n(this,S)),document.body.addEventListener("mouseenter",n(this,P))}destroy(){document.body.removeEventListener("mouseleave",n(this,S)),document.body.removeEventListener("mouseenter",n(this,P))}getExitReason(){if(this.isMouseOut){const t=this.tracker.getExtension("pushstate");return t&&t.lastPopStateEvent&&t.isLastEventRecent(100,t.lastPopStateEvent)||this.offsetX>=0&&this.offsetX>=0&&this.offsetX<150?void 0:""}}onMouseEnter(){this.isMouseOut=!1,this.offsetX=-1,this.offsetY=-1}onMouseLeave(t){this.isMouseOut=!0,this.offsetX=t.clientX,this.offsetY=t.clientY}}P=new WeakMap,S=new WeakMap;class G extends s{constructor(t,e){super(t);l(this,p,null);l(this,T,this.onPopState.bind(this));l(this,O,this.onPushState.bind(this));r(this,"lastPopStateEvent",null);const a=d(this,p,history.pushState);history.pushState=(F,Y,j)=>{n(this,O).call(this),a==null||a.apply(history,[F,Y,j])},addEventListener("popstate",n(this,T))}destroy(){n(this,p)&&(history.pushState=n(this,p)),removeEventListener("popstate",n(this,T))}onPopState(t){this.lastPopStateEvent=t,this.tracker.trackPageview()}onPushState(){this.tracker.trackPageview()}}p=new WeakMap,T=new WeakMap,O=new WeakMap;class K extends s{constructor(t,e){super(t);l(this,A,this.onVisibilityChange.bind(this));l(this,N);l(this,E,null);r(this,"visibilityState",document.visibilityState);d(this,N,e.hiddenTimeout||4e3),addEventListener("visibilitychange",n(this,A))}destroy(){removeEventListener("visibilitychange",n(this,A))}getExitReason(t=!1){if(t&&this.visibilityState==="hidden"&&this.lastEvent&&performance.now()-this.lastEvent.timeStamp>=1e3)return""}onTimeout(){document.visibilityState==="hidden"&&this.tracker.trackPageview({},!0)}onVisibilityChange(t){this.lastEvent=t,this.visibilityState=document.visibilityState,this.tracker.isMobile&&(n(this,E)&&clearTimeout(n(this,E)),document.visibilityState==="hidden"&&d(this,E,setTimeout(()=>{this.onTimeout()},n(this,N))))}}A=new WeakMap,N=new WeakMap,E=new WeakMap;const c=class c{constructor(i){l(this,C);l(this,g,this.onPageHide.bind(this));r(this,"isMobile",/Mobi|Android|iPhone|iPad|iPod|Opera Mini/i.test(this.getUserAgent()));r(this,"events",[]);r(this,"extensions",{});r(this,"globalName",null);r(this,"lastPageLoadAt",performance.now());r(this,"lastPageview",null);r(this,"returningVisitor",null);r(this,"trackedEvents",0);r(this,"trackedPageviews",0);if(this.options=i,!i.projectId)throw new Error("Parameter projectId required.");H(this,C,I).call(this),this.isDNTEnabled&&this.options.respectDnt===!0?this.log("DoNotTrack enabled."):(this.loadExtensions(),addEventListener("pagehide",n(this,g)),addEventListener("beforeunload",n(this,g)))}get apiUrl(){return this.options.apiUrl||c.DEFAULT_API_URL}get isDNTEnabled(){return"doNotTrack"in navigator&&navigator.doNotTrack==="1"||"globalPrivacyControl"in navigator&&navigator.globalPrivacyControl===!0}destroy(){this.flushEvents();for(const i in this.extensions)this.extensions[i].destroy();this.extensions={},removeEventListener("pagehide",n(this,g)),removeEventListener("beforeunload",n(this,g)),this.globalName&&delete globalThis[this.globalName]}flushEvents(){const i=this.events.splice(0);i.length&&this.sendBeacon(i)}getBeaconPayload(i){return{events:i,projectId:this.options.projectId,time:Date.now(),uniqueId:this.options.uniqueId}}getExitReason(i=!1){for(const t in this.extensions){const e=this.extensions[t].getExitReason(i);if(e!==void 0)return this.log("exit reason:",{ext:t,result:e}),e}}getExtension(i){return this.extensions[i]}getHostname(){return location.hostname}getOrigin(){return location.origin}getUserAgent(){return navigator.userAgent||""}getView(){return this.sanitizeUrl(location.href)}getPageviewOptions(i=!1){const t=this.getExitReason(i),e=this.getReferrer(),a=this.getOrigin();return{appVersion:this.options.appVersion,exit:t!==void 0,outbound:t,duration:Math.max(0,Math.floor(performance.now()-this.lastPageLoadAt)),referrer:e&&new URL(e,a).origin===a?"":e,returning:this.returningVisitor===null?void 0:this.returningVisitor,view:this.getView()}}getReferrer(){return document.referrer}hasExtension(i){return!!this.getExtension(i)}loadExtensions(){var i;for(const t in c.EXTENSIONS){let e=((i=this.options)==null?void 0:i[t])!==void 0?this.options[t]:{};typeof e=="boolean"?e={disable:!e}:e.disable=e.disable===void 0?!c.DEFAULT_EXTENSIONS.includes(t):e.disable,e.disable!==!0&&(this.extensions[t]=new c.EXTENSIONS[t](this,e))}}log(...i){this.options.debug&&console.log("[ALTCHA Tracker]",...i)}sanitizeUrl(i){var t;if(i=new URL(i),(t=this.options.allowSearchParams)!=null&&t.length&&i.hostname===this.getHostname())for(const[e]of i.searchParams)this.options.allowSearchParams.includes(e)||i.searchParams.delete(e);else i.search="";return this.hasExtension("hash")||(i.hash=""),i.toString()}sendBeacon(i){if("sendBeacon"in navigator)return navigator.sendBeacon(this.apiUrl,JSON.stringify(this.getBeaconPayload(i)))}shouldTrackEvent(i){for(const t in this.extensions)if(this.extensions[t].shouldTrackEvent(i)!==!0)return this.log("should not track event:",{ext:t,event:i}),!1;return!0}trackEvent(i={},t=!1){const e={timestamp:Date.now(),...i};return this.shouldTrackEvent(e)?(this.events.push(e),this.trackedEvents+=1,this.log("trackEvent",e),t&&this.flushEvents(),!0):!1}trackPageview(i={},t=!1){const e=this.getPageviewOptions(t);return this.events.length&&e.duration<100&&this.events[this.events.length-1].view===e.view?(this.log("duplicate pageview",e),!1):(this.log("trackPageview",e),this.trackEvent({...e,...i},t),this.trackedPageviews+=1,this.lastPageLoadAt=performance.now(),this.lastPageview=e.view,e.exit&&(this.trackedPageviews=0),!0)}onPageHide(){this.lastPageview!==this.getView()&&this.trackPageview({},!0)}};g=new WeakMap,C=new WeakSet,I=function(){if(this.globalName=this.options.globalName===void 0?c.DEAFAUL_GLOBAL_NAME:this.options.globalName||null,this.globalName){if(globalThis[this.globalName])throw new Error("Another instance of the Tracker is already present in globalThis. Set globalName:null to disable global reference.");globalThis[this.globalName]=this}},r(c,"EXTENSIONS",{click:h,cookie:R,filter:_,hash:X,keyboard:B,mouse:V,pushstate:G,visibility:K}),r(c,"DEFAULT_API_URL","https://eu.altcha.org/api/v1/event"),r(c,"DEFAULT_EXTENSIONS",["click","filter","keyboard","mouse","pushstate","visibility"]),r(c,"DEAFAUL_GLOBAL_NAME","altchaTracker");let M=c;o.Tracker=M,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});