Skip to content

Commit

Permalink
Merge pull request #4 from achiu/master
Browse files Browse the repository at this point in the history
page speed addition/Sync tag update
  • Loading branch information
leehambley committed Jun 3, 2011
2 parents 74729c7 + b29ed0a commit 9b0bb29
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
30 changes: 26 additions & 4 deletions README.md
Expand Up @@ -8,13 +8,35 @@ Formerly this gem had an environments setting, that has been removed pending ref

## Usage

require 'rack/google-analytics'
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
#### Gemfile:
gem 'rack-google-analytics', :require => 'rack/google-analytics'

#### Sinatra
## app.rb
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'

#### Padrino

## app/app.rb
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'

#### Rails

## environment.rb:
config.gem 'rack-google-analytcs', :lib => 'rack/google-analytics'
config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'


### Options

* :async - sets to use asyncronous tracker
* :multiple - sets track for multiple sub domains. (must also set :domain)
* :top_level - sets tracker for multiple top-level domains. (must also set :domain)


Note: since 0.2.0 this will use the asynchronous google tracker code, for the traditional behaviour please use:

require 'rack-google-analytics'
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x', :async => false
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x', :async => false

If you are not sure what's best, go with the defaults, and read here if you should opt-out

Expand Down
2 changes: 1 addition & 1 deletion lib/rack/google-analytics.rb
Expand Up @@ -32,7 +32,7 @@ def inject(response)
if @options[:async]
response.gsub(%r{</head>}, @template.result(binding) + "</head>")
else
response.gsub(%r{</body>}, "</body>" + @template.result(binding))
response.gsub(%r{</body>}, @template.result(binding) + "</body>")
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/rack/templates/async.erb
Expand Up @@ -10,6 +10,7 @@
_gaq.push(['_setAllowLinker', true]);
<% end %>
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
Expand Down
6 changes: 3 additions & 3 deletions test/test_rack-google-analytics.rb
Expand Up @@ -10,7 +10,7 @@ class TestRackGoogleAnalytics < Test::Unit::TestCase
assert_match %r{\_gaq\.push}, last_response.body
assert_match %r{\'\_setAccount\', \"somebody\"}, last_response.body
assert_match %r{</script></head>}, last_response.body
assert_equal "495", last_response.headers['Content-Length']
assert_equal "532", last_response.headers['Content-Length']
end

should "not add tracker to none html content-type" do
Expand All @@ -31,7 +31,7 @@ class TestRackGoogleAnalytics < Test::Unit::TestCase
should "add multiple domain script" do
get "/"
assert_match %r{'_setDomainName', \"mydomain.com\"}, last_response.body
assert_equal "542", last_response.headers['Content-Length']
assert_equal "579", last_response.headers['Content-Length']
end
end

Expand All @@ -51,7 +51,7 @@ class TestRackGoogleAnalytics < Test::Unit::TestCase
should "show non-asyncronous tracker" do
get "/bob"
assert_match %r{_gat._getTracker}, last_response.body
assert_match %r{</body><script type=\"text\/javascript\">}, last_response.body
assert_match %r{</script></body>}, last_response.body
assert_match %r{\"whatthe\"}, last_response.body
end
end
Expand Down

0 comments on commit 9b0bb29

Please sign in to comment.