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 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]))