How do I install google tag manager correctly?

Thats a good question for google … this is a good article on Tag Manager

Think of Tag manager as a way for you to manage ALL your ‘tags’ - for Google, Facebook, Google Ads, other ad networks, other analytics, etc

If you are happy with Google Analytics and just want Event Tracking … then stick with Google Analytics … and to implement GA event tagging, you just need to update your head.html to include a small script …

Assuming you have set your google_analytics_id in your config … then this will work

{{ "<!-- Global Site Tag (gtag.js) - Google Tag Manager -->" | safeHTML }}
{{ with .Site.Params.google_analytics_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());

  gtag('config', {{ . }});
  </script>
  {{ end }}

You then need to add the events … like onclick etc which I cover in my post on adding Event Tracking to Hugo

1 Like