Rails Google Analytics Plugin

I am a big fan of Google Analytics. If you manage a blog, or any type of website for that matter, you should think of adding the little bit of JavaScript code that Google Analytics requires to start breaking down your page views. If you are developing a Rails app take a look at the Google Analytics Plugin. To install the plugin run the following command from the base directory of your Rails app.

script/plugin install http://svn.rubaidh.com/plugins/trunk/google_analytics

Before Google Analytics starts collecting stats on your site you need to configure the plugin. At the very bottom of your config/environment.rb file add the following configuration magic.

Rubaidh::GoogleAnalytics.tracker_id = ‘UA-123456-7′
Rubaidh::GoogleAnalytics.domain_name = ‘mydomain.com’
Rubaidh::GoogleAnalytics.environments = ['production']

Make sure you have the right value for the tracker_id and domain_name. At this point, the Google Analytics code will be placed at the bottom of each page when running under the production environment. If you want to see the plugin in action under a development environment, replace production by development in the configuration snippet above.

One word of warning, the plugin basically works by replaces ‘</body>’ with analytics_code+’</body>’. If you, like me, normally capitalize HTML tag names then plugin will not output the analytics code. Make sure the your view template uses the lower case version of the ending body tag.

Also, you might not want to have Google Analytics collect stats on all your pages. To disable the Google Analytics code from a given action use the following filter function call on your controller.

# Don’t collection analytics stats on the about page
skip_after_filter :add_google_analytics_code, :only => [:about]

To disable Google Analytics from most of the actions in a controller, except one for the show action, you can use the following code in your controller.

# Collect analytics stats only on the show action
skip_after_filter :add_google_analytics_code, :except => [:show]

Technorati Tags: , , , , , , , , , ,

Related posts:

  1. Rails Google Maps Plugin
  2. Rails PDF Plugin
  3. Simply Helpful Rails Plugin
  4. Rails Flash Charting Plugin
  5. Graphs Rails Plugin
  6. Riff Rails Plugin


4 Responses to “Rails Google Analytics Plugin”

Leave a Reply