forked from grays/rack-google_analytics
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jason L Perry
committed
Aug 13, 2009
1 parent
ea8e7fe
commit 7d04aa3
Showing
5 changed files
with
102 additions
and
11 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |