Wood Chen

Dynamically Add umami Events to Pages, with Support for halo and wordpress

0 comments95 views18 words

This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文

Add a script before </body:

<script>
function addUmamiEvent() {
  document.querySelectorAll('a:not([data-umami-event])').forEach(function(a) {
    var href = (a.getAttribute('href') || '').trim();
    a.setAttribute('data-umami-event', '链接: ' + href);
  });
}
addUmamiEvent();
// Support dynamic rendering (such as pjax, ajax, etc.)
var observer = new MutationObserver(addUmamiEvent);
observer.observe(document.body, {childList: true, subtree: true});
</script>

The event attributes will be added automatically

Pasted image 20250425155645

They will also appear in umami:

Pasted image 20250425155710

Related posts

Comments 0