简体   繁体   English

这是在Rails 5应用中设置Google Analytics(分析)的正确方法吗?

[英]Is this the correct way to set up google analytics in a rails 5 app?

There's no shortage of "tutorials" that cover google analytics in Rails, but I can't find one that's actually complete. 在Rails中不乏涵盖Google分析的“教程”,但我找不到真正完成的教程。 Can someone please tell me if I have combined the correct information and have what I need to set up GA, and if not, what am I doing wrong? 有人可以告诉我我是否合并了正确的信息并且拥有设置GA所需的一切,如果没有,我在做什么错? Thank you. 谢谢。

I have made a file called google_analytics.js.coffee, it is in assets/javascript folder. 我制作了一个名为google_analytics.js.coffee的文件,该文件位于asset / javascript文件夹中。 It contains (from http://reed.github.io/turbolinks-compatibility/google_analytics.html ): 它包含(来自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()

Then, since no one ever talks about if this is all that's needed, I used this code from here ( https://gist.github.com/esBeee/545653241530f8f2c2e16371bec56f20 ) and put it in the head of application.html.erb, although that person was piecing together code from a conversation in a forum. 然后,由于没有人讨论过是否需要这样做,因此我在这里使用了此代码( https://gist.github.com/esBeee/545653241530f8f2c2e16371bec56f20 )并将其放在application.html.erb的开头该人正在论坛中的对话中拼凑代码。 His code also says to add a four lines into the google_analytics.js.coffee. 他的代码还说要在google_analytics.js.coffee中添加四行。 Is that an alternative method? 那是替代方法吗? An inferior one? 下一个? Does it require the class @GoogleAnalytics at the top? 它是否需要顶部的类@GoogleAnalytics? What's obvious to you veterans is not obvious to the noobs! 对您而言,老兵显而易见的是菜鸟看不到的! Anyway here's what I put in my application layout header: 无论如何,这就是我放入应用程序布局标头中的内容:

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

Now I have that G_A.js.coffee page of code and the above code, so am I ready to follow these steps to deploy to heroku (came from here: http://railsapps.github.io/rails-google-analytics.html )? 现在,我有了该代码页和上面的代码的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"

Thank you to anyone who makes proper tutorials that start from the beginning, don't make assumptions, and follow through to the end. 感谢所有从一开始就做适当的教程,不要做任何假设并一直进行到结尾的人。

Based on the link you provided for the Coffee Script: http://reed.github.io/turbolinks-compatibility/google_analytics.html 根据您为Coffee脚本提供的链接: http : //reed.github.io/turbolinks-compatibility/google_analytics.html

In the Coffee Script code 在Coffee Script代码中

if typeof Turbolinks isnt 'undefined' and Turbolinks.supported
  document.addEventListener "page:change", (->
    GoogleAnalytics.trackPageview()
  ), true
else
  GoogleAnalytics.trackPageview()

Change the 'page:change' to 'turbolinks:load' which become 将“ page:change”更改为“ turbolinks:load”,

if typeof Turbolinks isnt 'undefined' and Turbolinks.supported
  document.addEventListener "turbolinks:load", (->
    GoogleAnalytics.trackPageview()
  ), true
else
  GoogleAnalytics.trackPageview()

Then you are all set. 然后,您一切就绪。 No extra code needed to be add on the layouts/application.html.erb file, this works for me. 无需在layouts / application.html.erb文件上添加任何额外的代码,这对我有用。

This is how I have in my Rails 4 app (deployed on Heroku) Inside my View > 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 uses the javascript to run on every page a user visits and that's it. Google使用javascript在用户访问的每个页面上运行,仅此而已。 As long as you make sure that it is rendered in every page, you should be fine. 只要确保它在每个页面中都呈现,就可以了。 You could put inside a partial and render it in your layout. 您可以将局部放置在其中并在布局中进行渲染。 Google recommends you put it just before the closing tag. Google建议您将其放在结束标记之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM