• Stars
    star
    131
  • Rank 275,867 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 16 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatically insert Google Analytics code into your pages

GoogleAnalytics¶ ↑

This plugin enables Google Analytics support in your application. By default it will output the analytics code for every single page automatically, if it is configured correctly. This is done by adding:

Rubaidh::GoogleAnalytics.tracker_id = 'UA-12345-67'

to your config/environment.rb, inserting your own tracker id. This can be discovered by looking at the value assigned to _uacct in the Javascript code.

If you want to disable the code insertion for particular pages, add the following to controllers that don’t want it:

skip_after_filter :add_google_analytics_code

If you are running rails 2.1 or above add install this by adding:

config.gem 'rubaidh-google_analytics', :lib => 'rubaidh/google_analytics', :source => 'http://gems.github.com'

and run:

rake gems:install

Simple. :-)

Note¶ ↑

This version of the plugin uses the new Google Analytics code (ga.js) by default. To use the legacy tracking code add the following line to your config/environment.rb:

Rubaidh::GoogleAnalytics.legacy_mode = true

Google Analytics only tracks intra-site links by default. To create an outbound link that is tracked use the link_to_tracked helper:

link_to_tracked(name, track_path = "/", options = {}, html_options = {})

You can use the track_path parameter to group your outbound links into logical folders inside of Google Analytics.

The other forms of link_to are also supported:

link_to_tracked_if(condition, name, track_path = "/", options = {}, html_options = {}, &block)
link_to_tracked_unless(condition, name, track_path = "/", options = {}, html_options = {}, &block)
link_to_tracked_unless_current(name, track_path = "/", options = {}, html_options = {}, &block)

To track outbound links, you should set

Rubaidh::GoogleAnalytics.defer_load = false

This will move the tracking javascript to the top of your page. (see www.google.com/support/googleanalytics/bin/answer.py?answer=55527&topic=11006)

Tracked links respect the legacy_mode flag.

Note: Link-tracking works by inserting onclick() code in the HTML. Because of this, it will overwrite any onclick that you insert in the html_options hash.

Using local copies of the Analytics Javascript files¶ ↑

Under certain circumstances you might find it valuable to serve a copy of the Analytics JavaScript directly from your server to your visitors, and not directly from Google. If your visitors are geograhically very far from Google, or if they have low quality international bandwidth, the loading time for the Analytics JS might kill the user experience and force you to remove the valuable tracking code from your site.

This plugin now supports local copies of the legacy and new Analytics JavaScript files, updated via a rake task and served courtesy of the Rails AssetTagHelper methods. So even if you use asset hosts, the JS will be served from the correct source and under the correct protocol (HTTP/HTTPS).

To enable cached copies and the following to your initialization code:

Rubaidh::GoogleAnalytics.local_javascript = true

Use the following rake task to update the local copy of the JavaScript file:

rake google_analytics:updates

To keep the file updated you can add the following to your Capistrano configuration:

after "deploy:symlink", "deploy:google_analytics"

namespace :deploy do
  desc "Update local Google Analytics files"
  task :google_analytics, :role => :web do
    run "cd #{current_path} && rake google_analytics:update RAILS_ENV=#{ENV['RAILS_ENV']}"
  end
end

The above Capistrano recipe will almost certainly need some adjustments based on how you run your deployments, but you should get the idea.

Overriding application-default values¶ ↑

If you’re using one Rails application to serve pages across multiple domains, you may wish to override the domain and tracker ID values on a controller-by-controller or view-by-view basis. You can do this by setting the override_domain_name and override_tracker_id properties. These properties are automatically reset after each use, so the values you set for domain_name and tracker_id (usually in an initializer) will apply to all other requests.

before_filter :local_analytics

def local_analytics
  Rubaidh::GoogleAnalytics.override_domain_name  = 'foo.com'
  Rubaidh::GoogleAnalytics.override_tracker_id  = 'UA-123456-7'
end

See the documentation for the GoogleAnalytics class for other configuration options.

Note: You will need to have the mocha gem installed to run the tests for this plugin.

Copyright © 2006-2008 Rubaidh Ltd, released under the MIT license.

More Repositories

1

rubaidhstrano

Our collection of standard Capistrano recipes, all rolled into one place
Ruby
89
star
2

yui_on_rails

A Rails plugin to make it easy to use YUI on Rails instead of (or along with, I suppose) Prototype and scriptaculous
Ruby
21
star
3

zetta

ZFS bindings for Ruby
C
13
star
4

fsevent

Mechanism for watching filesystem changes on Mac OS X. Might be, err, useful for autotest at some point? ;-)
Ruby
11
star
5

google_ads

Rails plugin for inserting Google ads into a page. Nicely parameterised so that you can specify default values for particular ad groups and override on an individual basis
Ruby
9
star
6

delayed_unicorn

If you're thinking this is a cross between Unicorn and delayed_job, you're on the right track
Ruby
8
star
7

git-proxy

A caching proxy for your git requests
8
star
8

bamboo_rails

A bit of assistance for using Bamboo in your Rails applications
Ruby
5
star
9

default_values

Try very hard to set default values for attributes in an ActiveRecord model.
Ruby
5
star
10

timestamped_booleans

Rails plugin to allow allow particular datetime attributes to also be treated as booleans
Ruby
4
star
11

yet_another_form_builder

Form builder that simplifies the process of building forms in rails
Ruby
4
star
12

acts_as_param

A small Rails plugin to automatically maintain a 'param' field for a model which is usable in URLs
Ruby
4
star
13

bits_on_the_run

Ruby implementation of the Bits on the Run API
Ruby
4
star
14

form_builders

This was meant to be a general form_builders plugin with lots of variants. The only one that happened was tabular_form_builder. And I don't use it any longer...
Ruby
4
star
15

default_scope

Set the default scope for an ActiveRecord model in Rails. Usually, I use it to set a default ordering for results...
Ruby
4
star
16

namespaced_nested_resources

If you're looking at /posts/1/comments, then that's going to be Posts::CommentsController#index. Simple, really.
Ruby
4
star
17

hashtag_retweet_bot

Script that was thrown together to listen for #sor09 tags and retweet them
Ruby
3
star
18

resource_search

Easy to use RESTful searching convention
Ruby
3
star
19

enhance_composed_of

Tweaks composed_of slightly so that the value object can take a hash in its initializer instead of a list of arguments. Makes it feel more like an AR model.
Ruby
2
star
20

ruby_shim

Correctly set the LD_LIBRARY_PATH before running Ruby
C
2
star
21

mongrel-proxy

After a bit of hacking on Friday, I wound up writing a mongrel handler to act as a dumb proxy.
Ruby
2
star
22

inheritable_routing

You know, I'm not entirely sure what this plugin does. I suspect it allows sub-resources to inherit with_options passed to the parent resource.
Ruby
2
star
23

authentication

This is the authentication plugin/engine Rubaidh have built and been using of late.
Ruby
2
star
24

generators

Generate scaffolds, models, controllers and specs the Rubaidh way!
Ruby
1
star
25

fu_queue

Make life easier when using SMQueue in Rails
Ruby
1
star
26

validations

Common validations that appear over and over again in our apps, but aren't included in Rails
Ruby
1
star