Simple Rack middleware to help injecting the Google Analytics tracking code in your website.
This middleware injects either the synchronous or asynchronous Google Analytics tracking code into the correct place of any request only when the response's Content-Type
header contains html
(therefore text/html
and similar).
gem 'rack-google-analytics'
## app.rb
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
## app/app.rb
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
## environment.rb:
config.gem 'rack-google-analytics', :lib => 'rack/google-analytics'
config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
:async
- sets to use asynchronous tracker:multiple
- sets track for multiple subdomains. (must also set :domain):top_level
- sets tracker for multiple top-level domains. (must also set :domain):anonymize_ip
- sets the tracker to remove the last octet from all IP addresses, see https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat?hl=de#_gat._anonymizeIp for details.
Note: since 0.2.0 this will use the asynchronous Google Analytics tracking code, for the traditional behaviour please use:
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x', :async => false
If you are not sure what's best, go with the defaults, and read here if you should opt-out.
In your application controller, you may track a custom variable. For example:
set_ga_custom_var(1, "LoggedIn", value, GoogleAnalytics::CustomVar::SESSION_LEVEL)
See https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables for details.
In your application controller, you may track an event. For example:
track_ga_event("Users", "Login", "Standard")
See https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
This middleware should be thread safe. Although my experience in such areas is limited, having taken the advice of those with more experience; I defer the call to a shallow copy of the environment, if this is of consequence to you please review the implementation.
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
Copyright (c) 2009-2012 Lee Hambley. See LICENSE for details. With thanks to Ralph von der Heyden and Simon Schoeters - And the biggest hand to Arthur Chiu for the huge work that went into the massive 0.9 re-factor.