Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<head>` instead of `<body>`, 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
Expand Down
6 changes: 5 additions & 1 deletion lib/rack/google_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("</body>")
if options[:position] == :head
index = body.rindex("</head>")
else
index = body.rindex("</body>")
end
if index
if options[:async]
body.insert(index, tracking_code_async(options[:web_property_id]))
Expand Down