🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
feat: add opt-out tracking functionality and update metadata
Bill Yangce96e276b1bf
diff --git a/docs/src/content/_meta.js b/docs/src/content/_meta.jsindex f67f5bacd..244ce95cf 100644--- a/docs/src/content/_meta.js+++ b/docs/src/content/_meta.js@@ -9,6 +9,7 @@ export default {"track-events": "",sdks: "SDKs",integrations: "",+ "hiding-own-traffic": "",_4: {type: "separator",title: "Self-hosting",diff --git a/docs/src/content/hiding-own-traffic.mdx b/docs/src/content/hiding-own-traffic.mdxnew file mode 100644index 000000000..4a8c3973a--- /dev/null+++ b/docs/src/content/hiding-own-traffic.mdx@@ -0,0 +1,35 @@+import { Steps } from 'nextra/components'++# Hiding Your Own Traffic++You can disable Rybbit tracking in your browser using the localStorage API.++<Steps>++### 1. Open your browser's developer console++Press **F12** or right-click the page and select "Inspect"++Navigate to the "Console" tab++### 2. Disable tracking++Enter this command to disable tracking:++```js+localStorage.setItem('disable-rybbit', 'true');+```++### 3. Re-enable tracking (when needed)++If you want to re-enable tracking later, enter:++```js+localStorage.removeItem('disable-rybbit');+```++</Steps>++This method is useful for developers testing the site or site owners who don't want to track their own visits.++Once disabled, the Rybbit script will not send any tracking data while preserving the API functions to prevent JavaScript errors.diff --git a/server/public/script-full.js b/server/public/script-full.jsindex 21542288b..58437b657 100644--- a/server/public/script-full.js+++ b/server/public/script-full.js@@ -3,6 +3,17 @@const scriptTag = document.currentScript;const ANALYTICS_HOST = scriptTag.getAttribute("src").split("/script.js")[0];+ // Check if the user has opted out of tracking via localStorage+ if (localStorage.getItem("disable-rybbit") !== null) {+ // Create a no-op implementation to ensure the API still works+ window.rybbit = {+ pageview: () => {},+ event: () => {},+ trackOutbound: () => {},+ };+ return;+ }+if (!ANALYTICS_HOST) {console.error("Please provide a valid analytics host");return;diff --git a/server/public/script.js b/server/public/script.jsindex d77e88c32..7f300b858 100644--- a/server/public/script.js+++ b/server/public/script.js@@ -1 +1 @@-!function(){let t=document.currentScript,e=t.getAttribute("src").split("/script.js")[0];if(!e){console.error("Please provide a valid analytics host");return}let r=t.getAttribute("data-site-id")||t.getAttribute("site-id");if(!r||isNaN(Number(r))){console.error("Please provide a valid site ID using the data-site-id attribute");return}let a=t.getAttribute("data-debounce")?Math.max(0,parseInt(t.getAttribute("data-debounce"))):500,n="false"!==t.getAttribute("data-auto-track-pageview"),i="false"!==t.getAttribute("data-track-spa"),s="false"!==t.getAttribute("data-track-query"),o="false"!==t.getAttribute("data-track-outbound"),u=[];try{let c=t.getAttribute("data-skip-patterns");c&&(u=JSON.parse(c),Array.isArray(u)||(u=[]))}catch(l){console.error("Error parsing data-skip-patterns:",l)}let p=[];try{let d=t.getAttribute("data-mask-patterns");d&&(p=JSON.parse(d),Array.isArray(p)||(p=[]))}catch(g){console.error("Error parsing data-mask-patterns:",g)}function h(t){let e="__DOUBLE_ASTERISK_TOKEN__",r="__SINGLE_ASTERISK_TOKEN__",a=t.replace(/\*\*/g,e).replace(/\*/g,r).replace(/[.+?^${}()|[\]\\]/g,"\\$&"),n=(a=a.replace(/\//g,"\\/")).replace(RegExp(e,"g"),".*").replace(RegExp(r,"g"),"[^/]+");return RegExp("^"+n+"$")}function f(t,e){for(let r of e)try{let a=h(r);if(a.test(t))return r}catch(n){console.error(`Invalid pattern: ${r}`,n)}return null}let b=(t="pageview",a="",n={})=>{if("custom_event"===t&&(!a||"string"!=typeof a)){console.error("Event name is required and must be a string for custom events");return}let i=new URL(window.location.href),o=i.pathname;if(i.hash&&i.hash.startsWith("#/")&&(o=i.hash.substring(1)),f(o,u))return;let c=f(o,p);c&&(o=c);let l={site_id:r,hostname:i.hostname,pathname:o,querystring:s?i.search:"",screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,page_title:document.title,referrer:document.referrer,type:t,event_name:a,properties:"custom_event"===t||"outbound"===t?JSON.stringify(n):void 0};fetch(`${e}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(l),mode:"cors",keepalive:!0}).catch(console.error)},v=()=>b("pageview"),y=a>0?function t(e,r){let a;return(...t)=>{clearTimeout(a),a=setTimeout(()=>e.apply(this,t),r)}}(v,a):v;if(o&&document.addEventListener("click",function(t){let e=t.target.closest("a");e&&e.href&&function t(e){try{let r=window.location.hostname,a=new URL(e).hostname;return a!==r&&""!==a}catch(n){return!1}}(e.href)&&b("outbound","",{url:e.href,text:e.innerText||e.textContent||"",target:e.target||"_self"})}),i){let m=history.pushState,A=history.replaceState;history.pushState=function(...t){m.apply(this,t),y()},history.replaceState=function(...t){A.apply(this,t),y()},window.addEventListener("popstate",y),window.addEventListener("hashchange",y)}window.rybbit={pageview:v,event:(t,e={})=>b("custom_event",t,e),trackOutbound:(t,e="",r="_self")=>b("outbound","",{url:t,text:e,target:r})},n&&v()}();\ No newline at end of file+!function(){let t=document.currentScript,e=t.getAttribute("src").split("/script.js")[0];if(null!==localStorage.getItem("disable-rybbit")){window.rybbit={pageview(){},event(){},trackOutbound(){}};return}if(!e){console.error("Please provide a valid analytics host");return}let r=t.getAttribute("data-site-id")||t.getAttribute("site-id");if(!r||isNaN(Number(r))){console.error("Please provide a valid site ID using the data-site-id attribute");return}let a=t.getAttribute("data-debounce")?Math.max(0,parseInt(t.getAttribute("data-debounce"))):500,n="false"!==t.getAttribute("data-auto-track-pageview"),i="false"!==t.getAttribute("data-track-spa"),s="false"!==t.getAttribute("data-track-query"),u="false"!==t.getAttribute("data-track-outbound"),o=[];try{let l=t.getAttribute("data-skip-patterns");l&&(o=JSON.parse(l),Array.isArray(o)||(o=[]))}catch(c){console.error("Error parsing data-skip-patterns:",c)}let p=[];try{let d=t.getAttribute("data-mask-patterns");d&&(p=JSON.parse(d),Array.isArray(p)||(p=[]))}catch(g){console.error("Error parsing data-mask-patterns:",g)}function h(t){let e="__DOUBLE_ASTERISK_TOKEN__",r="__SINGLE_ASTERISK_TOKEN__",a=t.replace(/\*\*/g,e).replace(/\*/g,r).replace(/[.+?^${}()|[\]\\]/g,"\\$&"),n=(a=a.replace(/\//g,"\\/")).replace(RegExp(e,"g"),".*").replace(RegExp(r,"g"),"[^/]+");return RegExp("^"+n+"$")}function f(t,e){for(let r of e)try{let a=h(r);if(a.test(t))return r}catch(n){console.error(`Invalid pattern: ${r}`,n)}return null}let b=(t="pageview",a="",n={})=>{if("custom_event"===t&&(!a||"string"!=typeof a)){console.error("Event name is required and must be a string for custom events");return}let i=new URL(window.location.href),u=i.pathname;if(i.hash&&i.hash.startsWith("#/")&&(u=i.hash.substring(1)),f(u,o))return;let l=f(u,p);l&&(u=l);let c={site_id:r,hostname:i.hostname,pathname:u,querystring:s?i.search:"",screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,page_title:document.title,referrer:document.referrer,type:t,event_name:a,properties:"custom_event"===t||"outbound"===t?JSON.stringify(n):void 0};fetch(`${e}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c),mode:"cors",keepalive:!0}).catch(console.error)},v=()=>b("pageview"),y=a>0?function t(e,r){let a;return(...t)=>{clearTimeout(a),a=setTimeout(()=>e.apply(this,t),r)}}(v,a):v;if(u&&document.addEventListener("click",function(t){let e=t.target.closest("a");e&&e.href&&function t(e){try{let r=window.location.hostname,a=new URL(e).hostname;return a!==r&&""!==a}catch(n){return!1}}(e.href)&&b("outbound","",{url:e.href,text:e.innerText||e.textContent||"",target:e.target||"_self"})}),i){let m=history.pushState,A=history.replaceState;history.pushState=function(...t){m.apply(this,t),y()},history.replaceState=function(...t){A.apply(this,t),y()},window.addEventListener("popstate",y),window.addEventListener("hashchange",y)}window.rybbit={pageview:v,event:(t,e={})=>b("custom_event",t,e),trackOutbound:(t,e="",r="_self")=>b("outbound","",{url:t,text:e,target:r})},n&&v()}();\ No newline at end of file