Coder Social home page Coder Social logo

kristianmandrup / google_analytics Goto Github PK

View Code? Open in Web Editor NEW

This project forked from digitalmoksha/google_analytics

1.0 3.0 0.0 142 KB

Automatically insert Google Analytics code into your pages

Home Page: http://rubaidh.com/portfolio/open-source/google-analytics/

License: MIT License

google_analytics's Introduction

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. :-)

Google Analytics Tracking Code versions

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

To use new asynchronous tracking code add the following line to your config/environment.rb:

Rubaidh::GoogleAnalytics.asynchronous_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.

Tracking links with events

Google Analytics documentation says that events tracking is the preferred method of tracking outbound links. It has the advantages of being able to track urls with query strings, and organizing links with categories, actions, and labels. To create an link tracked with events (internal or outbound), use the link_to_tracked_event helper:

link_to_tracked_event(name, category, action, url_options = {}, event_options={}, html_options = {})

To track event links, you should set

Rubaidh::GoogleAnalytics.defer_load = false

Tracked event links *do not* respect the legacy_mode flag.

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.

google_analytics's People

Contributors

danielmorrison avatar digitalmoksha avatar jasoncodes avatar kennethkalmer avatar mathie avatar simianarmy avatar zsand avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.