[英]Is this the correct way to set up google analytics in a rails 5 app?
在Rails中不乏涵盖Google分析的“教程”,但我找不到真正完成的教程。 有人可以告诉我我是否合并了正确的信息并且拥有设置GA所需的一切,如果没有,我在做什么错? 谢谢。
我制作了一个名为google_analytics.js.coffee的文件,该文件位于asset / javascript文件夹中。 它包含(来自http://reed.github.io/turbolinks-compatibility/google_analytics.html ):
class @GoogleAnalytics
@load: ->
((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', GoogleAnalytics.analyticsId(), 'auto'
# 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()
@trackPageview: (url) ->
unless GoogleAnalytics.isLocalRequest()
ga 'send',
hitType: 'pageview'
page: location.pathname
@isLocalRequest: ->
GoogleAnalytics.documentDomainIncludes "local"
@documentDomainIncludes: (str) ->
document.domain.indexOf(str) isnt -1
@analyticsId: ->
# your google analytics ID(s) here...
'UA-MYIDHERE-X'
GoogleAnalytics.load()
然后,由于没有人讨论过是否需要这样做,因此我在这里使用了此代码( https://gist.github.com/esBeee/545653241530f8f2c2e16371bec56f20 )并将其放在application.html.erb的开头该人正在论坛中的对话中拼凑代码。 他的代码还说要在google_analytics.js.coffee中添加四行。 那是替代方法吗? 下一个? 它是否需要顶部的类@GoogleAnalytics? 对您而言,老兵显而易见的是菜鸟看不到的! 无论如何,这就是我放入应用程序布局标头中的内容:
<% if Rails.env.production? %>
<script type="text/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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-MYIDHERE-X', 'auto');
</script>
现在,我有了该代码页和上面的代码的G_A.js.coffee页面,因此,我准备好按照以下步骤部署到heroku了(来自此处: http : //railsapps.github.io/rails-google-analytics。 html )?
$ git add -A
$ git commit -m "analytics"
$ RAILS_ENV=production rake assets:precompile
$ git add -A
$ git commit -m "assets compiled for Heroku"
感谢所有从一开始就做适当的教程,不要做任何假设并一直进行到结尾的人。
根据您为Coffee脚本提供的链接: http : //reed.github.io/turbolinks-compatibility/google_analytics.html
在Coffee Script代码中
if typeof Turbolinks isnt 'undefined' and Turbolinks.supported
document.addEventListener "page:change", (->
GoogleAnalytics.trackPageview()
), true
else
GoogleAnalytics.trackPageview()
将“ page:change”更改为“ turbolinks:load”,
if typeof Turbolinks isnt 'undefined' and Turbolinks.supported
document.addEventListener "turbolinks:load", (->
GoogleAnalytics.trackPageview()
), true
else
GoogleAnalytics.trackPageview()
然后,您一切就绪。 无需在layouts / application.html.erb文件上添加任何额外的代码,这对我有用。
这就是我在“视图”>“布局”>“ Application.html.erb”中的Rails 4应用程序(在Heroku上部署)中的状态:
<html>
<head>
#meta tags
</head>
<body>
<%= yield %>
<script>
# google analytics javascript snippet.
</script>
</body>
</html>
Google使用javascript在用户访问的每个页面上运行,仅此而已。 只要确保它在每个页面中都呈现,就可以了。 您可以将局部放置在其中并在布局中进行渲染。 Google建议您将其放在结束标记之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.