Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about Google Analytics Tracking (w/ Rails)

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 into the layout for the application.

Now, if I use this locally, does analytics track the local use as well?

The reason I'm asking this is we've been running tests on our dev computers using rspec, and there seems to be a spike in the analytics. All these spikes also seem to show up as unique visitors.

I'd appreciate any insight on this.

Thanks!

like image 492
stringo0 Avatar asked Dec 06 '22 20:12

stringo0


2 Answers

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

like image 86
thatdankent Avatar answered Jan 15 '23 13:01

thatdankent


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 %>
like image 24
rcd Avatar answered Jan 15 '23 12:01

rcd