Skip to content

Instantly share code, notes, and snippets.

@n0nuser
Last active April 12, 2021 10:40
Show Gist options
  • Save n0nuser/0a77fe4412298a2d9815f406d19a3572 to your computer and use it in GitHub Desktop.
Save n0nuser/0a77fe4412298a2d9815f406d19a3572 to your computer and use it in GitHub Desktop.
Using Google Analytics 4 with GoHugo
# Page settings
baseURL = "baseurl"
languageCode = "en"
title = "Hey! I'm using GA4"
[markup]
[markup.goldmark]
[markup.goldmark.renderer]
unsafe = true
[params]
# Google Analytics
GA4 = "G-XXXXXXXXXX"
{{ $googleID := (printf "%#s" $.Site.Params.ga4 ) }}
{{ $googleURL := (printf "https://www.googletagmanager.com/gtag/js?id=%s" $googleID) }}
<!-- Global site tag (gtag.js) - Google Analytics -->
{{ with $googleURL }}<script async src="{{ . }}"></script>{{ end }}
{{ with $googleID }}<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ . }}');
</script> {{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment