Skip to content

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

Temporarily enable Web Vitals tracking for testing purposes

Bill Yangb2e86fc0906e
diff --git a/server/public/script-full.js b/server/public/script-full.js
index 8274fc9ff..450216fac 100644
--- a/server/public/script-full.js
+++ b/server/public/script-full.js
@@ -47,7 +47,9 @@
scriptTag.getAttribute("data-track-query") !== "false";
const trackOutbound =
scriptTag.getAttribute("data-track-outbound") !== "false";
- const enableWebVitals = scriptTag.getAttribute("data-web-vitals") === "true";
+ // only true temporarily to test web vitals
+ const enableWebVitals = true;
+ // const enableWebVitals = scriptTag.getAttribute("data-web-vitals") === "true";
// Load Web Vitals library dynamically from CDN
const loadWebVitals = () => {
diff --git a/server/public/script.js b/server/public/script.js
index ce63663bf..bc70ec08a 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(window.__RYBBIT_OPTOUT__||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"),l="false"!==t.getAttribute("data-track-query"),s="false"!==t.getAttribute("data-track-outbound"),u="true"===t.getAttribute("data-web-vitals");u&&new Promise((t,e)=>{let r=document.createElement("script");r.src="https://unpkg.com/web-vitals@3/dist/web-vitals.iife.js",r.onload=()=>t(),r.onerror=()=>e(Error("Failed to load web-vitals library")),document.head.appendChild(r)}).then(()=>{C()}).catch(t=>{console.warn("Failed to load web vitals library:",t)});let o=[];try{let c=t.getAttribute("data-skip-patterns");c&&(o=JSON.parse(c),Array.isArray(o)||(o=[]))}catch(p){console.error("Error parsing data-skip-patterns:",p)}let d=[];try{let b=t.getAttribute("data-mask-patterns");b&&(d=JSON.parse(b),Array.isArray(d)||(d=[]))}catch(f){console.error("Error parsing data-mask-patterns:",f)}let g=null;try{let h=localStorage.getItem("rybbit-user-id");h&&(g=h)}catch(y){}function v(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=v(r);if(a.test(t))return r}catch(i){console.error(`Invalid pattern: ${r}`,i)}return null}let w=()=>{let t=new URL(window.location.href),e=t.pathname;if(t.hash&&t.hash.startsWith("#/")&&(e=t.hash.substring(1)),m(e,o))return null;let a=m(e,d);a&&(e=a);let i={site_id:r,hostname:t.hostname,pathname:e,querystring:l?t.search:"",screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,page_title:document.title,referrer:document.referrer};return g&&(i.user_id=g),i},A=t=>{fetch(`${e}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t),mode:"cors",keepalive:!0}).catch(console.error)},I=(t="pageview",e="",r={})=>{if("custom_event"===t&&(!e||"string"!=typeof e)){console.error("Event name is required and must be a string for custom events");return}let a=w();if(!a)return;let i={...a,type:t,event_name:e,properties:"custom_event"===t||"outbound"===t?JSON.stringify(r):void 0};A(i)},k={lcp:null,cls:null,inp:null,fcp:null,ttfb:null},E=!1,S=null,T=()=>{if(E)return;let t=Object.values(k).every(t=>null!==t);t&&$()},$=()=>{if(E)return;E=!0,S&&(clearTimeout(S),S=null);let t=w();if(!t)return;let e={...t,type:"performance",event_name:"web-vitals",lcp:k.lcp,cls:k.cls,inp:k.inp,fcp:k.fcp,ttfb:k.ttfb};A(e)},L=t=>{E||(k[t.name.toLowerCase()]=t.value,T())},C=()=>{if("undefined"!=typeof webVitals&&u)try{webVitals.getLCP(L),webVitals.getCLS(L),webVitals.getINP(L),webVitals.getFCP(L),webVitals.getTTFB(L),S=setTimeout(()=>{E||$()},2e4),window.addEventListener("beforeunload",()=>{E||$()})}catch(t){console.warn("Error initializing web vitals tracking:",t)}},O=()=>I("pageview"),P=a>0?function t(e,r){let a;return(...t)=>{clearTimeout(a),a=setTimeout(()=>e.apply(this,t),r)}}(O,a):O;if(document.addEventListener("click",function(t){let e=t.target;for(;e&&e!==document;){if(e.hasAttribute("data-rybbit-event")){let r=e.getAttribute("data-rybbit-event");if(r){let a={};for(let i of e.attributes)if(i.name.startsWith("data-rybbit-prop-")){let n=i.name.replace("data-rybbit-prop-","");a[n]=i.value}I("custom_event",r,a)}break}e=e.parentElement}if(s){let l=t.target.closest("a");if(!l||!l.href)return;(function t(e){try{let r=window.location.hostname,a=new URL(e).hostname;return a!==r&&""!==a}catch(i){return!1}})(l.href)&&I("outbound","",{url:l.href,text:l.innerText||l.textContent||"",target:l.target||"_self"})}}),n){let U=history.pushState,x=history.replaceState;history.pushState=function(...t){U.apply(this,t),P()},history.replaceState=function(...t){x.apply(this,t),P()},window.addEventListener("popstate",P),window.addEventListener("hashchange",P)}window.rybbit={pageview:O,event:(t,e={})=>I("custom_event",t,e),trackOutbound:(t,e="",r="_self")=>I("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}g=t.trim();try{localStorage.setItem("rybbit-user-id",g)}catch(e){console.warn("Could not persist user ID to localStorage")}},clearUserId(){g=null;try{localStorage.removeItem("rybbit-user-id")}catch(t){}},getUserId:()=>g},i&&O()}();
\ No newline at end of file
+!function(){let t=document.currentScript,e=t.getAttribute("src").split("/script.js")[0];if(window.__RYBBIT_OPTOUT__||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"),l="false"!==t.getAttribute("data-track-query"),s="false"!==t.getAttribute("data-track-outbound");new Promise((t,e)=>{let r=document.createElement("script");r.src="https://unpkg.com/web-vitals@3/dist/web-vitals.iife.js",r.onload=()=>t(),r.onerror=()=>e(Error("Failed to load web-vitals library")),document.head.appendChild(r)}).then(()=>{L()}).catch(t=>{console.warn("Failed to load web vitals library:",t)});let u=[];try{let o=t.getAttribute("data-skip-patterns");o&&(u=JSON.parse(o),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(b){console.error("Error parsing data-mask-patterns:",b)}let f=null;try{let g=localStorage.getItem("rybbit-user-id");g&&(f=g)}catch(h){}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 v(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 m=()=>{let t=new URL(window.location.href),e=t.pathname;if(t.hash&&t.hash.startsWith("#/")&&(e=t.hash.substring(1)),v(e,u))return null;let a=v(e,p);a&&(e=a);let i={site_id:r,hostname:t.hostname,pathname:e,querystring:l?t.search:"",screenWidth:window.innerWidth,screenHeight:window.innerHeight,language:navigator.language,page_title:document.title,referrer:document.referrer};return f&&(i.user_id=f),i},w=t=>{fetch(`${e}/track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t),mode:"cors",keepalive:!0}).catch(console.error)},A=(t="pageview",e="",r={})=>{if("custom_event"===t&&(!e||"string"!=typeof e)){console.error("Event name is required and must be a string for custom events");return}let a=m();if(!a)return;let i={...a,type:t,event_name:e,properties:"custom_event"===t||"outbound"===t?JSON.stringify(r):void 0};w(i)},I={lcp:null,cls:null,inp:null,fcp:null,ttfb:null},k=!1,E=null,S=()=>{if(k)return;let t=Object.values(I).every(t=>null!==t);t&&T()},T=()=>{if(k)return;k=!0,E&&(clearTimeout(E),E=null);let t=m();if(!t)return;let e={...t,type:"performance",event_name:"web-vitals",lcp:I.lcp,cls:I.cls,inp:I.inp,fcp:I.fcp,ttfb:I.ttfb};w(e)},$=t=>{k||(I[t.name.toLowerCase()]=t.value,S())},L=()=>{if("undefined"!=typeof webVitals)try{webVitals.getLCP($),webVitals.getCLS($),webVitals.getINP($),webVitals.getFCP($),webVitals.getTTFB($),E=setTimeout(()=>{k||T()},2e4),window.addEventListener("beforeunload",()=>{k||T()})}catch(t){console.warn("Error initializing web vitals tracking:",t)}},C=()=>A("pageview"),O=a>0?function t(e,r){let a;return(...t)=>{clearTimeout(a),a=setTimeout(()=>e.apply(this,t),r)}}(C,a):C;if(document.addEventListener("click",function(t){let e=t.target;for(;e&&e!==document;){if(e.hasAttribute("data-rybbit-event")){let r=e.getAttribute("data-rybbit-event");if(r){let a={};for(let i of e.attributes)if(i.name.startsWith("data-rybbit-prop-")){let n=i.name.replace("data-rybbit-prop-","");a[n]=i.value}A("custom_event",r,a)}break}e=e.parentElement}if(s){let l=t.target.closest("a");if(!l||!l.href)return;(function t(e){try{let r=window.location.hostname,a=new URL(e).hostname;return a!==r&&""!==a}catch(i){return!1}})(l.href)&&A("outbound","",{url:l.href,text:l.innerText||l.textContent||"",target:l.target||"_self"})}}),n){let P=history.pushState,U=history.replaceState;history.pushState=function(...t){P.apply(this,t),O()},history.replaceState=function(...t){U.apply(this,t),O()},window.addEventListener("popstate",O),window.addEventListener("hashchange",O)}window.rybbit={pageview:C,event:(t,e={})=>A("custom_event",t,e),trackOutbound:(t,e="",r="_self")=>A("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}f=t.trim();try{localStorage.setItem("rybbit-user-id",f)}catch(e){console.warn("Could not persist user ID to localStorage")}},clearUserId(){f=null;try{localStorage.removeItem("rybbit-user-id")}catch(t){}},getUserId:()=>f},i&&C()}();
\ No newline at end of file