Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics does not work with blogdown

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?

like image 797
petzi Avatar asked Sep 11 '17 18:09

petzi


1 Answers

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/"

....
like image 104
Yihui Xie Avatar answered Oct 15 '22 11:10

Yihui Xie