Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

feat(tracker): implement site flood detection and baseline refresh

Bill Yang9aacb4f2e371
diff --git a/server/public/script-full.js b/server/public/script-full.js
index 997c20488..6087274f4 100644
--- a/server/public/script-full.js
+++ b/server/public/script-full.js
@@ -483,7 +483,8 @@
outerDimensionsWeird: 1 << 8,
pluginApiAbsence: 1 << 9,
defaultViewport1280x1200: 1 << 10,
- squareScreen: 1 << 11
+ squareScreen: 1 << 11,
+ missingScreenDimensions: 1 << 12
};
var CLIENT_BOT_SIGNAL_NAMES = Object.keys(CLIENT_BOT_SIGNAL_MASKS);
var CLIENT_BOT_SIGNAL_WEIGHTS = {
@@ -498,7 +499,8 @@
outerDimensionsWeird: 2,
pluginApiAbsence: 0,
defaultViewport1280x1200: 3,
- squareScreen: 3
+ squareScreen: 3,
+ missingScreenDimensions: 1
};
var ALL_CLIENT_BOT_SIGNAL_BITS = CLIENT_BOT_SIGNAL_NAMES.reduce(
(mask, name) => mask | CLIENT_BOT_SIGNAL_MASKS[name],
diff --git a/server/public/script.js b/server/public/script.js
index 9450b8709..68cff94b6 100644
--- a/server/public/script.js
+++ b/server/public/script.js
@@ -1 +1 @@
-"use strict";(()=>{var $e=Object.defineProperty;var je=(r,e,t)=>e in r?$e(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var w=(r,e,t)=>je(r,typeof e!="symbol"?e+"":e,t);function Ke(r){if(r.startsWith("re:")){let c=r.slice(3);if(!c)throw new Error("Empty regex pattern");return new RegExp(c)}let t="__DOUBLE_ASTERISK_TOKEN__",n="__SINGLE_ASTERISK_TOKEN__",s=r.replace(/\*\*/g,t).replace(/\*/g,n).replace(/[.+?^${}()|[\]\\]/g,"\\$&");s=s.replace(new RegExp(`/${t}/`,"g"),"/(?:.+/)?"),s=s.replace(new RegExp(t,"g"),".*"),s=s.replace(/\//g,"\\/");let a=s.replace(new RegExp(n,"g"),"[^/]+");return new RegExp("^"+a+"$")}function V(r,e){for(let t of e)try{if(Ke(t).test(r))return t}catch(n){console.error(`Invalid pattern: ${t}`,n)}return null}function oe(r,e){let t=null;return(...n)=>{t&&clearTimeout(t),t=setTimeout(()=>r(...n),e)}}function le(r){try{let e=window.location.hostname,t=new URL(r).hostname;return t!==e&&t!==""}catch{return!1}}function C(r,e){if(!r)return e;try{let t=JSON.parse(r);return Array.isArray(e)&&!Array.isArray(t)?e:t}catch(t){return console.error("Error parsing JSON:",t),e}}var qe=2e3;function ce(){try{if(crypto?.randomUUID)return crypto.randomUUID()}catch{}return`${Date.now().toString(36)}-${Math.random().toString(36).slice(2,12)}`}function Je(r){let e=`${r}-visitor-id`;try{let t=localStorage.getItem(e);if(t)return t;let n=ce();return localStorage.setItem(e,n),n}catch{return ce()}}function Xe(r){try{return localStorage.getItem(`${r}-user-id`)||void 0}catch{return}}function Qe(r){return r.hash&&r.hash.startsWith("#/")?r.hash.substring(1):r.pathname}async function Ye(r,e,t,n){let i=new AbortController,s=window.setTimeout(()=>i.abort(),qe);try{let a=new URL(window.location.href),c=await fetch(`${r}/site/${e}/feature-flags/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},credentials:"omit",signal:i.signal,body:JSON.stringify({anonymousId:n,identifiedUserId:Xe(t),hostname:a.hostname,pathname:Qe(a),querystring:a.search,query:Object.fromEntries(a.searchParams.entries()),referrer:document.referrer,language:navigator.language,screenWidth:screen.width,screenHeight:screen.height})});if(!c.ok)return{enabled:!0,flags:{}};let u=await c.json();return{enabled:u?.featureFlagsEnabled!==!1,flags:u?.flags&&typeof u.flags=="object"?u.flags:{}}}catch{return{enabled:!0,flags:{}}}finally{window.clearTimeout(s)}}async function ue(r){let e=r.getAttribute("src");if(!e)return console.error("Script src attribute is missing"),null;let t=e.split("/script.js")[0];if(!t)return console.error("Please provide a valid analytics host"),null;let n=r.getAttribute("data-site-id")||r.getAttribute("site-id");if(!n)return console.error("Please provide a valid site ID using the data-site-id attribute"),null;let i=r.getAttribute("data-namespace")||"rybbit",s=Je(i),a=C(r.getAttribute("data-skip-patterns"),[]),c=C(r.getAttribute("data-mask-patterns"),[]),u=C(r.getAttribute("data-replay-mask-text-selectors"),[]),d=r.getAttribute("data-debounce")?Math.max(0,parseInt(r.getAttribute("data-debounce"))):500,g=r.getAttribute("data-replay-batch-size")?Math.max(1,parseInt(r.getAttribute("data-replay-batch-size"))):250,y=r.getAttribute("data-replay-batch-interval")?Math.max(1e3,parseInt(r.getAttribute("data-replay-batch-interval"))):5e3,T=r.getAttribute("data-replay-block-class")||void 0,h=r.getAttribute("data-replay-block-selector")||void 0,o=r.getAttribute("data-replay-ignore-class")||void 0,l=r.getAttribute("data-replay-ignore-selector")||void 0,p=r.getAttribute("data-replay-mask-text-class")||void 0,S=r.getAttribute("data-replay-mask-all-inputs"),E=S!==null?S!=="false":void 0,R=r.getAttribute("data-replay-mask-input-options"),Ue=R?C(R,{password:!0,email:!0}):void 0,ne=r.getAttribute("data-replay-collect-fonts"),He=ne!==null?ne!=="false":void 0,re=r.getAttribute("data-replay-sampling"),We=re?C(re,{}):void 0,ie=r.getAttribute("data-replay-slim-dom-options"),Ve=ie?C(ie,{}):void 0,se=r.getAttribute("data-replay-sample-rate"),Ge=se?Math.min(100,Math.max(0,parseInt(se,10))):void 0,ze=r.getAttribute("data-tag")||"",f={namespace:i,analyticsHost:t,siteId:n,visitorId:s,debounceDuration:d,sessionReplayBatchSize:g,sessionReplayBatchInterval:y,sessionReplayMaskTextSelectors:u,skipPatterns:a,maskPatterns:c,autoTrackPageview:!0,autoTrackSpa:!0,trackQuerystring:!0,trackOutbound:!0,enableWebVitals:!1,trackErrors:!1,enableSessionReplay:!1,trackButtonClicks:!1,trackCopy:!1,trackFormInteractions:!1,tag:ze,featureFlagsEnabled:!1,featureFlags:{},sessionReplayBlockClass:T,sessionReplayBlockSelector:h,sessionReplayIgnoreClass:o,sessionReplayIgnoreSelector:l,sessionReplayMaskTextClass:p,sessionReplayMaskAllInputs:E,sessionReplayMaskInputOptions:Ue,sessionReplayCollectFonts:He,sessionReplaySampling:We,sessionReplaySlimDOMOptions:Ve,sessionReplaySampleRate:Ge},A=f;try{let I=`${t}/site/tracking-config/${n}`,ae=await fetch(I,{method:"GET",credentials:"omit"});if(ae.ok){let m=await ae.json();A={...f,autoTrackPageview:m.trackInitialPageView??f.autoTrackPageview,autoTrackSpa:m.trackSpaNavigation??f.autoTrackSpa,trackQuerystring:m.trackUrlParams??f.trackQuerystring,trackOutbound:m.trackOutbound??f.trackOutbound,enableWebVitals:m.webVitals??f.enableWebVitals,trackErrors:m.trackErrors??f.trackErrors,enableSessionReplay:m.sessionReplay??f.enableSessionReplay,trackButtonClicks:m.trackButtonClicks??f.trackButtonClicks,trackCopy:m.trackCopy??f.trackCopy,trackFormInteractions:m.trackFormInteractions??f.trackFormInteractions,featureFlagsEnabled:m.featureFlagsEnabled===!0}}else console.warn("Failed to fetch tracking config from API, using defaults")}catch(I){console.warn("Error fetching tracking config:",I)}if(A.featureFlagsEnabled){let I=await Ye(t,n,i,s);A.featureFlagsEnabled=I.enabled,A.featureFlags=I.flags}return A}var de="rybbit-replay-sampled";function Ze(r){if(r>=100)return!0;if(r<=0)return!1;try{let e=sessionStorage.getItem(de);if(e!==null)return e==="1";let t=Math.random()*100<r;return sessionStorage.setItem(de,t?"1":"0"),t}catch{return Math.random()*100<r}}var M=class{constructor(e,t,n){this.isRecording=!1;this.eventBuffer=[];this.config=e,this.userId=t,this.sendBatch=n}async initialize(){if(!this.config.enableSessionReplay)return;let e=this.config.sessionReplaySampleRate;e!==void 0&&!Ze(e)||(window.rrweb||await this.loadRrweb(),window.rrweb&&this.startRecording())}async loadRrweb(){return new Promise((e,t)=>{let n=document.createElement("script");n.src=`${this.config.analyticsHost}/replay.js`,n.async=!1,n.onload=()=>{e()},n.onerror=()=>t(new Error("Failed to load rrweb")),document.head.appendChild(n)})}startRecording(){if(!(this.isRecording||!window.rrweb||!this.config.enableSessionReplay))try{let e={mousemove:!1,mouseInteraction:{MouseUp:!1,MouseDown:!1,Click:!0,ContextMenu:!1,DblClick:!0,Focus:!0,Blur:!0,TouchStart:!1,TouchEnd:!1},scroll:500,input:"last",media:800},t={script:!1,comment:!0,headFavicon:!0,headWhitespace:!0,headMetaDescKeywords:!0,headMetaSocial:!0,headMetaRobots:!0,headMetaHttpEquiv:!0,headMetaAuthorship:!0,headMetaVerification:!0},n={emit:i=>{this.addEvent({type:i.type,data:i.data,timestamp:i.timestamp||Date.now()})},recordCanvas:!1,checkoutEveryNms:6e4,checkoutEveryNth:500,blockClass:this.config.sessionReplayBlockClass??"rr-block",blockSelector:this.config.sessionReplayBlockSelector??null,ignoreClass:this.config.sessionReplayIgnoreClass??"rr-ignore",ignoreSelector:this.config.sessionReplayIgnoreSelector??null,maskTextClass:this.config.sessionReplayMaskTextClass??"rr-mask",maskAllInputs:this.config.sessionReplayMaskAllInputs??!0,maskInputOptions:this.config.sessionReplayMaskInputOptions??{password:!0,email:!0},collectFonts:this.config.sessionReplayCollectFonts??!0,sampling:this.config.sessionReplaySampling??e,slimDOMOptions:this.config.sessionReplaySlimDOMOptions??t};this.config.sessionReplayMaskTextSelectors&&this.config.sessionReplayMaskTextSelectors.length>0&&(n.maskTextSelector=this.config.sessionReplayMaskTextSelectors.join(", ")),this.stopRecordingFn=window.rrweb.record(n),this.isRecording=!0,this.setupBatchTimer()}catch{}}stopRecording(){this.isRecording&&(this.stopRecordingFn&&this.stopRecordingFn(),this.isRecording=!1,this.clearBatchTimer(),this.eventBuffer.length>0&&this.flushEvents())}isActive(){return this.isRecording}addEvent(e){this.eventBuffer.push(e),this.eventBuffer.length>=this.config.sessionReplayBatchSize&&this.flushEvents()}setupBatchTimer(){this.clearBatchTimer(),this.batchTimer=window.setInterval(()=>{this.eventBuffer.length>0&&this.flushEvents()},this.config.sessionReplayBatchInterval)}clearBatchTimer(){this.batchTimer&&(clearInterval(this.batchTimer),this.batchTimer=void 0)}async flushEvents(){if(this.eventBuffer.length===0)return;let e=[...this.eventBuffer];this.eventBuffer=[];let t={userId:this.userId,events:e,metadata:{pageUrl:window.location.href,viewportWidth:screen.width,viewportHeight:screen.height,language:navigator.language}};try{await this.sendBatch(t)}catch{this.eventBuffer.unshift(...e)}}updateUserId(e){e!==this.userId&&(this.eventBuffer.length>0&&this.flushEvents(),this.userId=e)}onPageChange(){this.isRecording&&this.flushEvents()}cleanup(){this.stopRecording()}};var b={automationApi:1,zeroOuterDimensions:2,missingChrome:4,swiftShader:8,emptyPlugins:16,defaultViewport800x600:32,defaultViewport1024x768:64,impossibleDimensions:128,outerDimensionsWeird:256,pluginApiAbsence:512,defaultViewport1280x1200:1024,squareScreen:2048},et=Object.keys(b),he={automationApi:3,zeroOuterDimensions:2,missingChrome:1,swiftShader:1,emptyPlugins:1,defaultViewport800x600:3,defaultViewport1024x768:3,impossibleDimensions:3,outerDimensionsWeird:2,pluginApiAbsence:0,defaultViewport1280x1200:3,squareScreen:3},ht=et.reduce((r,e)=>r|b[e],0),ft=b.automationApi|b.impossibleDimensions|b.defaultViewport800x600|b.defaultViewport1024x768|b.defaultViewport1280x1200|b.squareScreen,fe=10,pe=200,ge=8192,tt=[{width:800,height:600,signal:"defaultViewport800x600"},{width:1024,height:768,signal:"defaultViewport1024x768"},{width:1280,height:1200,signal:"defaultViewport1280x1200"}];function nt(r,e){return Number.isFinite(r)&&Number.isFinite(e)&&r>=pe&&e>=pe&&r<=ge&&e<=ge}function rt(r){return/Windows NT|Macintosh|X11|Linux x86_64/.test(r)&&!/Mobile|Android|iPhone|iPad/.test(r)}function me(r,e,t){if(!nt(r,e))return["impossibleDimensions"];let n=[];if(r===e&&n.push("squareScreen"),rt(t))for(let i of tt)r===i.width&&e===i.height&&n.push(i.signal);return n}var ye=null;function ve(){return Se().score}function ke(){return Se().mask}function we(){return document.prerendering===!0}function Se(){return we()?be():(ye??(ye=be()),ye)}function be(){let r=0,e=0;function t(n){let i=b[n];(e&i)===0&&(e|=i,r+=he[n])}try{let n=navigator.userAgent,i=/Chrome\//.test(n)&&!/\bwv\b|; wv\)/.test(n),s=Number(window.screen?.width),a=Number(window.screen?.height),c=Number(window.outerWidth),u=Number(window.outerHeight),d=Number(window.innerWidth),g=Number(window.innerHeight),T=["__webdriver_evaluate","__selenium_evaluate","__webdriver_script_function","__webdriver_script_func","__webdriver_script_fn","__fxdriver_evaluate","__driver_unwrapped","__webdriver_unwrapped","__driver_evaluate","__selenium_unwrapped","__fxdriver_unwrapped","_phantom","callPhantom","__nightmare","domAutomation","domAutomationController"].some(o=>o in window||o in document);(navigator.webdriver===!0||T)&&t("automationApi"),(u===0||c===0)&&!we()&&t("zeroOuterDimensions");for(let o of me(s,a,n))t(o);Number.isFinite(c)&&Number.isFinite(u)&&Number.isFinite(d)&&Number.isFinite(g)&&c>0&&u>0&&d>0&&g>0&&(c+8<d||u+8<g)&&t("outerDimensionsWeird");let h=!1;!window.chrome&&i&&(t("missingChrome"),h=!0);try{let o=document.createElement("canvas"),l=o.getContext("webgl")||o.getContext("experimental-webgl");if(l)try{let p=[],S=l.getParameter(l.RENDERER);typeof S=="string"&&p.push(S);try{let E=l.getExtension("WEBGL_debug_renderer_info");if(E){let R=l.getParameter(E.UNMASKED_RENDERER_WEBGL);typeof R=="string"&&p.push(R)}}catch{}p.join(" ").toLowerCase().includes("swiftshader")&&t("swiftShader")}finally{it(o,l)}}catch{}(!navigator.plugins||navigator.plugins.length===0)&&i&&(t("emptyPlugins"),h=!0),h&&t("pluginApiAbsence")}catch{}return{score:Math.min(r,fe),mask:e}}function it(r,e){try{e.getExtension("WEBGL_lose_context")?.loseContext?.()}catch{}r.width=0,r.height=0}var st=2e3,x=class{constructor(e){this.customUserId=null;this.errorDedupeCache=new Map;this.errorDedupeLastCleanup=0;this.exposedFeatureFlags=new Set;this.config=e,this.loadUserId(),e.enableSessionReplay&&this.initializeSessionReplay()}serializeFeatureFlagValue(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return String(e);try{return JSON.stringify(e)}catch{return""}}getFeatureFlagEventPayload(){let e={};for(let[t,n]of Object.entries(this.config.featureFlags||{}))e[t]=this.serializeFeatureFlagValue(n.value);return e}getCurrentUrlContext(){let e=new URL(window.location.href),t=e.hash&&e.hash.startsWith("#/")?e.hash.substring(1):e.pathname;return{hostname:e.hostname,pathname:t,querystring:e.search,query:Object.fromEntries(e.searchParams.entries()),referrer:document.referrer,language:navigator.language,screenWidth:screen.width,screenHeight:screen.height}}async refreshFeatureFlags(){if(!this.config.featureFlagsEnabled)return;let e=new AbortController,t=window.setTimeout(()=>e.abort(),st);try{let n=await fetch(`${this.config.analyticsHost}/site/${this.config.siteId}/feature-flags/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({anonymousId:this.config.visitorId,identifiedUserId:this.customUserId||void 0,...this.getCurrentUrlContext()}),mode:"cors",credentials:"omit",keepalive:!0,signal:e.signal});if(!n.ok)return;let i=await n.json();this.config.featureFlags=i?.flags&&typeof i.flags=="object"?i.flags:{},i?.featureFlagsEnabled===!1&&(this.config.featureFlagsEnabled=!1)}catch{}finally{window.clearTimeout(t)}}loadUserId(){try{let e=localStorage.getItem(`${this.config.namespace}-user-id`);e&&(this.customUserId=e)}catch{}}async initializeSessionReplay(){try{this.sessionReplayRecorder=new M(this.config,this.customUserId||"",e=>this.sendSessionReplayBatch(e)),await this.sessionReplayRecorder.initialize()}catch(e){console.error("Failed to initialize session replay:",e)}}async sendSessionReplayBatch(e){try{await fetch(`${this.config.analyticsHost}/session-replay/record/${this.config.siteId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),mode:"cors",keepalive:!1})}catch(t){throw console.error("Failed to send session replay batch:",t),t}}createBasePayload(){let e=new URL(window.location.href),t=e.pathname;if(e.hash&&e.hash.startsWith("#/")&&(t=e.hash.substring(1)),V(t,this.config.skipPatterns))return null;let n=V(t,this.config.maskPatterns);n&&(t=n);let i={site_id:this.config.siteId,hostname:e.hostname,pathname:t,querystring:this.config.trackQuerystring?e.search:"",screenWidth:screen.width,screenHeight:screen.height,language:navigator.language,page_title:document.title,referrer:document.referrer,_bs:ve(),_bsm:ke()};this.customUserId&&(i.user_id=this.customUserId),this.config.tag&&(i.tag=this.config.tag);let s=this.getFeatureFlagEventPayload();return Object.keys(s).length>0&&(i.feature_flags=s),i}async sendTrackingData(e){try{await fetch(`${this.config.analyticsHost}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),mode:"cors",keepalive:!0})}catch(t){console.error("Failed to send tracking data:",t)}}track(e,t="",n={}){if(e==="custom_event"&&(!t||typeof t!="string")){console.error("Event name is required and must be a string for custom events");return}let i=this.createBasePayload();if(!i)return;let a={...i,type:e,event_name:t,properties:["custom_event","outbound","error","button_click","copy","form_submit","input_change"].includes(e)?JSON.stringify(n):void 0};this.sendTrackingData(a)}trackPageview(){this.track("pageview")}trackEvent(e,t={}){this.track("custom_event",e,t)}getFeatureFlag(e,t){let n=this.config.featureFlags?.[e];if(!n)return t;let i=`${e}:${n.version}:${this.serializeFeatureFlagValue(n.value)}`;return this.exposedFeatureFlags.has(i)||(this.exposedFeatureFlags.add(i),this.trackEvent("feature_flag_exposure",{key:e,value:this.serializeFeatureFlagValue(n.value),version:n.version,reason:n.reason})),n.value}getFeatureFlags(){return Object.fromEntries(Object.entries(this.config.featureFlags||{}).map(([e,t])=>[e,t.value]))}getFeatureFlagPayload(e,t){let n=this.config.featureFlags?.[e];return!n||n.payload===void 0?t:n.payload}getFeatureFlagPayloads(){return Object.fromEntries(Object.entries(this.config.featureFlags||{}).filter(([,e])=>e.payload!==void 0).map(([e,t])=>[e,t.payload]))}trackOutbound(e,t="",n="_self"){this.track("outbound","",{url:e,text:t,target:n})}trackWebVitals(e){let t=this.createBasePayload();if(!t)return;let n={...t,type:"performance",event_name:"web-vitals",...e};this.sendTrackingData(n)}trackError(e,t={}){let n=e?.message||"";if(n.includes("ResizeObserver loop completed with undelivered notifications")||n.includes("ResizeObserver loop limit exceeded"))return;let i=window.location.origin,s=t.filename||"",a=e.stack||"";if(s)try{if(new URL(s).origin!==i)return}catch{}else if(a&&!a.includes(i))return;let u=[e.name||"Error",n,t.filename||"",t.lineno??"",t.colno??""].join("|"),d=Date.now(),g=6e4,y=this.errorDedupeCache.get(u);if(y&&d-y<g)return;this.errorDedupeCache.set(u,d);let T=10*6e4;if(d-this.errorDedupeLastCleanup>g){for(let[o,l]of this.errorDedupeCache.entries())d-l>T&&this.errorDedupeCache.delete(o);this.errorDedupeLastCleanup=d}let h={message:e.message?.substring(0,500)||"Unknown error",stack:a.substring(0,2e3)||""};if(s&&(h.fileName=s),t.lineno){let o=typeof t.lineno=="string"?parseInt(t.lineno,10):t.lineno;o&&o!==0&&(h.lineNumber=o)}if(t.colno){let o=typeof t.colno=="string"?parseInt(t.colno,10):t.colno;o&&o!==0&&(h.columnNumber=o)}for(let o in t)!["lineno","colno"].includes(o)&&t[o]!==void 0&&(h[o]=t[o]);this.track("error",e.name||"Error",h)}trackButtonClick(e){this.track("button_click","",e)}trackCopy(e){this.track("copy","",e)}trackFormSubmit(e){this.track("form_submit","",e)}trackInputChange(e){this.track("input_change","",e)}identify(e,t){if(typeof e!="string"||e.trim()===""){console.error("User ID must be a non-empty string");return}this.customUserId=e.trim();try{localStorage.setItem(`${this.config.namespace}-user-id`,this.customUserId)}catch{console.warn("Could not persist user ID to localStorage")}this.sendIdentifyEvent(this.customUserId,t,!0).then(()=>this.refreshFeatureFlags()),this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(this.customUserId)}setTraits(e){if(!e||typeof e!="object"){console.error("Traits must be an object");return}let t=this.customUserId;if(!t){console.warn("Cannot set traits without identifying user first. Call identify() first.");return}this.sendIdentifyEvent(t,e,!1).then(()=>this.refreshFeatureFlags())}async sendIdentifyEvent(e,t,n=!0){try{await fetch(`${this.config.analyticsHost}/identify`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({site_id:this.config.siteId,user_id:e,traits:t,is_new_identify:n}),mode:"cors",keepalive:!0})}catch(i){console.error("Failed to send identify event:",i)}}clearUserId(){this.customUserId=null;try{localStorage.removeItem(`${this.config.namespace}-user-id`)}catch{}this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(""),this.refreshFeatureFlags()}getUserId(){return this.customUserId}startSessionReplay(){this.sessionReplayRecorder?this.sessionReplayRecorder.startRecording():console.warn("Session replay not initialized")}stopSessionReplay(){this.sessionReplayRecorder&&this.sessionReplayRecorder.stopRecording()}isSessionReplayActive(){return this.sessionReplayRecorder?.isActive()??!1}onPageChange(){this.refreshFeatureFlags(),this.sessionReplayRecorder&&this.sessionReplayRecorder.onPageChange()}cleanup(){this.sessionReplayRecorder&&this.sessionReplayRecorder.cleanup()}};var Le=-1,_=r=>{addEventListener("pageshow",(e=>{e.persisted&&(Le=e.timeStamp,r(e))}),!0)},v=(r,e,t,n)=>{let i,s;return a=>{e.value>=0&&(a||n)&&(s=e.value-(i??0),(s||i===void 0)&&(i=e.value,e.delta=s,e.rating=((c,u)=>c>u[1]?"poor":c>u[0]?"needs-improvement":"good")(e.value,t),r(e)))}},Q=r=>{requestAnimationFrame((()=>requestAnimationFrame((()=>r()))))},Y=()=>{let r=performance.getEntriesByType("navigation")[0];if(r&&r.responseStart>0&&r.responseStart<performance.now())return r},L=()=>Y()?.activationStart??0,k=(r,e=-1)=>{let t=Y(),n="navigate";return Le>=0?n="back-forward-cache":t&&(document.prerendering||L()>0?n="prerender":document.wasDiscarded?n="restore":t.type&&(n=t.type.replace(/_/g,"-"))),{name:r,value:e,rating:"good",delta:0,entries:[],id:`v5-${Date.now()}-${Math.floor(8999999999999*Math.random())+1e12}`,navigationType:n}},G=new WeakMap;function Z(r,e){return G.get(r)||G.set(r,new e),G.get(r)}var $=class{constructor(){w(this,"t");w(this,"i",0);w(this,"o",[])}h(e){if(e.hadRecentInput)return;let t=this.o[0],n=this.o.at(-1);this.i&&t&&n&&e.startTime-n.startTime<1e3&&e.startTime-t.startTime<5e3?(this.i+=e.value,this.o.push(e)):(this.i=e.value,this.o=[e]),this.t?.(e)}},F=(r,e,t={})=>{try{if(PerformanceObserver.supportedEntryTypes.includes(r)){let n=new PerformanceObserver((i=>{Promise.resolve().then((()=>{e(i.getEntries())}))}));return n.observe({type:r,buffered:!0,...t}),n}}catch{}},ee=r=>{let e=!1;return()=>{e||(r(),e=!0)}},P=-1,Fe=new Set,Ee=()=>document.visibilityState!=="hidden"||document.prerendering?1/0:0,j=r=>{if(document.visibilityState==="hidden"){if(r.type==="visibilitychange")for(let e of Fe)e();isFinite(P)||(P=r.type==="visibilitychange"?r.timeStamp:0,removeEventListener("prerenderingchange",j,!0))}},N=()=>{if(P<0){let r=L();P=(document.prerendering?void 0:globalThis.performance.getEntriesByType("visibility-state").filter((t=>t.name==="hidden"&&t.startTime>r))[0]?.startTime)??Ee(),addEventListener("visibilitychange",j,!0),addEventListener("prerenderingchange",j,!0),_((()=>{setTimeout((()=>{P=Ee()}))}))}return{get firstHiddenTime(){return P},onHidden(r){Fe.add(r)}}},O=r=>{document.prerendering?addEventListener("prerenderingchange",(()=>r()),!0):r()},Te=[1800,3e3],te=(r,e={})=>{O((()=>{let t=N(),n,i=k("FCP"),s=F("paint",(a=>{for(let c of a)c.name==="first-contentful-paint"&&(s.disconnect(),c.startTime<t.firstHiddenTime&&(i.value=Math.max(c.startTime-L(),0),i.entries.push(c),n(!0)))}));s&&(n=v(r,i,Te,e.reportAllChanges),_((a=>{i=k("FCP"),n=v(r,i,Te,e.reportAllChanges),Q((()=>{i.value=performance.now()-a.timeStamp,n(!0)}))})))}))},Re=[.1,.25],Me=(r,e={})=>{let t=N();te(ee((()=>{let n,i=k("CLS",0),s=Z(e,$),a=u=>{for(let d of u)s.h(d);s.i>i.value&&(i.value=s.i,i.entries=s.o,n())},c=F("layout-shift",a);c&&(n=v(r,i,Re,e.reportAllChanges),t.onHidden((()=>{a(c.takeRecords()),n(!0)})),_((()=>{s.i=0,i=k("CLS",0),n=v(r,i,Re,e.reportAllChanges),Q((()=>n()))})),setTimeout(n))})))},xe=0,z=1/0,B=0,at=r=>{for(let e of r)e.interactionId&&(z=Math.min(z,e.interactionId),B=Math.max(B,e.interactionId),xe=B?(B-z)/7+1:0)},K,Ce=()=>K?xe:performance.interactionCount??0,ot=()=>{"interactionCount"in performance||K||(K=F("event",at,{type:"event",buffered:!0,durationThreshold:0}))},Ie=0,q=class{constructor(){w(this,"u",[]);w(this,"l",new Map);w(this,"m");w(this,"p")}v(){Ie=Ce(),this.u.length=0,this.l.clear()}L(){let e=Math.min(this.u.length-1,Math.floor((Ce()-Ie)/50));return this.u[e]}h(e){if(this.m?.(e),!e.interactionId&&e.entryType!=="first-input")return;let t=this.u.at(-1),n=this.l.get(e.interactionId);if(n||this.u.length<10||e.duration>t.P){if(n?e.duration>n.P?(n.entries=[e],n.P=e.duration):e.duration===n.P&&e.startTime===n.entries[0].startTime&&n.entries.push(e):(n={id:e.interactionId,entries:[e],P:e.duration},this.l.set(n.id,n),this.u.push(n)),this.u.sort(((i,s)=>s.P-i.P)),this.u.length>10){let i=this.u.splice(10);for(let s of i)this.l.delete(s.id)}this.p?.(n)}}},Be=r=>{let e=globalThis.requestIdleCallback||setTimeout;document.visibilityState==="hidden"?r():(r=ee(r),addEventListener("visibilitychange",r,{once:!0,capture:!0}),e((()=>{r(),removeEventListener("visibilitychange",r,{capture:!0})})))},Pe=[200,500],Ne=(r,e={})=>{if(!globalThis.PerformanceEventTiming||!("interactionId"in PerformanceEventTiming.prototype))return;let t=N();O((()=>{ot();let n,i=k("INP"),s=Z(e,q),a=u=>{Be((()=>{for(let g of u)s.h(g);let d=s.L();d&&d.P!==i.value&&(i.value=d.P,i.entries=d.entries,n())}))},c=F("event",a,{durationThreshold:e.durationThreshold??40});n=v(r,i,Pe,e.reportAllChanges),c&&(c.observe({type:"first-input",buffered:!0}),t.onHidden((()=>{a(c.takeRecords()),n(!0)})),_((()=>{s.v(),i=k("INP"),n=v(r,i,Pe,e.reportAllChanges)})))}))},J=class{constructor(){w(this,"m")}h(e){this.m?.(e)}},_e=[2500,4e3],Oe=(r,e={})=>{O((()=>{let t=N(),n,i=k("LCP"),s=Z(e,J),a=u=>{e.reportAllChanges||(u=u.slice(-1));for(let d of u)s.h(d),d.startTime<t.firstHiddenTime&&(i.value=Math.max(d.startTime-L(),0),i.entries=[d],n())},c=F("largest-contentful-paint",a);if(c){n=v(r,i,_e,e.reportAllChanges);let u=ee((()=>{a(c.takeRecords()),c.disconnect(),n(!0)})),d=g=>{g.isTrusted&&(Be(u),removeEventListener(g.type,d,{capture:!0}))};for(let g of["keydown","click","visibilitychange"])addEventListener(g,d,{capture:!0});_((g=>{i=k("LCP"),n=v(r,i,_e,e.reportAllChanges),Q((()=>{i.value=performance.now()-g.timeStamp,n(!0)}))}))}}))},Ae=[800,1800],X=r=>{document.prerendering?O((()=>X(r))):document.readyState!=="complete"?addEventListener("load",(()=>X(r)),!0):setTimeout(r)},De=(r,e={})=>{let t=k("TTFB"),n=v(r,t,Ae,e.reportAllChanges);X((()=>{let i=Y();i&&(t.value=Math.max(i.responseStart-L(),0),t.entries=[i],n(!0),_((()=>{t=k("TTFB",0),n=v(r,t,Ae,e.reportAllChanges),n(!0)})))}))};var D=class{constructor(e){this.data={lcp:null,cls:null,inp:null,fcp:null,ttfb:null};this.sent=!1;this.timeout=null;this.onReadyCallback=null;this.onReadyCallback=e}initialize(){try{Oe(this.collectMetric.bind(this)),Me(this.collectMetric.bind(this)),Ne(this.collectMetric.bind(this)),te(this.collectMetric.bind(this)),De(this.collectMetric.bind(this)),this.timeout=setTimeout(()=>{this.sent||this.sendData()},2e4),window.addEventListener("beforeunload",()=>{this.sent||this.sendData()})}catch(e){console.warn("Error initializing web vitals tracking:",e)}}collectMetric(e){if(this.sent)return;let t=e.name.toLowerCase();this.data[t]=e.value,Object.values(this.data).every(i=>i!==null)&&this.sendData()}sendData(){this.sent||(this.sent=!0,this.timeout&&(clearTimeout(this.timeout),this.timeout=null),this.onReadyCallback&&this.onReadyCallback(this.data))}getData(){return{...this.data}}};var lt=1e3,U=class{constructor(e,t){this.lastClickAt=new WeakMap;this.tracker=e,this.config=t}initialize(){document.addEventListener("click",this.handleClick.bind(this),!0)}handleClick(e){let t=e.target;this.config.trackButtonClicks&&this.isButton(t)&&this.trackButtonClick(t)}isButton(e){if(e.tagName==="BUTTON"||e.getAttribute("role")==="button")return!0;if(e.tagName==="INPUT"){let i=e.type?.toLowerCase();if(i==="submit"||i==="button")return!0}let t=e.parentElement,n=0;for(;t&&n<3;){if(t.tagName==="BUTTON"||t.getAttribute("role")==="button")return!0;t=t.parentElement,n++}return!1}trackButtonClick(e){let t=this.findButton(e);if(!t||t.hasAttribute("data-rybbit-event"))return;let n=Date.now(),i=this.lastClickAt.get(t);if(i!==void 0&&n-i<lt)return;this.lastClickAt.set(t,n);let s={text:this.getElementText(t),...this.extractDataAttributes(t)};this.tracker.trackButtonClick(s)}extractDataAttributes(e){let t={};for(let n of e.attributes)if(n.name.startsWith("data-rybbit-prop-")){let i=n.name.replace("data-rybbit-prop-","");t[i]=n.value}return t}findButton(e){if(e.tagName==="BUTTON"||e.getAttribute("role")==="button")return e;if(e.tagName==="INPUT"){let i=e.type?.toLowerCase();if(i==="submit"||i==="button")return e}let t=e.parentElement,n=0;for(;t&&n<3;){if(t.tagName==="BUTTON"||t.getAttribute("role")==="button")return t;t=t.parentElement,n++}return null}getElementText(e){let t=e.textContent?.trim().substring(0,100);if(t)return t;let n=e.getAttribute("aria-label")?.trim().substring(0,100);if(n)return n;if(e.tagName==="INPUT"){let s=e.value?.trim().substring(0,100);if(s)return s}let i=e.getAttribute("title")?.trim().substring(0,100);if(i)return i}cleanup(){document.removeEventListener("click",this.handleClick.bind(this),!0)}};var H=class{constructor(e){this.tracker=e}initialize(){document.addEventListener("copy",this.handleCopy.bind(this))}handleCopy(){let e=window.getSelection();if(!e||e.isCollapsed)return;let t=e.toString(),n=t.length;if(n===0)return;let i=e.anchorNode,s=i instanceof HTMLElement?i:i?.parentElement;if(!s)return;let a={text:t.substring(0,500),...n>500&&{textLength:n},sourceElement:s.tagName.toLowerCase()};this.tracker.trackCopy(a)}cleanup(){document.removeEventListener("copy",this.handleCopy.bind(this))}};var W=class{constructor(e,t){this.tracker=e,this.config=t,this.boundHandleSubmit=this.handleSubmit.bind(this),this.boundHandleChange=this.handleChange.bind(this)}initialize(){document.addEventListener("submit",this.boundHandleSubmit,!0),document.addEventListener("change",this.boundHandleChange,!0)}cleanup(){document.removeEventListener("submit",this.boundHandleSubmit,!0),document.removeEventListener("change",this.boundHandleChange,!0)}handleSubmit(e){let t=e.target;if(t.tagName!=="FORM")return;let n={formId:t.id||"",formName:t.name||"",formAction:t.action||"",method:(t.method||"get").toUpperCase(),fieldCount:t.elements.length,ariaLabel:t.getAttribute("aria-label")||void 0,...this.extractDataAttributes(t)};this.tracker.trackFormSubmit(n)}handleChange(e){let t=e.target,n=t.tagName.toUpperCase();if(!["INPUT","SELECT","TEXTAREA"].includes(n)||t.disabled||t.readOnly)return;if(n==="INPUT"){let a=t.type?.toLowerCase();if(a==="hidden"||a==="password")return}let i=t.name||t.id||t.getAttribute("aria-label")||t.placeholder||"",s={element:n.toLowerCase(),inputType:n==="INPUT"?t.type?.toLowerCase():void 0,inputName:i,formId:t.form?.id||void 0,formName:t.form?.name||void 0,...this.extractDataAttributes(t)};this.tracker.trackInputChange(s)}extractDataAttributes(e){let t={};for(let n of e.attributes)if(n.name.startsWith("data-rybbit-prop-")){let i=n.name.replace("data-rybbit-prop-","");t[i]=n.value}return t}};(async function(){let r=document.currentScript;if(!r){console.error("Could not find current script tag");return}let e=r.getAttribute("data-namespace")||"rybbit",t=`disable-${e}`;if(window.__RYBBIT_OPTOUT__||localStorage.getItem(t)!==null){window[e]={pageview:()=>{},event:()=>{},error:()=>{},trackOutbound:()=>{},identify:()=>{},setTraits:()=>{},clearUserId:()=>{},getUserId:()=>null,flag:(o,l)=>l,flagPayload:(o,l)=>l,flags:()=>({}),flagPayloads:()=>({}),onReady:()=>{},startSessionReplay:()=>{},stopSessionReplay:()=>{},isSessionReplayActive:()=>!1};return}let n=[],i=o=>(...l)=>{n.push([o,l])};window[e]={pageview:i("pageview"),event:i("event"),error:i("error"),trackOutbound:i("trackOutbound"),identify:i("identify"),setTraits:i("setTraits"),clearUserId:i("clearUserId"),getUserId:()=>null,flag:(o,l)=>l,flagPayload:(o,l)=>l,flags:()=>({}),flagPayloads:()=>({}),onReady:i("onReady"),startSessionReplay:i("startSessionReplay"),stopSessionReplay:i("stopSessionReplay"),isSessionReplayActive:()=>!1};let s=await ue(r);if(!s)return;let a=new x(s);s.enableWebVitals&&new D(l=>{a.trackWebVitals(l)}).initialize();let c=null,u=null,d=null;s.trackButtonClicks&&(c=new U(a,s),c.initialize()),s.trackCopy&&(u=new H(a),u.initialize()),s.trackFormInteractions&&(d=new W(a,s),d.initialize()),s.trackErrors&&(window.addEventListener("error",o=>{a.trackError(o.error||new Error(o.message),{filename:o.filename,lineno:o.lineno,colno:o.colno})}),window.addEventListener("unhandledrejection",o=>{let l=o.reason instanceof Error?o.reason:new Error(String(o.reason));a.trackError(l,{type:"unhandledrejection"})}));let g=()=>a.trackPageview(),y=s.debounceDuration>0?oe(g,s.debounceDuration):g;function T(){if(document.addEventListener("click",function(o){let l=o.target;for(;l&&l!==document.documentElement;){if(l.hasAttribute("data-rybbit-event")){let p=l.getAttribute("data-rybbit-event");if(p){let S={};for(let E of l.attributes)if(E.name.startsWith("data-rybbit-prop-")){let R=E.name.replace("data-rybbit-prop-","");S[R]=E.value}a.trackEvent(p,S)}break}l=l.parentElement}if(s.trackOutbound){let p=o.target.closest("a");p?.href&&le(p.href)&&a.trackOutbound(p.href,p.innerText||p.textContent||"",p.target||"_self")}}),s.autoTrackSpa){let o=history.pushState,l=history.replaceState;history.pushState=function(...p){o.apply(this,p),y(),a.onPageChange()},history.replaceState=function(...p){l.apply(this,p),y(),a.onPageChange()},window.addEventListener("popstate",()=>{y(),a.onPageChange()}),window.addEventListener("hashchange",()=>{y(),a.onPageChange()})}}window[s.namespace]={pageview:()=>a.trackPageview(),event:(o,l={})=>a.trackEvent(o,l),error:(o,l={})=>a.trackError(o,l),trackOutbound:(o,l="",p="_self")=>a.trackOutbound(o,l,p),identify:(o,l)=>a.identify(o,l),setTraits:o=>a.setTraits(o),clearUserId:()=>a.clearUserId(),getUserId:()=>a.getUserId(),flag:(o,l)=>a.getFeatureFlag(o,l),flagPayload:(o,l)=>a.getFeatureFlagPayload(o,l),flags:()=>a.getFeatureFlags(),flagPayloads:()=>a.getFeatureFlagPayloads(),onReady:o=>o(window[s.namespace]),startSessionReplay:()=>a.startSessionReplay(),stopSessionReplay:()=>a.stopSessionReplay(),isSessionReplayActive:()=>a.isSessionReplayActive()};let h=window[s.namespace];for(let[o,l]of n)h[o](...l);T(),window.addEventListener("beforeunload",()=>{c?.cleanup(),u?.cleanup(),a.cleanup()}),s.autoTrackPageview&&a.trackPageview()})();})();
+"use strict";(()=>{var $e=Object.defineProperty;var je=(r,e,t)=>e in r?$e(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var w=(r,e,t)=>je(r,typeof e!="symbol"?e+"":e,t);function Ke(r){if(r.startsWith("re:")){let c=r.slice(3);if(!c)throw new Error("Empty regex pattern");return new RegExp(c)}let t="__DOUBLE_ASTERISK_TOKEN__",n="__SINGLE_ASTERISK_TOKEN__",s=r.replace(/\*\*/g,t).replace(/\*/g,n).replace(/[.+?^${}()|[\]\\]/g,"\\$&");s=s.replace(new RegExp(`/${t}/`,"g"),"/(?:.+/)?"),s=s.replace(new RegExp(t,"g"),".*"),s=s.replace(/\//g,"\\/");let a=s.replace(new RegExp(n,"g"),"[^/]+");return new RegExp("^"+a+"$")}function V(r,e){for(let t of e)try{if(Ke(t).test(r))return t}catch(n){console.error(`Invalid pattern: ${t}`,n)}return null}function oe(r,e){let t=null;return(...n)=>{t&&clearTimeout(t),t=setTimeout(()=>r(...n),e)}}function le(r){try{let e=window.location.hostname,t=new URL(r).hostname;return t!==e&&t!==""}catch{return!1}}function C(r,e){if(!r)return e;try{let t=JSON.parse(r);return Array.isArray(e)&&!Array.isArray(t)?e:t}catch(t){return console.error("Error parsing JSON:",t),e}}var qe=2e3;function ce(){try{if(crypto?.randomUUID)return crypto.randomUUID()}catch{}return`${Date.now().toString(36)}-${Math.random().toString(36).slice(2,12)}`}function Je(r){let e=`${r}-visitor-id`;try{let t=localStorage.getItem(e);if(t)return t;let n=ce();return localStorage.setItem(e,n),n}catch{return ce()}}function Xe(r){try{return localStorage.getItem(`${r}-user-id`)||void 0}catch{return}}function Qe(r){return r.hash&&r.hash.startsWith("#/")?r.hash.substring(1):r.pathname}async function Ye(r,e,t,n){let i=new AbortController,s=window.setTimeout(()=>i.abort(),qe);try{let a=new URL(window.location.href),c=await fetch(`${r}/site/${e}/feature-flags/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},credentials:"omit",signal:i.signal,body:JSON.stringify({anonymousId:n,identifiedUserId:Xe(t),hostname:a.hostname,pathname:Qe(a),querystring:a.search,query:Object.fromEntries(a.searchParams.entries()),referrer:document.referrer,language:navigator.language,screenWidth:screen.width,screenHeight:screen.height})});if(!c.ok)return{enabled:!0,flags:{}};let u=await c.json();return{enabled:u?.featureFlagsEnabled!==!1,flags:u?.flags&&typeof u.flags=="object"?u.flags:{}}}catch{return{enabled:!0,flags:{}}}finally{window.clearTimeout(s)}}async function ue(r){let e=r.getAttribute("src");if(!e)return console.error("Script src attribute is missing"),null;let t=e.split("/script.js")[0];if(!t)return console.error("Please provide a valid analytics host"),null;let n=r.getAttribute("data-site-id")||r.getAttribute("site-id");if(!n)return console.error("Please provide a valid site ID using the data-site-id attribute"),null;let i=r.getAttribute("data-namespace")||"rybbit",s=Je(i),a=C(r.getAttribute("data-skip-patterns"),[]),c=C(r.getAttribute("data-mask-patterns"),[]),u=C(r.getAttribute("data-replay-mask-text-selectors"),[]),d=r.getAttribute("data-debounce")?Math.max(0,parseInt(r.getAttribute("data-debounce"))):500,g=r.getAttribute("data-replay-batch-size")?Math.max(1,parseInt(r.getAttribute("data-replay-batch-size"))):250,y=r.getAttribute("data-replay-batch-interval")?Math.max(1e3,parseInt(r.getAttribute("data-replay-batch-interval"))):5e3,T=r.getAttribute("data-replay-block-class")||void 0,h=r.getAttribute("data-replay-block-selector")||void 0,o=r.getAttribute("data-replay-ignore-class")||void 0,l=r.getAttribute("data-replay-ignore-selector")||void 0,p=r.getAttribute("data-replay-mask-text-class")||void 0,S=r.getAttribute("data-replay-mask-all-inputs"),E=S!==null?S!=="false":void 0,R=r.getAttribute("data-replay-mask-input-options"),Ue=R?C(R,{password:!0,email:!0}):void 0,ne=r.getAttribute("data-replay-collect-fonts"),He=ne!==null?ne!=="false":void 0,re=r.getAttribute("data-replay-sampling"),We=re?C(re,{}):void 0,ie=r.getAttribute("data-replay-slim-dom-options"),Ve=ie?C(ie,{}):void 0,se=r.getAttribute("data-replay-sample-rate"),Ge=se?Math.min(100,Math.max(0,parseInt(se,10))):void 0,ze=r.getAttribute("data-tag")||"",f={namespace:i,analyticsHost:t,siteId:n,visitorId:s,debounceDuration:d,sessionReplayBatchSize:g,sessionReplayBatchInterval:y,sessionReplayMaskTextSelectors:u,skipPatterns:a,maskPatterns:c,autoTrackPageview:!0,autoTrackSpa:!0,trackQuerystring:!0,trackOutbound:!0,enableWebVitals:!1,trackErrors:!1,enableSessionReplay:!1,trackButtonClicks:!1,trackCopy:!1,trackFormInteractions:!1,tag:ze,featureFlagsEnabled:!1,featureFlags:{},sessionReplayBlockClass:T,sessionReplayBlockSelector:h,sessionReplayIgnoreClass:o,sessionReplayIgnoreSelector:l,sessionReplayMaskTextClass:p,sessionReplayMaskAllInputs:E,sessionReplayMaskInputOptions:Ue,sessionReplayCollectFonts:He,sessionReplaySampling:We,sessionReplaySlimDOMOptions:Ve,sessionReplaySampleRate:Ge},A=f;try{let I=`${t}/site/tracking-config/${n}`,ae=await fetch(I,{method:"GET",credentials:"omit"});if(ae.ok){let m=await ae.json();A={...f,autoTrackPageview:m.trackInitialPageView??f.autoTrackPageview,autoTrackSpa:m.trackSpaNavigation??f.autoTrackSpa,trackQuerystring:m.trackUrlParams??f.trackQuerystring,trackOutbound:m.trackOutbound??f.trackOutbound,enableWebVitals:m.webVitals??f.enableWebVitals,trackErrors:m.trackErrors??f.trackErrors,enableSessionReplay:m.sessionReplay??f.enableSessionReplay,trackButtonClicks:m.trackButtonClicks??f.trackButtonClicks,trackCopy:m.trackCopy??f.trackCopy,trackFormInteractions:m.trackFormInteractions??f.trackFormInteractions,featureFlagsEnabled:m.featureFlagsEnabled===!0}}else console.warn("Failed to fetch tracking config from API, using defaults")}catch(I){console.warn("Error fetching tracking config:",I)}if(A.featureFlagsEnabled){let I=await Ye(t,n,i,s);A.featureFlagsEnabled=I.enabled,A.featureFlags=I.flags}return A}var de="rybbit-replay-sampled";function Ze(r){if(r>=100)return!0;if(r<=0)return!1;try{let e=sessionStorage.getItem(de);if(e!==null)return e==="1";let t=Math.random()*100<r;return sessionStorage.setItem(de,t?"1":"0"),t}catch{return Math.random()*100<r}}var M=class{constructor(e,t,n){this.isRecording=!1;this.eventBuffer=[];this.config=e,this.userId=t,this.sendBatch=n}async initialize(){if(!this.config.enableSessionReplay)return;let e=this.config.sessionReplaySampleRate;e!==void 0&&!Ze(e)||(window.rrweb||await this.loadRrweb(),window.rrweb&&this.startRecording())}async loadRrweb(){return new Promise((e,t)=>{let n=document.createElement("script");n.src=`${this.config.analyticsHost}/replay.js`,n.async=!1,n.onload=()=>{e()},n.onerror=()=>t(new Error("Failed to load rrweb")),document.head.appendChild(n)})}startRecording(){if(!(this.isRecording||!window.rrweb||!this.config.enableSessionReplay))try{let e={mousemove:!1,mouseInteraction:{MouseUp:!1,MouseDown:!1,Click:!0,ContextMenu:!1,DblClick:!0,Focus:!0,Blur:!0,TouchStart:!1,TouchEnd:!1},scroll:500,input:"last",media:800},t={script:!1,comment:!0,headFavicon:!0,headWhitespace:!0,headMetaDescKeywords:!0,headMetaSocial:!0,headMetaRobots:!0,headMetaHttpEquiv:!0,headMetaAuthorship:!0,headMetaVerification:!0},n={emit:i=>{this.addEvent({type:i.type,data:i.data,timestamp:i.timestamp||Date.now()})},recordCanvas:!1,checkoutEveryNms:6e4,checkoutEveryNth:500,blockClass:this.config.sessionReplayBlockClass??"rr-block",blockSelector:this.config.sessionReplayBlockSelector??null,ignoreClass:this.config.sessionReplayIgnoreClass??"rr-ignore",ignoreSelector:this.config.sessionReplayIgnoreSelector??null,maskTextClass:this.config.sessionReplayMaskTextClass??"rr-mask",maskAllInputs:this.config.sessionReplayMaskAllInputs??!0,maskInputOptions:this.config.sessionReplayMaskInputOptions??{password:!0,email:!0},collectFonts:this.config.sessionReplayCollectFonts??!0,sampling:this.config.sessionReplaySampling??e,slimDOMOptions:this.config.sessionReplaySlimDOMOptions??t};this.config.sessionReplayMaskTextSelectors&&this.config.sessionReplayMaskTextSelectors.length>0&&(n.maskTextSelector=this.config.sessionReplayMaskTextSelectors.join(", ")),this.stopRecordingFn=window.rrweb.record(n),this.isRecording=!0,this.setupBatchTimer()}catch{}}stopRecording(){this.isRecording&&(this.stopRecordingFn&&this.stopRecordingFn(),this.isRecording=!1,this.clearBatchTimer(),this.eventBuffer.length>0&&this.flushEvents())}isActive(){return this.isRecording}addEvent(e){this.eventBuffer.push(e),this.eventBuffer.length>=this.config.sessionReplayBatchSize&&this.flushEvents()}setupBatchTimer(){this.clearBatchTimer(),this.batchTimer=window.setInterval(()=>{this.eventBuffer.length>0&&this.flushEvents()},this.config.sessionReplayBatchInterval)}clearBatchTimer(){this.batchTimer&&(clearInterval(this.batchTimer),this.batchTimer=void 0)}async flushEvents(){if(this.eventBuffer.length===0)return;let e=[...this.eventBuffer];this.eventBuffer=[];let t={userId:this.userId,events:e,metadata:{pageUrl:window.location.href,viewportWidth:screen.width,viewportHeight:screen.height,language:navigator.language}};try{await this.sendBatch(t)}catch{this.eventBuffer.unshift(...e)}}updateUserId(e){e!==this.userId&&(this.eventBuffer.length>0&&this.flushEvents(),this.userId=e)}onPageChange(){this.isRecording&&this.flushEvents()}cleanup(){this.stopRecording()}};var b={automationApi:1,zeroOuterDimensions:2,missingChrome:4,swiftShader:8,emptyPlugins:16,defaultViewport800x600:32,defaultViewport1024x768:64,impossibleDimensions:128,outerDimensionsWeird:256,pluginApiAbsence:512,defaultViewport1280x1200:1024,squareScreen:2048,missingScreenDimensions:4096},et=Object.keys(b),he={automationApi:3,zeroOuterDimensions:2,missingChrome:1,swiftShader:1,emptyPlugins:1,defaultViewport800x600:3,defaultViewport1024x768:3,impossibleDimensions:3,outerDimensionsWeird:2,pluginApiAbsence:0,defaultViewport1280x1200:3,squareScreen:3,missingScreenDimensions:1},ht=et.reduce((r,e)=>r|b[e],0),ft=b.automationApi|b.impossibleDimensions|b.defaultViewport800x600|b.defaultViewport1024x768|b.defaultViewport1280x1200|b.squareScreen,fe=10,pe=200,ge=8192,tt=[{width:800,height:600,signal:"defaultViewport800x600"},{width:1024,height:768,signal:"defaultViewport1024x768"},{width:1280,height:1200,signal:"defaultViewport1280x1200"}];function nt(r,e){return Number.isFinite(r)&&Number.isFinite(e)&&r>=pe&&e>=pe&&r<=ge&&e<=ge}function rt(r){return/Windows NT|Macintosh|X11|Linux x86_64/.test(r)&&!/Mobile|Android|iPhone|iPad/.test(r)}function me(r,e,t){if(!nt(r,e))return["impossibleDimensions"];let n=[];if(r===e&&n.push("squareScreen"),rt(t))for(let i of tt)r===i.width&&e===i.height&&n.push(i.signal);return n}var ye=null;function ve(){return Se().score}function ke(){return Se().mask}function we(){return document.prerendering===!0}function Se(){return we()?be():(ye??(ye=be()),ye)}function be(){let r=0,e=0;function t(n){let i=b[n];(e&i)===0&&(e|=i,r+=he[n])}try{let n=navigator.userAgent,i=/Chrome\//.test(n)&&!/\bwv\b|; wv\)/.test(n),s=Number(window.screen?.width),a=Number(window.screen?.height),c=Number(window.outerWidth),u=Number(window.outerHeight),d=Number(window.innerWidth),g=Number(window.innerHeight),T=["__webdriver_evaluate","__selenium_evaluate","__webdriver_script_function","__webdriver_script_func","__webdriver_script_fn","__fxdriver_evaluate","__driver_unwrapped","__webdriver_unwrapped","__driver_evaluate","__selenium_unwrapped","__fxdriver_unwrapped","_phantom","callPhantom","__nightmare","domAutomation","domAutomationController"].some(o=>o in window||o in document);(navigator.webdriver===!0||T)&&t("automationApi"),(u===0||c===0)&&!we()&&t("zeroOuterDimensions");for(let o of me(s,a,n))t(o);Number.isFinite(c)&&Number.isFinite(u)&&Number.isFinite(d)&&Number.isFinite(g)&&c>0&&u>0&&d>0&&g>0&&(c+8<d||u+8<g)&&t("outerDimensionsWeird");let h=!1;!window.chrome&&i&&(t("missingChrome"),h=!0);try{let o=document.createElement("canvas"),l=o.getContext("webgl")||o.getContext("experimental-webgl");if(l)try{let p=[],S=l.getParameter(l.RENDERER);typeof S=="string"&&p.push(S);try{let E=l.getExtension("WEBGL_debug_renderer_info");if(E){let R=l.getParameter(E.UNMASKED_RENDERER_WEBGL);typeof R=="string"&&p.push(R)}}catch{}p.join(" ").toLowerCase().includes("swiftshader")&&t("swiftShader")}finally{it(o,l)}}catch{}(!navigator.plugins||navigator.plugins.length===0)&&i&&(t("emptyPlugins"),h=!0),h&&t("pluginApiAbsence")}catch{}return{score:Math.min(r,fe),mask:e}}function it(r,e){try{e.getExtension("WEBGL_lose_context")?.loseContext?.()}catch{}r.width=0,r.height=0}var st=2e3,x=class{constructor(e){this.customUserId=null;this.errorDedupeCache=new Map;this.errorDedupeLastCleanup=0;this.exposedFeatureFlags=new Set;this.config=e,this.loadUserId(),e.enableSessionReplay&&this.initializeSessionReplay()}serializeFeatureFlagValue(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return String(e);try{return JSON.stringify(e)}catch{return""}}getFeatureFlagEventPayload(){let e={};for(let[t,n]of Object.entries(this.config.featureFlags||{}))e[t]=this.serializeFeatureFlagValue(n.value);return e}getCurrentUrlContext(){let e=new URL(window.location.href),t=e.hash&&e.hash.startsWith("#/")?e.hash.substring(1):e.pathname;return{hostname:e.hostname,pathname:t,querystring:e.search,query:Object.fromEntries(e.searchParams.entries()),referrer:document.referrer,language:navigator.language,screenWidth:screen.width,screenHeight:screen.height}}async refreshFeatureFlags(){if(!this.config.featureFlagsEnabled)return;let e=new AbortController,t=window.setTimeout(()=>e.abort(),st);try{let n=await fetch(`${this.config.analyticsHost}/site/${this.config.siteId}/feature-flags/evaluate`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({anonymousId:this.config.visitorId,identifiedUserId:this.customUserId||void 0,...this.getCurrentUrlContext()}),mode:"cors",credentials:"omit",keepalive:!0,signal:e.signal});if(!n.ok)return;let i=await n.json();this.config.featureFlags=i?.flags&&typeof i.flags=="object"?i.flags:{},i?.featureFlagsEnabled===!1&&(this.config.featureFlagsEnabled=!1)}catch{}finally{window.clearTimeout(t)}}loadUserId(){try{let e=localStorage.getItem(`${this.config.namespace}-user-id`);e&&(this.customUserId=e)}catch{}}async initializeSessionReplay(){try{this.sessionReplayRecorder=new M(this.config,this.customUserId||"",e=>this.sendSessionReplayBatch(e)),await this.sessionReplayRecorder.initialize()}catch(e){console.error("Failed to initialize session replay:",e)}}async sendSessionReplayBatch(e){try{await fetch(`${this.config.analyticsHost}/session-replay/record/${this.config.siteId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),mode:"cors",keepalive:!1})}catch(t){throw console.error("Failed to send session replay batch:",t),t}}createBasePayload(){let e=new URL(window.location.href),t=e.pathname;if(e.hash&&e.hash.startsWith("#/")&&(t=e.hash.substring(1)),V(t,this.config.skipPatterns))return null;let n=V(t,this.config.maskPatterns);n&&(t=n);let i={site_id:this.config.siteId,hostname:e.hostname,pathname:t,querystring:this.config.trackQuerystring?e.search:"",screenWidth:screen.width,screenHeight:screen.height,language:navigator.language,page_title:document.title,referrer:document.referrer,_bs:ve(),_bsm:ke()};this.customUserId&&(i.user_id=this.customUserId),this.config.tag&&(i.tag=this.config.tag);let s=this.getFeatureFlagEventPayload();return Object.keys(s).length>0&&(i.feature_flags=s),i}async sendTrackingData(e){try{await fetch(`${this.config.analyticsHost}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),mode:"cors",keepalive:!0})}catch(t){console.error("Failed to send tracking data:",t)}}track(e,t="",n={}){if(e==="custom_event"&&(!t||typeof t!="string")){console.error("Event name is required and must be a string for custom events");return}let i=this.createBasePayload();if(!i)return;let a={...i,type:e,event_name:t,properties:["custom_event","outbound","error","button_click","copy","form_submit","input_change"].includes(e)?JSON.stringify(n):void 0};this.sendTrackingData(a)}trackPageview(){this.track("pageview")}trackEvent(e,t={}){this.track("custom_event",e,t)}getFeatureFlag(e,t){let n=this.config.featureFlags?.[e];if(!n)return t;let i=`${e}:${n.version}:${this.serializeFeatureFlagValue(n.value)}`;return this.exposedFeatureFlags.has(i)||(this.exposedFeatureFlags.add(i),this.trackEvent("feature_flag_exposure",{key:e,value:this.serializeFeatureFlagValue(n.value),version:n.version,reason:n.reason})),n.value}getFeatureFlags(){return Object.fromEntries(Object.entries(this.config.featureFlags||{}).map(([e,t])=>[e,t.value]))}getFeatureFlagPayload(e,t){let n=this.config.featureFlags?.[e];return!n||n.payload===void 0?t:n.payload}getFeatureFlagPayloads(){return Object.fromEntries(Object.entries(this.config.featureFlags||{}).filter(([,e])=>e.payload!==void 0).map(([e,t])=>[e,t.payload]))}trackOutbound(e,t="",n="_self"){this.track("outbound","",{url:e,text:t,target:n})}trackWebVitals(e){let t=this.createBasePayload();if(!t)return;let n={...t,type:"performance",event_name:"web-vitals",...e};this.sendTrackingData(n)}trackError(e,t={}){let n=e?.message||"";if(n.includes("ResizeObserver loop completed with undelivered notifications")||n.includes("ResizeObserver loop limit exceeded"))return;let i=window.location.origin,s=t.filename||"",a=e.stack||"";if(s)try{if(new URL(s).origin!==i)return}catch{}else if(a&&!a.includes(i))return;let u=[e.name||"Error",n,t.filename||"",t.lineno??"",t.colno??""].join("|"),d=Date.now(),g=6e4,y=this.errorDedupeCache.get(u);if(y&&d-y<g)return;this.errorDedupeCache.set(u,d);let T=10*6e4;if(d-this.errorDedupeLastCleanup>g){for(let[o,l]of this.errorDedupeCache.entries())d-l>T&&this.errorDedupeCache.delete(o);this.errorDedupeLastCleanup=d}let h={message:e.message?.substring(0,500)||"Unknown error",stack:a.substring(0,2e3)||""};if(s&&(h.fileName=s),t.lineno){let o=typeof t.lineno=="string"?parseInt(t.lineno,10):t.lineno;o&&o!==0&&(h.lineNumber=o)}if(t.colno){let o=typeof t.colno=="string"?parseInt(t.colno,10):t.colno;o&&o!==0&&(h.columnNumber=o)}for(let o in t)!["lineno","colno"].includes(o)&&t[o]!==void 0&&(h[o]=t[o]);this.track("error",e.name||"Error",h)}trackButtonClick(e){this.track("button_click","",e)}trackCopy(e){this.track("copy","",e)}trackFormSubmit(e){this.track("form_submit","",e)}trackInputChange(e){this.track("input_change","",e)}identify(e,t){if(typeof e!="string"||e.trim()===""){console.error("User ID must be a non-empty string");return}this.customUserId=e.trim();try{localStorage.setItem(`${this.config.namespace}-user-id`,this.customUserId)}catch{console.warn("Could not persist user ID to localStorage")}this.sendIdentifyEvent(this.customUserId,t,!0).then(()=>this.refreshFeatureFlags()),this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(this.customUserId)}setTraits(e){if(!e||typeof e!="object"){console.error("Traits must be an object");return}let t=this.customUserId;if(!t){console.warn("Cannot set traits without identifying user first. Call identify() first.");return}this.sendIdentifyEvent(t,e,!1).then(()=>this.refreshFeatureFlags())}async sendIdentifyEvent(e,t,n=!0){try{await fetch(`${this.config.analyticsHost}/identify`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({site_id:this.config.siteId,user_id:e,traits:t,is_new_identify:n}),mode:"cors",keepalive:!0})}catch(i){console.error("Failed to send identify event:",i)}}clearUserId(){this.customUserId=null;try{localStorage.removeItem(`${this.config.namespace}-user-id`)}catch{}this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(""),this.refreshFeatureFlags()}getUserId(){return this.customUserId}startSessionReplay(){this.sessionReplayRecorder?this.sessionReplayRecorder.startRecording():console.warn("Session replay not initialized")}stopSessionReplay(){this.sessionReplayRecorder&&this.sessionReplayRecorder.stopRecording()}isSessionReplayActive(){return this.sessionReplayRecorder?.isActive()??!1}onPageChange(){this.refreshFeatureFlags(),this.sessionReplayRecorder&&this.sessionReplayRecorder.onPageChange()}cleanup(){this.sessionReplayRecorder&&this.sessionReplayRecorder.cleanup()}};var Le=-1,_=r=>{addEventListener("pageshow",(e=>{e.persisted&&(Le=e.timeStamp,r(e))}),!0)},v=(r,e,t,n)=>{let i,s;return a=>{e.value>=0&&(a||n)&&(s=e.value-(i??0),(s||i===void 0)&&(i=e.value,e.delta=s,e.rating=((c,u)=>c>u[1]?"poor":c>u[0]?"needs-improvement":"good")(e.value,t),r(e)))}},Q=r=>{requestAnimationFrame((()=>requestAnimationFrame((()=>r()))))},Y=()=>{let r=performance.getEntriesByType("navigation")[0];if(r&&r.responseStart>0&&r.responseStart<performance.now())return r},L=()=>Y()?.activationStart??0,k=(r,e=-1)=>{let t=Y(),n="navigate";return Le>=0?n="back-forward-cache":t&&(document.prerendering||L()>0?n="prerender":document.wasDiscarded?n="restore":t.type&&(n=t.type.replace(/_/g,"-"))),{name:r,value:e,rating:"good",delta:0,entries:[],id:`v5-${Date.now()}-${Math.floor(8999999999999*Math.random())+1e12}`,navigationType:n}},G=new WeakMap;function Z(r,e){return G.get(r)||G.set(r,new e),G.get(r)}var $=class{constructor(){w(this,"t");w(this,"i",0);w(this,"o",[])}h(e){if(e.hadRecentInput)return;let t=this.o[0],n=this.o.at(-1);this.i&&t&&n&&e.startTime-n.startTime<1e3&&e.startTime-t.startTime<5e3?(this.i+=e.value,this.o.push(e)):(this.i=e.value,this.o=[e]),this.t?.(e)}},F=(r,e,t={})=>{try{if(PerformanceObserver.supportedEntryTypes.includes(r)){let n=new PerformanceObserver((i=>{Promise.resolve().then((()=>{e(i.getEntries())}))}));return n.observe({type:r,buffered:!0,...t}),n}}catch{}},ee=r=>{let e=!1;return()=>{e||(r(),e=!0)}},P=-1,Fe=new Set,Ee=()=>document.visibilityState!=="hidden"||document.prerendering?1/0:0,j=r=>{if(document.visibilityState==="hidden"){if(r.type==="visibilitychange")for(let e of Fe)e();isFinite(P)||(P=r.type==="visibilitychange"?r.timeStamp:0,removeEventListener("prerenderingchange",j,!0))}},N=()=>{if(P<0){let r=L();P=(document.prerendering?void 0:globalThis.performance.getEntriesByType("visibility-state").filter((t=>t.name==="hidden"&&t.startTime>r))[0]?.startTime)??Ee(),addEventListener("visibilitychange",j,!0),addEventListener("prerenderingchange",j,!0),_((()=>{setTimeout((()=>{P=Ee()}))}))}return{get firstHiddenTime(){return P},onHidden(r){Fe.add(r)}}},O=r=>{document.prerendering?addEventListener("prerenderingchange",(()=>r()),!0):r()},Te=[1800,3e3],te=(r,e={})=>{O((()=>{let t=N(),n,i=k("FCP"),s=F("paint",(a=>{for(let c of a)c.name==="first-contentful-paint"&&(s.disconnect(),c.startTime<t.firstHiddenTime&&(i.value=Math.max(c.startTime-L(),0),i.entries.push(c),n(!0)))}));s&&(n=v(r,i,Te,e.reportAllChanges),_((a=>{i=k("FCP"),n=v(r,i,Te,e.reportAllChanges),Q((()=>{i.value=performance.now()-a.timeStamp,n(!0)}))})))}))},Re=[.1,.25],Me=(r,e={})=>{let t=N();te(ee((()=>{let n,i=k("CLS",0),s=Z(e,$),a=u=>{for(let d of u)s.h(d);s.i>i.value&&(i.value=s.i,i.entries=s.o,n())},c=F("layout-shift",a);c&&(n=v(r,i,Re,e.reportAllChanges),t.onHidden((()=>{a(c.takeRecords()),n(!0)})),_((()=>{s.i=0,i=k("CLS",0),n=v(r,i,Re,e.reportAllChanges),Q((()=>n()))})),setTimeout(n))})))},xe=0,z=1/0,B=0,at=r=>{for(let e of r)e.interactionId&&(z=Math.min(z,e.interactionId),B=Math.max(B,e.interactionId),xe=B?(B-z)/7+1:0)},K,Ce=()=>K?xe:performance.interactionCount??0,ot=()=>{"interactionCount"in performance||K||(K=F("event",at,{type:"event",buffered:!0,durationThreshold:0}))},Ie=0,q=class{constructor(){w(this,"u",[]);w(this,"l",new Map);w(this,"m");w(this,"p")}v(){Ie=Ce(),this.u.length=0,this.l.clear()}L(){let e=Math.min(this.u.length-1,Math.floor((Ce()-Ie)/50));return this.u[e]}h(e){if(this.m?.(e),!e.interactionId&&e.entryType!=="first-input")return;let t=this.u.at(-1),n=this.l.get(e.interactionId);if(n||this.u.length<10||e.duration>t.P){if(n?e.duration>n.P?(n.entries=[e],n.P=e.duration):e.duration===n.P&&e.startTime===n.entries[0].startTime&&n.entries.push(e):(n={id:e.interactionId,entries:[e],P:e.duration},this.l.set(n.id,n),this.u.push(n)),this.u.sort(((i,s)=>s.P-i.P)),this.u.length>10){let i=this.u.splice(10);for(let s of i)this.l.delete(s.id)}this.p?.(n)}}},Be=r=>{let e=globalThis.requestIdleCallback||setTimeout;document.visibilityState==="hidden"?r():(r=ee(r),addEventListener("visibilitychange",r,{once:!0,capture:!0}),e((()=>{r(),removeEventListener("visibilitychange",r,{capture:!0})})))},Pe=[200,500],Ne=(r,e={})=>{if(!globalThis.PerformanceEventTiming||!("interactionId"in PerformanceEventTiming.prototype))return;let t=N();O((()=>{ot();let n,i=k("INP"),s=Z(e,q),a=u=>{Be((()=>{for(let g of u)s.h(g);let d=s.L();d&&d.P!==i.value&&(i.value=d.P,i.entries=d.entries,n())}))},c=F("event",a,{durationThreshold:e.durationThreshold??40});n=v(r,i,Pe,e.reportAllChanges),c&&(c.observe({type:"first-input",buffered:!0}),t.onHidden((()=>{a(c.takeRecords()),n(!0)})),_((()=>{s.v(),i=k("INP"),n=v(r,i,Pe,e.reportAllChanges)})))}))},J=class{constructor(){w(this,"m")}h(e){this.m?.(e)}},_e=[2500,4e3],Oe=(r,e={})=>{O((()=>{let t=N(),n,i=k("LCP"),s=Z(e,J),a=u=>{e.reportAllChanges||(u=u.slice(-1));for(let d of u)s.h(d),d.startTime<t.firstHiddenTime&&(i.value=Math.max(d.startTime-L(),0),i.entries=[d],n())},c=F("largest-contentful-paint",a);if(c){n=v(r,i,_e,e.reportAllChanges);let u=ee((()=>{a(c.takeRecords()),c.disconnect(),n(!0)})),d=g=>{g.isTrusted&&(Be(u),removeEventListener(g.type,d,{capture:!0}))};for(let g of["keydown","click","visibilitychange"])addEventListener(g,d,{capture:!0});_((g=>{i=k("LCP"),n=v(r,i,_e,e.reportAllChanges),Q((()=>{i.value=performance.now()-g.timeStamp,n(!0)}))}))}}))},Ae=[800,1800],X=r=>{document.prerendering?O((()=>X(r))):document.readyState!=="complete"?addEventListener("load",(()=>X(r)),!0):setTimeout(r)},De=(r,e={})=>{let t=k("TTFB"),n=v(r,t,Ae,e.reportAllChanges);X((()=>{let i=Y();i&&(t.value=Math.max(i.responseStart-L(),0),t.entries=[i],n(!0),_((()=>{t=k("TTFB",0),n=v(r,t,Ae,e.reportAllChanges),n(!0)})))}))};var D=class{constructor(e){this.data={lcp:null,cls:null,inp:null,fcp:null,ttfb:null};this.sent=!1;this.timeout=null;this.onReadyCallback=null;this.onReadyCallback=e}initialize(){try{Oe(this.collectMetric.bind(this)),Me(this.collectMetric.bind(this)),Ne(this.collectMetric.bind(this)),te(this.collectMetric.bind(this)),De(this.collectMetric.bind(this)),this.timeout=setTimeout(()=>{this.sent||this.sendData()},2e4),window.addEventListener("beforeunload",()=>{this.sent||this.sendData()})}catch(e){console.warn("Error initializing web vitals tracking:",e)}}collectMetric(e){if(this.sent)return;let t=e.name.toLowerCase();this.data[t]=e.value,Object.values(this.data).every(i=>i!==null)&&this.sendData()}sendData(){this.sent||(this.sent=!0,this.timeout&&(clearTimeout(this.timeout),this.timeout=null),this.onReadyCallback&&this.onReadyCallback(this.data))}getData(){return{...this.data}}};var lt=1e3,U=class{constructor(e,t){this.lastClickAt=new WeakMap;this.tracker=e,this.config=t}initialize(){document.addEventListener("click",this.handleClick.bind(this),!0)}handleClick(e){let t=e.target;this.config.trackButtonClicks&&this.isButton(t)&&this.trackButtonClick(t)}isButton(e){if(e.tagName==="BUTTON"||e.getAttribute("role")==="button")return!0;if(e.tagName==="INPUT"){let i=e.type?.toLowerCase();if(i==="submit"||i==="button")return!0}let t=e.parentElement,n=0;for(;t&&n<3;){if(t.tagName==="BUTTON"||t.getAttribute("role")==="button")return!0;t=t.parentElement,n++}return!1}trackButtonClick(e){let t=this.findButton(e);if(!t||t.hasAttribute("data-rybbit-event"))return;let n=Date.now(),i=this.lastClickAt.get(t);if(i!==void 0&&n-i<lt)return;this.lastClickAt.set(t,n);let s={text:this.getElementText(t),...this.extractDataAttributes(t)};this.tracker.trackButtonClick(s)}extractDataAttributes(e){let t={};for(let n of e.attributes)if(n.name.startsWith("data-rybbit-prop-")){let i=n.name.replace("data-rybbit-prop-","");t[i]=n.value}return t}findButton(e){if(e.tagName==="BUTTON"||e.getAttribute("role")==="button")return e;if(e.tagName==="INPUT"){let i=e.type?.toLowerCase();if(i==="submit"||i==="button")return e}let t=e.parentElement,n=0;for(;t&&n<3;){if(t.tagName==="BUTTON"||t.getAttribute("role")==="button")return t;t=t.parentElement,n++}return null}getElementText(e){let t=e.textContent?.trim().substring(0,100);if(t)return t;let n=e.getAttribute("aria-label")?.trim().substring(0,100);if(n)return n;if(e.tagName==="INPUT"){let s=e.value?.trim().substring(0,100);if(s)return s}let i=e.getAttribute("title")?.trim().substring(0,100);if(i)return i}cleanup(){document.removeEventListener("click",this.handleClick.bind(this),!0)}};var H=class{constructor(e){this.tracker=e}initialize(){document.addEventListener("copy",this.handleCopy.bind(this))}handleCopy(){let e=window.getSelection();if(!e||e.isCollapsed)return;let t=e.toString(),n=t.length;if(n===0)return;let i=e.anchorNode,s=i instanceof HTMLElement?i:i?.parentElement;if(!s)return;let a={text:t.substring(0,500),...n>500&&{textLength:n},sourceElement:s.tagName.toLowerCase()};this.tracker.trackCopy(a)}cleanup(){document.removeEventListener("copy",this.handleCopy.bind(this))}};var W=class{constructor(e,t){this.tracker=e,this.config=t,this.boundHandleSubmit=this.handleSubmit.bind(this),this.boundHandleChange=this.handleChange.bind(this)}initialize(){document.addEventListener("submit",this.boundHandleSubmit,!0),document.addEventListener("change",this.boundHandleChange,!0)}cleanup(){document.removeEventListener("submit",this.boundHandleSubmit,!0),document.removeEventListener("change",this.boundHandleChange,!0)}handleSubmit(e){let t=e.target;if(t.tagName!=="FORM")return;let n={formId:t.id||"",formName:t.name||"",formAction:t.action||"",method:(t.method||"get").toUpperCase(),fieldCount:t.elements.length,ariaLabel:t.getAttribute("aria-label")||void 0,...this.extractDataAttributes(t)};this.tracker.trackFormSubmit(n)}handleChange(e){let t=e.target,n=t.tagName.toUpperCase();if(!["INPUT","SELECT","TEXTAREA"].includes(n)||t.disabled||t.readOnly)return;if(n==="INPUT"){let a=t.type?.toLowerCase();if(a==="hidden"||a==="password")return}let i=t.name||t.id||t.getAttribute("aria-label")||t.placeholder||"",s={element:n.toLowerCase(),inputType:n==="INPUT"?t.type?.toLowerCase():void 0,inputName:i,formId:t.form?.id||void 0,formName:t.form?.name||void 0,...this.extractDataAttributes(t)};this.tracker.trackInputChange(s)}extractDataAttributes(e){let t={};for(let n of e.attributes)if(n.name.startsWith("data-rybbit-prop-")){let i=n.name.replace("data-rybbit-prop-","");t[i]=n.value}return t}};(async function(){let r=document.currentScript;if(!r){console.error("Could not find current script tag");return}let e=r.getAttribute("data-namespace")||"rybbit",t=`disable-${e}`;if(window.__RYBBIT_OPTOUT__||localStorage.getItem(t)!==null){window[e]={pageview:()=>{},event:()=>{},error:()=>{},trackOutbound:()=>{},identify:()=>{},setTraits:()=>{},clearUserId:()=>{},getUserId:()=>null,flag:(o,l)=>l,flagPayload:(o,l)=>l,flags:()=>({}),flagPayloads:()=>({}),onReady:()=>{},startSessionReplay:()=>{},stopSessionReplay:()=>{},isSessionReplayActive:()=>!1};return}let n=[],i=o=>(...l)=>{n.push([o,l])};window[e]={pageview:i("pageview"),event:i("event"),error:i("error"),trackOutbound:i("trackOutbound"),identify:i("identify"),setTraits:i("setTraits"),clearUserId:i("clearUserId"),getUserId:()=>null,flag:(o,l)=>l,flagPayload:(o,l)=>l,flags:()=>({}),flagPayloads:()=>({}),onReady:i("onReady"),startSessionReplay:i("startSessionReplay"),stopSessionReplay:i("stopSessionReplay"),isSessionReplayActive:()=>!1};let s=await ue(r);if(!s)return;let a=new x(s);s.enableWebVitals&&new D(l=>{a.trackWebVitals(l)}).initialize();let c=null,u=null,d=null;s.trackButtonClicks&&(c=new U(a,s),c.initialize()),s.trackCopy&&(u=new H(a),u.initialize()),s.trackFormInteractions&&(d=new W(a,s),d.initialize()),s.trackErrors&&(window.addEventListener("error",o=>{a.trackError(o.error||new Error(o.message),{filename:o.filename,lineno:o.lineno,colno:o.colno})}),window.addEventListener("unhandledrejection",o=>{let l=o.reason instanceof Error?o.reason:new Error(String(o.reason));a.trackError(l,{type:"unhandledrejection"})}));let g=()=>a.trackPageview(),y=s.debounceDuration>0?oe(g,s.debounceDuration):g;function T(){if(document.addEventListener("click",function(o){let l=o.target;for(;l&&l!==document.documentElement;){if(l.hasAttribute("data-rybbit-event")){let p=l.getAttribute("data-rybbit-event");if(p){let S={};for(let E of l.attributes)if(E.name.startsWith("data-rybbit-prop-")){let R=E.name.replace("data-rybbit-prop-","");S[R]=E.value}a.trackEvent(p,S)}break}l=l.parentElement}if(s.trackOutbound){let p=o.target.closest("a");p?.href&&le(p.href)&&a.trackOutbound(p.href,p.innerText||p.textContent||"",p.target||"_self")}}),s.autoTrackSpa){let o=history.pushState,l=history.replaceState;history.pushState=function(...p){o.apply(this,p),y(),a.onPageChange()},history.replaceState=function(...p){l.apply(this,p),y(),a.onPageChange()},window.addEventListener("popstate",()=>{y(),a.onPageChange()}),window.addEventListener("hashchange",()=>{y(),a.onPageChange()})}}window[s.namespace]={pageview:()=>a.trackPageview(),event:(o,l={})=>a.trackEvent(o,l),error:(o,l={})=>a.trackError(o,l),trackOutbound:(o,l="",p="_self")=>a.trackOutbound(o,l,p),identify:(o,l)=>a.identify(o,l),setTraits:o=>a.setTraits(o),clearUserId:()=>a.clearUserId(),getUserId:()=>a.getUserId(),flag:(o,l)=>a.getFeatureFlag(o,l),flagPayload:(o,l)=>a.getFeatureFlagPayload(o,l),flags:()=>a.getFeatureFlags(),flagPayloads:()=>a.getFeatureFlagPayloads(),onReady:o=>o(window[s.namespace]),startSessionReplay:()=>a.startSessionReplay(),stopSessionReplay:()=>a.stopSessionReplay(),isSessionReplayActive:()=>a.isSessionReplayActive()};let h=window[s.namespace];for(let[o,l]of n)h[o](...l);T(),window.addEventListener("beforeunload",()=>{c?.cleanup(),u?.cleanup(),a.cleanup()}),s.autoTrackPageview&&a.trackPageview()})();})();
diff --git a/server/src/index.ts b/server/src/index.ts
index 461d9091e..5d1e0c27b 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -185,6 +185,7 @@ import { reengagementService } from "./services/reengagement/reengagementService
import { telemetryService } from "./services/telemetryService.js";
import { handleIdentify } from "./services/tracker/identifyService.js";
import { trackEvent } from "./services/tracker/trackEvent.js";
+import { startSiteBaselineRefresh } from "./services/tracker/botBlocking/siteBaseline.js";
import { usageService } from "./services/usageService.js";
import { weeklyReportService } from "./services/weekyReports/weeklyReportService.js";
import { handleAppSumoWebhook, activateAppSumoLicense } from "./api/as/index.js";
@@ -568,6 +569,10 @@ const start = async () => {
await Promise.all([initializeClickhouse(), initPostgres()]);
}
+ // Every process runs this: the ClickHouse refresh is elected through Redis,
+ // and each worker mirrors the shared result for the site-flood rules.
+ startSiteBaselineRefresh();
+
// Cron jobs should only run on the primary process (or in single-process mode)
if (!cluster.isWorker) {
telemetryService.startTelemetryCron();
diff --git a/server/src/services/tracker/botBlocking/README.md b/server/src/services/tracker/botBlocking/README.md
index 90ae692fb..100d2921a 100644
--- a/server/src/services/tracker/botBlocking/README.md
+++ b/server/src/services/tracker/botBlocking/README.md
@@ -63,7 +63,7 @@ Detected bot requests emit one consolidated log line:
Each detection object contains compact method-specific details such as matched UA pattern, header score, ASN metadata, or anomaly counters.
-`botDetectionStats.ts` also logs process-lifetime totals for tracker requests that reach the bot-blocking entry point every 5 seconds:
+`botDetectionStats.ts` also logs process-lifetime totals for tracker requests that reach the bot-blocking entry point every 60 seconds:
- `totalRequests`
- `totalBotRequests` β€” what was **detected**
@@ -119,8 +119,9 @@ It tracks:
- events, distinct UAs, and distinct hostnames per IP
- site-wide volume for a UA hash
- missing client bot score volume
-- browser-version spread within a cohort (`siteId + screen + language + browser family`)
+- browser-version spread within a cohort (`siteId + screen + language + browser family`), in one-hour buckets
- events per actor per day
+- site volume and distinct actors, and per-cohort volume, distinct actors and direct hits, in 10-minute buckets (the site-flood rules)
- enumeration shape per cohort, in 15-minute buckets
Reasons come back in two lists, and the distinction is structural rather than a matter of how the scores happen to add up:
@@ -128,9 +129,11 @@ Reasons come back in two lists, and the distinction is structural rather than a
- **convicting** β€” keyed on a single actor, or on a distribution no organic population produces. These can open a conviction.
- **supporting** β€” keyed on dimensions real visitors share (an IP, a popular browser), plus long-window volume, where one address may be an entire office behind a SASE gateway. These are only ever added to a score convicting evidence has already opened.
-Three rules deserve their own note:
+Five rules deserve their own note:
-- **Cohort version uniformity** convicts, and is the only rule that can catch a paced distributed crawler β€” one that stays under every per-identity threshold by design. Its key must carry the browser family: without it, Chrome, Safari and Firefox version numbers pool into one distribution, which inflates the distinct-version count and depresses the modal share, i.e. manufactures exactly the two conditions the rule fires on.
+- **Site flood** (`site_flood_oneshot_cohort_10m`, `site_flood_actor_1d`) is the only site-relative logic in the layer. Every other threshold is absolute, so a fleet of disposable identities β€” one event each, a fresh address per hit, a real browser β€” can push a four-events-an-hour site to four thousand and trip nothing (measured: 14% caught in such hours versus 48% at rest). The gate is the site's 10-minute volume against its own padded weekly median, computed every 15 minutes from ClickHouse by `siteBaseline.ts` and shared through Redis; a site with no baseline or under a week old is never in flood. The gate alone never convicts. Inside a flood, a device cohort convicts when it is at least a quarter of the site's traffic, at least 60% distinct actors, and at least 95% direct β€” a launch is many devices, several events per visitor and referrers set, and a campaign spreads over real devices so no cohort reaches a quarter. An actor convicts inside a flood at 200 events a day from a hosting address, 1,000 otherwise.
+- **Hosting actor volume** (`hosting_actor_events_1d`) convicts a hosting/datacenter address at 1,000 events a day on its own. The supporting-only daily rule below exists because a residential or office address can be hundreds of people; a datacenter address is not. SASE egress (Cato, Zscaler, Netskope) is exempt by ASN, on top of the many-user-agents guard.
+- **Cohort version uniformity** convicts, and is the only rule that can catch a paced distributed crawler β€” one that stays under every per-identity threshold by design. Its key must carry the browser family: without it, Chrome, Safari and Firefox version numbers pool into one distribution, which inflates the distinct-version count and depresses the modal share, i.e. manufactures exactly the two conditions the rule fires on. Its bucket is an hour: the crawler paces itself, and in a one-minute bucket its busiest cohort never came near the volume gate, so the rule did not fire once in two weeks of production.
- **Events per actor per day** is supporting-only and scores 1. Its actor is the exact request IP, deliberately *not* the /24 identity bucket the per-visitor rules use β€” bucketing merges a datacenter range or a SASE egress pool into one actor, and a day is long enough for that to reach any threshold on legitimate traffic. It is additionally dropped outright for an address showing more than three user agents in five minutes, which is what a shared gateway looks like.
- **Enumeration** (`enumerationObserver.ts`) is in shadow mode: it measures path novelty, events per actor, and direct share per cohort, requires two consecutive qualifying buckets, and **never scores**. It logs. Promoting it should follow from reading those logs, not from a threshold nudge. Its direct share is measured on the raw referrer, before self-referrers are cleared for storage β€” a stored empty referrer means "direct *or* internal navigation", which most ordinary browsing satisfies.
diff --git a/server/src/services/tracker/botBlocking/anomalyScorer.test.ts b/server/src/services/tracker/botBlocking/anomalyScorer.test.ts
index 9621badd7..db701e42a 100644
--- a/server/src/services/tracker/botBlocking/anomalyScorer.test.ts
+++ b/server/src/services/tracker/botBlocking/anomalyScorer.test.ts
@@ -224,8 +224,8 @@ describe("cohort version uniformity (in-process fallback)", () => {
const result = await driveCohort(versions);
expect(result.isAnomalous).toBe(true);
- expect(result.reasons.map(reason => reason.rule)).toContain("cohort_version_uniformity_60s");
- expect(result.counters.cohortDistinctVersions60s).toBe(16);
+ expect(result.reasons.map(reason => reason.rule)).toContain("cohort_version_uniformity_1h");
+ expect(result.counters.cohortDistinctVersions1h).toBe(16);
});
it("leaves an equally busy organic cohort alone (one dominant current version)", async () => {
@@ -236,23 +236,23 @@ describe("cohort version uniformity (in-process fallback)", () => {
const result = await driveCohort(versions);
expect(result.isAnomalous).toBe(false);
- expect(result.reasons.map(reason => reason.rule)).not.toContain("cohort_version_uniformity_60s");
+ expect(result.reasons.map(reason => reason.rule)).not.toContain("cohort_version_uniformity_1h");
});
it("ignores a flat cohort that is too small to be a fleet", async () => {
- const versions = Array.from({ length: 100 }, (_, index) => 103 + (index % 16));
+ const versions = Array.from({ length: 60 }, (_, index) => 103 + (index % 16));
const result = await driveCohort(versions);
expect(result.isAnomalous).toBe(false);
- expect(result.counters.cohortEvents60s).toBeLessThan(300);
+ expect(result.counters.cohortEvents1h).toBeLessThan(100);
});
it("skips the cohort counter when the fingerprint is incomplete", async () => {
const result = await observeTrackingAnomaly({ ...baseInput, screenWidth: undefined });
- expect(result.counters.cohortEvents60s).toBe(0);
- expect(result.counters.cohortDistinctVersions60s).toBe(0);
+ expect(result.counters.cohortEvents1h).toBe(0);
+ expect(result.counters.cohortDistinctVersions1h).toBe(0);
});
it("skips the cohort counter when the language is unset", async () => {
@@ -260,7 +260,7 @@ describe("cohort version uniformity (in-process fallback)", () => {
// cohort, which is the same defect as merging every browser.
const result = await observeTrackingAnomaly({ ...cohortInput, language: undefined });
- expect(result.counters.cohortEvents60s).toBe(0);
+ expect(result.counters.cohortEvents1h).toBe(0);
});
/**
@@ -278,7 +278,8 @@ describe("cohort version uniformity (in-process fallback)", () => {
it("does not convict a mixed-browser population sharing one screen and language", async () => {
const families = [
(version: number) => desktopChrome(version),
- (version: number) => `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:${version}.0) Gecko/20100101 Firefox/${version}.0`,
+ (version: number) =>
+ `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:${version}.0) Gecko/20100101 Firefox/${version}.0`,
(version: number) =>
`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/${version}.0 Safari/605.1.15`,
(version: number) =>
@@ -304,10 +305,10 @@ describe("cohort version uniformity (in-process fallback)", () => {
}
}
- expect(result?.reasons.map(reason => reason.rule)).not.toContain("cohort_version_uniformity_60s");
+ expect(result?.reasons.map(reason => reason.rule)).not.toContain("cohort_version_uniformity_1h");
expect(result?.isAnomalous).toBe(false);
// The last family's cohort saw only its own traffic, not all 1,280 events.
- expect(result?.counters.cohortEvents60s).toBe(320);
+ expect(result?.counters.cohortEvents1h).toBe(320);
});
/**
@@ -351,7 +352,7 @@ describe("cohort version uniformity (in-process fallback)", () => {
});
index++;
}
- sizes.push(result!.counters.cohortEvents60s);
+ sizes.push(result!.counters.cohortEvents1h);
}
return sizes;
}
@@ -371,7 +372,7 @@ describe("cohort version uniformity (in-process fallback)", () => {
await cohortSizesPerAgent([chrome120, edge120, opera120]);
const result = await observeTrackingAnomaly({ ...cohortInput, userAgent: edge120 });
- expect(result.counters.cohortDistinctVersions60s).toBe(1);
+ expect(result.counters.cohortDistinctVersions1h).toBe(1);
});
it("separates families whose version numbers move independently", async () => {
@@ -393,7 +394,7 @@ describe("cohort version uniformity (in-process fallback)", () => {
const result = await driveCohort(versions);
- expect(result.reasons.map(reason => reason.rule)).toContain("cohort_version_uniformity_60s");
+ expect(result.reasons.map(reason => reason.rule)).toContain("cohort_version_uniformity_1h");
});
});
@@ -468,6 +469,223 @@ describe("enumeration observer (shadow mode)", () => {
});
});
+describe("site flood (in-process fallback)", () => {
+ beforeEach(() => {
+ resetAnomalyScorerForTests();
+ setRedisAnomalyEnabledForTests(false);
+ });
+
+ // A 10-minute bucket boundary, so a run of events stays inside one bucket.
+ const bucketStart = 1_200_000;
+ const quietSite = { events10m: 0, eligible: true };
+ const desktopChrome = (version: number) =>
+ `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${version}.0.0.0 Safari/537.36`;
+
+ /**
+ * The measured shape of the site-7650 flood: one screen size, one language,
+ * a fresh IP and a user agent drawn from a short list per hit, no referrer,
+ * one event per identity, at a rate hundreds of times the site's normal.
+ */
+ async function driveOneShotFleet(count: number, overrides: Partial<AnomalyInputForTest> = {}) {
+ let result;
+ for (let index = 0; index < count; index++) {
+ result = await observeTrackingAnomaly({
+ ...baseInput,
+ siteBaseline: quietSite,
+ ipAddress: `203.0.${Math.floor(index / 250)}.${index % 250}`,
+ userAgent: desktopChrome(118 + (index % 4)),
+ screenWidth: 1920,
+ screenHeight: 1080,
+ language: "en-US",
+ pathname: `/showcase/${index % 70}`,
+ referrer: undefined,
+ nowMs: bucketStart + index * 1_000,
+ ...overrides,
+ });
+ }
+ return result!;
+ }
+
+ type AnomalyInputForTest = Parameters<typeof observeTrackingAnomaly>[0];
+
+ it("convicts a one-shot fleet flooding a quiet site", async () => {
+ const result = await driveOneShotFleet(120);
+
+ expect(result.isAnomalous).toBe(true);
+ expect(result.convictingReasons.map(reason => reason.rule)).toContain("site_flood_oneshot_cohort_10m");
+ expect(result.counters.siteEvents10m).toBe(120);
+ expect(result.counters.cohortDistinctActors10m).toBe(120);
+ });
+
+ it("stays quiet below the flood gate", async () => {
+ const result = await driveOneShotFleet(90);
+
+ expect(result.isAnomalous).toBe(false);
+ });
+
+ it("never fires for a site with no baseline", async () => {
+ const result = await driveOneShotFleet(200, { siteBaseline: null });
+
+ expect(result.isAnomalous).toBe(false);
+ });
+
+ it("never fires for a site too young to have a baseline", async () => {
+ const result = await driveOneShotFleet(200, { siteBaseline: { events10m: 0, eligible: false } });
+
+ expect(result.isAnomalous).toBe(false);
+ });
+
+ it("scales the gate with the site's own baseline", async () => {
+ // A site that normally does 30 events per 10 minutes needs 600 to be in flood.
+ const result = await driveOneShotFleet(300, { siteBaseline: { events10m: 30, eligible: true } });
+
+ expect(result.isAnomalous).toBe(false);
+ });
+
+ it("leaves a launch-shaped surge alone", async () => {
+ // Many devices, several events per visitor, arrivals carrying a referrer:
+ // the mirror image of the fleet, at the same rate on the same quiet site.
+ const screens = [
+ [390, 844],
+ [393, 852],
+ [430, 932],
+ [1920, 1080],
+ [1536, 864],
+ [1366, 768],
+ [412, 915],
+ [1440, 900],
+ ];
+ let result;
+ for (let index = 0; index < 300; index++) {
+ const visitor = Math.floor(index / 3);
+ const [screenWidth, screenHeight] = screens[visitor % screens.length];
+ result = await observeTrackingAnomaly({
+ ...baseInput,
+ siteBaseline: quietSite,
+ ipAddress: `198.51.${Math.floor(visitor / 250)}.${visitor % 250}`,
+ userAgent: desktopChrome(150),
+ screenWidth,
+ screenHeight,
+ language: "en-US",
+ pathname: `/launch/${index % 5}`,
+ referrer: "https://news.ycombinator.com/",
+ nowMs: bucketStart + index * 1_000,
+ });
+ }
+
+ expect(result!.counters.siteEvents10m).toBe(300);
+ expect(result!.isAnomalous).toBe(false);
+ });
+
+ it("leaves a one-shot campaign alone when devices are diverse", async () => {
+ // Email/push campaign: one hit per recipient, no referrer β€” but real devices,
+ // so no single cohort holds a quarter of the site's traffic.
+ const screens = [
+ [390, 844],
+ [393, 852],
+ [430, 932],
+ [1920, 1080],
+ [1536, 864],
+ [1366, 768],
+ [412, 915],
+ [1440, 900],
+ ];
+ let result;
+ for (let index = 0; index < 300; index++) {
+ const [screenWidth, screenHeight] = screens[index % screens.length];
+ result = await observeTrackingAnomaly({
+ ...baseInput,
+ siteBaseline: quietSite,
+ ipAddress: `198.51.${Math.floor(index / 250)}.${index % 250}`,
+ userAgent: desktopChrome(150),
+ screenWidth,
+ screenHeight,
+ language: "en-US",
+ pathname: "/promo",
+ referrer: undefined,
+ nowMs: bucketStart + index * 1_000,
+ });
+ }
+
+ expect(result!.isAnomalous).toBe(false);
+ });
+
+ it("convicts a hosting address carrying a flood on its own", async () => {
+ // The Azure archetype: one datacenter address, ~300 events a day, paced
+ // under every short-window rule, on a site it has pushed into flood.
+ let result;
+ for (let index = 0; index < 250; index++) {
+ result = await observeTrackingAnomaly({
+ ...baseInput,
+ siteBaseline: quietSite,
+ ipAddress: "20.0.0.5",
+ isHostingAsn: true,
+ asn: 8075,
+ screenWidth: 1920,
+ screenHeight: 1080,
+ language: "en-US",
+ pathname: "/",
+ nowMs: bucketStart + index * 2_000,
+ });
+ }
+
+ expect(result!.isAnomalous).toBe(true);
+ expect(result!.convictingReasons.map(reason => reason.rule)).toContain("site_flood_actor_1d");
+ });
+
+ it("holds a residential address to the ordinary daily threshold inside a flood", async () => {
+ let result;
+ for (let index = 0; index < 250; index++) {
+ result = await observeTrackingAnomaly({
+ ...baseInput,
+ siteBaseline: quietSite,
+ ipAddress: "203.0.113.77",
+ isHostingAsn: false,
+ pathname: "/",
+ nowMs: bucketStart + index * 2_000,
+ });
+ }
+
+ expect(result!.reasons.map(reason => reason.rule)).not.toContain("site_flood_actor_1d");
+ expect(result!.isAnomalous).toBe(false);
+ });
+});
+
+describe("hosting actor volume (Redis-backed)", () => {
+ beforeEach(() => {
+ resetAnomalyScorerForTests();
+ setRedisAnomalyEnabledForTests(true);
+ mocks.anomalyObserve.mockReset();
+ });
+
+ // Spec order for baseInput: te10, te60, tdp, ie60, idua, idh, sue, av, se, sa.
+ it("convicts a hosting address on long-window volume alone", async () => {
+ mocks.anomalyObserve.mockResolvedValue(rollingReadings(1, 2, 1, 10, 1, 1, 5, 1500, 20, 20));
+
+ const result = await observeTrackingAnomaly({ ...baseInput, isHostingAsn: true, asn: 8075 });
+
+ expect(result.isAnomalous).toBe(true);
+ expect(result.convictingReasons.map(reason => reason.rule)).toEqual(["hosting_actor_events_1d"]);
+ });
+
+ it("exempts SASE egress from the hosting rule", async () => {
+ mocks.anomalyObserve.mockResolvedValue(rollingReadings(1, 2, 1, 10, 1, 1, 5, 1500, 20, 20));
+
+ const result = await observeTrackingAnomaly({ ...baseInput, isHostingAsn: true, asn: 13150 });
+
+ expect(result.isAnomalous).toBe(false);
+ expect(result.reasons.map(reason => reason.rule)).not.toContain("hosting_actor_events_1d");
+ });
+
+ it("drops the hosting rule for an address showing many user agents", async () => {
+ mocks.anomalyObserve.mockResolvedValue(rollingReadings(1, 2, 1, 10, 12, 1, 5, 1500, 20, 20));
+
+ const result = await observeTrackingAnomaly({ ...baseInput, isHostingAsn: true, asn: 8075 });
+
+ expect(result.isAnomalous).toBe(false);
+ });
+});
+
describe("observeTrackingAnomaly (Redis-backed)", () => {
beforeEach(() => {
resetAnomalyScorerForTests();
@@ -476,16 +694,17 @@ describe("observeTrackingAnomaly (Redis-backed)", () => {
});
it("sends one spec per enabled counter and maps results back by counter name", async () => {
- // 9 counters (path + host present, no client score). The cohort and
- // enumeration counters stay out: this input has no screen dimensions.
- mocks.anomalyObserve.mockResolvedValue(rollingReadings(31, 5, 2, 9, 3, 1, 4, 7, 12));
+ // 11 counters (path + host present, no client score, plus the two site-flood
+ // counters). The cohort and enumeration counters stay out: this input has
+ // no screen dimensions.
+ mocks.anomalyObserve.mockResolvedValue(rollingReadings(31, 5, 2, 9, 3, 1, 4, 7, 12, 1, 1));
const result = await observeTrackingAnomaly({ ...baseInput, hasClientBotScore: false });
expect(mocks.anomalyObserve).toHaveBeenCalledTimes(1);
const [nowMs, specs] = mocks.anomalyObserve.mock.calls[0];
expect(nowMs).toBe(baseInput.nowMs);
- expect(specs).toHaveLength(9);
+ expect(specs).toHaveLength(11);
expect(specs.map((spec: { key: string }) => spec.key)).toEqual([
expect.stringContaining("bot:a:te10:"),
expect.stringContaining("bot:a:te60:"),
@@ -496,6 +715,8 @@ describe("observeTrackingAnomaly (Redis-backed)", () => {
expect.stringContaining("bot:a:sue:"),
expect.stringContaining("bot:a:mcs:"),
expect.stringContaining("bot:a:av:"),
+ expect.stringContaining("bot:f:se:"),
+ expect.stringContaining("bot:f:sa:"),
]);
expect(result.counters.tupleEvents10s).toBe(31);
@@ -505,7 +726,7 @@ describe("observeTrackingAnomaly (Redis-backed)", () => {
});
// Spec order for baseInput (client score present, so no `mcs`):
- // te10, te60, tdp, ie60, idua, idh, sue, av.
+ // te10, te60, tdp, ie60, idua, idh, sue, av, se, sa.
it("never convicts on long-window volume alone", async () => {
// 1,500 events in a day from one actor β€” five times the measured p99.99 β€”
// and nothing else. A scraper looks like this; so does a single office
@@ -545,7 +766,7 @@ describe("observeTrackingAnomaly (Redis-backed)", () => {
it("omits conditional counters that don't apply and reports them as zero", async () => {
// No pathname, no hostname, client score present β†’ 3 counters dropped.
- mocks.anomalyObserve.mockResolvedValue(rollingReadings(1, 1, 1, 1, 1, 1));
+ mocks.anomalyObserve.mockResolvedValue(rollingReadings(1, 1, 1, 1, 1, 1, 1, 1));
const result = await observeTrackingAnomaly({
...baseInput,
@@ -555,7 +776,7 @@ describe("observeTrackingAnomaly (Redis-backed)", () => {
});
const [, specs] = mocks.anomalyObserve.mock.calls[0];
- expect(specs).toHaveLength(6);
+ expect(specs).toHaveLength(8);
expect(result.counters.tupleDistinctPaths60s).toBe(0);
expect(result.counters.ipDistinctHosts60s).toBe(0);
expect(result.counters.missingClientScore60s).toBe(0);
diff --git a/server/src/services/tracker/botBlocking/anomalyScorer.ts b/server/src/services/tracker/botBlocking/anomalyScorer.ts
index c3fa806d0..6f8915dac 100644
--- a/server/src/services/tracker/botBlocking/anomalyScorer.ts
+++ b/server/src/services/tracker/botBlocking/anomalyScorer.ts
@@ -6,9 +6,11 @@ import {
resetEnumerationObserverForTests,
type EnumerationObservation,
} from "./enumerationObserver.js";
+import { getSiteBaseline, type SiteBaseline } from "./siteBaseline.js";
const SECOND = 1000;
const MINUTE = 60 * SECOND;
+const HOUR = 60 * MINUTE;
const ANOMALY_SCORE_THRESHOLD = 4;
const CLEANUP_INTERVAL_MS = 60 * SECOND;
@@ -25,7 +27,7 @@ const MAX_LOCAL_COUNTER_KEYS = 100_000;
/**
* Cohort-uniformity rule. A cohort is one exact device fingerprint on a site β€”
* (screen, language, browser) β€” and the rule measures how its traffic is spread
- * across browser major versions inside a fixed one-minute bucket.
+ * across browser major versions inside a fixed one-hour bucket.
*
* An organic cohort is steeply peaked: browsers auto-update, so one current
* version holds most of it (measured 0.52-0.98 modal share, and >=0.74 for every
@@ -40,8 +42,15 @@ const MAX_LOCAL_COUNTER_KEYS = 100_000;
* to any single-visitor view because the evidence only exists in aggregate.
* Thresholds are set with a wide margin β€” over 24h of production traffic across
* every site, only the crawler's own cohort satisfied all three.
+ *
+ * The bucket is an hour, not a minute. The crawler paces itself: its busiest
+ * cohort peaked at 19 events in any one minute against a 300-event gate, and
+ * over 14 days of production the minute-bucket version never fired once. Over
+ * an hour the same cohort holds 3,000+ events with a modal share of 0.1, and
+ * with a 100-event gate it was the only cohort platform-wide to qualify in 22
+ * of 24 hours. The window was wrong; the shape test was not.
*/
-const COHORT_MIN_EVENTS_60S = 300;
+const COHORT_MIN_EVENTS_1H = 100;
const COHORT_MIN_DISTINCT_VERSIONS = 8;
const COHORT_MAX_MODAL_SHARE = 0.25;
@@ -71,6 +80,56 @@ const ACTOR_EVENTS_1D_SCORE = 1;
const PROXY_MERGE_MAX_USER_AGENTS = 3;
const DAY = 24 * 60 * MINUTE;
+/**
+ * Site-flood rules. The failure these exist for: a fleet of disposable
+ * identities β€” one event each, a fresh IP and user agent per hit, spread over
+ * thousands of residential ASNs, running a real browser β€” lands on a site that
+ * normally sees four events an hour and pushes it to four thousand. Every rule
+ * above is keyed on an actor and sees one well-behaved event per actor; nothing
+ * fires. Measured across production, detection caught 14% of traffic in such
+ * hours against 48% at rest β€” worst exactly where the site owner is looking.
+ *
+ * What the fleet cannot hide is the site's aggregate: the rate against the
+ * site's own baseline (`siteBaseline.ts`), and, within the flood, a single
+ * device cohort that is nearly all distinct actors, nearly all direct, and a
+ * large share of the site's traffic. A launch or a press bump is the mirror
+ * image β€” many devices, several events per visitor, referrers set β€” so the
+ * flood gate alone never convicts; a cohort or an actor still has to show a
+ * shape no organic audience produces.
+ *
+ * The gate is the site's 10-minute volume at twenty times its padded weekly
+ * median and at least 100 events. Sites without a baseline, or younger than a
+ * week, never pass it.
+ */
+const FLOOD_MULTIPLE = 20;
+const FLOOD_MIN_EVENTS_10M = 100;
+const FLOOD_COHORT_MIN_EVENTS_10M = 50;
+const FLOOD_COHORT_MIN_ACTOR_RATIO = 0.6;
+const FLOOD_COHORT_MIN_DIRECT_SHARE = 0.95;
+const FLOOD_COHORT_MIN_SITE_SHARE = 0.25;
+const FLOOD_ACTOR_EVENTS_1D_HOSTING = 200;
+const FLOOD_ACTOR_EVENTS_1D_THRESHOLD = ACTOR_EVENTS_1D_THRESHOLD;
+const TEN_MINUTES = 10 * MINUTE;
+
+/**
+ * Long-window volume from a hosting address convicts on its own. The
+ * supporting-only `actor_events_1d` above exists because one residential or
+ * office address can be hundreds of people; a datacenter address is not. The
+ * Azure-hosted fleet this targets ran ~300 events per address per day across
+ * 3,000 sites and was caught under half the time, because the hosting ASN is
+ * only ever corroborating and nothing corroborated it.
+ *
+ * SASE egress is the exception a hosting flag cannot see β€” Cato, Zscaler and
+ * Netskope route whole companies through cloud addresses β€” so those are
+ * exempt outright, in addition to the many-user-agents guard.
+ */
+const HOSTING_ACTOR_EVENTS_1D_THRESHOLD = 1000;
+const SASE_EGRESS_ASNS: ReadonlySet<number> = new Set([
+ 13150, // Cato Networks
+ 22616, // Zscaler
+ 55256, // Netskope
+]);
+
const logger = createServiceLogger("anomaly-scorer");
// Counters live in Redis so every worker/replica shares one view; an in-process
@@ -98,10 +157,15 @@ export interface AnomalyCounters {
ipDistinctHosts60s: number;
siteUserAgentEvents60s: number;
missingClientScore60s: number;
- cohortEvents60s: number;
- cohortTopVersionEvents60s: number;
- cohortDistinctVersions60s: number;
+ cohortEvents1h: number;
+ cohortTopVersionEvents1h: number;
+ cohortDistinctVersions1h: number;
actorEvents1d: number;
+ siteEvents10m: number;
+ siteDistinctActors10m: number;
+ cohortEvents10m: number;
+ cohortDistinctActors10m: number;
+ cohortDirectEvents10m: number;
enumerationEvents15m: number;
enumerationDistinctPaths15m: number;
enumerationDistinctActors15m: number;
@@ -142,6 +206,11 @@ export interface AnomalyInput {
screenWidth?: number;
screenHeight?: number;
language?: string;
+ /** Whether the request address resolved to hosting/datacenter or bot-provider space. */
+ isHostingAsn?: boolean;
+ asn?: number;
+ /** Overrides the site baseline lookup; tests only. */
+ siteBaseline?: SiteBaseline | null;
nowMs?: number;
}
@@ -366,9 +435,14 @@ const tupleDistinctPaths60s = new RollingDistinctCounter();
const ipDistinctUserAgents5m = new RollingDistinctCounter();
const ipDistinctHosts60s = new RollingDistinctCounter();
-const cohortBrowserVersions60s = new BucketedDistributionCounter();
+const cohortBrowserVersions1h = new BucketedDistributionCounter();
const actorEvents1d = new BucketedCounter();
+const siteEvents10m = new BucketedCounter();
+const cohortEvents10m = new BucketedCounter();
+const cohortDirectEvents10m = new BucketedCounter();
+const siteDistinctActors10m = new BucketedDistinctCounter();
+const cohortDistinctActors10m = new BucketedDistinctCounter();
const enumerationEvents15m = new BucketedCounter();
const enumerationDirectEvents15m = new BucketedCounter();
const enumerationDistinctPaths15m = new BucketedDistinctCounter();
@@ -492,8 +566,13 @@ function maybeCleanup(nowMs: number) {
tupleDistinctPaths60s.cleanup(nowMs, MINUTE);
ipDistinctUserAgents5m.cleanup(nowMs, 5 * MINUTE);
ipDistinctHosts60s.cleanup(nowMs, MINUTE);
- cohortBrowserVersions60s.cleanup(nowMs, MINUTE);
+ cohortBrowserVersions1h.cleanup(nowMs, HOUR);
actorEvents1d.cleanup(nowMs, DAY);
+ siteEvents10m.cleanup(nowMs, TEN_MINUTES);
+ cohortEvents10m.cleanup(nowMs, TEN_MINUTES);
+ cohortDirectEvents10m.cleanup(nowMs, TEN_MINUTES);
+ siteDistinctActors10m.cleanup(nowMs, TEN_MINUTES);
+ cohortDistinctActors10m.cleanup(nowMs, TEN_MINUTES);
enumerationEvents15m.cleanup(nowMs, ENUMERATION_BUCKET_MS);
enumerationDirectEvents15m.cleanup(nowMs, ENUMERATION_BUCKET_MS);
enumerationDistinctPaths15m.cleanup(nowMs, ENUMERATION_BUCKET_MS);
@@ -578,7 +657,9 @@ function buildCounterPlan(input: AnomalyInput, nowMs: number): AnomalyPlan {
input.screenWidth > 0 &&
input.screenHeight > 0;
const cohortKey = `${siteId}:${input.screenWidth}x${input.screenHeight}:${language}:${browserFamily}`;
- const cohortBucket = Math.floor(nowMs / MINUTE);
+ const cohortBucket = Math.floor(nowMs / HOUR);
+ const floodBucket = Math.floor(nowMs / TEN_MINUTES);
+ const siteKey = String(siteId);
// Enumeration shape is measured over the same cohort, on a much longer bucket:
// a crawler paced at one hit per identity produces nothing legible in a
@@ -671,18 +752,18 @@ function buildCounterPlan(input: AnomalyInput, nowMs: number): AnomalyPlan {
observeLocal: now => missingClientScore60s.observe(tupleKey, now, MINUTE),
},
{
- name: "cohortEvents60s",
+ name: "cohortEvents1h",
enabled: hasCohort,
kind: "distribution",
redisKey: `bot:a:cbv:${cohortKey}:${cohortBucket}`,
member: browserMajorVersion,
- windowMs: 2 * MINUTE,
+ windowMs: 2 * HOUR,
maxSize: MAX_DISTRIBUTION_FIELDS,
observeLocal: () => 0,
- topCounterName: "cohortTopVersionEvents60s",
- distinctCounterName: "cohortDistinctVersions60s",
+ topCounterName: "cohortTopVersionEvents1h",
+ distinctCounterName: "cohortDistinctVersions1h",
observeLocalDistribution: now =>
- cohortBrowserVersions60s.observe(cohortKey, browserMajorVersion, now, MINUTE, MAX_DISTRIBUTION_FIELDS),
+ cohortBrowserVersions1h.observe(cohortKey, browserMajorVersion, now, HOUR, MAX_DISTRIBUTION_FIELDS),
},
{
name: "actorEvents1d",
@@ -694,6 +775,60 @@ function buildCounterPlan(input: AnomalyInput, nowMs: number): AnomalyPlan {
maxSize: 0,
observeLocal: now => actorEvents1d.observe(ipKey, now, DAY, MAX_LOCAL_COUNTER_KEYS),
},
+ // Site-flood counters, all on one fixed 10-minute bucket. The actor here is
+ // the visitor tuple (ip + user agent), the identity a fleet mints per hit.
+ {
+ name: "siteEvents10m",
+ enabled: true,
+ kind: "counter",
+ redisKey: `bot:f:se:${siteKey}:${floodBucket}`,
+ member: "",
+ windowMs: 2 * TEN_MINUTES,
+ maxSize: 0,
+ observeLocal: now => siteEvents10m.observe(siteKey, now, TEN_MINUTES, MAX_LOCAL_COUNTER_KEYS),
+ },
+ {
+ name: "siteDistinctActors10m",
+ enabled: true,
+ kind: "cardinality",
+ redisKey: `bot:f:sa:${siteKey}:${floodBucket}`,
+ member: tupleKey,
+ windowMs: 2 * TEN_MINUTES,
+ maxSize: 0,
+ observeLocal: now =>
+ siteDistinctActors10m.observe(siteKey, tupleKey, now, TEN_MINUTES, MAX_LOCAL_DISTINCT_VALUES),
+ },
+ {
+ name: "cohortEvents10m",
+ enabled: hasCohort,
+ kind: "counter",
+ redisKey: `bot:f:ce:${cohortKey}:${floodBucket}`,
+ member: "",
+ windowMs: 2 * TEN_MINUTES,
+ maxSize: 0,
+ observeLocal: now => cohortEvents10m.observe(cohortKey, now, TEN_MINUTES, MAX_LOCAL_COUNTER_KEYS),
+ },
+ {
+ name: "cohortDistinctActors10m",
+ enabled: hasCohort,
+ kind: "cardinality",
+ redisKey: `bot:f:ca:${cohortKey}:${floodBucket}`,
+ member: tupleKey,
+ windowMs: 2 * TEN_MINUTES,
+ maxSize: 0,
+ observeLocal: now =>
+ cohortDistinctActors10m.observe(cohortKey, tupleKey, now, TEN_MINUTES, MAX_LOCAL_DISTINCT_VALUES),
+ },
+ {
+ name: "cohortDirectEvents10m",
+ enabled: hasCohort && isDirect,
+ kind: "counter",
+ redisKey: `bot:f:cd:${cohortKey}:${floodBucket}`,
+ member: "",
+ windowMs: 2 * TEN_MINUTES,
+ maxSize: 0,
+ observeLocal: now => cohortDirectEvents10m.observe(cohortKey, now, TEN_MINUTES, MAX_LOCAL_COUNTER_KEYS),
+ },
{
name: "enumerationEvents15m",
enabled: hasEnumerationCohort,
@@ -727,7 +862,13 @@ function buildCounterPlan(input: AnomalyInput, nowMs: number): AnomalyPlan {
windowMs: ENUMERATION_BUCKET_MS,
maxSize: 0,
observeLocal: now =>
- enumerationDistinctActors15m.observe(cohortKey, ipAddress, now, ENUMERATION_BUCKET_MS, MAX_LOCAL_DISTINCT_VALUES),
+ enumerationDistinctActors15m.observe(
+ cohortKey,
+ ipAddress,
+ now,
+ ENUMERATION_BUCKET_MS,
+ MAX_LOCAL_DISTINCT_VALUES
+ ),
},
{
name: "enumerationDirectEvents15m",
@@ -737,7 +878,8 @@ function buildCounterPlan(input: AnomalyInput, nowMs: number): AnomalyPlan {
member: "",
windowMs: ENUMERATION_BUCKET_MS,
maxSize: 0,
- observeLocal: now => enumerationDirectEvents15m.observe(cohortKey, now, ENUMERATION_BUCKET_MS, MAX_LOCAL_COUNTER_KEYS),
+ observeLocal: now =>
+ enumerationDirectEvents15m.observe(cohortKey, now, ENUMERATION_BUCKET_MS, MAX_LOCAL_COUNTER_KEYS),
},
];
@@ -758,10 +900,15 @@ function emptyCounters(): AnomalyCounters {
ipDistinctHosts60s: 0,
siteUserAgentEvents60s: 0,
missingClientScore60s: 0,
- cohortEvents60s: 0,
- cohortTopVersionEvents60s: 0,
- cohortDistinctVersions60s: 0,
+ cohortEvents1h: 0,
+ cohortTopVersionEvents1h: 0,
+ cohortDistinctVersions1h: 0,
actorEvents1d: 0,
+ siteEvents10m: 0,
+ siteDistinctActors10m: 0,
+ cohortEvents10m: 0,
+ cohortDistinctActors10m: 0,
+ cohortDirectEvents10m: 0,
enumerationEvents15m: 0,
enumerationDistinctPaths15m: 0,
enumerationDistinctActors15m: 0,
@@ -818,7 +965,25 @@ function observeViaLocal(plan: CounterPlan[], nowMs: number): AnomalyCounters {
return counters;
}
-function computeAnomalyResult(counters: AnomalyCounters): AnomalyResult {
+interface AnomalyContext {
+ siteBaseline: SiteBaseline | undefined;
+ isHostingAsn: boolean;
+ asn: number | undefined;
+}
+
+/**
+ * Whether the site is currently in flood: its 10-minute volume is at least
+ * FLOOD_MULTIPLE times its own padded weekly median and at least
+ * FLOOD_MIN_EVENTS_10M. A site with no baseline, or one too young for its
+ * baseline to mean anything, is never in flood.
+ */
+function isSiteInFlood(counters: AnomalyCounters, baseline: SiteBaseline | undefined): boolean {
+ if (!baseline || !baseline.eligible) return false;
+ const gate = Math.max(FLOOD_MULTIPLE * baseline.events10m, FLOOD_MIN_EVENTS_10M);
+ return counters.siteEvents10m >= gate;
+}
+
+function computeAnomalyResult(counters: AnomalyCounters, context: AnomalyContext): AnomalyResult {
// Convicting rules are keyed on (site, ip, ua) β€” they describe a single actor
// and may convict on their own. The interaction-burst threshold is set beyond
// human clicking speed (10/s sustained); ordinary widget bursts stay below it.
@@ -836,19 +1001,66 @@ function computeAnomalyResult(counters: AnomalyCounters): AnomalyResult {
// one. It convicts because the alternative is not convicting at all: a paced
// distributed crawler leaves no per-identity evidence to corroborate.
if (
- counters.cohortEvents60s >= COHORT_MIN_EVENTS_60S &&
- counters.cohortDistinctVersions60s >= COHORT_MIN_DISTINCT_VERSIONS &&
- counters.cohortTopVersionEvents60s < counters.cohortEvents60s * COHORT_MAX_MODAL_SHARE
+ counters.cohortEvents1h >= COHORT_MIN_EVENTS_1H &&
+ counters.cohortDistinctVersions1h >= COHORT_MIN_DISTINCT_VERSIONS &&
+ counters.cohortTopVersionEvents1h < counters.cohortEvents1h * COHORT_MAX_MODAL_SHARE
) {
convictingReasons.push({
- rule: "cohort_version_uniformity_60s",
+ rule: "cohort_version_uniformity_1h",
score: 4,
- value: Math.round((counters.cohortTopVersionEvents60s / counters.cohortEvents60s) * 100),
+ value: Math.round((counters.cohortTopVersionEvents1h / counters.cohortEvents1h) * 100),
threshold: COHORT_MAX_MODAL_SHARE * 100,
- windowSeconds: 60,
+ windowSeconds: 3600,
});
}
+ const sharedEgressGuard = counters.ipDistinctUserAgents5m <= PROXY_MERGE_MAX_USER_AGENTS;
+ const saseExempt = context.asn !== undefined && SASE_EGRESS_ASNS.has(context.asn);
+
+ // Site flood. See the constants' comment for the design; in short, the gate
+ // is the site against its own baseline, and the conviction is a cohort or an
+ // actor inside the flood showing a shape an organic surge does not.
+ if (isSiteInFlood(counters, context.siteBaseline)) {
+ const cohortEvents = counters.cohortEvents10m;
+ if (
+ cohortEvents >= FLOOD_COHORT_MIN_EVENTS_10M &&
+ counters.cohortDistinctActors10m >= cohortEvents * FLOOD_COHORT_MIN_ACTOR_RATIO &&
+ counters.cohortDirectEvents10m >= cohortEvents * FLOOD_COHORT_MIN_DIRECT_SHARE &&
+ cohortEvents >= counters.siteEvents10m * FLOOD_COHORT_MIN_SITE_SHARE
+ ) {
+ convictingReasons.push({
+ rule: "site_flood_oneshot_cohort_10m",
+ score: 4,
+ value: Math.round((counters.cohortDistinctActors10m / cohortEvents) * 100),
+ threshold: FLOOD_COHORT_MIN_ACTOR_RATIO * 100,
+ windowSeconds: 600,
+ });
+ }
+
+ if (sharedEgressGuard && !saseExempt) {
+ addReason(
+ convictingReasons,
+ "site_flood_actor_1d",
+ 4,
+ counters.actorEvents1d,
+ context.isHostingAsn ? FLOOD_ACTOR_EVENTS_1D_HOSTING : FLOOD_ACTOR_EVENTS_1D_THRESHOLD,
+ 86400
+ );
+ }
+ }
+
+ // Hosting-address volume convicts outside a flood too; see the constant.
+ if (context.isHostingAsn && sharedEgressGuard && !saseExempt) {
+ addReason(
+ convictingReasons,
+ "hosting_actor_events_1d",
+ 4,
+ counters.actorEvents1d,
+ HOSTING_ACTOR_EVENTS_1D_THRESHOLD,
+ 86400
+ );
+ }
+
// Supporting rules are keyed on shared dimensions (ip, site+ua) that many real
// visitors legitimately share β€” one busy CGNAT IP or a popular browser on a
// busy site exceeds them with zero per-visitor evidence. Like generic hosting
@@ -864,7 +1076,7 @@ function computeAnomalyResult(counters: AnomalyCounters): AnomalyResult {
// where one address is hundreds of real people and a four-figure daily count
// is unremarkable. A single scraper presents one user agent, so the guard
// costs nothing against the traffic this rule exists for.
- if (counters.ipDistinctUserAgents5m <= PROXY_MERGE_MAX_USER_AGENTS) {
+ if (sharedEgressGuard) {
addReason(
supportingReasons,
"actor_events_1d",
@@ -914,7 +1126,11 @@ export async function observeTrackingAnomaly(input: AnomalyInput): Promise<Anoma
counters = observeViaLocal(plan.counters, nowMs);
}
- const result = computeAnomalyResult(counters);
+ const result = computeAnomalyResult(counters, {
+ siteBaseline: input.siteBaseline === undefined ? getSiteBaseline(input.siteId) : (input.siteBaseline ?? undefined),
+ isHostingAsn: input.isHostingAsn === true,
+ asn: input.asn,
+ });
// Only a direct hit can read a meaningful direct share, since only a direct
// hit incremented that counter. The observation is attached to the result and
@@ -953,8 +1169,13 @@ export function resetAnomalyScorerForTests() {
tupleDistinctPaths60s.clear();
ipDistinctUserAgents5m.clear();
ipDistinctHosts60s.clear();
- cohortBrowserVersions60s.clear();
+ cohortBrowserVersions1h.clear();
actorEvents1d.clear();
+ siteEvents10m.clear();
+ cohortEvents10m.clear();
+ cohortDirectEvents10m.clear();
+ siteDistinctActors10m.clear();
+ cohortDistinctActors10m.clear();
enumerationEvents15m.clear();
enumerationDirectEvents15m.clear();
enumerationDistinctPaths15m.clear();
diff --git a/server/src/services/tracker/botBlocking/botEventQueue.ts b/server/src/services/tracker/botBlocking/botEventQueue.ts
index 62a937a14..453b385f6 100644
--- a/server/src/services/tracker/botBlocking/botEventQueue.ts
+++ b/server/src/services/tracker/botBlocking/botEventQueue.ts
@@ -37,55 +37,55 @@ class BotEventQueue {
this.processing = true;
const batch = this.queue.splice(0, this.batchSize);
- const ips = [...new Set(batch.map(event => event.ipAddress))];
- const geoData = await getLocation(ips);
+ try {
+ const ips = [...new Set(batch.map(event => event.ipAddress))];
+ const geoData = await getLocation(ips);
- const processedBotEvents = batch.map(event => {
- const dataForIp = geoData?.[event.ipAddress];
+ const processedBotEvents = batch.map(event => {
+ const dataForIp = geoData?.[event.ipAddress];
- const countryCode = dataForIp?.countryIso || "";
- const regionCode = dataForIp?.region || "";
- const referrer = clearSelfReferrer(event.referrer || "", event.hostname || "");
+ const countryCode = dataForIp?.countryIso || "";
+ const regionCode = dataForIp?.region || "";
+ const referrer = clearSelfReferrer(event.referrer || "", event.hostname || "");
- return {
- site_id: event.site_id,
- timestamp: DateTime.fromISO(event.timestamp).toFormat("yyyy-MM-dd HH:mm:ss"),
- session_id: event.sessionId,
- user_id: event.userId,
- hostname: event.hostname || "",
- pathname: event.pathname || "",
- querystring: event.querystring || "",
- referrer,
- browser: event.ua.browser.name || "",
- browser_version: event.ua.browser.major || "",
- operating_system: event.ua.os.name || "",
- operating_system_version: event.ua.os.version || "",
- country: countryCode,
- region: countryCode && regionCode ? countryCode + "-" + regionCode : "",
- city: dataForIp?.city || "",
- lat: dataForIp?.latitude || 0,
- lon: dataForIp?.longitude || 0,
- screen_width: event.screenWidth || 0,
- screen_height: event.screenHeight || 0,
- device_type: getDeviceType(event.screenWidth, event.screenHeight, event.ua),
- type: event.type || "pageview",
- asn: event.botAsn ?? null,
- asn_org: event.botAsnOrg || "",
- detected_ua_pattern: event.detectedUaPattern || false,
- detected_header_heuristics: event.detectedHeaderHeuristics || false,
- detected_client_signals: event.detectedClientSignals || false,
- detected_bot_asn: event.detectedBotAsn || false,
- detected_rate_anomaly: event.detectedRateAnomaly || false,
- matched_ua_pattern: event.matchedUaPattern || "",
- bot_category: event.botCategory || "",
- client_bot_score: event.clientBotScore ?? null,
- client_signal_mask: event.clientSignalMask ?? 0,
- anomaly_reasons: event.anomalyReasons || "",
- anomaly_score: event.anomalyScore ?? 0,
- };
- });
+ return {
+ site_id: event.site_id,
+ timestamp: DateTime.fromISO(event.timestamp).toFormat("yyyy-MM-dd HH:mm:ss"),
+ session_id: event.sessionId,
+ user_id: event.userId,
+ hostname: event.hostname || "",
+ pathname: event.pathname || "",
+ querystring: event.querystring || "",
+ referrer,
+ browser: event.ua.browser.name || "",
+ browser_version: event.ua.browser.major || "",
+ operating_system: event.ua.os.name || "",
+ operating_system_version: event.ua.os.version || "",
+ country: countryCode,
+ region: countryCode && regionCode ? countryCode + "-" + regionCode : "",
+ city: dataForIp?.city || "",
+ lat: dataForIp?.latitude || 0,
+ lon: dataForIp?.longitude || 0,
+ screen_width: event.screenWidth || 0,
+ screen_height: event.screenHeight || 0,
+ device_type: getDeviceType(event.screenWidth, event.screenHeight, event.ua),
+ type: event.type || "pageview",
+ asn: event.botAsn ?? null,
+ asn_org: event.botAsnOrg || "",
+ detected_ua_pattern: event.detectedUaPattern || false,
+ detected_header_heuristics: event.detectedHeaderHeuristics || false,
+ detected_client_signals: event.detectedClientSignals || false,
+ detected_bot_asn: event.detectedBotAsn || false,
+ detected_rate_anomaly: event.detectedRateAnomaly || false,
+ matched_ua_pattern: event.matchedUaPattern || "",
+ bot_category: event.botCategory || "",
+ client_bot_score: event.clientBotScore ?? null,
+ client_signal_mask: event.clientSignalMask ?? 0,
+ anomaly_reasons: event.anomalyReasons || "",
+ anomaly_score: event.anomalyScore ?? 0,
+ };
+ });
- try {
await clickhouse.insert({
table: this.table,
values: processedBotEvents,
diff --git a/server/src/services/tracker/botBlocking/index.ts b/server/src/services/tracker/botBlocking/index.ts
index f16004a85..aa5a12c4f 100644
--- a/server/src/services/tracker/botBlocking/index.ts
+++ b/server/src/services/tracker/botBlocking/index.ts
@@ -314,9 +314,11 @@ export async function checkBotBlocking({
const ipForAsn = payload.ipAddress;
let asnInfo: AsnInfo | null = null;
let supportingHostingAsnDetection: BotBlockingDetection | null = null;
+ let isHostingAsn = false;
if (ipForAsn) {
asnInfo = asnLookup(ipForAsn);
const botAsnMatch = classifyBotAsn(asnInfo?.asn);
+ isHostingAsn = asnInfo !== null && botAsnMatch.isBotInfrastructure;
if (asnInfo && botAsnMatch.isBotInfrastructure) {
const asnDetection: BotBlockingDetection = {
layer: "bot_asn",
@@ -349,6 +351,8 @@ export async function checkBotBlocking({
screenWidth: payload.screenWidth,
screenHeight: payload.screenHeight,
language: payload.language,
+ isHostingAsn,
+ asn: asnInfo?.asn,
});
if (anomaly.isAnomalous) {
addDetection("Bot detected using rate anomaly", {
diff --git a/server/src/services/tracker/botBlocking/siteBaseline.ts b/server/src/services/tracker/botBlocking/siteBaseline.ts
new file mode 100644
index 000000000..4a7386950
--- /dev/null
+++ b/server/src/services/tracker/botBlocking/siteBaseline.ts
@@ -0,0 +1,195 @@
+import { processResults } from "../../../api/analytics/utils/utils.js";
+import { clickhouse } from "../../../db/clickhouse/clickhouse.js";
+import { redis } from "../../../db/redis/redis.js";
+import { createServiceLogger } from "../../../lib/logger/logger.js";
+
+/**
+ * Per-site traffic baseline for the site-flood rules in `anomalyScorer.ts`.
+ *
+ * Every rate rule elsewhere in the layer uses an absolute threshold, so a site
+ * that normally sees four events an hour can be hit at a thousand times that
+ * rate by traffic that is individually well-behaved and trip nothing. The flood
+ * rules compare the site's current 10-minute volume against *its own* normal,
+ * and this module is where that normal comes from.
+ *
+ * The baseline is the median of the site's 10-minute event counts over the
+ * trailing seven days, with the empty buckets counted as zero (1,008 buckets in
+ * a week). The padding matters: a quiet site's non-zero buckets are exactly the
+ * busy ones, and a median over only those overstates its normal. The padded
+ * median is also immune to the site's own previous floods β€” a 20-hour flood is
+ * 120 buckets out of 1,008 and cannot move the median.
+ *
+ * A site whose first event in the window is less than six and a half days old
+ * is marked not-yet-eligible: an onboarding site's baseline is zero because it
+ * did not exist, and its first real day of traffic would otherwise read as a
+ * flood. A site with no row at all β€” new, or a cold self-host install, or the
+ * refresh failed β€” has no baseline, and the flood rules stay off for it. Every
+ * failure mode here fails towards not accusing.
+ *
+ * Refresh is one ClickHouse aggregate every 15 minutes, written to a Redis hash
+ * so every worker shares it, and mirrored into a process-local map that the hot
+ * path reads. The hot path never touches Redis or ClickHouse for this.
+ */
+
+const logger = createServiceLogger("site-baseline");
+
+const TEN_MINUTES_PER_WEEK = 7 * 24 * 6;
+const MIN_SITE_AGE_HOURS = 6 * 24 + 12;
+export const SITE_BASELINE_REFRESH_MS = 15 * 60 * 1000;
+export const SITE_BASELINE_MIRROR_MS = 5 * 60 * 1000;
+const REDIS_HASH_KEY = "bot:sb";
+const REDIS_HASH_TTL_MS = 2 * 60 * 60 * 1000;
+// Refresh election: only the worker that wins this lock runs the ClickHouse
+// query; the rest read the hash it writes.
+const REFRESH_LOCK_KEY = "bot:sb:lock";
+const REFRESH_LOCK_TTL_MS = SITE_BASELINE_REFRESH_MS - 30_000;
+
+export interface SiteBaseline {
+ /** Median events per 10-minute bucket over the trailing week, zero-padded. */
+ events10m: number;
+ /** Whether the site has enough history for a baseline to mean anything. */
+ eligible: boolean;
+}
+
+const baselines = new Map<number, SiteBaseline>();
+let refreshTimer: NodeJS.Timeout | null = null;
+let mirrorTimer: NodeJS.Timeout | null = null;
+
+export function getSiteBaseline(siteId: number): SiteBaseline | undefined {
+ return baselines.get(siteId);
+}
+
+function encode(baseline: SiteBaseline): string {
+ return `${baseline.events10m}:${baseline.eligible ? 1 : 0}`;
+}
+
+function decode(value: string): SiteBaseline | null {
+ const [events, eligible] = value.split(":");
+ const events10m = Number(events);
+ if (!Number.isFinite(events10m)) return null;
+ return { events10m, eligible: eligible === "1" };
+}
+
+export async function computeSiteBaselines(): Promise<Map<number, SiteBaseline>> {
+ const result = await clickhouse.query({
+ query: `
+ SELECT
+ site_id,
+ arraySort(arrayResize(groupArray(c), {buckets:UInt32}, 0))[{median:UInt32}] AS baseline_10m,
+ min(b) <= now() - INTERVAL {minAgeHours:UInt32} HOUR AS eligible
+ FROM (
+ SELECT site_id, toStartOfInterval(timestamp, INTERVAL 10 MINUTE) AS b, count() AS c
+ FROM events
+ WHERE timestamp >= now() - INTERVAL 7 DAY
+ GROUP BY site_id, b
+ )
+ GROUP BY site_id
+ `,
+ format: "JSONEachRow",
+ query_params: {
+ buckets: TEN_MINUTES_PER_WEEK,
+ median: Math.floor(TEN_MINUTES_PER_WEEK / 2),
+ minAgeHours: MIN_SITE_AGE_HOURS,
+ },
+ });
+ const rows = await processResults<{ site_id: number; baseline_10m: number | string; eligible: number | boolean }>(
+ result
+ );
+
+ const next = new Map<number, SiteBaseline>();
+ for (const row of rows) {
+ next.set(Number(row.site_id), {
+ events10m: Number(row.baseline_10m),
+ eligible: row.eligible === true || Number(row.eligible) === 1,
+ });
+ }
+ return next;
+}
+
+async function refreshFromClickhouse(): Promise<void> {
+ try {
+ const won = await redis.set(REFRESH_LOCK_KEY, String(process.pid), "PX", REFRESH_LOCK_TTL_MS, "NX");
+ if (won !== "OK") return;
+ } catch (error) {
+ // Without Redis there is no sharing anyway; refresh locally.
+ logger.warn({ err: error }, "Site baseline refresh lock unavailable; refreshing locally");
+ }
+
+ try {
+ const next = await computeSiteBaselines();
+ replaceBaselines(next);
+
+ if (next.size > 0) {
+ const flat: string[] = [];
+ for (const [siteId, baseline] of next) {
+ flat.push(String(siteId), encode(baseline));
+ }
+ await redis
+ .multi()
+ .del(REDIS_HASH_KEY)
+ .hset(REDIS_HASH_KEY, ...flat)
+ .pexpire(REDIS_HASH_KEY, REDIS_HASH_TTL_MS)
+ .exec();
+ }
+ logger.info({ sites: next.size }, "Site baselines refreshed");
+ } catch (error) {
+ logger.error({ err: error }, "Site baseline refresh failed");
+ }
+}
+
+async function mirrorFromRedis(): Promise<void> {
+ try {
+ const entries = await redis.hgetall(REDIS_HASH_KEY);
+ const keys = Object.keys(entries);
+ if (keys.length === 0) return;
+ const next = new Map<number, SiteBaseline>();
+ for (const key of keys) {
+ const decoded = decode(entries[key]);
+ if (decoded) next.set(Number(key), decoded);
+ }
+ replaceBaselines(next);
+ } catch (error) {
+ logger.warn({ err: error }, "Site baseline mirror from Redis failed");
+ }
+}
+
+function replaceBaselines(next: Map<number, SiteBaseline>) {
+ baselines.clear();
+ for (const [siteId, baseline] of next) {
+ baselines.set(siteId, baseline);
+ }
+}
+
+/**
+ * Start the refresh loop. Safe to call from every worker: the ClickHouse query
+ * is elected through Redis, and the others pick up its result on the mirror
+ * interval. The first mirror runs immediately so a restarted worker is not blind
+ * until the next refresh.
+ */
+export function startSiteBaselineRefresh(): void {
+ if (refreshTimer) return;
+ void mirrorFromRedis().then(() => refreshFromClickhouse());
+ refreshTimer = setInterval(() => void refreshFromClickhouse(), SITE_BASELINE_REFRESH_MS);
+ mirrorTimer = setInterval(() => void mirrorFromRedis(), SITE_BASELINE_MIRROR_MS);
+ refreshTimer.unref();
+ mirrorTimer.unref();
+}
+
+export function stopSiteBaselineRefresh(): void {
+ if (refreshTimer) clearInterval(refreshTimer);
+ if (mirrorTimer) clearInterval(mirrorTimer);
+ refreshTimer = null;
+ mirrorTimer = null;
+}
+
+export function setSiteBaselineForTests(siteId: number, baseline: SiteBaseline | null): void {
+ if (baseline) {
+ baselines.set(siteId, baseline);
+ } else {
+ baselines.delete(siteId);
+ }
+}
+
+export function resetSiteBaselinesForTests(): void {
+ baselines.clear();
+}
diff --git a/server/src/services/tracker/pageviewQueue.ts b/server/src/services/tracker/pageviewQueue.ts
index 259f4128a..af3466ee0 100644
--- a/server/src/services/tracker/pageviewQueue.ts
+++ b/server/src/services/tracker/pageviewQueue.ts
@@ -45,90 +45,91 @@ class PageviewQueue {
// Get batch of pageviews
const batch = this.queue.splice(0, this.batchSize);
- const ips = [...new Set(batch.map(pv => pv.ipAddress))];
-
- const geoData = await getLocation(ips);
-
- // Process each pageview with its geo data
- const processedPageviews = batch.filter(pv => {
- if (pv.site_id == 9133 && pv.screenWidth == 800 && pv.screenHeight == 600) {
- return false;
- }
- return true;
- }).map(pv => {
- const dataForIp = geoData?.[pv.ipAddress];
-
- const countryCode = dataForIp?.countryIso || "";
- const regionCode = dataForIp?.region || "";
- const latitude = dataForIp?.latitude || 0;
- const longitude = dataForIp?.longitude || 0;
- const city = dataForIp?.city || "";
- const timezone = dataForIp?.timeZone || "";
-
- // Same MaxMind lookup already used to decide identity bucketing
- // (bucketIpForIdentity) and bot-detection eligibility; stored here purely
- // for debugging identity-splitting reports, treated like geo data
- // (independent of storeIp) since it identifies a network, not a device.
- const asnInfo = lookupAsn(pv.ipAddress);
-
- // Check if referrer is from the same domain and clear it if so
- let referrer = clearSelfReferrer(pv.referrer || "", pv.hostname || "");
-
- // Get all URL parameters for the url_parameters map
- const allUrlParams = getAllUrlParams(pv.querystring || "");
-
-
- return {
- site_id: pv.site_id,
- timestamp: DateTime.fromISO(pv.timestamp).toFormat("yyyy-MM-dd HH:mm:ss"),
- session_id: pv.sessionId,
- user_id: pv.userId, // Always the device fingerprint
- identified_user_id: pv.identifiedUserId || "", // Custom user ID when identified
- hostname: pv.hostname || "",
- pathname: pv.pathname || "",
- querystring: pv.querystring || "",
- page_title: pv.page_title || "",
- referrer: referrer,
- channel: getChannel(referrer, pv.querystring, pv.hostname),
- browser: pv.ua.browser.name || "",
- browser_version: pv.ua.browser.major || "",
- operating_system: pv.ua.os.name || "",
- operating_system_version: pv.ua.os.version || "",
- language: pv.language || "",
- screen_width: pv.screenWidth || 0,
- screen_height: pv.screenHeight || 0,
- device_type: getDeviceType(pv.screenWidth, pv.screenHeight, pv.ua),
- country: countryCode,
- region: countryCode && regionCode ? countryCode + "-" + regionCode : "",
- city: city || "",
- lat: latitude || 0,
- lon: longitude || 0,
- type: pv.type || "pageview",
- event_name: pv.event_name || "",
- props: getParsedProperties(pv.properties),
- url_parameters: allUrlParams,
- // Performance metrics (only included for performance events).
- // ?? not ||: 0 is a legitimate measurement (a perfect CLS score is 0)
- // and must not be coerced to NULL, which would skew percentiles.
- lcp: pv.lcp ?? null,
- cls: pv.cls ?? null,
- inp: pv.inp ?? null,
- fcp: pv.fcp ?? null,
- ttfb: pv.ttfb ?? null,
- ip: pv.storeIp ? pv.ipAddress : null,
- timezone: timezone,
- tag: pv.tag || "",
- feature_flags: pv.feature_flags || {},
- import_id: null,
- asn: asnInfo?.asn ?? null,
- asn_org: asnInfo?.organization || "",
- is_datacenter_asn: asnInfo && isDatacenterAsn(asnInfo.asn) ? 1 : 0,
- };
- });
-
- // this.logger.info({ count: processedPageviews.length }, "Bulk insert to ClickHouse");
- // Bulk insert into database
try {
+ const ips = [...new Set(batch.map(pv => pv.ipAddress))];
+
+ const geoData = await getLocation(ips);
+
+ // Process each pageview with its geo data
+ const processedPageviews = batch
+ .filter(pv => {
+ if (pv.site_id == 9133 && pv.screenWidth == 800 && pv.screenHeight == 600) {
+ return false;
+ }
+ return true;
+ })
+ .map(pv => {
+ const dataForIp = geoData?.[pv.ipAddress];
+
+ const countryCode = dataForIp?.countryIso || "";
+ const regionCode = dataForIp?.region || "";
+ const latitude = dataForIp?.latitude || 0;
+ const longitude = dataForIp?.longitude || 0;
+ const city = dataForIp?.city || "";
+ const timezone = dataForIp?.timeZone || "";
+
+ // Same MaxMind lookup already used to decide identity bucketing
+ // (bucketIpForIdentity) and bot-detection eligibility; stored here purely
+ // for debugging identity-splitting reports, treated like geo data
+ // (independent of storeIp) since it identifies a network, not a device.
+ const asnInfo = lookupAsn(pv.ipAddress);
+
+ // Check if referrer is from the same domain and clear it if so
+ let referrer = clearSelfReferrer(pv.referrer || "", pv.hostname || "");
+
+ // Get all URL parameters for the url_parameters map
+ const allUrlParams = getAllUrlParams(pv.querystring || "");
+
+ return {
+ site_id: pv.site_id,
+ timestamp: DateTime.fromISO(pv.timestamp).toFormat("yyyy-MM-dd HH:mm:ss"),
+ session_id: pv.sessionId,
+ user_id: pv.userId, // Always the device fingerprint
+ identified_user_id: pv.identifiedUserId || "", // Custom user ID when identified
+ hostname: pv.hostname || "",
+ pathname: pv.pathname || "",
+ querystring: pv.querystring || "",
+ page_title: pv.page_title || "",
+ referrer: referrer,
+ channel: getChannel(referrer, pv.querystring, pv.hostname),
+ browser: pv.ua.browser.name || "",
+ browser_version: pv.ua.browser.major || "",
+ operating_system: pv.ua.os.name || "",
+ operating_system_version: pv.ua.os.version || "",
+ language: pv.language || "",
+ screen_width: pv.screenWidth || 0,
+ screen_height: pv.screenHeight || 0,
+ device_type: getDeviceType(pv.screenWidth, pv.screenHeight, pv.ua),
+ country: countryCode,
+ region: countryCode && regionCode ? countryCode + "-" + regionCode : "",
+ city: city || "",
+ lat: latitude || 0,
+ lon: longitude || 0,
+ type: pv.type || "pageview",
+ event_name: pv.event_name || "",
+ props: getParsedProperties(pv.properties),
+ url_parameters: allUrlParams,
+ // Performance metrics (only included for performance events).
+ // ?? not ||: 0 is a legitimate measurement (a perfect CLS score is 0)
+ // and must not be coerced to NULL, which would skew percentiles.
+ lcp: pv.lcp ?? null,
+ cls: pv.cls ?? null,
+ inp: pv.inp ?? null,
+ fcp: pv.fcp ?? null,
+ ttfb: pv.ttfb ?? null,
+ ip: pv.storeIp ? pv.ipAddress : null,
+ timezone: timezone,
+ tag: pv.tag || "",
+ feature_flags: pv.feature_flags || {},
+ import_id: null,
+ asn: asnInfo?.asn ?? null,
+ asn_org: asnInfo?.organization || "",
+ is_datacenter_asn: asnInfo && isDatacenterAsn(asnInfo.asn) ? 1 : 0,
+ };
+ });
+
+ // this.logger.info({ count: processedPageviews.length }, "Bulk insert to ClickHouse");
+ // Bulk insert into database
await clickhouse.insert({
table: "events",
values: processedPageviews,