Skip to content

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

fix(tracking): conditionally add user_id to tracking payload

Bill Yang666ff9bfb2c7
diff --git a/server/public/script-full.js b/server/public/script-full.js
index 4d914e841..3003d6db0 100644
--- a/server/public/script-full.js
+++ b/server/public/script-full.js
@@ -182,10 +182,13 @@
eventType === "custom_event" || eventType === "outbound"
? JSON.stringify(properties)
: undefined,
- // Add custom user ID if available
- user_id: customUserId,
};
+ // Add custom user ID only if it's set
+ if (customUserId) {
+ payload.user_id = customUserId;
+ }
+
fetch(`${ANALYTICS_HOST}/track`, {
method: "POST",
headers: {
diff --git a/server/public/script.js b/server/public/script.js
index 097c4a1c6..db8b4e9a1 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(null!==localStorage.getItem("disable-rybbit")){window.rybbit={pageview(){},event(){},trackOutbound(){},identify(){},clearUserId(){},getUserId:()=>null};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,i="false"!==t.getAttribute("data-auto-track-pageview"),n="false"!==t.getAttribute("data-track-spa"),s="false"!==t.getAttribute("data-track-query"),u="false"!==t.getAttribute("data-track-outbound"),l=[];try{let o=t.getAttribute("data-skip-patterns");o&&(l=JSON.parse(o),Array.isArray(l)||(l=[]))}catch(c){console.error("Error parsing data-skip-patterns:",c)}let d=[];try{let p=t.getAttribute("data-mask-patterns");p&&(d=JSON.parse(p),Array.isArray(d)||(d=[]))}catch(g){console.error("Error parsing data-mask-patterns:",g)}let h=null;try{let b=localStorage.getItem("rybbit-user-id");b&&(h=b)}catch(f){}function y(t){let e="__DOUBLE_ASTERISK_TOKEN__",r="__SINGLE_ASTERISK_TOKEN__",a=t.replace(/\*\*/g,e).replace(/\*/g,r).replace(/[.+?^${}()|[\]\\]/g,"\\$&"),i=(a=a.replace(/\//g,"\\/")).replace(RegExp(e,"g"),".*").replace(RegExp(r,"g"),"[^/]+");return RegExp("^"+i+"$")}function m(t,e){for(let r of e)try{let a=y(r);if(a.test(t))return r}catch(i){console.error(`Invalid pattern: ${r}`,i)}return null}let v=(t="pageview",a="",i={})=>{if("custom_event"===t&&(!a||"string"!=typeof a)){console.error("Event name is required and must be a string for custom events");return}let n=new URL(window.location.href),u=n.pathname;if(n.hash&&n.hash.startsWith("#/")&&(u=n.hash.substring(1)),m(u,l))return;let o=m(u,d);o&&(u=o);let c={site_id:r,hostname:n.hostname,pathname:u,querystring:s?n.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(i):void 0,user_id:h};fetch(`${e}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c),mode:"cors",keepalive:!0}).catch(console.error)},A=()=>v("pageview"),I=a>0?function t(e,r){let a;return(...t)=>{clearTimeout(a),a=setTimeout(()=>e.apply(this,t),r)}}(A,a):A;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(i){return!1}}(e.href)&&v("outbound","",{url:e.href,text:e.innerText||e.textContent||"",target:e.target||"_self"})}),n){let k=history.pushState,E=history.replaceState;history.pushState=function(...t){k.apply(this,t),I()},history.replaceState=function(...t){E.apply(this,t),I()},window.addEventListener("popstate",I),window.addEventListener("hashchange",I)}window.rybbit={pageview:A,event:(t,e={})=>v("custom_event",t,e),trackOutbound:(t,e="",r="_self")=>v("outbound","",{url:t,text:e,target:r}),identify(t){if("string"!=typeof t||""===t.trim()){console.error("User ID must be a non-empty string");return}h=t.trim();try{localStorage.setItem("rybbit-user-id",h)}catch(e){console.warn("Could not persist user ID to localStorage")}},clearUserId(){h=null;try{localStorage.removeItem("rybbit-user-id")}catch(t){}},getUserId:()=>h},i&&A()}();
\ 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(){},identify(){},clearUserId(){},getUserId:()=>null};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,i="false"!==t.getAttribute("data-auto-track-pageview"),n="false"!==t.getAttribute("data-track-spa"),s="false"!==t.getAttribute("data-track-query"),u="false"!==t.getAttribute("data-track-outbound"),l=[];try{let o=t.getAttribute("data-skip-patterns");o&&(l=JSON.parse(o),Array.isArray(l)||(l=[]))}catch(c){console.error("Error parsing data-skip-patterns:",c)}let d=[];try{let p=t.getAttribute("data-mask-patterns");p&&(d=JSON.parse(p),Array.isArray(d)||(d=[]))}catch(g){console.error("Error parsing data-mask-patterns:",g)}let h=null;try{let b=localStorage.getItem("rybbit-user-id");b&&(h=b)}catch(f){}function y(t){let e="__DOUBLE_ASTERISK_TOKEN__",r="__SINGLE_ASTERISK_TOKEN__",a=t.replace(/\*\*/g,e).replace(/\*/g,r).replace(/[.+?^${}()|[\]\\]/g,"\\$&"),i=(a=a.replace(/\//g,"\\/")).replace(RegExp(e,"g"),".*").replace(RegExp(r,"g"),"[^/]+");return RegExp("^"+i+"$")}function m(t,e){for(let r of e)try{let a=y(r);if(a.test(t))return r}catch(i){console.error(`Invalid pattern: ${r}`,i)}return null}let v=(t="pageview",a="",i={})=>{if("custom_event"===t&&(!a||"string"!=typeof a)){console.error("Event name is required and must be a string for custom events");return}let n=new URL(window.location.href),u=n.pathname;if(n.hash&&n.hash.startsWith("#/")&&(u=n.hash.substring(1)),m(u,l))return;let o=m(u,d);o&&(u=o);let c={site_id:r,hostname:n.hostname,pathname:u,querystring:s?n.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(i):void 0};h&&(c.user_id=h),fetch(`${e}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c),mode:"cors",keepalive:!0}).catch(console.error)},A=()=>v("pageview"),I=a>0?function t(e,r){let a;return(...t)=>{clearTimeout(a),a=setTimeout(()=>e.apply(this,t),r)}}(A,a):A;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(i){return!1}}(e.href)&&v("outbound","",{url:e.href,text:e.innerText||e.textContent||"",target:e.target||"_self"})}),n){let k=history.pushState,E=history.replaceState;history.pushState=function(...t){k.apply(this,t),I()},history.replaceState=function(...t){E.apply(this,t),I()},window.addEventListener("popstate",I),window.addEventListener("hashchange",I)}window.rybbit={pageview:A,event:(t,e={})=>v("custom_event",t,e),trackOutbound:(t,e="",r="_self")=>v("outbound","",{url:t,text:e,target:r}),identify(t){if("string"!=typeof t||""===t.trim()){console.error("User ID must be a non-empty string");return}h=t.trim();try{localStorage.setItem("rybbit-user-id",h)}catch(e){console.warn("Could not persist user ID to localStorage")}},clearUserId(){h=null;try{localStorage.removeItem("rybbit-user-id")}catch(t){}},getUserId:()=>h},i&&A()}();
\ No newline at end of file