Google Analytics Plugin
Tuesday, May 9, 2006 at 4:47PM Since the existing one from the [Plugins](http://wiki.rubyonrails.com/rails/pages/Plugins) page on the rails wiki isn't resolving correctly, I've created my own [Google Analytics](http://www.google.com/analytics/) plugin. I got fed up adding the same chunk of code to `app/views/layouts/application.rhtml` for every new application. So hopefully this will make things nice and easy.
To install it, add my plugin repository to your rails plugin sources:
script/plugin source http://svn.rubaidh.com/plugins/trunk
and to install the Google Analytics plugin:
script/plugin install google_analytics
To configure it, add the following to `config/environment.rb`:
Rubaidh::GoogleAnalytics.tracker_id = 'UA-12345-67'
where the `tracker_id` is what `_uacct` gets set to in the chunk of javascript that Google gives you to insert. That's it! All configured! Your tracking information will be inserted into every page. If you don't want it in particular controller, you can do the usual to skip after_filters:
skip_after_filter :add_google_analytics_code
Simple, eh?
**Update 2006/05/10** I've updated plugin so that it should automatically use the SSL version of the Google Analytics URL if it's on an SSL page (so that you don't get warnings about a mixture of secure and insecure content on a page). I've also added support for specifying the `_udn` which you'll want to put in if you have multiple subdomains appearing on the same report.
Reader Comments (9)
Hi. Looks like just the thing, but unfortunately it's keeling over at startup for me :0((. I'm using rails 1.2.3 and it does the same thing in either production of development mode ...
** Starting Rails with development environment...
Exiting
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:266:in
load_missing_constant': uninitialized constant Rubaidh (NameError)const_missing'from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:452:in
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:464:in
const_missing'gem_original_require'from /Users/attila/src/trunk/config/environment.rb:115
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require'require'from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in
new_constants_in'require'from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in
... 21 levels...
from /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require'require'from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
from script/server:3
Any ideas ?. Would be nice to use if I can get past this little hickup :0).
Thanks.
Hi,
How can I test google analytics at the development stage ? In Rubaidh::GoogleAnalytics.domain_name = 'example.com', what should be the domain name in the development environment ?
Is there any chance of this plugin being update to reflect the recent code changes to the GA code snippets?
As reported here: http://www.niallkennedy.com/blog/2008/01/google-analytics-ga-js.html
Nice work! I'd be interested in seeing a GA plugin that could handle goals too. Like a snippet you could add to a controller that was part of a goal.
Hi,
I've took the liberty to modify your plugin so that it will also track ajax calls and check the content type of the response.
Here it is:
module Rubaidh # :nodoc:
module GoogleAnalyticsMixin
def google_analytics_code(request = nil, headers = nil, flash = nil)
return unless GoogleAnalytics.enabled?
GoogleAnalytics.google_analytics_code(request, headers, flash)
end
end
class GoogleAnalytics
# Specify the Google Analytics ID for this web site. This can be found
# as the value of +_uacct+ in the Javascript excerpt
@@tracker_id = nil
cattr_accessor :tracker_id
end
end
Hi,
This url is not working. I am not able to download this plugin.
Thanks & Regards
Ganesh Mohan
Hi,
This url is not working. I am not able to download this plugin.
I wrote a small script for myself a while ago that does something like this. Feel free to check it out. http://github.com/djanowski/google_services
Can I add a read-only user to my analytics profile via RoR?
Regards
Ajit