pax_global_header00006660000000000000000000000064121217067630014517gustar00rootroot0000000000000052 comment=759224bced86dc42926dc05ac8a7e7e5b67f3367 ruby-rack-google-analytics-0.11.0/000077500000000000000000000000001212170676300167345ustar00rootroot00000000000000ruby-rack-google-analytics-0.11.0/.gitignore000077500000000000000000000002671212170676300207340ustar00rootroot00000000000000## MAC OS .DS_Store ## TEXTMATE *.tmproj tmtags ## EMACS *~ \#* .\#* ## VIM *.swp ## PROJECT::GENERAL coverage rdoc pkg .bundle Gemfile.lock ## PROJECT::SPECIFIC ._* *.gem pkg/* ruby-rack-google-analytics-0.11.0/CHANGELOG.md000066400000000000000000000035241212170676300205510ustar00rootroot00000000000000## Master ### Bug fix - [#11][] Fix an issue that was causing `(deadlock; recursive locking)` errors due to body not closed. ([@rymai][]) ### Improvements - [#11][] Usage of Bundler. ([@rymai][]) - [#11][] Addition of development dependencies. ([@rymai][]) - [#11][] Creation of `lib/rack-google-analytics.rb` so `:require => 'rack/google-analytics'` in the Gemfile shouldn't needed anymore. ([@rymai][]) ## 0.10.0 ### Improvements - Include the Google pagespeed code. - `README` typos fixed. ## 0.9.2 ### Bug fix - Fixed a bug with lots of missing files from the Gem... how silly! ## 0.9.1 ### Improvement - Updated `README` to reflect 0.9.0 merge from achiu. ## 0.9.0 ### Improvement - Name changed from 'rack-google-analytics' to 'rack/google-analytics' more inline with the norm. ## 0.6.0 ### Improvement - Class now named `Rack::GoogleAnalytics`, in 0.5 and earlier this was incorrectly documented as `Rack::GoogleTracker`. ## 0.2.0 ### Improvement - Asynchronous code is now the default. ## 22 Jul, 2010 ### Improvement - Major re-write from Arthur Chiu, now correctly writes the `Content-Length` header, and comes with tests. ([@achiu][]) - This patch also backs-out the changes from [@cimm][] - but they were un-tested (I intend to bring these back as soon as possible; this will probably constitute a 1.0 release when it happens). ## 19 Jan, 2010 ### Improvement - Makes the default snippet the async version from Google. Use regular synchronous code with: `:async => false`. ([@ralph][]) ## 27 Dec, 2009 - Initial release, extracted from the Capistrano-Website project. [#11]: https://github.com/jilion/rack/issues/11 [@achiu]: https://github.com/achiu [@cimm]: https://github.com/cimm [@ralph]: https://github.com/ralph [@rymai]: https://github.com/rymairuby-rack-google-analytics-0.11.0/Gemfile000066400000000000000000000000631212170676300202260ustar00rootroot00000000000000source 'https://rubygems.org' gemspec gem 'rake' ruby-rack-google-analytics-0.11.0/LICENSE000077500000000000000000000020441212170676300177440ustar00rootroot00000000000000Copyright (c) 2009-2012 Lee Hambley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ruby-rack-google-analytics-0.11.0/README.md000077500000000000000000000044551212170676300202260ustar00rootroot00000000000000# Rack google Analytics 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). ## Usage #### Gemfile ```ruby gem 'rack-google-analytics' ``` #### Sinatra ```ruby ## app.rb use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x' ``` #### Padrino ```ruby ## app/app.rb use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x' ``` #### Rails 2.X ```ruby ## environment.rb: config.gem 'rack-google-analytics', :lib => 'rack/google-analytics' config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x' ``` ### Options * `: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) Note: since 0.2.0 this will use the asynchronous Google Analytics tracking code, for the traditional behaviour please use: ```ruby 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. ## Thread Safety 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. ## Note on Patches/Pull Requests * 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 Copyright (c) 2009-2012 Lee Hambley. See LICENSE for details. With thanks to [Ralph von der Heyden](https://github.com/ralph) and [Simon Schoeters](https://github.com/cimm) - And the biggest hand to [Arthur Chiu](https://github.com/achiu) for the huge work that went into the massive 0.9 re-factor. ruby-rack-google-analytics-0.11.0/Rakefile000077500000000000000000000012751212170676300204110ustar00rootroot00000000000000require 'rubygems' require 'rake' require 'rake/testtask' Rake::TestTask.new(:test) do |test| test.libs << 'lib' << 'test' test.pattern = 'test/**/test_*.rb' test.verbose = true end begin require 'rcov/rcovtask' Rcov::RcovTask.new do |test| test.libs << 'test' test.pattern = 'test/**/test_*.rb' test.verbose = true end rescue LoadError task :rcov do abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov" end end task :default => :test begin require 'yard' YARD::Rake::YardocTask.new rescue LoadError task :yardoc do abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard" end end ruby-rack-google-analytics-0.11.0/lib/000077500000000000000000000000001212170676300175025ustar00rootroot00000000000000ruby-rack-google-analytics-0.11.0/lib/rack-google-analytics.rb000066400000000000000000000000401212170676300242000ustar00rootroot00000000000000require 'rack/google-analytics' ruby-rack-google-analytics-0.11.0/lib/rack/000077500000000000000000000000001212170676300204225ustar00rootroot00000000000000ruby-rack-google-analytics-0.11.0/lib/rack/google-analytics.rb000066400000000000000000000021431212170676300242100ustar00rootroot00000000000000require 'rack' require 'erb' module Rack class GoogleAnalytics DEFAULT = { :async => true } def initialize(app, options = {}) raise ArgumentError, "Tracker must be set!" unless options[:tracker] and !options[:tracker].empty? @app, @options = app, DEFAULT.merge(options) end def call(env); dup._call(env); end def _call(env) @status, @headers, @body = @app.call(env) return [@status, @headers, @body] unless html? response = Rack::Response.new([], @status, @headers) @body.each { |fragment| response.write inject(fragment) } @body.close if @body.respond_to?(:close) response.finish end private def html?; @headers['Content-Type'] =~ /html/; end def inject(response) file = @options[:async] ? 'async' : 'sync' @template ||= ::ERB.new ::File.read ::File.expand_path("../templates/#{file}.erb",__FILE__) if @options[:async] response.gsub(%r{}, @template.result(binding) + "") else response.gsub(%r{}, @template.result(binding) + "") end end end end ruby-rack-google-analytics-0.11.0/lib/rack/google-analytics/000077500000000000000000000000001212170676300236635ustar00rootroot00000000000000ruby-rack-google-analytics-0.11.0/lib/rack/google-analytics/version.rb000066400000000000000000000001051212170676300256710ustar00rootroot00000000000000module Rack class GoogleAnalytics VERSION = '0.11.0' end end ruby-rack-google-analytics-0.11.0/lib/rack/templates/000077500000000000000000000000001212170676300224205ustar00rootroot00000000000000ruby-rack-google-analytics-0.11.0/lib/rack/templates/async.erb000066400000000000000000000013641212170676300242330ustar00rootroot00000000000000ruby-rack-google-analytics-0.11.0/lib/rack/templates/sync.erb000066400000000000000000000006471212170676300240750ustar00rootroot00000000000000 ruby-rack-google-analytics-0.11.0/lib/rack/version.rb000066400000000000000000000001051212170676300224300ustar00rootroot00000000000000module Rack class GoogleAnalytics VERSION = '0.11.0' end end ruby-rack-google-analytics-0.11.0/rack-google-analytics.gemspec000066400000000000000000000021051212170676300244560ustar00rootroot00000000000000# -*- encoding: utf-8 -*- $LOAD_PATH.unshift File.expand_path('../lib', __FILE__) require 'rack/google-analytics/version' Gem::Specification.new do |s| s.name = "rack-google-analytics" s.version = Rack::GoogleAnalytics::VERSION s.platform = Gem::Platform::RUBY s.authors = ["Lee Hambley"] s.email = ["lee.hambley@gmail.com"] s.homepage = "https://github.com/leehambley/rack-google-analytics" s.summary = "Rack middleware to inject the Google Analytics tracking code into outgoing responses." s.description = "Simple Rack middleware for implementing google analytics tracking in your Ruby-Rack based project. Supports synchronous and asynchronous insertion and configurable load options." s.files = Dir.glob("lib/**/*") + %w(README.md LICENSE) s.require_path = 'lib' s.add_development_dependency 'bundler' s.add_development_dependency 'test-unit', '~> 2.5.1' s.add_development_dependency 'shoulda', '~> 2.11.3' s.add_development_dependency 'rack', '~> 1.2.0' s.add_development_dependency 'rack-test', '~> 0.5.4' end ruby-rack-google-analytics-0.11.0/test/000077500000000000000000000000001212170676300177135ustar00rootroot00000000000000ruby-rack-google-analytics-0.11.0/test/helper.rb000077500000000000000000000016041212170676300215230ustar00rootroot00000000000000require 'rubygems' require 'test/unit' require 'shoulda' require 'rack/test' $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'rack/google-analytics' class Test::Unit::TestCase include Rack::Test::Methods def app; Rack::Lint.new(@app); end def mock_app(options) main_app = lambda { |env| request = Rack::Request.new(env) case request.path when '/' then [200,{ 'Content-Type' => 'application/html' },['Hello world']] when '/test.xml' then [200,{'Content-Type' => 'application/xml'}, ['Xml here']] when '/bob' then [200,{'Content-Type' => 'application/html'} ,['bob here']] else [404,'Nothing here'] end } builder = Rack::Builder.new builder.use Rack::GoogleAnalytics, options builder.run main_app @app = builder.to_app end end ruby-rack-google-analytics-0.11.0/test/test_rack-google-analytics.rb000077500000000000000000000037411212170676300254660ustar00rootroot00000000000000require 'helper' class TestRackGoogleAnalytics < Test::Unit::TestCase context "Asyncronous" do context "default" do setup { mock_app :async => true, :tracker => 'somebody' } should "show asyncronous tracker" do get "/" assert_match %r{\_gaq\.push}, last_response.body assert_match %r{\'\_setAccount\', \"somebody\"}, last_response.body assert_match %r{}, last_response.body assert_equal "532", last_response.headers['Content-Length'] end should "not add tracker to none html content-type" do get "/test.xml" assert_no_match %r{\_gaq\.push}, last_response.body assert_match %r{Xml here}, last_response.body end should "not add without " do get "/bob" assert_no_match %r{\_gaq\.push}, last_response.body assert_match %r{bob here}, last_response.body end end context "multiple sub domains" do setup { mock_app :async => true, :multiple => true, :tracker => 'gonna', :domain => 'mydomain.com' } should "add multiple domain script" do get "/" assert_match %r{'_setDomainName', \"mydomain.com\"}, last_response.body assert_equal "579", last_response.headers['Content-Length'] end end context "multiple top-level domains" do setup { mock_app :async => true, :top_level => true, :tracker => 'get', :domain => 'mydomain.com' } should "add top_level domain script" do get "/" assert_match %r{'_setDomainName', 'none'}, last_response.body assert_match %r{'_setAllowLinker', true}, last_response.body end end end context "Syncronous" do setup { mock_app :async => false, :tracker => 'whatthe' } should "show non-asyncronous tracker" do get "/bob" assert_match %r{_gat._getTracker}, last_response.body assert_match %r{}, last_response.body assert_match %r{\"whatthe\"}, last_response.body end end end