Google Analytics不适用于blogdown

时间:2021-02-12 15:18:41

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

我使用了hugo-academic主题。由于Google Analytics不起作用,我发现Google Analytics模板丢失了。

{{ 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.

我将它们添加到single.html和list.html中。我没有收到任何错误消息,但谷歌仍未跟踪。

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?

不知道这里有什么问题。博客,雨果还是学术主题的问题?或者只是我的困惑?

1 个解决方案

#1


10  

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:

这听起来像是一个TOML问题。我不完全理解它(可能是Hugo或某个TOML解析器的bug,或者它就是TOML的工作原理)。但是您需要将config.toml中的*选项移动到配置文件的开头。例如,您当前的config.toml如下所示:

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


10  

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:

这听起来像是一个TOML问题。我不完全理解它(可能是Hugo或某个TOML解析器的bug,或者它就是TOML的工作原理)。但是您需要将config.toml中的*选项移动到配置文件的开头。例如,您当前的config.toml如下所示:

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

....