Google 分析不适用于 blogdown

Google Analytics does not work with blogdown

我用的是hugo-academic主题。由于 Google 分析无效,我注意到缺少 Google Analytics Templates

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

我将它们添加到 single.htmllist.html。我没有收到任何错误消息,但 Google 仍然没有跟踪。

然后我添加了完整的代码 Google 提供和跟踪工作正常!

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

不知道这里出了什么问题。 blogdown、Hugo 或学术主题有问题吗?或者只是我的困惑?

听起来像是 TOML 问题。我不完全理解它(可能是 Hugo 或某个 TOML 解析器的错误,或者这就是 TOML 的工作方式)。但是您需要将 config.toml 中的顶级选项移动到配置文件的开头。例如,您的 current 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"

....

将选项的顺序更改为:

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

....