Rails 4与谷歌分析联系在一起 [英] Rails 4 turbolinks with Google Analytics

查看:109
本文介绍了Rails 4与谷歌分析联系在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Rails 4中实施Google Analytics跟踪代码以及Turbo链接的最佳方式是什么?请问正常的代码段是否正常工作?我也看过宝石,但我不确定它的功能。

解决方案

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

Universal Analytics解决方案

 (函数(i,s,o,g, r,a,m){i ['GoogleAnalyticsObject'] = r; i [r] = i [r] || function(){
(i [r] .q = i [r] .q | (o),
m = s.getElementsByTagName(o)[0]; a [s] .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');

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

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

注意在代码体内渲染这些代码,而不是在头部。 Turbo-links只会取代机身。



另外请注意:

<1> GA_URL需要匹配您的网页的网址



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



3)您的帐户的媒体资源必须为通用才能使用此解决方案



Universal Analytics文档:

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


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.

解决方案

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

Universal Analytics Solution

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

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

And also be careful:

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

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

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

Universal Analytics docs:

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

这篇关于Rails 4与谷歌分析联系在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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