From caad94c8483fbcaaf725cbc20e1e6196a33413f4 Mon Sep 17 00:00:00 2001 From: Francesco Gianni Date: Tue, 22 Jan 2013 22:53:12 +0100 Subject: [PATCH 1/2] ADD: position option to put code in head --- lib/rack/google_analytics.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/rack/google_analytics.rb b/lib/rack/google_analytics.rb index 558d62a..7a2f26f 100644 --- a/lib/rack/google_analytics.rb +++ b/lib/rack/google_analytics.rb @@ -7,7 +7,11 @@ def call(env) if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/ body = "" response.each { |part| body << part } - index = body.rindex("") + if options[:position] == :head + index = body.rindex("") + else + index = body.rindex("") + end if index if options[:async] body.insert(index, tracking_code_async(options[:web_property_id])) From c35c84953ce1e51345b1d6ba09f889c0d87da015 Mon Sep 17 00:00:00 2001 From: Francesco Gianni Date: Wed, 23 Jan 2013 14:21:40 +0100 Subject: [PATCH 2/2] UPD: readme for new :position option --- README.rdoc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.rdoc b/README.rdoc index 3b0d4d5..e9d21cb 100644 --- a/README.rdoc +++ b/README.rdoc @@ -20,6 +20,10 @@ Rack middleware to embed Google Analytics tracking code. config.middleware.use "Rack::GoogleAnalytics", :web_property_id => "UA-0000000-1", :async => true + To put code in `` instead of ``, pass `:position => :head` option + + config.middleware.use "Rack::GoogleAnalytics", :web_property_id => "UA-0000000-1", :async => true, :position => :head + == TODO and Motivations This isn't very "feature rich", because I've mostly written it as an