Add GoatCounter analytics

Add GoatCounter analytics
This commit is contained in:
Akitake 2025-02-27 13:02:25 +01:00 committed by GitHub
commit 9d07a7ecd1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 1 deletions

View File

@ -180,6 +180,7 @@ not_in_nav: |
theme:
name: material
custom_dir: overrides
logo: img/logo/website-logo.svg
favicon: img/favicon.ico
icon:
@ -236,4 +237,9 @@ markdown_extensions:
plugins:
- search
- git-revision-date-localized
- git-revision-date-localized
extra:
analytics:
provider: custom
property: https://tuah-torial.goatcounter.com/count

View File

@ -0,0 +1,37 @@
<script data-goatcounter="{{ config.extra.analytics.property }}"
{# We also need to disable sending a pageview on load if using instant navigation, see below. #}
{% if "navigation.instant" in config.theme.features %}
data-goatcounter-settings='{"no_onload": true}'
{% endif %}
async src="//gc.zgo.at/count.js"></script>
{#
The below code block is for handling "instant navigation" in Material MkDocs.
It is currently not applicable to us, but if we wish to switch to it in the future,
this will properly handle URL changes for analytics.
#}
{% if "navigation.instant" in config.theme.features %}
<script>
document.addEventListener("DOMContentLoaded", () => {
// GoatCounter might not load, because people might have it blocked
// from uBlock Origin or other content blockers.
if (!window.goatcounter) {
return;
}
let referrer = document.referrer;
document$.subscribe((d) => {
// We don't need to pass the path, because when this observable is called,
// `window.location` will already be on the new path.
// We still need to manually handle referrals and site titles though.
window.goatcounter.count({
title: d.querySelector("h1")?.textContent || d.title,
referrer,
});
referrer = window.location.href;
});
});
</script>
{% endif %}