使用 Google Analytics 的 Rails 4 turbolinks [英] Rails 4 turbolinks with Google Analytics

查看:23
本文介绍了使用 Google Analytics 的 Rails 4 turbolinks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在 Rails 4 中实现 Google Analytics 跟踪代码和 turbo 链接的最佳方法是什么.普通代码段是否有效?我还看到了 一个 gems,但我不确定它的作用.

I'm wondering what is the best way to implement Google Analytics tracking code along with the turbo linking in Rails 4. Will the normal snippet work? I've also seen a gems for this but I'm not sure what it does.

推荐答案

我认为更好的主意是使用新的 Universal Analytics(来自 analytics.js 文件).

I think a better idea is to use the new Universal Analytics (from analytics.js file).

通用分析解决方案

  (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', "#{GA_UA}", "#{GA_URL}");
  ga('send', 'pageview');

然后当你想发送一个事件时,你可以使用

And then when you wanna send an event for example, you can use

<script> ga('send', "event", "#{category}", "#{action}", "#{label}", "#{count}"); </script>

小心在正文中呈现此代码,而不是在头部.Turbo-links 只替换了 body.

Be careful to render this code in the body, and not in the head. Turbo-links only replaces the body.

还要小心:

1) GA_URL 需要与您网页的 url 匹配

1) The GA_URL needs to match your pages's url

2) 事件实时显示,但在事件选项卡中,它们仅在 24 小时后出现 +

2) The Events show up in real time, but in the events tab, they only appear after 24h +

3) 您帐户的属性需要为通用"才能使此解决方案起作用

3) Your account's property need to be 'Universal' for this solution to work

Universal Analytics 文档:

Universal Analytics docs:

https://support.google.com/analytics/answer/2790010?hl=zh-CN&ref_topic=2790009

这篇关于使用 Google Analytics 的 Rails 4 turbolinks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆