Move to minimalanalytics for GA tracking

This commit is contained in:
Bjørn Erik Pedersen
2025-11-04 18:34:19 +01:00
parent 4e8a523fbe
commit 7040dc5eed
10 changed files with 24 additions and 50 deletions
+2
View File
@@ -10,3 +10,5 @@ node_modules/
nohup.out
package-lock.json
trace.out
assets/jsconfig.json
+3
View File
@@ -47,6 +47,9 @@ var debug = 0 ? console.log.bind(console, '[index]') : function () {};
bridgeTurboAndAlpine(Alpine);
{
// Minimal Analytics does not support SPA out of the box, so we need to manually track page views on Turbo navigation.
addEventListener('turbo:visit', () => window.track('page_view'));
let containerScrollTops = {};
// To preserve scroll position in scrolling elements on navigation add data-turbo-preserve-scroll-container="somename" to the scrolling container.
-10
View File
@@ -1,10 +0,0 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [
"*"
]
}
}
}
+5
View File
@@ -1,3 +1,8 @@
module github.com/gohugoio/hugoDocs
go 1.22.0
require (
github.com/bep/hugo-mod-minimalanalytics v0.3.0 // indirect
github.com/jahilldev/minimal-analytics v0.0.0-20220825110705-ee335ed38334 // indirect
)
+4 -2
View File
@@ -1,2 +1,4 @@
github.com/gohugoio/gohugoioTheme v0.0.0-20250116152525-2d382cae7743 h1:gjoqq8+RnGwpuU/LQVYGGR/LsDplrfUjOabWwoROYsM=
github.com/gohugoio/gohugoioTheme v0.0.0-20250116152525-2d382cae7743/go.mod h1:GOYeAPQJ/ok8z7oz1cjfcSlsFpXrmx6VkzQ5RpnyhZM=
github.com/bep/hugo-mod-minimalanalytics v0.3.0 h1:fctZN3nFOFrapX1dhPBszyCUKOGjEpBJmp1jBf3YkJE=
github.com/bep/hugo-mod-minimalanalytics v0.3.0/go.mod h1:3QVkKqSikBRTI+hS3dm/zCAfGukd+opSfAwfjkyxqIw=
github.com/jahilldev/minimal-analytics v0.0.0-20220825110705-ee335ed38334 h1:r8/jLqGw5uwSmgJ4eSGbxa1XBaSIkp3MY99GhY61NeE=
github.com/jahilldev/minimal-analytics v0.0.0-20220825110705-ee335ed38334/go.mod h1:w4l2fXZWGxmcVQfLLBdTqUT6HpSIA9FhmUtVArJFLYw=
+7 -4
View File
@@ -98,6 +98,8 @@ ignoreLogs = ['warning-frontmatter-params-overrides']
disableWatch = true
source = "hugo_stats.json"
target = "assets/notwatching/hugo_stats.json"
[[module.imports]]
path = "github.com/bep/hugo-mod-minimalanalytics"
[outputFormats]
[outputFormats.redir]
@@ -120,6 +122,11 @@ ignoreLogs = ['warning-frontmatter-params-overrides']
[params]
description = "The worlds fastest framework for building websites"
ghrepo = "https://github.com/gohugoio/hugoDocs/"
[params.minimalanalytics]
# GA4 tracking ID
tracking_id = "G-MBZGKNMDWC"
[params.render_hooks.link]
errorLevel = 'warning' # ignore (default), warning, or error (fails the build)
[params.social.mastodon]
@@ -148,10 +155,6 @@ ignoreLogs = ['warning-frontmatter-params-overrides']
[[server.headers]]
for = "/**.{css,js}"
[services]
[services.googleAnalytics]
ID = 'G-MBZGKNMDWC'
[taxonomies]
category = 'categories'
-27
View File
@@ -1,27 +0,0 @@
{{ with site.Config.Services.GoogleAnalytics.ID }}
<script
async
src="https://www.googletagmanager.com/gtag/js?id={{ . }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
{{ $site := site.BaseURL | replaceRE "^https?://(www\\.)?([^/]+).*" "$2" }}
gtag('config', '{{ . }}', {'anonymize_ip': true, 'dimension1': '{{ $site }}', 'dimension2': '{{ getenv "BRANCH" }}'});
/**
* Function that tracks a click on an outbound link in Analytics.
* Setting the transport method to 'beacon' lets the hit be sent
* using 'navigator.sendBeacon' in browser that support it.
*/
var trackOutboundLink = function(id, url) {
gtag('event', 'click', {
'event_category': 'outbound',
'event_label': id,
'transport_type': 'beacon'
});
}
</script>
{{ end }}
@@ -1,3 +1,6 @@
{{ if or hugo.IsProduction (not hugo.IsDevelopment) }}
{{ partial "minimalanalytics.html" . }}
{{ end }}
{{ $githubInfo := partialCached "helpers/funcs/get-github-info.html" . "-" }}
{{ $opts := dict "minify" true }}
{{ with resources.Get "js/head-early.js" | js.Build $opts }}
-3
View File
@@ -36,6 +36,3 @@
{{ partial "schema.html" . }}
{{ partial "twitter_cards.html" . }}
{{ if hugo.IsProduction }}
{{ partial "helpers/gtag.html" . }}
{{ end }}
@@ -15,13 +15,9 @@
{{ $url = printf "%s?%s%s" .link $query_params (querify "utm_source" (.utm_source | default $utmSource ) "utm_medium" (.utm_medium | default "banner") "utm_campaign" (.utm_campaign | default "hugosponsor") "utm_content" (.utm_content | default "gohugoio")) | safeURL }}
{{ end }}
{{ $logo := resources.Get .logo }}
{{ $gtagID := printf "Sponsor %s %s" .name $gtag | title }}
<a
href="{{ $url }}"
title="{{ .title | default .name }}"
{{ if hugo.IsProduction }}
onclick="trackOutboundLink({{ printf "'%s', '%s'" $gtagID $url | safeJS }});"
{{ end }}
class="group inline-block w-full h-full shadow-md dark:shadow-gray-600"
{{ with .link_attr }}{{ . | safeHTMLAttr }}{{ end }}>
<div