Add Google Analytics to Hugo
If you look at the HTML source code for this page in your web browser, you will see code for Google Analytics at the very end of the body
tags. This was not copy-pasted; Hugo includes a built-in template to add Google Analytics to the site’s web pages.
I use the “ananke” theme, so I copied the file ./themes/ananke/layouts/partials/site-footer.html to ./layouts/partials (create the partials subdirectory if it doesn’t exist). Then, open the new site-footer.html file and add the following below the closing footer tag:
{{ template "_internal/google_analytics.html" . }}
{{ template "_internal/google_analytics_async.html" . }}
Then, in the config.toml
file, add:
googleAnalytics = “the-code-from-google-analytics”
PS: Gideon Wolfe explains this in his blog with the difference that his instruction is to place the Google Analytics code in the header instead of the footer. @Gideon, here’s a backlink in support of more awesome content.