Making Google Analytics anonymize IPs in Hugo

2 minute read Published:

Google Analytics has a cool feature called IP Anonymization. Google has a good explanation but basically they discard all IP information as its collected. It’s not a replacement for tracker blocking extensions, but it does give your users/readers a little extra privacy.

Hugo has internal Google Analytics support, just specify a googleAnalytics = “token” in your config file and most themes will just add GA to all pages for you. This internal support (as of now) doesn’t include a way to enable the IP Anonymization feature. Thankfully Hugo is pretty flexible and it’s easy enough to add it in.

Create a file named layouts/partials/google_analytics_async.html in the root of your Hugo site:

<script>||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
  ga('create', '{{ .Site.GoogleAnalytics}}', 'auto');
  ga('set', 'anonymizeIp', true);
  ga('send', 'pageview');
<script async src='//'></script>

Now we need to update our template to use our version of the Google Analytics template. In the theme I’m using calls this template from themes/after-dark/layouts/_default/baseof.html. Copy this file to layouts/_default/baseof.html and look for the following line to replace:

    {{ template "_internal/google_analytics_async.html" . }}

and change it to:

    {{ partial "google_analytics_async.html" . }}

That should be it. Fire the blog up in the local server mode and view the source on your pages to see if anonymizeIp is being set. Republish your site and enjoy a little more privacy for your users.