12

I use the hugo-academic theme. As Google Analytics did not work I noticed that Google Analytics Templates were missing.

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

I added them to single.html and list.html. I got no error message, but Google still did not track.

Then I added the complete code Google provides and tracking worked fine!

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-106XXXXXX-1', 'auto');
  ga('send', 'pageview');

</script>

No idea what is wrong here. A problem with blogdown, Hugo or the academic-theme? Or just a confusion on my part?

2
  • It will be easier for us to diagnose your problem if you can provide a link to the page on which Google Analytics does not work, or your Github repo. If you are using Github + Netlify, you can get a unique URL for each commit, which means you will be able to show a historical page.
    – Yihui Xie
    Commented Sep 11, 2017 at 21:23
  • My repo is: github.com/petzi53/weblog My website is: thought-splinters.netlify.com GA now works as I have added the Google script. Should I remove it? The developer of hugo-academic theme says that GA works for him perfect.
    – petzi
    Commented Sep 11, 2017 at 21:49

1 Answer 1

19

It sounds like a TOML issue. I don't completely understand it (might be a bug of Hugo or a certain TOML parser, or it is just how TOML works). but you need to move your top-level options in config.toml to the beginning of the config file. For example, your current config.toml looks like this:

baseurl = "/"  # End your URL with a `/` trailing slash.

....

[permalinks]
    post = "/:year/:month/:day/:slug/"

# Enable comments by entering your Disqus shortname
disqusShortname = "petzi"

# Enable analytics by entering your Google Analytics tracking ID
GoogleAnalytics = "UA-106334854-2"

....

Change the order of options to:

baseurl = "/"  # End your URL with a `/` trailing slash.

# Enable comments by entering your Disqus shortname
disqusShortname = "petzi"

# Enable analytics by entering your Google Analytics tracking ID
GoogleAnalytics = "UA-106334854-2"

....

[permalinks]
    post = "/:year/:month/:day/:slug/"

....
1
  • 1
    Excellent! Changing the order works for some reason. strange though.
    – Dinesh
    Commented Jun 3, 2018 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.