Skip to content

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

fix script

Bill Yangf4114c373ece
diff --git a/server/public/script-full.js b/server/public/script-full.js
index 51e59faff..7253b7841 100644
--- a/server/public/script-full.js
+++ b/server/public/script-full.js
@@ -26,7 +26,10 @@
});
// Check if the user has opted out of tracking
- if (!window.__RYBBIT_OPTOUT__ || localStorage.getItem("disable-rybbit") !== null) {
+ if (
+ !!window.__RYBBIT_OPTOUT__ ||
+ localStorage.getItem("disable-rybbit") !== null
+ ) {
// Create a no-op implementation to ensure the API still works
window.rybbit = {
pageview: () => {},
@@ -368,7 +371,7 @@
}
target = target.parentElement;
}
-
+
// Then check for outbound links
if (trackOutbound) {
const link = e.target.closest("a");
diff --git a/server/public/script.js b/server/public/script.js
index c5c487f08..20532aa0e 100644
--- a/server/public/script.js
+++ b/server/public/script.js
@@ -1 +1 @@
-!function(){const t=document.currentScript,e=t.getAttribute("src").split("/script.js")[0];if(new Promise(((t,e)=>{const r=document.createElement("script");r.src="https://unpkg.com/web-vitals@3/dist/web-vitals.iife.js",r.onload=()=>t(),r.onerror=()=>e(new Error("Failed to load web-vitals library")),document.head.appendChild(r)})).then((()=>{_()})).catch((t=>{console.warn("Failed to load web vitals library:",t)})),!window.__RYBBIT_OPTOUT__||null!==localStorage.getItem("disable-rybbit"))return void(window.rybbit={pageview:()=>{},event:()=>{},trackOutbound:()=>{},identify:()=>{},clearUserId:()=>{},getUserId:()=>null});if(!e)return void console.error("Please provide a valid analytics host");const r=t.getAttribute("data-site-id")||t.getAttribute("site-id");if(!r||isNaN(Number(r)))return void console.error("Please provide a valid site ID using the data-site-id attribute");const n=t.getAttribute("data-debounce")?Math.max(0,parseInt(t.getAttribute("data-debounce"))):500,a="false"!==t.getAttribute("data-auto-track-pageview"),i="false"!==t.getAttribute("data-track-spa"),o="false"!==t.getAttribute("data-track-query"),s="false"!==t.getAttribute("data-track-outbound");let c=[];try{const e=t.getAttribute("data-skip-patterns");e&&(c=JSON.parse(e),Array.isArray(c)||(c=[]))}catch(t){console.error("Error parsing data-skip-patterns:",t)}let l=[];try{const e=t.getAttribute("data-mask-patterns");e&&(l=JSON.parse(e),Array.isArray(l)||(l=[]))}catch(t){console.error("Error parsing data-mask-patterns:",t)}let u=null;try{const t=localStorage.getItem("rybbit-user-id");t&&(u=t)}catch(t){}function d(t){const e="__DOUBLE_ASTERISK_TOKEN__",r="__SINGLE_ASTERISK_TOKEN__";let n=t.replace(/\*\*/g,e).replace(/\*/g,r).replace(/[.+?^${}()|[\]\\]/g,"\\$&");n=n.replace(/\//g,"\\/");let a=n.replace(new RegExp(e,"g"),".*").replace(new RegExp(r,"g"),"[^/]+");return new RegExp("^"+a+"$")}function p(t,e){for(const r of e)try{if(d(r).test(t))return r}catch(t){console.error(`Invalid pattern: ${r}`,t)}return null}const b=()=>{const t=new URL(window.location.href);let e=t.pathname;if(t.hash&&t.hash.startsWith("#/")&&(e=t.hash.substring(1)),p(e,c))return null;const n=p(e,l);n&&(e=n);const a={site_id:r,hostname:t.hostname,pathname:e,querystring:o?t.search:"",screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,page_title:document.title,referrer:document.referrer};return u&&(a.user_id=u),a},g=t=>{fetch(`${e}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t),mode:"cors",keepalive:!0}).catch(console.error)},f=(t="pageview",e="",r={})=>{if("custom_event"===t&&(!e||"string"!=typeof e))return void console.error("Event name is required and must be a string for custom events");const n=b();if(!n)return;const a={...n,type:t,event_name:e,properties:"custom_event"===t||"outbound"===t?JSON.stringify(r):void 0};g(a)};let h={lcp:null,cls:null,inp:null,fcp:null,ttfb:null},m=!1,w=null;const y=()=>{if(m)return;m=!0,w&&(clearTimeout(w),w=null);const t=b();if(!t)return;const e={...t,type:"performance",event_name:"web-vitals",lcp:h.lcp,cls:h.cls,inp:h.inp,fcp:h.fcp,ttfb:h.ttfb};g(e)},v=t=>{m||(h[t.name.toLowerCase()]=t.value,(()=>{if(m)return;Object.values(h).every((t=>null!==t))&&y()})())},_=()=>{if("undefined"!=typeof webVitals)try{webVitals.getLCP(v),webVitals.getCLS(v),webVitals.getINP(v),webVitals.getFCP(v),webVitals.getTTFB(v),w=setTimeout((()=>{m||y()}),2e4),window.addEventListener("beforeunload",(()=>{m||y()}))}catch(t){console.warn("Error initializing web vitals tracking:",t)}},S=()=>f("pageview"),E=n>0?function(t,e){let r;return(...n)=>{clearTimeout(r),r=setTimeout((()=>t.apply(this,n)),e)}}(S,n):S;if(document.addEventListener("click",(function(t){let e=t.target;for(;e&&e!==document;){if(e.hasAttribute("data-rybbit-event")){const t=e.getAttribute("data-rybbit-event");if(t){const r={};for(const t of e.attributes)if(t.name.startsWith("data-rybbit-prop-")){r[t.name.replace("data-rybbit-prop-","")]=t.value}f("custom_event",t,r)}break}e=e.parentElement}if(s){const e=t.target.closest("a");if(!e||!e.href)return;(function(t){try{const e=window.location.hostname,r=new URL(t).hostname;return r!==e&&""!==r}catch(t){return!1}})(e.href)&&f("outbound","",{url:e.href,text:e.innerText||e.textContent||"",target:e.target||"_self"})}})),i){const t=history.pushState,e=history.replaceState;history.pushState=function(...e){t.apply(this,e),E()},history.replaceState=function(...t){e.apply(this,t),E()},window.addEventListener("popstate",E),window.addEventListener("hashchange",E)}window.rybbit={pageview:S,event:(t,e={})=>f("custom_event",t,e),trackOutbound:(t,e="",r="_self")=>f("outbound","",{url:t,text:e,target:r}),identify:t=>{if("string"==typeof t&&""!==t.trim()){u=t.trim();try{localStorage.setItem("rybbit-user-id",u)}catch(t){console.warn("Could not persist user ID to localStorage")}}else console.error("User ID must be a non-empty string")},clearUserId:()=>{u=null;try{localStorage.removeItem("rybbit-user-id")}catch(t){}},getUserId:()=>u},a&&S()}();
\ No newline at end of file
+!function(){const t=document.currentScript,e=t.getAttribute("src").split("/script.js")[0];if(new Promise(((t,e)=>{const r=document.createElement("script");r.src="https://unpkg.com/web-vitals@3/dist/web-vitals.iife.js",r.onload=()=>t(),r.onerror=()=>e(new Error("Failed to load web-vitals library")),document.head.appendChild(r)})).then((()=>{_()})).catch((t=>{console.warn("Failed to load web vitals library:",t)})),!!window.__RYBBIT_OPTOUT__||null!==localStorage.getItem("disable-rybbit"))return void(window.rybbit={pageview:()=>{},event:()=>{},trackOutbound:()=>{},identify:()=>{},clearUserId:()=>{},getUserId:()=>null});if(!e)return void console.error("Please provide a valid analytics host");const r=t.getAttribute("data-site-id")||t.getAttribute("site-id");if(!r||isNaN(Number(r)))return void console.error("Please provide a valid site ID using the data-site-id attribute");const n=t.getAttribute("data-debounce")?Math.max(0,parseInt(t.getAttribute("data-debounce"))):500,a="false"!==t.getAttribute("data-auto-track-pageview"),i="false"!==t.getAttribute("data-track-spa"),o="false"!==t.getAttribute("data-track-query"),s="false"!==t.getAttribute("data-track-outbound");let c=[];try{const e=t.getAttribute("data-skip-patterns");e&&(c=JSON.parse(e),Array.isArray(c)||(c=[]))}catch(t){console.error("Error parsing data-skip-patterns:",t)}let l=[];try{const e=t.getAttribute("data-mask-patterns");e&&(l=JSON.parse(e),Array.isArray(l)||(l=[]))}catch(t){console.error("Error parsing data-mask-patterns:",t)}let u=null;try{const t=localStorage.getItem("rybbit-user-id");t&&(u=t)}catch(t){}function d(t){const e="__DOUBLE_ASTERISK_TOKEN__",r="__SINGLE_ASTERISK_TOKEN__";let n=t.replace(/\*\*/g,e).replace(/\*/g,r).replace(/[.+?^${}()|[\]\\]/g,"\\$&");n=n.replace(/\//g,"\\/");let a=n.replace(new RegExp(e,"g"),".*").replace(new RegExp(r,"g"),"[^/]+");return new RegExp("^"+a+"$")}function p(t,e){for(const r of e)try{if(d(r).test(t))return r}catch(t){console.error(`Invalid pattern: ${r}`,t)}return null}const b=()=>{const t=new URL(window.location.href);let e=t.pathname;if(t.hash&&t.hash.startsWith("#/")&&(e=t.hash.substring(1)),p(e,c))return null;const n=p(e,l);n&&(e=n);const a={site_id:r,hostname:t.hostname,pathname:e,querystring:o?t.search:"",screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,page_title:document.title,referrer:document.referrer};return u&&(a.user_id=u),a},g=t=>{fetch(`${e}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t),mode:"cors",keepalive:!0}).catch(console.error)},f=(t="pageview",e="",r={})=>{if("custom_event"===t&&(!e||"string"!=typeof e))return void console.error("Event name is required and must be a string for custom events");const n=b();if(!n)return;const a={...n,type:t,event_name:e,properties:"custom_event"===t||"outbound"===t?JSON.stringify(r):void 0};g(a)};let h={lcp:null,cls:null,inp:null,fcp:null,ttfb:null},m=!1,w=null;const y=()=>{if(m)return;m=!0,w&&(clearTimeout(w),w=null);const t=b();if(!t)return;const e={...t,type:"performance",event_name:"web-vitals",lcp:h.lcp,cls:h.cls,inp:h.inp,fcp:h.fcp,ttfb:h.ttfb};g(e)},v=t=>{m||(h[t.name.toLowerCase()]=t.value,(()=>{if(m)return;Object.values(h).every((t=>null!==t))&&y()})())},_=()=>{if("undefined"!=typeof webVitals)try{webVitals.getLCP(v),webVitals.getCLS(v),webVitals.getINP(v),webVitals.getFCP(v),webVitals.getTTFB(v),w=setTimeout((()=>{m||y()}),2e4),window.addEventListener("beforeunload",(()=>{m||y()}))}catch(t){console.warn("Error initializing web vitals tracking:",t)}},S=()=>f("pageview"),E=n>0?function(t,e){let r;return(...n)=>{clearTimeout(r),r=setTimeout((()=>t.apply(this,n)),e)}}(S,n):S;if(document.addEventListener("click",(function(t){let e=t.target;for(;e&&e!==document;){if(e.hasAttribute("data-rybbit-event")){const t=e.getAttribute("data-rybbit-event");if(t){const r={};for(const t of e.attributes)if(t.name.startsWith("data-rybbit-prop-")){r[t.name.replace("data-rybbit-prop-","")]=t.value}f("custom_event",t,r)}break}e=e.parentElement}if(s){const e=t.target.closest("a");if(!e||!e.href)return;(function(t){try{const e=window.location.hostname,r=new URL(t).hostname;return r!==e&&""!==r}catch(t){return!1}})(e.href)&&f("outbound","",{url:e.href,text:e.innerText||e.textContent||"",target:e.target||"_self"})}})),i){const t=history.pushState,e=history.replaceState;history.pushState=function(...e){t.apply(this,e),E()},history.replaceState=function(...t){e.apply(this,t),E()},window.addEventListener("popstate",E),window.addEventListener("hashchange",E)}window.rybbit={pageview:S,event:(t,e={})=>f("custom_event",t,e),trackOutbound:(t,e="",r="_self")=>f("outbound","",{url:t,text:e,target:r}),identify:t=>{if("string"==typeof t&&""!==t.trim()){u=t.trim();try{localStorage.setItem("rybbit-user-id",u)}catch(t){console.warn("Could not persist user ID to localStorage")}}else console.error("User ID must be a non-empty string")},clearUserId:()=>{u=null;try{localStorage.removeItem("rybbit-user-id")}catch(t){}},getUserId:()=>u},a&&S()}();
\ No newline at end of file