Skip to content

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

fix: update NoData component link and enhance script for hash-based routing (#281)

Bill Yangac6b1adf4e3c
diff --git a/client/src/app/[site]/components/Header/NoData.tsx b/client/src/app/[site]/components/Header/NoData.tsx
index 8c89c445e..01d6ba67c 100644
--- a/client/src/app/[site]/components/Header/NoData.tsx
+++ b/client/src/app/[site]/components/Header/NoData.tsx
@@ -34,7 +34,7 @@ export function NoData() {
<span className="text-xs text-muted-foreground">
See our{" "}
<a
- href="https://rybbit.io/docs"
+ href="https://rybbit.io/docs/script"
className="text-blue-500 hover:underline"
>
docs
diff --git a/server/public/script-full.js b/server/public/script-full.js
index 0b18679d4..21542288b 100644
--- a/server/public/script-full.js
+++ b/server/public/script-full.js
@@ -22,13 +22,13 @@
? Math.max(0, parseInt(scriptTag.getAttribute("data-debounce")))
: 500;
+ const autoTrackPageview =
+ scriptTag.getAttribute("data-auto-track-pageview") !== "false";
const autoTrackSpa = scriptTag.getAttribute("data-track-spa") !== "false";
const trackQuerystring =
scriptTag.getAttribute("data-track-query") !== "false";
const trackOutbound =
scriptTag.getAttribute("data-track-outbound") !== "false";
- const autoTrackPageview =
- scriptTag.getAttribute("data-auto-track-pageview") !== "false";
let skipPatterns = [];
try {
@@ -124,6 +124,12 @@
const url = new URL(window.location.href);
let pathname = url.pathname;
+ // Always handle hash-based SPA routing
+ if (url.hash && url.hash.startsWith("#/")) {
+ // For #/path format, replace pathname with just /path
+ pathname = url.hash.substring(1);
+ }
+
if (findMatchingPattern(pathname, skipPatterns)) {
return;
}
@@ -200,6 +206,8 @@
};
window.addEventListener("popstate", debouncedTrackPageview);
+ // Always listen for hashchange events for hash-based routing
+ window.addEventListener("hashchange", debouncedTrackPageview);
}
window.rybbit = {
diff --git a/server/public/script.js b/server/public/script.js
index a96f3d955..d77e88c32 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-track-spa"),i="false"!==t.getAttribute("data-track-query"),s="false"!==t.getAttribute("data-track-outbound"),o="false"!==t.getAttribute("data-auto-track-pageview"),u=[];try{let l=t.getAttribute("data-skip-patterns");l&&(u=JSON.parse(l),Array.isArray(u)||(u=[]))}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 f(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 h(t,e){for(let r of e)try{let a=f(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 s=new URL(window.location.href),o=s.pathname;if(h(o,u))return;let l=h(o,p);l&&(o=l);let c={site_id:r,hostname:s.hostname,pathname:o,querystring:i?s.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(s&&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"})}),n){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.rybbit={pageview:v,event:(t,e={})=>b("custom_event",t,e),trackOutbound:(t,e="",r="_self")=>b("outbound","",{url:t,text:e,target:r})},o&&v()}();
\ No newline at end of file
+!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