简体   繁体   中英

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. 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? Thank you.

I have made a file called google_analytics.js.coffee, it is in assets/javascript folder. It contains (from 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. His code also says to add a four lines into the google_analytics.js.coffee. Is that an alternative method? An inferior one? Does it require the class @GoogleAnalytics at the top? 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 )?

$ 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

In the Coffee Script code

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

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.

This is how I have in my Rails 4 app (deployed on Heroku) Inside my View > layouts > Application.html.erb :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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