You just want to get a simple google analytics tracker in your rails 3 application, and you start to research. You get your analytics account set up, and you think you might just stick the javascript google gives you into your application layout file. But then you find article after article, a time-honored plugin/gem, and a couple of pieces of rack middleware that all seem to suggest different ways to do it. You start to feel like an Archaeologist going through the history of rails versions, and you just want to get this working so you can deploy and go home, right?
Right. Here's the simple, modern way to set up Google Analytics with your Rails3 app:
In your Gemfile, add this:
group :production do gem 'rack-google_analytics', :require => "rack/google_analytics" end
and then bundle install.
in your config/application.rb, add this inside the application class definition:
if Rails.env == "production"
config.middleware.use("Rack::GoogleAnalytics", :web_property_id => "UA-0000000-1")
end
and set that web_property_id to the urchin id that google analytics gives you.
Deploy, and watch the cool stats come into your analytics dashboard.
This is using the rack-google_analytics gem from Jason Perry, installing it as a piece of Rails middleware (as opposed to putting it directly in the config.ru file), and with a little bit of specificity to only hook it up when you are in your production environment.