Skip to content

Commit 2fd004f

Browse files
committed
add outbound link umami analytics script
1 parent e1bb8c8 commit 2fd004f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/routes/+layout.svelte

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
<!-- Setup Privacy-preserving Umami Analytics -->
88
<svelte:head>
9+
<!-- Umami base script -->
910
<script
1011
async
1112
defer
1213
data-website-id="2d656c7d-b41a-4e19-934c-d7c9d91d784d"
1314
src="https://analytics.server.orcfax.io/umami.js"
1415
></script>
16+
<!-- Outbound-link helper -->
17+
<script async defer src="/track-external.js"></script>
1518
</svelte:head>
1619

1720
{@render children()}

static/track-external.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Auto-tag all external links for Umami */
2+
(() => {
3+
const EVENT = "outbound-link-click";
4+
5+
function tagLinks() {
6+
document.querySelectorAll("a").forEach((a) => {
7+
if (
8+
a.host !== window.location.host && // external
9+
!a.hasAttribute("data-umami-event") // not already tagged
10+
) {
11+
a.setAttribute("data-umami-event", EVENT);
12+
a.setAttribute("data-umami-event-url", a.href);
13+
}
14+
});
15+
}
16+
17+
// First run
18+
tagLinks();
19+
20+
// Re-run after every client-side navigation
21+
if (window.__sveltekit) {
22+
import("$app/navigation").then(({ afterNavigate }) => {
23+
afterNavigate(tagLinks);
24+
});
25+
}
26+
})();

0 commit comments

Comments
 (0)