🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor session replay logic and remove deprecated cron jobs
Bill Yangb99a8c4635e1
diff --git a/docs/src/app/pricing/components/ComparisonSection.jsx b/docs/src/app/pricing/components/ComparisonSection.jsxindex 6e8ac1a64..5daf931db 100644--- a/docs/src/app/pricing/components/ComparisonSection.jsx+++ b/docs/src/app/pricing/components/ComparisonSection.jsx@@ -57,6 +57,11 @@ const COMPARISON_FEATURES = [free: false,pro: true,},+ {+ name: "Error tracking",+ free: false,+ pro: true,+ },{name: "Pages view",free: false,diff --git a/server/public/script-full.js b/server/public/script-full.jsindex 988258830..280a3cc16 100644--- a/server/public/script-full.js+++ b/server/public/script-full.js@@ -364,7 +364,7 @@try {this.sessionReplayRecorder = new SessionReplayRecorder(this.config,- this.customUserId || this.generateUserId(),+ this.customUserId || "",(batch) => this.sendSessionReplayBatch(batch));await this.sessionReplayRecorder.initialize();@@ -372,9 +372,6 @@console.error("Failed to initialize session replay:", error);}}- generateUserId() {- return "anon_" + Math.random().toString(36).substring(2) + Date.now().toString(36);- }async sendSessionReplayBatch(batch) {try {await fetch(diff --git a/server/public/script.js b/server/public/script.jsindex 2a30f8fd9..c3a92c090 100644--- a/server/public/script.js+++ b/server/public/script.js@@ -1 +1 @@-"use strict";(()=>{var se=Object.defineProperty;var ae=(i,e,t)=>e in i?se(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var h=(i,e,t)=>ae(i,typeof e!="symbol"?e+"":e,t);function oe(i){let e="__DOUBLE_ASTERISK_TOKEN__",t="__SINGLE_ASTERISK_TOKEN__",n=i.replace(/\*\*/g,e).replace(/\*/g,t).replace(/[.+?^${}()|[\]\\]/g,"\\$&");n=n.replace(new RegExp(`/${e}/`,"g"),"/(?:.+/)?"),n=n.replace(new RegExp(e,"g"),".*"),n=n.replace(/\//g,"\\/");let c=n.replace(new RegExp(t,"g"),"[^/]+");return new RegExp("^"+c+"$")}function k(i,e){for(let t of e)try{if(oe(t).test(i))return t}catch(r){console.error(`Invalid pattern: ${t}`,r)}return null}function x(i,e){let t=null;return(...r)=>{t&&clearTimeout(t),t=setTimeout(()=>i(...r),e)}}function F(i){try{let e=window.location.hostname,t=new URL(i).hostname;return t!==e&&t!==""}catch{return!1}}function E(i,e){if(!i)return e;try{let t=JSON.parse(i);return Array.isArray(e)&&!Array.isArray(t)?e:t}catch(t){return console.error("Error parsing JSON:",t),e}}function W(i){let e=i.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 r=i.getAttribute("data-site-id")||i.getAttribute("site-id");if(!r||isNaN(Number(r)))return console.error("Please provide a valid site ID using the data-site-id attribute"),null;let n=i.getAttribute("data-debounce")?Math.max(0,parseInt(i.getAttribute("data-debounce"))):500,c=E(i.getAttribute("data-skip-patterns"),[]),a=E(i.getAttribute("data-mask-patterns"),[]),s=i.getAttribute("data-api-key")||void 0,o=i.getAttribute("data-replay-batch-size")?Math.max(1,parseInt(i.getAttribute("data-replay-batch-size"))):3,l=i.getAttribute("data-replay-batch-interval")?Math.max(1e3,parseInt(i.getAttribute("data-replay-batch-interval"))):2e3;return console.info(i),{analyticsHost:t,siteId:r,debounceDuration:n,autoTrackPageview:i.getAttribute("data-auto-track-pageview")!=="false",autoTrackSpa:i.getAttribute("data-track-spa")!=="false",trackQuerystring:i.getAttribute("data-track-query")!=="false",trackOutbound:i.getAttribute("data-track-outbound")!=="false",enableWebVitals:i.getAttribute("data-web-vitals")==="true",trackErrors:i.getAttribute("data-track-errors")==="true",enableSessionReplay:i.getAttribute("data-session-replay")==="true",sessionReplayBatchSize:o,sessionReplayBatchInterval:l,skipPatterns:c,maskPatterns:a,apiKey:s}}var y=class{constructor(e,t,r){this.isRecording=!1;this.eventBuffer=[];this.config=e,this.userId=t,this.sendBatch=r}async initialize(){this.config.enableSessionReplay&&(window.rrweb||await this.loadRrweb(),window.rrweb?this.startRecording():console.warn("Failed to load rrweb, session replay disabled"))}async loadRrweb(){return new Promise((e,t)=>{let r=document.createElement("script");r.src="https://cdn.jsdelivr.net/npm/[email protected]/dist/rrweb.min.js",r.async=!1,r.onload=()=>{console.log("[Session Replay] rrweb loaded successfully"),e()},r.onerror=()=>t(new Error("Failed to load rrweb")),document.head.appendChild(r)})}startRecording(){if(this.isRecording||!window.rrweb||!this.config.enableSessionReplay){console.log("[Session Replay] Cannot start recording:",{isRecording:this.isRecording,hasRrweb:!!window.rrweb,enableSessionReplay:this.config.enableSessionReplay});return}console.log("[Session Replay] Starting recording at",new Date().toISOString()),console.log("[Session Replay] Document ready state:",document.readyState);try{this.stopRecordingFn=window.rrweb.record({emit:e=>{let r={0:"DOMContentLoaded",1:"Load",2:"FullSnapshot",3:"IncrementalSnapshot",4:"Meta",5:"Custom",6:"Plugin"}[e.type]||`Unknown(${e.type})`;console.log(`[Session Replay] Event collected: Type ${e.type} (${r}) at ${new Date(e.timestamp||Date.now()).toISOString()}`),this.addEvent({type:e.type,data:e.data,timestamp:e.timestamp||Date.now()})},recordCanvas:!0,collectFonts:!0,checkoutEveryNms:3e4,checkoutEveryNth:200,maskAllInputs:!0,maskInputOptions:{password:!0,email:!0},slimDOMOptions:{script:!1,comment:!0,headFavicon:!0,headWhitespace:!0,headMetaDescKeywords:!0,headMetaSocial:!0,headMetaRobots:!0,headMetaHttpEquiv:!0,headMetaAuthorship:!0,headMetaVerification:!0},sampling:{mousemove:!1,mouseInteraction:!0,scroll:150,input:"last"}}),this.isRecording=!0,this.setupBatchTimer(),console.log("Session replay recording started")}catch(e){console.error("Failed to start session replay recording:",e)}}stopRecording(){this.isRecording&&(this.stopRecordingFn&&this.stopRecordingFn(),this.isRecording=!1,this.clearBatchTimer(),this.eventBuffer.length>0&&this.flushEvents(),console.log("Session replay recording stopped"))}isActive(){return this.isRecording}addEvent(e){this.eventBuffer.push(e),console.log(`[Session Replay] Event added to buffer (${this.eventBuffer.length}/${this.config.sessionReplayBatchSize})`),this.eventBuffer.length>=this.config.sessionReplayBatchSize&&(console.log(`[Session Replay] Buffer full, flushing ${this.eventBuffer.length} events`),this.flushEvents())}setupBatchTimer(){this.clearBatchTimer(),this.batchTimer=window.setInterval(()=>{this.eventBuffer.length>0&&(console.log(`[Session Replay] Timer triggered, flushing ${this.eventBuffer.length} events`),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=[],console.log(`[Session Replay] Sending batch with ${e.length} events to server`),console.log("[Session Replay] Event types in batch:",e.map(r=>`Type ${r.type}`).join(", ")),console.log("[Session Replay] Batch size:",JSON.stringify(e).length,"characters");let t={userId:this.userId,events:e,metadata:{pageUrl:window.location.href,viewportWidth:window.innerWidth,viewportHeight:window.innerHeight,language:navigator.language}};try{await this.sendBatch(t),console.log(`[Session Replay] Successfully sent batch with ${e.length} events`)}catch(r){console.error("Failed to send session replay batch:",r),console.error("Failed batch details:",{eventCount:e.length,eventTypes:e.map(n=>n.type),batchSize:JSON.stringify(t).length,userId:this.userId,url:window.location.href}),console.log(`[Session Replay] Re-queuing ${e.length} failed events for retry`),this.eventBuffer.unshift(...e)}}updateUserId(e){this.userId=e}onPageChange(){this.isRecording&&this.flushEvents()}cleanup(){this.stopRecording()}};var v=class{constructor(e){this.customUserId=null;this.config=e,this.loadUserId(),e.enableSessionReplay&&this.initializeSessionReplay()}loadUserId(){try{let e=localStorage.getItem("rybbit-user-id");e&&(this.customUserId=e)}catch{}}async initializeSessionReplay(){try{this.sessionReplayRecorder=new y(this.config,this.customUserId||this.generateUserId(),e=>this.sendSessionReplayBatch(e)),await this.sessionReplayRecorder.initialize()}catch(e){console.error("Failed to initialize session replay:",e)}}generateUserId(){return"anon_"+Math.random().toString(36).substring(2)+Date.now().toString(36)}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)),k(t,this.config.skipPatterns))return null;let r=k(t,this.config.maskPatterns);r&&(t=r);let n={site_id:this.config.siteId,hostname:e.hostname,pathname:t,querystring:this.config.trackQuerystring?e.search:"",screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,page_title:document.title,referrer:document.referrer};return this.customUserId&&(n.user_id=this.customUserId),this.config.apiKey&&(n.api_key=this.config.apiKey),n}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="",r={}){if(e==="custom_event"&&(!t||typeof t!="string")){console.error("Event name is required and must be a string for custom events");return}let n=this.createBasePayload();if(!n)return;let c={...n,type:e,event_name:t,properties:e==="custom_event"||e==="outbound"||e==="error"?JSON.stringify(r):void 0};this.sendTrackingData(c)}trackPageview(){this.track("pageview")}trackEvent(e,t={}){this.track("custom_event",e,t)}trackOutbound(e,t="",r="_self"){this.track("outbound","",{url:e,text:t,target:r})}trackWebVitals(e){let t=this.createBasePayload();if(!t)return;let r={...t,type:"performance",event_name:"web-vitals",...e};this.sendTrackingData(r)}trackError(e,t={}){let r=window.location.origin,n=t.filename||"",c=e.stack||"";if(n)try{if(new URL(n).origin!==r)return}catch{}else if(c&&!c.includes(r))return;let a={message:e.message?.substring(0,500)||"Unknown error",stack:c.substring(0,2e3)||""};if(n&&(a.fileName=n),t.lineno){let s=typeof t.lineno=="string"?parseInt(t.lineno,10):t.lineno;s&&s!==0&&(a.lineNumber=s)}if(t.colno){let s=typeof t.colno=="string"?parseInt(t.colno,10):t.colno;s&&s!==0&&(a.columnNumber=s)}for(let s in t)!["lineno","colno"].includes(s)&&t[s]!==void 0&&(a[s]=t[s]);this.track("error",e.name||"Error",a)}identify(e){if(typeof e!="string"||e.trim()===""){console.error("User ID must be a non-empty string");return}this.customUserId=e.trim();try{localStorage.setItem("rybbit-user-id",this.customUserId)}catch{console.warn("Could not persist user ID to localStorage")}this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(this.customUserId)}clearUserId(){this.customUserId=null;try{localStorage.removeItem("rybbit-user-id")}catch{}}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.sessionReplayRecorder&&this.sessionReplayRecorder.onPageChange()}cleanup(){this.sessionReplayRecorder&&this.sessionReplayRecorder.cleanup()}};var Q=-1,g=i=>{addEventListener("pageshow",e=>{e.persisted&&(Q=e.timeStamp,i(e))},!0)},d=(i,e,t,r)=>{let n,c;return a=>{e.value>=0&&(a||r)&&(c=e.value-(n??0),(c||n===void 0)&&(n=e.value,e.delta=c,e.rating=((s,o)=>s>o[1]?"poor":s>o[0]?"needs-improvement":"good")(e.value,t),i(e)))}},M=i=>{requestAnimationFrame(()=>requestAnimationFrame(()=>i()))},O=()=>{let i=performance.getEntriesByType("navigation")[0];if(i&&i.responseStart>0&&i.responseStart<performance.now())return i},f=()=>O()?.activationStart??0,u=(i,e=-1)=>{let t=O(),r="navigate";return Q>=0?r="back-forward-cache":t&&(document.prerendering||f()>0?r="prerender":document.wasDiscarded?r="restore":t.type&&(r=t.type.replace(/_/g,"-"))),{name:i,value:e,rating:"good",delta:0,entries:[],id:`v5-${Date.now()}-${Math.floor(8999999999999*Math.random())+1e12}`,navigationType:r}},P=new WeakMap;function _(i,e){return P.get(i)||P.set(i,new e),P.get(i)}var C=class{constructor(){h(this,"t");h(this,"i",0);h(this,"o",[])}h(e){if(e.hadRecentInput)return;let t=this.o[0],r=this.o.at(-1);this.i&&t&&r&&e.startTime-r.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)}},m=(i,e,t={})=>{try{if(PerformanceObserver.supportedEntryTypes.includes(i)){let r=new PerformanceObserver(n=>{Promise.resolve().then(()=>{e(n.getEntries())})});return r.observe({type:i,buffered:!0,...t}),r}}catch{}},N=i=>{let e=!1;return()=>{e||(i(),e=!0)}},p=-1,$=()=>document.visibilityState!=="hidden"||document.prerendering?1/0:0,R=i=>{document.visibilityState==="hidden"&&p>-1&&(p=i.type==="visibilitychange"?i.timeStamp:0,ce())},H=()=>{addEventListener("visibilitychange",R,!0),addEventListener("prerenderingchange",R,!0)},ce=()=>{removeEventListener("visibilitychange",R,!0),removeEventListener("prerenderingchange",R,!0)},Y=()=>{if(p<0){let i=f();p=(document.prerendering?void 0:globalThis.performance.getEntriesByType("visibility-state").filter(t=>t.name==="hidden"&&t.startTime>i)[0]?.startTime)??$(),H(),g(()=>{setTimeout(()=>{p=$(),H()})})}return{get firstHiddenTime(){return p}}},w=i=>{document.prerendering?addEventListener("prerenderingchange",()=>i(),!0):i()},z=[1800,3e3],U=(i,e={})=>{w(()=>{let t=Y(),r,n=u("FCP"),c=m("paint",a=>{for(let s of a)s.name==="first-contentful-paint"&&(c.disconnect(),s.startTime<t.firstHiddenTime&&(n.value=Math.max(s.startTime-f(),0),n.entries.push(s),r(!0)))});c&&(r=d(i,n,z,e.reportAllChanges),g(a=>{n=u("FCP"),r=d(i,n,z,e.reportAllChanges),M(()=>{n.value=performance.now()-a.timeStamp,r(!0)})}))})},V=[.1,.25],X=(i,e={})=>{U(N(()=>{let t,r=u("CLS",0),n=_(e,C),c=s=>{for(let o of s)n.h(o);n.i>r.value&&(r.value=n.i,r.entries=n.o,t())},a=m("layout-shift",c);a&&(t=d(i,r,V,e.reportAllChanges),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&(c(a.takeRecords()),t(!0))}),g(()=>{n.i=0,r=u("CLS",0),t=d(i,r,V,e.reportAllChanges),M(()=>t())}),setTimeout(t))}))},Z=0,I=1/0,b=0,le=i=>{for(let e of i)e.interactionId&&(I=Math.min(I,e.interactionId),b=Math.max(b,e.interactionId),Z=b?(b-I)/7+1:0)},B,J=()=>B?Z:performance.interactionCount??0,de=()=>{"interactionCount"in performance||B||(B=m("event",le,{type:"event",buffered:!0,durationThreshold:0}))},K=0,A=class{constructor(){h(this,"u",[]);h(this,"l",new Map);h(this,"m");h(this,"v")}p(){K=J(),this.u.length=0,this.l.clear()}P(){let e=Math.min(this.u.length-1,Math.floor((J()-K)/50));return this.u[e]}h(e){if(this.m?.(e),!e.interactionId&&e.entryType!=="first-input")return;let t=this.u.at(-1),r=this.l.get(e.interactionId);if(r||this.u.length<10||e.duration>t.T){if(r?e.duration>r.T?(r.entries=[e],r.T=e.duration):e.duration===r.T&&e.startTime===r.entries[0].startTime&&r.entries.push(e):(r={id:e.interactionId,entries:[e],T:e.duration},this.l.set(r.id,r),this.u.push(r)),this.u.sort((n,c)=>c.T-n.T),this.u.length>10){let n=this.u.splice(10);for(let c of n)this.l.delete(c.id)}this.v?.(r)}}},ee=i=>{let e=globalThis.requestIdleCallback||setTimeout;document.visibilityState==="hidden"?i():(i=N(i),document.addEventListener("visibilitychange",i,{once:!0}),e(()=>{i(),document.removeEventListener("visibilitychange",i)}))},j=[200,500],te=(i,e={})=>{globalThis.PerformanceEventTiming&&"interactionId"in PerformanceEventTiming.prototype&&w(()=>{de();let t,r=u("INP"),n=_(e,A),c=s=>{ee(()=>{for(let l of s)n.h(l);let o=n.P();o&&o.T!==r.value&&(r.value=o.T,r.entries=o.entries,t())})},a=m("event",c,{durationThreshold:e.durationThreshold??40});t=d(i,r,j,e.reportAllChanges),a&&(a.observe({type:"first-input",buffered:!0}),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&(c(a.takeRecords()),t(!0))}),g(()=>{n.p(),r=u("INP"),t=d(i,r,j,e.reportAllChanges)}))})},L=class{constructor(){h(this,"m")}h(e){this.m?.(e)}},q=[2500,4e3],ie=(i,e={})=>{w(()=>{let t=Y(),r,n=u("LCP"),c=_(e,L),a=o=>{e.reportAllChanges||(o=o.slice(-1));for(let l of o)c.h(l),l.startTime<t.firstHiddenTime&&(n.value=Math.max(l.startTime-f(),0),n.entries=[l],r())},s=m("largest-contentful-paint",a);if(s){r=d(i,n,q,e.reportAllChanges);let o=N(()=>{a(s.takeRecords()),s.disconnect(),r(!0)});for(let l of["keydown","click","visibilitychange"])addEventListener(l,()=>ee(o),{capture:!0,once:!0});g(l=>{n=u("LCP"),r=d(i,n,q,e.reportAllChanges),M(()=>{n.value=performance.now()-l.timeStamp,r(!0)})})}})},G=[800,1800],D=i=>{document.prerendering?w(()=>D(i)):document.readyState!=="complete"?addEventListener("load",()=>D(i),!0):setTimeout(i)},re=(i,e={})=>{let t=u("TTFB"),r=d(i,t,G,e.reportAllChanges);D(()=>{let n=O();n&&(t.value=Math.max(n.responseStart-f(),0),t.entries=[n],r(!0),g(()=>{t=u("TTFB",0),r=d(i,t,G,e.reportAllChanges),r(!0)}))})};var S=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{ie(this.collectMetric.bind(this)),X(this.collectMetric.bind(this)),te(this.collectMetric.bind(this)),U(this.collectMetric.bind(this)),re(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(n=>n!==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}}};(function(){let i=document.currentScript;if(!i){console.error("Could not find current script tag");return}if(window.__RYBBIT_OPTOUT__||localStorage.getItem("disable-rybbit")!==null){window.rybbit={pageview:()=>{},event:()=>{},trackOutbound:()=>{},identify:()=>{},clearUserId:()=>{},getUserId:()=>null,startSessionReplay:()=>{},stopSessionReplay:()=>{},isSessionReplayActive:()=>!1};return}let e=W(i);if(!e)return;let t=new v(e);e.enableWebVitals&&new S(s=>{t.trackWebVitals(s)}).initialize(),e.trackErrors&&(window.addEventListener("error",a=>{t.trackError(a.error||new Error(a.message),{filename:a.filename,lineno:a.lineno,colno:a.colno})}),window.addEventListener("unhandledrejection",a=>{let s=a.reason instanceof Error?a.reason:new Error(String(a.reason));t.trackError(s,{type:"unhandledrejection"})}));let r=()=>t.trackPageview(),n=e.debounceDuration>0?x(r,e.debounceDuration):r;function c(){if(document.addEventListener("click",function(a){let s=a.target;for(;s&&s!==document.documentElement;){if(s.hasAttribute("data-rybbit-event")){let o=s.getAttribute("data-rybbit-event");if(o){let l={};for(let T of s.attributes)if(T.name.startsWith("data-rybbit-prop-")){let ne=T.name.replace("data-rybbit-prop-","");l[ne]=T.value}t.trackEvent(o,l)}break}s=s.parentElement}if(e.trackOutbound){let o=a.target.closest("a");o?.href&&F(o.href)&&t.trackOutbound(o.href,o.innerText||o.textContent||"",o.target||"_self")}}),e.autoTrackSpa){let a=history.pushState,s=history.replaceState;history.pushState=function(...o){a.apply(this,o),n(),t.onPageChange()},history.replaceState=function(...o){s.apply(this,o),n(),t.onPageChange()},window.addEventListener("popstate",()=>{n(),t.onPageChange()}),window.addEventListener("hashchange",()=>{n(),t.onPageChange()})}}window.rybbit={pageview:()=>t.trackPageview(),event:(a,s={})=>t.trackEvent(a,s),trackOutbound:(a,s="",o="_self")=>t.trackOutbound(a,s,o),identify:a=>t.identify(a),clearUserId:()=>t.clearUserId(),getUserId:()=>t.getUserId(),startSessionReplay:()=>t.startSessionReplay(),stopSessionReplay:()=>t.stopSessionReplay(),isSessionReplayActive:()=>t.isSessionReplayActive()},c(),window.addEventListener("beforeunload",()=>{t.cleanup()}),e.autoTrackPageview&&t.trackPageview()})();})();+"use strict";(()=>{var se=Object.defineProperty;var ae=(i,e,t)=>e in i?se(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var h=(i,e,t)=>ae(i,typeof e!="symbol"?e+"":e,t);function oe(i){let e="__DOUBLE_ASTERISK_TOKEN__",t="__SINGLE_ASTERISK_TOKEN__",n=i.replace(/\*\*/g,e).replace(/\*/g,t).replace(/[.+?^${}()|[\]\\]/g,"\\$&");n=n.replace(new RegExp(`/${e}/`,"g"),"/(?:.+/)?"),n=n.replace(new RegExp(e,"g"),".*"),n=n.replace(/\//g,"\\/");let c=n.replace(new RegExp(t,"g"),"[^/]+");return new RegExp("^"+c+"$")}function k(i,e){for(let t of e)try{if(oe(t).test(i))return t}catch(r){console.error(`Invalid pattern: ${t}`,r)}return null}function x(i,e){let t=null;return(...r)=>{t&&clearTimeout(t),t=setTimeout(()=>i(...r),e)}}function F(i){try{let e=window.location.hostname,t=new URL(i).hostname;return t!==e&&t!==""}catch{return!1}}function E(i,e){if(!i)return e;try{let t=JSON.parse(i);return Array.isArray(e)&&!Array.isArray(t)?e:t}catch(t){return console.error("Error parsing JSON:",t),e}}function W(i){let e=i.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 r=i.getAttribute("data-site-id")||i.getAttribute("site-id");if(!r||isNaN(Number(r)))return console.error("Please provide a valid site ID using the data-site-id attribute"),null;let n=i.getAttribute("data-debounce")?Math.max(0,parseInt(i.getAttribute("data-debounce"))):500,c=E(i.getAttribute("data-skip-patterns"),[]),a=E(i.getAttribute("data-mask-patterns"),[]),s=i.getAttribute("data-api-key")||void 0,o=i.getAttribute("data-replay-batch-size")?Math.max(1,parseInt(i.getAttribute("data-replay-batch-size"))):3,l=i.getAttribute("data-replay-batch-interval")?Math.max(1e3,parseInt(i.getAttribute("data-replay-batch-interval"))):2e3;return console.info(i),{analyticsHost:t,siteId:r,debounceDuration:n,autoTrackPageview:i.getAttribute("data-auto-track-pageview")!=="false",autoTrackSpa:i.getAttribute("data-track-spa")!=="false",trackQuerystring:i.getAttribute("data-track-query")!=="false",trackOutbound:i.getAttribute("data-track-outbound")!=="false",enableWebVitals:i.getAttribute("data-web-vitals")==="true",trackErrors:i.getAttribute("data-track-errors")==="true",enableSessionReplay:i.getAttribute("data-session-replay")==="true",sessionReplayBatchSize:o,sessionReplayBatchInterval:l,skipPatterns:c,maskPatterns:a,apiKey:s}}var y=class{constructor(e,t,r){this.isRecording=!1;this.eventBuffer=[];this.config=e,this.userId=t,this.sendBatch=r}async initialize(){this.config.enableSessionReplay&&(window.rrweb||await this.loadRrweb(),window.rrweb?this.startRecording():console.warn("Failed to load rrweb, session replay disabled"))}async loadRrweb(){return new Promise((e,t)=>{let r=document.createElement("script");r.src="https://cdn.jsdelivr.net/npm/[email protected]/dist/rrweb.min.js",r.async=!1,r.onload=()=>{console.log("[Session Replay] rrweb loaded successfully"),e()},r.onerror=()=>t(new Error("Failed to load rrweb")),document.head.appendChild(r)})}startRecording(){if(this.isRecording||!window.rrweb||!this.config.enableSessionReplay){console.log("[Session Replay] Cannot start recording:",{isRecording:this.isRecording,hasRrweb:!!window.rrweb,enableSessionReplay:this.config.enableSessionReplay});return}console.log("[Session Replay] Starting recording at",new Date().toISOString()),console.log("[Session Replay] Document ready state:",document.readyState);try{this.stopRecordingFn=window.rrweb.record({emit:e=>{let r={0:"DOMContentLoaded",1:"Load",2:"FullSnapshot",3:"IncrementalSnapshot",4:"Meta",5:"Custom",6:"Plugin"}[e.type]||`Unknown(${e.type})`;console.log(`[Session Replay] Event collected: Type ${e.type} (${r}) at ${new Date(e.timestamp||Date.now()).toISOString()}`),this.addEvent({type:e.type,data:e.data,timestamp:e.timestamp||Date.now()})},recordCanvas:!0,collectFonts:!0,checkoutEveryNms:3e4,checkoutEveryNth:200,maskAllInputs:!0,maskInputOptions:{password:!0,email:!0},slimDOMOptions:{script:!1,comment:!0,headFavicon:!0,headWhitespace:!0,headMetaDescKeywords:!0,headMetaSocial:!0,headMetaRobots:!0,headMetaHttpEquiv:!0,headMetaAuthorship:!0,headMetaVerification:!0},sampling:{mousemove:!1,mouseInteraction:!0,scroll:150,input:"last"}}),this.isRecording=!0,this.setupBatchTimer(),console.log("Session replay recording started")}catch(e){console.error("Failed to start session replay recording:",e)}}stopRecording(){this.isRecording&&(this.stopRecordingFn&&this.stopRecordingFn(),this.isRecording=!1,this.clearBatchTimer(),this.eventBuffer.length>0&&this.flushEvents(),console.log("Session replay recording stopped"))}isActive(){return this.isRecording}addEvent(e){this.eventBuffer.push(e),console.log(`[Session Replay] Event added to buffer (${this.eventBuffer.length}/${this.config.sessionReplayBatchSize})`),this.eventBuffer.length>=this.config.sessionReplayBatchSize&&(console.log(`[Session Replay] Buffer full, flushing ${this.eventBuffer.length} events`),this.flushEvents())}setupBatchTimer(){this.clearBatchTimer(),this.batchTimer=window.setInterval(()=>{this.eventBuffer.length>0&&(console.log(`[Session Replay] Timer triggered, flushing ${this.eventBuffer.length} events`),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=[],console.log(`[Session Replay] Sending batch with ${e.length} events to server`),console.log("[Session Replay] Event types in batch:",e.map(r=>`Type ${r.type}`).join(", ")),console.log("[Session Replay] Batch size:",JSON.stringify(e).length,"characters");let t={userId:this.userId,events:e,metadata:{pageUrl:window.location.href,viewportWidth:window.innerWidth,viewportHeight:window.innerHeight,language:navigator.language}};try{await this.sendBatch(t),console.log(`[Session Replay] Successfully sent batch with ${e.length} events`)}catch(r){console.error("Failed to send session replay batch:",r),console.error("Failed batch details:",{eventCount:e.length,eventTypes:e.map(n=>n.type),batchSize:JSON.stringify(t).length,userId:this.userId,url:window.location.href}),console.log(`[Session Replay] Re-queuing ${e.length} failed events for retry`),this.eventBuffer.unshift(...e)}}updateUserId(e){this.userId=e}onPageChange(){this.isRecording&&this.flushEvents()}cleanup(){this.stopRecording()}};var v=class{constructor(e){this.customUserId=null;this.config=e,this.loadUserId(),e.enableSessionReplay&&this.initializeSessionReplay()}loadUserId(){try{let e=localStorage.getItem("rybbit-user-id");e&&(this.customUserId=e)}catch{}}async initializeSessionReplay(){try{this.sessionReplayRecorder=new y(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)),k(t,this.config.skipPatterns))return null;let r=k(t,this.config.maskPatterns);r&&(t=r);let n={site_id:this.config.siteId,hostname:e.hostname,pathname:t,querystring:this.config.trackQuerystring?e.search:"",screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,page_title:document.title,referrer:document.referrer};return this.customUserId&&(n.user_id=this.customUserId),this.config.apiKey&&(n.api_key=this.config.apiKey),n}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="",r={}){if(e==="custom_event"&&(!t||typeof t!="string")){console.error("Event name is required and must be a string for custom events");return}let n=this.createBasePayload();if(!n)return;let c={...n,type:e,event_name:t,properties:e==="custom_event"||e==="outbound"||e==="error"?JSON.stringify(r):void 0};this.sendTrackingData(c)}trackPageview(){this.track("pageview")}trackEvent(e,t={}){this.track("custom_event",e,t)}trackOutbound(e,t="",r="_self"){this.track("outbound","",{url:e,text:t,target:r})}trackWebVitals(e){let t=this.createBasePayload();if(!t)return;let r={...t,type:"performance",event_name:"web-vitals",...e};this.sendTrackingData(r)}trackError(e,t={}){let r=window.location.origin,n=t.filename||"",c=e.stack||"";if(n)try{if(new URL(n).origin!==r)return}catch{}else if(c&&!c.includes(r))return;let a={message:e.message?.substring(0,500)||"Unknown error",stack:c.substring(0,2e3)||""};if(n&&(a.fileName=n),t.lineno){let s=typeof t.lineno=="string"?parseInt(t.lineno,10):t.lineno;s&&s!==0&&(a.lineNumber=s)}if(t.colno){let s=typeof t.colno=="string"?parseInt(t.colno,10):t.colno;s&&s!==0&&(a.columnNumber=s)}for(let s in t)!["lineno","colno"].includes(s)&&t[s]!==void 0&&(a[s]=t[s]);this.track("error",e.name||"Error",a)}identify(e){if(typeof e!="string"||e.trim()===""){console.error("User ID must be a non-empty string");return}this.customUserId=e.trim();try{localStorage.setItem("rybbit-user-id",this.customUserId)}catch{console.warn("Could not persist user ID to localStorage")}this.sessionReplayRecorder&&this.sessionReplayRecorder.updateUserId(this.customUserId)}clearUserId(){this.customUserId=null;try{localStorage.removeItem("rybbit-user-id")}catch{}}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.sessionReplayRecorder&&this.sessionReplayRecorder.onPageChange()}cleanup(){this.sessionReplayRecorder&&this.sessionReplayRecorder.cleanup()}};var Q=-1,g=i=>{addEventListener("pageshow",e=>{e.persisted&&(Q=e.timeStamp,i(e))},!0)},d=(i,e,t,r)=>{let n,c;return a=>{e.value>=0&&(a||r)&&(c=e.value-(n??0),(c||n===void 0)&&(n=e.value,e.delta=c,e.rating=((s,o)=>s>o[1]?"poor":s>o[0]?"needs-improvement":"good")(e.value,t),i(e)))}},M=i=>{requestAnimationFrame(()=>requestAnimationFrame(()=>i()))},O=()=>{let i=performance.getEntriesByType("navigation")[0];if(i&&i.responseStart>0&&i.responseStart<performance.now())return i},f=()=>O()?.activationStart??0,u=(i,e=-1)=>{let t=O(),r="navigate";return Q>=0?r="back-forward-cache":t&&(document.prerendering||f()>0?r="prerender":document.wasDiscarded?r="restore":t.type&&(r=t.type.replace(/_/g,"-"))),{name:i,value:e,rating:"good",delta:0,entries:[],id:`v5-${Date.now()}-${Math.floor(8999999999999*Math.random())+1e12}`,navigationType:r}},P=new WeakMap;function _(i,e){return P.get(i)||P.set(i,new e),P.get(i)}var I=class{constructor(){h(this,"t");h(this,"i",0);h(this,"o",[])}h(e){if(e.hadRecentInput)return;let t=this.o[0],r=this.o.at(-1);this.i&&t&&r&&e.startTime-r.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)}},m=(i,e,t={})=>{try{if(PerformanceObserver.supportedEntryTypes.includes(i)){let r=new PerformanceObserver(n=>{Promise.resolve().then(()=>{e(n.getEntries())})});return r.observe({type:i,buffered:!0,...t}),r}}catch{}},N=i=>{let e=!1;return()=>{e||(i(),e=!0)}},p=-1,$=()=>document.visibilityState!=="hidden"||document.prerendering?1/0:0,R=i=>{document.visibilityState==="hidden"&&p>-1&&(p=i.type==="visibilitychange"?i.timeStamp:0,ce())},H=()=>{addEventListener("visibilitychange",R,!0),addEventListener("prerenderingchange",R,!0)},ce=()=>{removeEventListener("visibilitychange",R,!0),removeEventListener("prerenderingchange",R,!0)},Y=()=>{if(p<0){let i=f();p=(document.prerendering?void 0:globalThis.performance.getEntriesByType("visibility-state").filter(t=>t.name==="hidden"&&t.startTime>i)[0]?.startTime)??$(),H(),g(()=>{setTimeout(()=>{p=$(),H()})})}return{get firstHiddenTime(){return p}}},w=i=>{document.prerendering?addEventListener("prerenderingchange",()=>i(),!0):i()},z=[1800,3e3],U=(i,e={})=>{w(()=>{let t=Y(),r,n=u("FCP"),c=m("paint",a=>{for(let s of a)s.name==="first-contentful-paint"&&(c.disconnect(),s.startTime<t.firstHiddenTime&&(n.value=Math.max(s.startTime-f(),0),n.entries.push(s),r(!0)))});c&&(r=d(i,n,z,e.reportAllChanges),g(a=>{n=u("FCP"),r=d(i,n,z,e.reportAllChanges),M(()=>{n.value=performance.now()-a.timeStamp,r(!0)})}))})},V=[.1,.25],X=(i,e={})=>{U(N(()=>{let t,r=u("CLS",0),n=_(e,I),c=s=>{for(let o of s)n.h(o);n.i>r.value&&(r.value=n.i,r.entries=n.o,t())},a=m("layout-shift",c);a&&(t=d(i,r,V,e.reportAllChanges),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&(c(a.takeRecords()),t(!0))}),g(()=>{n.i=0,r=u("CLS",0),t=d(i,r,V,e.reportAllChanges),M(()=>t())}),setTimeout(t))}))},Z=0,C=1/0,b=0,le=i=>{for(let e of i)e.interactionId&&(C=Math.min(C,e.interactionId),b=Math.max(b,e.interactionId),Z=b?(b-C)/7+1:0)},B,J=()=>B?Z:performance.interactionCount??0,de=()=>{"interactionCount"in performance||B||(B=m("event",le,{type:"event",buffered:!0,durationThreshold:0}))},K=0,A=class{constructor(){h(this,"u",[]);h(this,"l",new Map);h(this,"m");h(this,"v")}p(){K=J(),this.u.length=0,this.l.clear()}P(){let e=Math.min(this.u.length-1,Math.floor((J()-K)/50));return this.u[e]}h(e){if(this.m?.(e),!e.interactionId&&e.entryType!=="first-input")return;let t=this.u.at(-1),r=this.l.get(e.interactionId);if(r||this.u.length<10||e.duration>t.T){if(r?e.duration>r.T?(r.entries=[e],r.T=e.duration):e.duration===r.T&&e.startTime===r.entries[0].startTime&&r.entries.push(e):(r={id:e.interactionId,entries:[e],T:e.duration},this.l.set(r.id,r),this.u.push(r)),this.u.sort((n,c)=>c.T-n.T),this.u.length>10){let n=this.u.splice(10);for(let c of n)this.l.delete(c.id)}this.v?.(r)}}},ee=i=>{let e=globalThis.requestIdleCallback||setTimeout;document.visibilityState==="hidden"?i():(i=N(i),document.addEventListener("visibilitychange",i,{once:!0}),e(()=>{i(),document.removeEventListener("visibilitychange",i)}))},j=[200,500],te=(i,e={})=>{globalThis.PerformanceEventTiming&&"interactionId"in PerformanceEventTiming.prototype&&w(()=>{de();let t,r=u("INP"),n=_(e,A),c=s=>{ee(()=>{for(let l of s)n.h(l);let o=n.P();o&&o.T!==r.value&&(r.value=o.T,r.entries=o.entries,t())})},a=m("event",c,{durationThreshold:e.durationThreshold??40});t=d(i,r,j,e.reportAllChanges),a&&(a.observe({type:"first-input",buffered:!0}),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&(c(a.takeRecords()),t(!0))}),g(()=>{n.p(),r=u("INP"),t=d(i,r,j,e.reportAllChanges)}))})},L=class{constructor(){h(this,"m")}h(e){this.m?.(e)}},q=[2500,4e3],ie=(i,e={})=>{w(()=>{let t=Y(),r,n=u("LCP"),c=_(e,L),a=o=>{e.reportAllChanges||(o=o.slice(-1));for(let l of o)c.h(l),l.startTime<t.firstHiddenTime&&(n.value=Math.max(l.startTime-f(),0),n.entries=[l],r())},s=m("largest-contentful-paint",a);if(s){r=d(i,n,q,e.reportAllChanges);let o=N(()=>{a(s.takeRecords()),s.disconnect(),r(!0)});for(let l of["keydown","click","visibilitychange"])addEventListener(l,()=>ee(o),{capture:!0,once:!0});g(l=>{n=u("LCP"),r=d(i,n,q,e.reportAllChanges),M(()=>{n.value=performance.now()-l.timeStamp,r(!0)})})}})},G=[800,1800],D=i=>{document.prerendering?w(()=>D(i)):document.readyState!=="complete"?addEventListener("load",()=>D(i),!0):setTimeout(i)},re=(i,e={})=>{let t=u("TTFB"),r=d(i,t,G,e.reportAllChanges);D(()=>{let n=O();n&&(t.value=Math.max(n.responseStart-f(),0),t.entries=[n],r(!0),g(()=>{t=u("TTFB",0),r=d(i,t,G,e.reportAllChanges),r(!0)}))})};var S=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{ie(this.collectMetric.bind(this)),X(this.collectMetric.bind(this)),te(this.collectMetric.bind(this)),U(this.collectMetric.bind(this)),re(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(n=>n!==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}}};(function(){let i=document.currentScript;if(!i){console.error("Could not find current script tag");return}if(window.__RYBBIT_OPTOUT__||localStorage.getItem("disable-rybbit")!==null){window.rybbit={pageview:()=>{},event:()=>{},trackOutbound:()=>{},identify:()=>{},clearUserId:()=>{},getUserId:()=>null,startSessionReplay:()=>{},stopSessionReplay:()=>{},isSessionReplayActive:()=>!1};return}let e=W(i);if(!e)return;let t=new v(e);e.enableWebVitals&&new S(s=>{t.trackWebVitals(s)}).initialize(),e.trackErrors&&(window.addEventListener("error",a=>{t.trackError(a.error||new Error(a.message),{filename:a.filename,lineno:a.lineno,colno:a.colno})}),window.addEventListener("unhandledrejection",a=>{let s=a.reason instanceof Error?a.reason:new Error(String(a.reason));t.trackError(s,{type:"unhandledrejection"})}));let r=()=>t.trackPageview(),n=e.debounceDuration>0?x(r,e.debounceDuration):r;function c(){if(document.addEventListener("click",function(a){let s=a.target;for(;s&&s!==document.documentElement;){if(s.hasAttribute("data-rybbit-event")){let o=s.getAttribute("data-rybbit-event");if(o){let l={};for(let T of s.attributes)if(T.name.startsWith("data-rybbit-prop-")){let ne=T.name.replace("data-rybbit-prop-","");l[ne]=T.value}t.trackEvent(o,l)}break}s=s.parentElement}if(e.trackOutbound){let o=a.target.closest("a");o?.href&&F(o.href)&&t.trackOutbound(o.href,o.innerText||o.textContent||"",o.target||"_self")}}),e.autoTrackSpa){let a=history.pushState,s=history.replaceState;history.pushState=function(...o){a.apply(this,o),n(),t.onPageChange()},history.replaceState=function(...o){s.apply(this,o),n(),t.onPageChange()},window.addEventListener("popstate",()=>{n(),t.onPageChange()}),window.addEventListener("hashchange",()=>{n(),t.onPageChange()})}}window.rybbit={pageview:()=>t.trackPageview(),event:(a,s={})=>t.trackEvent(a,s),trackOutbound:(a,s="",o="_self")=>t.trackOutbound(a,s,o),identify:a=>t.identify(a),clearUserId:()=>t.clearUserId(),getUserId:()=>t.getUserId(),startSessionReplay:()=>t.startSessionReplay(),stopSessionReplay:()=>t.stopSessionReplay(),isSessionReplayActive:()=>t.isSessionReplayActive()},c(),window.addEventListener("beforeunload",()=>{t.cleanup()}),e.autoTrackPageview&&t.trackPageview()})();})();diff --git a/server/src/cron/index.ts b/server/src/cron/index.tsdeleted file mode 100644index dadb5efdb..000000000--- a/server/src/cron/index.ts+++ /dev/null@@ -1,15 +0,0 @@-import * as cron from "node-cron";-import { IS_CLOUD } from "../lib/const.js";-import { updateOrganizationsMonthlyUsage } from "./monthly-usage-checker.js";--export async function initializeCronJobs() {- console.log("Initializing cron jobs...");-- if (IS_CLOUD && process.env.NODE_ENV !== "development") {- // Schedule the monthly usage checker to run every 30 minutes- cron.schedule("*/30 * * * *", updateOrganizationsMonthlyUsage);- updateOrganizationsMonthlyUsage();- }-- console.log("Cron jobs initialized successfully");-}diff --git a/server/src/cron/monthly-usage-checker.ts b/server/src/cron/monthly-usage-checker.tsdeleted file mode 100644index 28af0a253..000000000--- a/server/src/cron/monthly-usage-checker.ts+++ /dev/null@@ -1,268 +0,0 @@-import { eq, inArray, isNotNull } from "drizzle-orm";-import { DateTime } from "luxon";-import Stripe from "stripe";-import { processResults } from "../api/analytics/utils.js";-import { clickhouse } from "../db/clickhouse/clickhouse.js";-import { db } from "../db/postgres/postgres.js";-import { organization, sites } from "../db/postgres/schema.js";-import {- getStripePrices,- StripePlan,- DEFAULT_EVENT_LIMIT,-} from "../lib/const.js";-import { stripe } from "../lib/stripe.js";--// Global set to track site IDs that have exceeded their monthly limits-export const sitesOverLimit = new Set<number>();--/**- * Gets the first day of the current month in YYYY-MM-DD format using Luxon- */-function getStartOfMonth(): string {- return DateTime.now().startOf("month").toISODate() as string;-}--/**- * Gets all site IDs for an organization- */-async function getSiteIdsForOrganization(- organizationId: string-): Promise<number[]> {- try {- const siteRecords = await db- .select({ siteId: sites.siteId })- .from(sites)- .where(eq(sites.organizationId, organizationId));-- return siteRecords.map((record) => record.siteId);- } catch (error) {- console.error(- `Error getting sites for organization ${organizationId}:`,- error- );- return [];- }-}--/**- * Gets event limit and billing period start date for an organization based on their Stripe subscription.- * Fetches directly from Stripe if the organization has a stripeCustomerId.- * @returns [eventLimit, periodStartDate]- */-async function getOrganizationSubscriptionInfo(orgData: {- id: string;- stripeCustomerId: string | null;- createdAt: string;- name: string;-}): Promise<[number, string | null]> {- if (orgData.name === "tomato 2") {- return [Infinity, getStartOfMonth()];- }- if (!orgData.stripeCustomerId) {- // No Stripe customer ID, use default limit and start of current month- return [DEFAULT_EVENT_LIMIT, getStartOfMonth()];- }-- try {- // Fetch active subscriptions for the customer from Stripe- const subscriptions = await (stripe as Stripe).subscriptions.list({- customer: orgData.stripeCustomerId,- status: "active",- limit: 1,- });-- if (subscriptions.data.length === 0) {- return [DEFAULT_EVENT_LIMIT, getStartOfMonth()];- }-- const subscription = subscriptions.data[0];- const subscriptionItem = subscription.items.data[0];-- const priceId = subscriptionItem.price.id;-- if (!priceId) {- console.error(- `Subscription item price ID not found for organization ${orgData.id}, sub ${subscription.id}`- );- return [DEFAULT_EVENT_LIMIT, getStartOfMonth()];- }-- // Find corresponding plan details from constants- const planDetails = getStripePrices().find(- (plan: StripePlan) =>- plan.priceId === priceId ||- (plan.annualDiscountPriceId && plan.annualDiscountPriceId === priceId)- );-- // Get the event limit from the plan- const eventLimit = planDetails- ? planDetails.limits.events // This is already the monthly event limit, regardless of billing interval- : DEFAULT_EVENT_LIMIT;-- // For the period start, we need to handle several cases:- const currentMonthStart = getStartOfMonth();- let periodStart = currentMonthStart;-- if (subscriptionItem.current_period_start) {- // Convert subscription start timestamp to DateTime- const subscriptionStartDate = DateTime.fromSeconds(- subscriptionItem.current_period_start- );- const currentMonth = DateTime.now().startOf("month");-- // If the subscription started within the current month, use that as the start date- // This ensures we don't count events from before they subscribed (e.g., during their free trial)- if (subscriptionStartDate >= currentMonth) {- periodStart = subscriptionStartDate.toISODate() as string;- console.log(- `[Monthly Usage Checker] Organization ${orgData.name} subscribed during current month on ${periodStart}. Using subscription start date for counting.`- );- } else {- console.log(- `[Monthly Usage Checker] Organization ${orgData.name} subscription started before current month. Using month start for counting.`- );- }- }-- // Include subscription info for logging purposes- const interval = subscriptionItem.price.recurring?.interval || "unknown";- console.log(- `[Monthly Usage Checker] Organization ${orgData.name} has a ${interval} subscription.`- );-- return [eventLimit, periodStart];- } catch (error: any) {- console.error(- `Error fetching Stripe subscription info for organization ${orgData.name}:`,- error- );- // Fallback to default limit and current month start on Stripe API error- return [DEFAULT_EVENT_LIMIT, getStartOfMonth()];- }-}--/**- * Gets monthly pageview count from ClickHouse for the given site IDs- */-async function getMonthlyPageviews(- siteIds: number[],- startDate: string | null-): Promise<number> {- if (!siteIds.length) {- return 0;- }-- // If no startDate is provided (e.g., no subscription), default to start of month- const periodStart = startDate || getStartOfMonth();-- try {- const result = await clickhouse.query({- query: `- SELECT COUNT(*) as count- FROM events- WHERE site_id IN (${siteIds.join(",")}) AND type = 'pageview'- AND timestamp >= toDate('${periodStart}')- `,- format: "JSONEachRow",- });- const rows = await processResults<{ count: string }>(result);- return parseInt(rows[0].count, 10);- } catch (error) {- console.error(- `Error querying ClickHouse for pageviews for sites ${siteIds}:`,- error- );- return 0;- }-}--/**- * Updates monthly event usage for all organizations- */-export async function updateOrganizationsMonthlyUsage() {- console.log(- "[Monthly Usage Checker] Starting check of monthly event usage for organizations..."- );-- try {- // Get all organizations (both with and without Stripe customer IDs)- const organizations = await db- .select({- id: organization.id,- name: organization.name,- stripeCustomerId: organization.stripeCustomerId,- createdAt: organization.createdAt,- })- .from(organization);-- for (const orgData of organizations) {- try {- // Get site IDs for this organization- const siteIds = await getSiteIdsForOrganization(orgData.id);-- // If organization has no sites, continue to next organization- if (!siteIds.length) {- continue;- }-- // Get organization's subscription information (limit and period start)- const [eventLimit, periodStart] =- await getOrganizationSubscriptionInfo(orgData);-- // Get monthly pageview count from ClickHouse using the billing period start date- const pageviewCount = await getMonthlyPageviews(siteIds, periodStart);-- // Check if over limit and update global set- const isOverLimit = pageviewCount > eventLimit;-- // Update organization's monthlyEventCount and overMonthlyLimit fields- await db- .update(organization)- .set({- monthlyEventCount: pageviewCount,- overMonthlyLimit: isOverLimit,- })- .where(eq(organization.id, orgData.id));-- // If over the limit, add all this organization's sites to the global set- if (isOverLimit) {- for (const siteId of siteIds) {- sitesOverLimit.add(siteId);- }- console.log(- `[Monthly Usage Checker] Organization ${orgData.name} is over limit. Added ${siteIds.length} sites to blocked list.`- );- } else {- for (const siteId of siteIds) {- sitesOverLimit.delete(siteId);- }- }-- // Format additional date info for logging if available- const periodInfo = periodStart- ? `period started ${periodStart}`- : "this month";-- console.log(- `[Monthly Usage Checker] Updated organization ${- orgData.name- }: ${pageviewCount.toLocaleString()} events, limit ${eventLimit.toLocaleString()}, ${periodInfo}`- );- } catch (error) {- console.error(- `[Monthly Usage Checker] Error processing organization ${orgData.id}:`,- error- );- }- }-- console.log(- `[Monthly Usage Checker] Completed monthly event usage check. ${sitesOverLimit.size} sites are over their limit.`- );- } catch (error) {- console.error(- "[Monthly Usage Checker] Error updating monthly usage:",- error- );- }-}diff --git a/server/src/index.ts b/server/src/index.tsindex fac1033b3..cb0b8abb3 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -13,6 +13,9 @@ import { createFunnel } from "./api/analytics/funnels/createFunnel.js";import { deleteFunnel } from "./api/analytics/funnels/deleteFunnel.js";import { getFunnel } from "./api/analytics/funnels/getFunnel.js";import { getFunnels } from "./api/analytics/funnels/getFunnels.js";+import { getErrorBucketed } from "./api/analytics/getErrorBucketed.js";+import { getErrorEvents } from "./api/analytics/getErrorEvents.js";+import { getErrorNames } from "./api/analytics/getErrorNames.js";import { getJourneys } from "./api/analytics/getJourneys.js";import { getLiveSessionLocations } from "./api/analytics/getLiveSessionLocations.js";import { getLiveUsercount } from "./api/analytics/getLiveUsercount.js";@@ -20,9 +23,6 @@ import { getOrgEventCount } from "./api/analytics/getOrgEventCount.js";import { getOverview } from "./api/analytics/getOverview.js";import { getOverviewBucketed } from "./api/analytics/getOverviewBucketed.js";import { getPageTitles } from "./api/analytics/getPageTitles.js";-import { getErrorNames } from "./api/analytics/getErrorNames.js";-import { getErrorEvents } from "./api/analytics/getErrorEvents.js";-import { getErrorBucketed } from "./api/analytics/getErrorBucketed.js";import { getRetention } from "./api/analytics/getRetention.js";import { getSession } from "./api/analytics/getSession.js";import { getSessions } from "./api/analytics/getSessions.js";@@ -38,10 +38,10 @@ import { updateGoal } from "./api/analytics/goals/updateGoal.js";import { getPerformanceByDimension } from "./api/analytics/performance/getPerformanceByDimension.js";import { getPerformanceOverview } from "./api/analytics/performance/getPerformanceOverview.js";import { getPerformanceTimeSeries } from "./api/analytics/performance/getPerformanceTimeSeries.js";-import { recordSessionReplay } from "./api/sessionReplay/recordSessionReplay.js";-import { getSessionReplays } from "./api/sessionReplay/getSessionReplays.js";-import { getSessionReplayEvents } from "./api/sessionReplay/getSessionReplayEvents.js";import { getConfig } from "./api/getConfig.js";+import { getSessionReplayEvents } from "./api/sessionReplay/getSessionReplayEvents.js";+import { getSessionReplays } from "./api/sessionReplay/getSessionReplays.js";+import { recordSessionReplay } from "./api/sessionReplay/recordSessionReplay.js";import { addSite } from "./api/sites/addSite.js";import { changeSiteBlockBots } from "./api/sites/changeSiteBlockBots.js";import { changeSiteDomain } from "./api/sites/changeSiteDomain.js";@@ -49,10 +49,10 @@ import { changeSitePublic } from "./api/sites/changeSitePublic.js";import { changeSiteSalt } from "./api/sites/changeSiteSalt.js";import { deleteSite } from "./api/sites/deleteSite.js";import { getSite } from "./api/sites/getSite.js";+import { getSiteApiConfig } from "./api/sites/getSiteApiConfig.js";import { getSiteHasData } from "./api/sites/getSiteHasData.js";import { getSiteIsPublic } from "./api/sites/getSiteIsPublic.js";import { getSitesFromOrg } from "./api/sites/getSitesFromOrg.js";-import { getSiteApiConfig } from "./api/sites/getSiteApiConfig.js";import { updateSiteApiConfig } from "./api/sites/updateSiteApiConfig.js";import { createCheckoutSession } from "./api/stripe/createCheckoutSession.js";import { createPortalSession } from "./api/stripe/createPortalSession.js";@@ -60,15 +60,14 @@ import { getSubscription } from "./api/stripe/getSubscription.js";import { handleWebhook } from "./api/stripe/webhook.js";import { getUserOrganizations } from "./api/user/getUserOrganizations.js";import { listOrganizationMembers } from "./api/user/listOrganizationMembers.js";-import { initializeCronJobs } from "./cron/index.js";import { initializeClickhouse } from "./db/clickhouse/clickhouse.js";-import { allowList, loadAllowedDomains } from "./lib/allowedDomains.js";+import { loadAllowedDomains } from "./lib/allowedDomains.js";import { getSessionFromReq, mapHeaders } from "./lib/auth-utils.js";import { auth } from "./lib/auth.js";import { IS_CLOUD } from "./lib/const.js";import { siteConfig } from "./lib/siteConfig.js";import { trackEvent } from "./tracker/trackEvent.js";-import { extractSiteId, isSitePublic, normalizeOrigin } from "./utils.js";+import { extractSiteId, isSitePublic } from "./utils.js";const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);@@ -327,8 +326,6 @@ const start = async () => {// Start the serverawait server.listen({ port: 3001, host: "0.0.0.0" });-- initializeCronJobs();} catch (err) {server.log.error(err);process.exit(1);diff --git a/server/src/services/usageCheckerService.ts b/server/src/services/usageCheckerService.tsnew file mode 100644index 000000000..9c0334bb5--- /dev/null+++ b/server/src/services/usageCheckerService.ts@@ -0,0 +1,319 @@+import { eq } from "drizzle-orm";+import { DateTime } from "luxon";+import * as cron from "node-cron";+import Stripe from "stripe";+import { processResults } from "../api/analytics/utils.js";+import { clickhouse } from "../db/clickhouse/clickhouse.js";+import { db } from "../db/postgres/postgres.js";+import { organization, sites } from "../db/postgres/schema.js";+import {+ getStripePrices,+ StripePlan,+ DEFAULT_EVENT_LIMIT,+ IS_CLOUD,+} from "../lib/const.js";+import { stripe } from "../lib/stripe.js";++export class UsageCheckerService {+ private sitesOverLimit = new Set<number>();+ private usageCheckTask: cron.ScheduledTask | null = null;++ constructor() {+ this.initializeUsageCheckCron();+ }++ /**+ * Initialize the cron job for checking monthly usage+ */+ private initializeUsageCheckCron() {+ if (IS_CLOUD && process.env.NODE_ENV !== "development") {+ // Schedule the monthly usage checker to run every 30 minutes+ this.usageCheckTask = cron.schedule("*/30 * * * *", async () => {+ try {+ await this.updateOrganizationsMonthlyUsage();+ } catch (error) {+ console.error("[UsageCheckerService] Error during usage check:", error);+ }+ });++ // Run immediately on startup+ this.updateOrganizationsMonthlyUsage();++ console.log(+ "[UsageCheckerService] Monthly usage check cron initialized (runs every 30 minutes)"+ );+ }+ }++ /**+ * Gets the set of site IDs that are over their monthly limit+ */+ public getSitesOverLimit(): Set<number> {+ return this.sitesOverLimit;+ }++ /**+ * Gets the first day of the current month in YYYY-MM-DD format using Luxon+ */+ private getStartOfMonth(): string {+ return DateTime.now().startOf("month").toISODate() as string;+ }++ /**+ * Gets all site IDs for an organization+ */+ private async getSiteIdsForOrganization(+ organizationId: string+ ): Promise<number[]> {+ try {+ const siteRecords = await db+ .select({ siteId: sites.siteId })+ .from(sites)+ .where(eq(sites.organizationId, organizationId));++ return siteRecords.map((record) => record.siteId);+ } catch (error) {+ console.error(+ `Error getting sites for organization ${organizationId}:`,+ error+ );+ return [];+ }+ }++ /**+ * Gets event limit and billing period start date for an organization based on their Stripe subscription.+ * Fetches directly from Stripe if the organization has a stripeCustomerId.+ * @returns [eventLimit, periodStartDate]+ */+ private async getOrganizationSubscriptionInfo(orgData: {+ id: string;+ stripeCustomerId: string | null;+ createdAt: string;+ name: string;+ }): Promise<[number, string | null]> {+ if (orgData.name === "tomato 2") {+ return [Infinity, this.getStartOfMonth()];+ }+ if (!orgData.stripeCustomerId) {+ // No Stripe customer ID, use default limit and start of current month+ return [DEFAULT_EVENT_LIMIT, this.getStartOfMonth()];+ }++ try {+ // Fetch active subscriptions for the customer from Stripe+ const subscriptions = await (stripe as Stripe).subscriptions.list({+ customer: orgData.stripeCustomerId,+ status: "active",+ limit: 1,+ });++ if (subscriptions.data.length === 0) {+ return [DEFAULT_EVENT_LIMIT, this.getStartOfMonth()];+ }++ const subscription = subscriptions.data[0];+ const subscriptionItem = subscription.items.data[0];++ const priceId = subscriptionItem.price.id;++ if (!priceId) {+ console.error(+ `Subscription item price ID not found for organization ${orgData.id}, sub ${subscription.id}`+ );+ return [DEFAULT_EVENT_LIMIT, this.getStartOfMonth()];+ }++ // Find corresponding plan details from constants+ const planDetails = getStripePrices().find(+ (plan: StripePlan) =>+ plan.priceId === priceId ||+ (plan.annualDiscountPriceId && plan.annualDiscountPriceId === priceId)+ );++ // Get the event limit from the plan+ const eventLimit = planDetails+ ? planDetails.limits.events // This is already the monthly event limit, regardless of billing interval+ : DEFAULT_EVENT_LIMIT;++ // For the period start, we need to handle several cases:+ const currentMonthStart = this.getStartOfMonth();+ let periodStart = currentMonthStart;++ if (subscriptionItem.current_period_start) {+ // Convert subscription start timestamp to DateTime+ const subscriptionStartDate = DateTime.fromSeconds(+ subscriptionItem.current_period_start+ );+ const currentMonth = DateTime.now().startOf("month");++ // If the subscription started within the current month, use that as the start date+ // This ensures we don't count events from before they subscribed (e.g., during their free trial)+ if (subscriptionStartDate >= currentMonth) {+ periodStart = subscriptionStartDate.toISODate() as string;+ console.log(+ `[Monthly Usage Checker] Organization ${orgData.name} subscribed during current month on ${periodStart}. Using subscription start date for counting.`+ );+ } else {+ console.log(+ `[Monthly Usage Checker] Organization ${orgData.name} subscription started before current month. Using month start for counting.`+ );+ }+ }++ // Include subscription info for logging purposes+ const interval = subscriptionItem.price.recurring?.interval || "unknown";+ console.log(+ `[Monthly Usage Checker] Organization ${orgData.name} has a ${interval} subscription.`+ );++ return [eventLimit, periodStart];+ } catch (error: any) {+ console.error(+ `Error fetching Stripe subscription info for organization ${orgData.name}:`,+ error+ );+ // Fallback to default limit and current month start on Stripe API error+ return [DEFAULT_EVENT_LIMIT, this.getStartOfMonth()];+ }+ }++ /**+ * Gets monthly pageview count from ClickHouse for the given site IDs+ */+ private async getMonthlyPageviews(+ siteIds: number[],+ startDate: string | null+ ): Promise<number> {+ if (!siteIds.length) {+ return 0;+ }++ // If no startDate is provided (e.g., no subscription), default to start of month+ const periodStart = startDate || this.getStartOfMonth();++ try {+ const result = await clickhouse.query({+ query: `+ SELECT COUNT(*) as count+ FROM events+ WHERE site_id IN (${siteIds.join(",")}) AND type = 'pageview'+ AND timestamp >= toDate('${periodStart}')+ `,+ format: "JSONEachRow",+ });+ const rows = await processResults<{ count: string }>(result);+ return parseInt(rows[0].count, 10);+ } catch (error) {+ console.error(+ `Error querying ClickHouse for pageviews for sites ${siteIds}:`,+ error+ );+ return 0;+ }+ }++ /**+ * Updates monthly event usage for all organizations+ */+ public async updateOrganizationsMonthlyUsage(): Promise<void> {+ console.log(+ "[Monthly Usage Checker] Starting check of monthly event usage for organizations..."+ );++ try {+ // Get all organizations (both with and without Stripe customer IDs)+ const organizations = await db+ .select({+ id: organization.id,+ name: organization.name,+ stripeCustomerId: organization.stripeCustomerId,+ createdAt: organization.createdAt,+ })+ .from(organization);++ for (const orgData of organizations) {+ try {+ // Get site IDs for this organization+ const siteIds = await this.getSiteIdsForOrganization(orgData.id);++ // If organization has no sites, continue to next organization+ if (!siteIds.length) {+ continue;+ }++ // Get organization's subscription information (limit and period start)+ const [eventLimit, periodStart] =+ await this.getOrganizationSubscriptionInfo(orgData);++ // Get monthly pageview count from ClickHouse using the billing period start date+ const pageviewCount = await this.getMonthlyPageviews(siteIds, periodStart);++ // Check if over limit and update global set+ const isOverLimit = pageviewCount > eventLimit;++ // Update organization's monthlyEventCount and overMonthlyLimit fields+ await db+ .update(organization)+ .set({+ monthlyEventCount: pageviewCount,+ overMonthlyLimit: isOverLimit,+ })+ .where(eq(organization.id, orgData.id));++ // If over the limit, add all this organization's sites to the global set+ if (isOverLimit) {+ for (const siteId of siteIds) {+ this.sitesOverLimit.add(siteId);+ }+ console.log(+ `[Monthly Usage Checker] Organization ${orgData.name} is over limit. Added ${siteIds.length} sites to blocked list.`+ );+ } else {+ for (const siteId of siteIds) {+ this.sitesOverLimit.delete(siteId);+ }+ }++ // Format additional date info for logging if available+ const periodInfo = periodStart+ ? `period started ${periodStart}`+ : "this month";++ console.log(+ `[Monthly Usage Checker] Updated organization ${+ orgData.name+ }: ${pageviewCount.toLocaleString()} events, limit ${eventLimit.toLocaleString()}, ${periodInfo}`+ );+ } catch (error) {+ console.error(+ `[Monthly Usage Checker] Error processing organization ${orgData.id}:`,+ error+ );+ }+ }++ console.log(+ `[Monthly Usage Checker] Completed monthly event usage check. ${this.sitesOverLimit.size} sites are over their limit.`+ );+ } catch (error) {+ console.error(+ "[Monthly Usage Checker] Error updating monthly usage:",+ error+ );+ }+ }++ /**+ * Method to stop the usage check cron job (useful for graceful shutdown)+ */+ public stopUsageCheckCron() {+ if (this.usageCheckTask) {+ this.usageCheckTask.stop();+ console.log("[UsageCheckerService] Monthly usage check cron stopped");+ }+ }+}++// Create a singleton instance+export const usageCheckerService = new UsageCheckerService();\ No newline at end of filediff --git a/server/src/tracker/utils.ts b/server/src/tracker/utils.tsindex 0656d415b..cb4d2dba1 100644--- a/server/src/tracker/utils.ts+++ b/server/src/tracker/utils.ts@@ -1,12 +1,10 @@-import crypto from "crypto";import { FastifyRequest } from "fastify";import UAParser, { UAParser as userAgentParser } from "ua-parser-js";import { z } from "zod";-import { sitesOverLimit } from "../cron/monthly-usage-checker.js";-import { siteConfig } from "../lib/siteConfig.js";+import { usageCheckerService } from "../services/usageCheckerService.js";+import { userIdService } from "../services/userId/userIdService.js";import { trackingPayloadSchema } from "./trackEvent.js";import { TrackingPayload } from "./types.js";-import { userIdService } from "../services/userId/userIdService.js";export type TotalTrackingPayload = TrackingPayload & {type?: string;@@ -87,7 +85,7 @@ export function clearSelfReferrer(referrer: string, hostname: string): string {// Check if site is over the monthly limitexport function isSiteOverLimit(siteId: number | string): boolean {- return sitesOverLimit.has(Number(siteId));+ return usageCheckerService.getSitesOverLimit().has(Number(siteId));}// Create base tracking payload from request@@ -126,7 +124,6 @@ export function createBasePayload(};}-// Helper function to get IP addressconst getIpAddress = (request: FastifyRequest): string => {const cfConnectingIp = request.headers["cf-connecting-ip"];