Rails Google Analytics Plugin
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]