Add page view counter

This commit is contained in:
2024-10-12 16:13:39 +02:00
parent 479a4d8ebc
commit 8ff3722780
9 changed files with 69 additions and 4 deletions
@@ -8,7 +8,7 @@
{{- if .Site.Config.Services.GoogleAnalytics.ID }}
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ .Site.Config.Services.GoogleAnalytics.ID }}"></script>
<script>
<script async>
window.dataLayer = window.dataLayer || [];
function gtag() {
@@ -24,4 +24,10 @@
{{- if gt (len $val) 0 }}
<script src="{{ $val }}"></script>
{{- end }}
{{- end }}
{{- end }}
{{- if .Site.Params.goatCounter }}
<script data-goatcounter="https://{{ .Site.Params.goatCounter }}/count"
data-goatcounter-settings='{"path": "{{ .RelPermalink }}"}'
async src="//gc.zgo.at/count.js"></script>
{{- end}}
@@ -0,0 +1,16 @@
{{- if .Site.Params.goatCounter }}
<span id="{{ .File.UniqueID }}" title="{{ i18n "post.pageViews" }}">{{ i18n "post.unknownViews" }}</span>
<script async>
let url =
"https://{{ .Site.Params.goatCounter }}/counter/" +
encodeURIComponent({{ .RelPermalink }}.replace(/(\/)?$/, "")) +
".json";
fetch(url)
.then(res => res.json())
.then(data => data.count)
.then(count => {
document.getElementById("{{ .File.UniqueID }}").innerText = count + " " + {{ i18n "post.pageViews" }};
});
</script>
{{- end}}