简体   繁体   English

Rails 5:如何使用Google Analytics(分析)?

[英]Rails 5: How can I get Google Analytics to work?

I tried to install google analytics on a Rails 5 website adding the Google Analytics Tracking Script created by Jonathon Wolfe and provided on Nick Reed's Turbolinks Compatibility site. 我试图在Rails 5网站上安装Google Analytics(分析),添加了Jonathon Wolfe创建的Google Analytics(分析)跟踪脚本,并在Nick Reed的Turbolinks兼容性网站上提供了该脚本。

Google Analytics still don't get tracking data from my website. Google Analytics(分析)仍然无法从我的网站获取跟踪数据。 What am I missing? 我想念什么?

I use slim templating, so make changes according to your templating engine. 我使用的是细长模板,因此请根据您的模板引擎进行更改。 In application.html.slim in head initialize ga application.html.slim头中初始化ga

javascript:
  (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','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXXXXXX-XX', 'auto');
  ga('send', 'pageview');

in ga.js.coffee ga.js.coffee

document.addEventListener 'turbolinks:load', (event) ->
  if typeof ga is 'function'
    ga('set', 'location', event.data.url)
    ga('send', 'pageview')

Bonus. 奖金。 I also use infinite scroll, so when I load data in index.js.erb I use this to send another page view to ga 我也使用无限滚动,所以当我在index.js.erb中加载数据时,我使用它来向ga发送另一个页面视图

ga('send', 'pageview');
... rest of the code

If you used this page http://reed.github.io/turbolinks-compatibility/google_analytics.html with the set up you posted above you need to change your application.js to application.js.coffee . 如果您将此页面http://reed.github.io/turbolinks-compatibility/google_analytics.html用于上面发布的设置,则需要将application.js更改为application.js.coffee All of that code is written in coffeescript. 所有这些代码都是用coffeescript编写的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM