From b160dfe699629d913c7d8e4f813ee985a69a0b9f Mon Sep 17 00:00:00 2001 From: not-matthias <26800596+not-matthias@users.noreply.github.com> Date: Tue, 24 Dec 2024 12:30:04 +0100 Subject: [PATCH] feat: add umami analytics --- config.toml | 7 +++++++ static/js/imamu.js | 2 ++ templates/partials/head.html | 15 +++------------ 3 files changed, 12 insertions(+), 12 deletions(-) create mode 100644 static/js/imamu.js diff --git a/config.toml b/config.toml index 06d4f2b..8b4ffaf 100644 --- a/config.toml +++ b/config.toml @@ -13,3 +13,10 @@ menu = [ { name = "/linz", url = "/linz", weight = 1 }, { name = "/about", url = "/about", weight = 2 }, ] + +# See https://github.com/not-matthias/apollo +[extra.analytics] +enabled = true + +[extra.analytics.umami] +website_id = "4d764d5e-6d26-4286-b72a-a31637f3cdd5" diff --git a/static/js/imamu.js b/static/js/imamu.js new file mode 100644 index 0000000..17ba0c3 --- /dev/null +++ b/static/js/imamu.js @@ -0,0 +1,2 @@ +// https://cloud.umami.is/script.js +!function(){"use strict";(t=>{const{screen:{width:e,height:a},navigator:{language:r},location:n,document:i,history:c}=t,{hostname:s,href:o,origin:u}=n,{currentScript:l,referrer:d}=i,h=o.startsWith("data:")?void 0:t.localStorage;if(!l)return;const m="data-",f=l.getAttribute.bind(l),p=f(m+"website-id"),g=f(m+"host-url"),y=f(m+"tag"),b="false"!==f(m+"auto-track"),v="true"===f(m+"exclude-search"),w=f(m+"domains")||"",S=w.split(",").map((t=>t.trim())),N=`${(g||"https://api-gateway.umami.dev"||l.src.split("/").slice(0,-1).join("/")).replace(/\/$/,"")}/api/send`,T=`${e}x${a}`,A=/data-umami-event-([\w-_]+)/,x=m+"umami-event",O=300,U=t=>{if(t){try{const e=decodeURI(t);if(e!==t)return e}catch(e){return t}return encodeURI(t)}},j=t=>{try{const{pathname:e,search:a,hash:r}=new URL(t,n.href);t=e+a+r}catch(t){}return v?t.split("?")[0]:t},k=()=>({website:p,hostname:s,screen:T,language:r,title:U(q),url:U(W),referrer:U(_),tag:y||void 0}),E=(t,e,a)=>{a&&(_=W,W=j(a.toString()),W!==_&&setTimeout(K,O))},L=()=>!p||h&&h.getItem("umami.disabled")||w&&!S.includes(s),$=async(t,e="event")=>{if(L())return;const a={"Content-Type":"application/json"};void 0!==B&&(a["x-umami-cache"]=B);try{const r=await fetch(N,{method:"POST",body:JSON.stringify({type:e,payload:t}),headers:a}),n=await r.text();return B=n}catch(t){}},I=()=>{D||(K(),(()=>{const t=(t,e,a)=>{const r=t[e];return(...e)=>(a.apply(null,e),r.apply(t,e))};c.pushState=t(c,"pushState",E),c.replaceState=t(c,"replaceState",E)})(),(()=>{const t=new MutationObserver((([t])=>{q=t&&t.target?t.target.text:void 0})),e=i.querySelector("head > title");e&&t.observe(e,{subtree:!0,characterData:!0,childList:!0})})(),i.addEventListener("click",(async t=>{const e=t=>["BUTTON","A"].includes(t),a=async t=>{const e=t.getAttribute.bind(t),a=e(x);if(a){const r={};return t.getAttributeNames().forEach((t=>{const a=t.match(A);a&&(r[a[1]]=e(t))})),K(a,r)}},r=t.target,i=e(r.tagName)?r:((t,a)=>{let r=t;for(let t=0;t{s||(n.href=e)}))}else if("BUTTON"===i.tagName)return a(i)}}),!0),D=!0)},K=(t,e)=>$("string"==typeof t?{...k(),name:t,data:"object"==typeof e?e:void 0}:"object"==typeof t?t:"function"==typeof t?t(k()):k()),R=t=>$({...k(),data:t},"identify");t.umami||(t.umami={track:K,identify:R});let B,D,W=j(o),_=d.startsWith(u)?"":d,q=i.title;b&&!L()&&("complete"===i.readyState?I():i.addEventListener("readystatechange",I,!0))})(window)}(); diff --git a/templates/partials/head.html b/templates/partials/head.html index 519ef23..9199eb3 100644 --- a/templates/partials/head.html +++ b/templates/partials/head.html @@ -21,22 +21,13 @@ - {# Analytics #} + {# Analytics, taken from not-matthias/apollo #} {% if config.extra.analytics.enabled %} {% if config.extra.analytics.umami.website_id %} {% set website_id = config.extra.analytics.umami.website_id %} - {% set host_url = config.extra.analytics.umami.host_url | default(value="https://analytics.eu.umami.is") %} + {% set host_url = config.extra.analytics.umami.host_url | default(value="https://api-gateway.umami.dev/") %} - - + {% endif %} {% if config.extra.analytics.goatcounter.user %}