我想知道在Rails 4中实现Turbo链接的同时如何最好地实现Google Analytics跟踪代码。一般的片段代码会起作用吗?我还看到了一个宝石包,但我不确定它是做什么的。
我想知道在Rails 4中实现Turbo链接的同时如何最好地实现Google Analytics跟踪代码。一般的片段代码会起作用吗?我还看到了一个宝石包,但我不确定它是做什么的。
我喜欢vladCovaliov的解决方案,因为大多数新帐户默认使用通用分析(在我看来也更好)。然而,我认为这个答案需要与其他建议结合起来,注释掉初始页面视图,使用page:change
事件,跟踪页面视图,而不是事件。
例如,在你的<head>
中:
<% if Rails.env.production? %>
<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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'auto');
//ga('send', 'pageview');
</script>
<% else %>
<script>
function ga () {
var params = Array.prototype.slice.call(arguments, ga.length);
console.log("GoogleAnalytics: " + params);
};
</script>
<% end %>
然后在通过资源管道加载的js文件中:
$(document).on('page:change', function() {
ga('send', 'pageview', window.location.pathname);
});
这将为每个使用Turbolinks或不使用Turbolinks的加载的页面记录一个页面浏览量。请注意,window.location.pathname
是必需的,否则您可能会在所有后续页面加载中获取第一个加载的页面的URL。(如果您想要剥离RESTful URL中的:id
路径段,则这也为您提供了一个不错的位置来编辑报告的URL。)
您还可以轻松调用:
ga('send', "event", category, action, label, count);
报告您网站中其他有趣的JavaScript事件。
jquery-turbolinks
gem,所以我用下面的代码替换了$(document).on('page:change', function() {
改为
$(document).ready(function() {
- Alexanderjquery-turbolinks
,如果我将其改为 $(document).ready
,它会导致向 Google Analytics 报告的页面浏览量增加。第一次点击是 2 次浏览,第二次是 4 次,第三次是 8 次,以此类推。请小心操作。(附注:也许是我做错了什么!) - Chris Peterswindow.location.pathname
部分正是我在寻找的。非常感谢! - animatedgifga('send', 'pageview', window.location.pathname + window.location.search);
其中 window.location.search
包括在发送到 Google Analytics 的 URL 中的搜索查询。 - Alexander我采用了一个在某个网站上看到的不同方法,但它似乎是合理的。
像往常一样将GA添加到标题中,但要进行轻微修改,即注释掉自动发生的trackPageview
事件。
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-FOOBAR']);
//_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
因为 body 在 turbolinks 重新加载时也会重新加载,所以请将此代码添加到您的 body
中的某个位置。
<% if Rails.env.production? %>
<script>_gaq.push(['_trackPageview']);</script>
<% end %>
生产检查是可选的,但这是一个不错的方法,如果你正在积极开发,就不必让GA跟踪localhost命中。另一个好处是你不必担心破坏page:change或page:load绑定,并且你可以确信它将在任何可被GA跟踪的浏览器上工作,而不必担心双重命中或任何奇怪的东西。
我认为更好的做法是使用新的通用分析(来自analytics.js文件)。
通用分析方案
(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');
当您想要发送事件时,您可以使用以下方法:
<script> ga('send', "event", "#{category}", "#{action}", "#{label}", "#{count}"); </script>
请注意,将此代码呈现在正文中,而不是头部。Turbo-links 仅替换正文。
还要注意:
1)GA_URL 需要与您页面的URL匹配
2)事件实时显示,但在事件选项卡中,它们仅在24小时后出现
3)为使此解决方案起作用,您帐户的属性需要为“通用”
通用分析文档:
https://support.google.com/analytics/answer/2790010?hl=en&ref_topic=2790009
ga('send', 'pageview')
放在<body>标签中。GA_URL参数可以替换为“auto”,因为即使使用Turbolinks,“pageview”调用也始终能正确确定URL。 - hannes_lga('send', 'pageview', '<%= request.path %>');
来重写 URL——这确实对我有用。 - Tim Jones# google-analytics-turbolinks/lib/assets/javascripts/google-analytics-turbolinks.js.coffee
if window.history?.pushState and window.history.replaceState
document.addEventListener 'page:change', (event) =>
# Google Analytics
if window.ga != undefined
ga('set', 'location', location.href.split('#')[0])
ga('send', 'pageview')
else if window._gaq != undefined
_gaq.push(['_trackPageview'])
else if window.pageTracker != undefined
pageTracker._trackPageview();
<head>
中放置以下代码即可。 <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','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'auto');
//ga('send', 'pageview'); //moved to an asset file because of turbolinks
</script>
然后在你的资产管道中的js文件(例如application.js
)中:
$(document).on('turbolinks:load', function() {
ga('send', 'pageview', window.location.pathname + window.location.search);
});
请记得将“UA-XXXXXXXX-X”替换为您的Google Analytics ID。
((i, s, o, g, r, a, m) ->
i["GoogleAnalyticsObject"] = r
i[r] = i[r] or ->
(i[r].q = i[r].q or []).push arguments
return
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
return
) window, document, "script", "//www.google-analytics.com/analytics.js", "ga"
ga "create", 'UA-XXXXXXXX-XX', "yourdomain.com"
$(document).on 'ready page:load', ->
ga "send", "pageview", window.location.pathname unless $('body').data('admin') is true
$(document).on 'page:change', ->
if window._gaq?
_gaq.push ['_trackPageview']
else if window.pageTracker?
pageTracker._trackPageview()
注意:这不是我的代码,它直接取自先前链接的网站。