Question about Google Analytics Tracking (w/ Rails)

前端 未结 5 1511
深忆病人
深忆病人 2021-02-10 21:17

I\'ve got google analytics setup on a rails project, and I\'ve got \"A single domain (default)\" selected for the tracking options.

I\'ve copied and pasted the js code i

相关标签:
5条回答
  • 2021-02-10 21:46

    This tutorial shows you how to set it up. I don't know why there's a gem for this. Just put the code in a partial and render it in your application layout if rails.env.production?

    http://aihuiong.com/post/452550136/google-analytics-and-rails-in-3-steps-and-less-than-5

    0 讨论(0)
  • 2021-02-10 21:58

    I tried the gems but they didn't work; wouldn't spit out any code, etc. Seemed dumb for something so simple. So I ended up just doing this, in application.html.erb:

    <% if Rails.env.production? %>
    (GA JS Code Snippet)
    <% end %>
    
    0 讨论(0)
  • 2021-02-10 22:02

    In the google analytics admin, you can filter out visitors based on various attributes (e.g. ip address) This would also be a good idea to do.

    Another option that I've done is add another analytics tracking account that you use for the dev/test environment so that you can test whether and how analytics are working.

    0 讨论(0)
  • 2021-02-10 22:04

    Also, using a Google Analytics gem will automatically set some of these features for you. Here is a great way to do it:

    Google Analytics and Rails in 5 EASY Steps:

    If you are in Rails 3, I just found a great solution for doing Google Analytics in Rails apps.

    (1) In your Gemfile:

    group :production do
      gem 'rack-google_analytics', :require => "rack/google_analytics"
    end
    

    (2) Bundle Install

    (3) In your config/application.rb (put this in the class definition section - careful not to drop it in a module. I put mine right under "class Application"):

    if Rails.env == "production"
      config.middleware.use("Rack::GoogleAnalytics", :web_property_id => "UA-0000000-1")
    end
    

    (4) Initiate your Google Analytics account

    (5) Copy and paste that funky web_property_id from Google's supplied code into the code from (3), replacing 'UA-000000-1'

    That's it!

    I originally found this solution here: David Bock Article

    0 讨论(0)
  • 2021-02-10 22:09

    Yes, it does track local visits as well. You should probably use ruby conditional statement to exclude it for the local conneciton. For example, at the bottom of the layout file

    <% if !request.local? %>
    Your source codes for Google Analytics
    <% end %>
    

    This way, Google Analytics will not be printed if connection is made from local.

    0 讨论(0)
提交回复
热议问题