How to Add Google Analytics to Hugo Theme

Date: 2018-08-29 |

Problem: I have a Hugo theme (currently using manis-hugo-theme) and I want to use Google Analytics to keep track of my traffic. How do I do this?

Solution:

Hugo actually has a really awesome built-in solution that you can read about in their guide Google Analytics with Hugo.

Basically, you can just add this line to your config.toml:

googleAnalytics = "my-google-analytics-code"

See an example commit

Then you can add the internal template that Hugo maintains by adding this line somewhere in your header partial (so that the Google Analytics JS payload is generated on each page):

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

See an example commit

Simple as that, your Hugo site is backed by Google Analytics.

Want more like this?

The best / easiest way to support my work is by subscribing for future updates and sharing with your network.