我在我的 rails 4 项目中实施谷歌分析时遇到了一些困难。我将跟踪代码包含在我的布局文件的底部,甚至尝试按照此处的建议删除 turbolinks但是我谷歌仍然无法检测到这个跟踪 cookie。有任何想法吗 ?
6 回答
几天前我设置了谷歌分析..
1.) Turbolink 解决方法
使用 Turbolinks,Google Analytics JavaScript 库不足以记录每次页面更新的页面访问。
解决方法应该在每个页面上加载,并且可以作为应用程序范围的资产包含(它将在 Turbolinks 之前加载)。
添加文件app/assets/javascripts/analytics.js.coffee
以包含 Turbolinks 解决方法:
app/assets/javascripts/analytics.js
// Coffee
$(document).on 'page:change', ->
if window._gaq?
_gaq.push ['_trackPageview']
else if window.pageTracker?
pageTracker._trackPageview()
// Javascript
$(document).on('page:change', function() {
if (window._gaq != null) {
return _gaq.push(['_trackPageview']);
} else if (window.pageTracker != null) {
return pageTracker._trackPageview();
}
});
2.) 创建一个页脚部分
只需将 aa Partial 创建到app/views/layouts
-> _footer.html.erb
然后在您的 application.html.erb 上调用您的 Partial -><%= render 'layouts/footer' %>
在您的页脚中插入分析跟踪代码:
<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-XXXXXXX-XX', 'herokuapp.com');
ga('send', 'pageview');
</script>
您必须将 UA-XXXXXXX-XX 替换为您的跟踪 ID,并将herokuapp.com 替换为您的域(如果有)。
这是不使用咖啡脚本的代码:
app/assets/javascripts/analytics.js
$(document).on('page:change', function() {
if (window._gaq != null) {
return _gaq.push(['_trackPageview']);
} else if (window.pageTracker != null) {
return pageTracker._trackPageview();
}
});
I used this gem and was very easy to set up. No extra .js file creation. Rails 3 helpers to manage google analytics tracking. Mostly intended for small to medium websites.
Just install the gem:
gem 'google-analytics-rails', '1.1.0'
Then run:
bundle install
Finally, this goes @ the production environment configuration:
Production only config/environments/production.rb:
# replace this with your tracker code
GA.tracker = "UA-112233-4"
app/views/layout/application.html.erb, in the <head>
tag :
<%= analytics_init if GoogleAnalytics.valid_tracker? %>
I saw results immediately after pushing to Heroku.
我删除了 Turbolinks,并使用了此处提供的 RailsProjects 脚本:
http://railsapps.github.io/rails-google-analytics.html
为我工作没有问题。它检测到它,过了一会儿我可以看到实时用户,所以我知道它正在工作。
编辑:该脚本似乎支持打开或关闭 Turbolinks,这很棒。
# If Turbolinks is supported, set up a callback to track pageviews on page:change.
# If it isn't supported, just track the pageview now.
if typeof Turbolinks isnt 'undefined' and Turbolinks.supported
document.addEventListener "page:change", (->
GoogleAnalytics.trackPageview()
), true
else
GoogleAnalytics.trackPageview()
我在使用 turbolinks 添加 GA 时遇到了一些问题,所以我参考了一些关于 stackoverflow 的博客和答案,并为此写了一个小博客。请参考此链接 http://tarungarg402.blogspot.in/2015/01/google-analyicts-wth-rails.html
我在我的 Rails 应用程序中使用它。我只是把这个放在我的application.js.erb
// GA racking code
(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-XXXXX-<%= Rails.env.production? ? '1' : '2' %>', 'auto');
// I have 2 GA properties- one for debug and another for production
// accommodate Turbolinks and track page views
$(document).on('ready page:change', function() {
ga('send', 'pageview');
});