Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Added support for automatically setting of domain #12

wants to merge 1 commit into from

2 participants


Setting domain automatically based on Rack request environment.


I must admit I am a bit new to Google Analytics...

Is this Pull Request pointless? Is the google analytics behaviour provided with this:

    config.middleware.use Rack::GoogleAnalytics,{
      :tracker => 'UA-12345-1',
      :async => true,
      :multiple => true,

exactly the same as this:

    config.middleware.use Rack::GoogleAnalytics,{
      :tracker => 'UA-12345-1',
      :async => true,

I ask, because if so, the pull request seem pointless


As the default behavior of setDomainName is auto which will try to obtain the current host from _setDomainName this will be pretty much the same. Only thing is that you'll have to also set the domain config if you use the multiple option.


So your point is when not having multiple domains, the default behaviour is automatic domain detection and setting. Setting the domain option is only necessary when :multiple => true.

If so, maybe some description of this in the README would help.


Thats how i get it :) I've added a bit to the README

@kangguru kangguru closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 8, 2012
  1. @jarl-dk
This page is out of date. Refresh to see the latest.
1  lib/rack/google-analytics.rb
@@ -16,6 +16,7 @@ def call(env); dup._call(env); end
def _call(env)
@status, @headers, @body =
+ @domain = @options[:domain] || env['SERVER_NAME']
return [@status, @headers, @body] unless html?
response =[], @status, @headers)
@body.each { |fragment| response.write inject(fragment) }
2  lib/rack/templates/async.erb
@@ -3,7 +3,7 @@
var _gaq = _gaq || [];
_gaq.push(['_setAccount', <%= @options[:tracker].inspect %>]);
<% if @options[:multiple] %>
- _gaq.push(['_setDomainName', <%= @options[:domain].inspect %>]);
+ _gaq.push(['_setDomainName', <%= @domain.inspect %>]);
<% end %>
<% if @options[:top_level] %>
_gaq.push(['_setDomainName', 'none']);
9 test/test_rack-google-analytics.rb
@@ -35,6 +35,15 @@ class TestRackGoogleAnalytics < Test::Unit::TestCase
+ context "multiple sub domains without domain option" do
+ setup { mock_app :async => true, :multiple => true, :tracker => 'gonna'}
+ should "add multiple domain script" do
+ get "/"
+ assert_match %r{'_setDomainName', \"\"}, last_response.body
+ assert_equal "578", last_response.headers['Content-Length']
+ end
+ end
context "multiple top-level domains" do
setup { mock_app :async => true, :top_level => true, :tracker => 'get', :domain => '' }
should "add top_level domain script" do
Something went wrong with that request. Please try again.