Skip to content

Commit

Permalink
Implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason L Perry committed Aug 13, 2009
1 parent ea8e7fe commit 7d04aa3
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 11 deletions.
Empty file removed lib/rack-google_analytics.rb
Empty file.
40 changes: 40 additions & 0 deletions lib/rack/google_analytics.rb
@@ -0,0 +1,40 @@
module Rack #:nodoc:
class GoogleAnalytics < Struct.new :app, :options

def call env
status, headers, response = app.call(env)

if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
body = ""
response.each { |part| body += part }
index = body.rindex("</body>")
if index
body.insert(index, tracking_code(options[:web_property_id]))
headers["Content-Length"] = body.length.to_s
response = body
end
end

[status, headers, response]
end

private

# Returns JS to be embeded. This takes one argument, a Web Property ID
# (aka UA number).
def tracking_code web_property_id
return <<-EOF
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("#{web_property_id}");
pageTracker._trackPageview();
} catch(err) {}</script>
EOF
end

end
end
7 changes: 0 additions & 7 deletions test/rack-google_analytics_test.rb

This file was deleted.

61 changes: 61 additions & 0 deletions test/rack/google_analytics_test.rb
@@ -0,0 +1,61 @@
require 'test_helper'
require 'rack/mock'

class Rack::GoogleAnalyticsTest < Test::Unit::TestCase

def test_embed_tracking_code_at_the_end_of_html_body
assert_match TRACKER_EXPECT, request.body
end

def test_embed_tracking_code_in_xhtml_documents
assert_match TRACKER_EXPECT, request(:content_type => "application/xhtml+xml").body
end

def test_dont_embed_code_in_non_html_documents
assert_no_match TRACKER_EXPECT, request(:content_type => "text/xml", :body => XML_DOC).body
end

def test_should_not_raise_exception_if_theres_no_html_body_tag
assert_nothing_raised { request(:body => "<html></html>") }
end

def test_shoud_buff_content_length_by_the_size_of_tracker_code
assert_equal HTML_DOC.length + 406, request.content_length
end

private

TRACKER_EXPECT = /<script.*pageTracker.*<\/script>\s?<\/body>/m

HTML_DOC = <<-EOF
<html>
<head>
<title>Rack::GoogleAnalytics</title>
</head>
<body>
<h1>Rack::GoogleAnalytics</h1>
</body>
</html>
EOF

XML_DOC = <<-EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<poem>
<title>Old Pond</title>
<author>Matsuo Basho</author>
<body>an ancient pond / a frog jumps in / the splash of water</body>
</poem>
EOF

def request opts = {}
Rack::MockRequest.new(app(opts)).get("/")
end

def app opts = {}
opts[:content_type] ||= "text/html"
opts[:body] ||= HTML_DOC
rack_app = lambda { |env| [200, { 'Content-Type' => opts[:content_type] }, opts[:body]] }
Rack::GoogleAnalytics.new(rack_app, :web_property_id => "UA-0000000-0")
end

end
5 changes: 1 addition & 4 deletions test/test_helper.rb
@@ -1,10 +1,7 @@
require 'rubygems'
require 'test/unit'
require 'shoulda'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rack-google_analytics'
require 'rack/google_analytics'

class Test::Unit::TestCase
end

0 comments on commit 7d04aa3

Please sign in to comment.