diff --git a/README.md b/README.md index 45605a6..da16551 100755 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x' ### Options +* `:tracker` - sets the Google Analytics tracker +* `:trackers` - array of arrays to set multiple trackers. Takes the form `[['name1', 'tracker1'], ['name2', 'tracker2'], ...]`. Note that `name1` in this example will be ignored because the first tracker is the default. All additional trackers must be named. See https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#multipletrackers for details. * `:anonymize_ip` - sets the tracker to remove the last octet from all IP addresses, see https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat?hl=de#_gat._anonymizeIp for details. * `:domain` - sets the domain name for the GATC cookies. Defaults to `auto`. * `:site_speed_sample_rate` - Defines a new sample set size for Site Speed data collection, see https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiBasicConfiguration?hl=de#_gat.GA_Tracker_._setSiteSpeedSampleRate diff --git a/lib/rack/google-analytics.rb b/lib/rack/google-analytics.rb index 8c744c0..eae4e3d 100644 --- a/lib/rack/google-analytics.rb +++ b/lib/rack/google-analytics.rb @@ -33,11 +33,14 @@ def _call(env) env["rack.session"][EVENT_TRACKING_KEY] = env[EVENT_TRACKING_KEY] end - @options[:tracker] = expand_tracker(env, @options[:tracker]) + @tracker = expand_tracker(env, @options[:tracker]) + @trackers = expand_trackers(env, @options[:trackers]) @body.each { |fragment| response.write inject(fragment) } @body.close if @body.respond_to?(:close) + @tracker = nil + @trackers = nil response.finish end @@ -46,9 +49,8 @@ def _call(env) def html?; @headers['Content-Type'] =~ /html/; end def inject(response) - @tracker_options = { cookieDomain: @options[:domain] }.select{|k,v| v }.to_json + @tracker_options = { cookieDomain: @options[:domain] }.select{|k,v| v } @template ||= ::ERB.new ::File.read ::File.expand_path("../templates/async.erb",__FILE__) - response.gsub(%r{}, @template.result(binding) + "") end @@ -56,6 +58,10 @@ def expand_tracker(env, tracker) tracker.respond_to?(:call) ? tracker.call(env) : tracker end + def expand_trackers(env, trackers) + trackers.respond_to?(:call) ? trackers.call(env) : trackers + end + end end diff --git a/lib/rack/templates/async.erb b/lib/rack/templates/async.erb index 2d3c27d..27de317 100644 --- a/lib/rack/templates/async.erb +++ b/lib/rack/templates/async.erb @@ -1,12 +1,19 @@ \ No newline at end of file diff --git a/test/test_rack-google-analytics.rb b/test/test_rack-google-analytics.rb index a32ff5c..0a24c25 100755 --- a/test/test_rack-google-analytics.rb +++ b/test/test_rack-google-analytics.rb @@ -86,6 +86,15 @@ class TestRackGoogleAnalytics < Test::Unit::TestCase get '/' assert_match %r{ga\('create', 'foobar', {}\)}, last_response.body end + + # TODO couldn't figure out how to alter the env for the same app + # should 'call tracker lambdas for each request' do + # get '/' + # assert_match %r{ga\('create', 'foobar', {}\)}, last_response.body + # # TODO magically change env['misc'] to "beeblebrax" + # get '/' + # assert_match %r{ga\('create', 'beeblebrax', {}\)}, last_response.body + # end end context 'adjusted bounce rate' do @@ -99,6 +108,33 @@ class TestRackGoogleAnalytics < Test::Unit::TestCase end end + context "with multiple trackers" do + setup { mock_app trackers: [['name1','horchata'], ['name2','slurpee']]} + should "show multiple trackers" do + get "/" + assert_match %r{ga\('create', 'horchata', {}\)}, last_response.body + assert_match %r{ga\('create', 'slurpee', {"name":"name2"}\)}, last_response.body + end + should "should trigger pageview for each tracker" do + get "/" + assert_match %r{ga\('send', 'pageview'\);}, last_response.body + assert_match %r{ga\('name2.send', 'pageview'\);}, last_response.body + end + end + + context "with multiple trackers block" do + setup do + mock_app trackers: lambda {|env| + [['name1','horchata'], ['name2','slurpee']] + } + end + should "show multiple trackers" do + get "/" + assert_match %r{ga\('create', 'horchata', {}\)}, last_response.body + assert_match %r{ga\('create', 'slurpee', {"name":"name2"}\)}, last_response.body + end + end + # context "with custom _setSiteSpeedSampleRate" do # setup { mock_app :async => true, :tracker => 'happy', :site_speed_sample_rate => 5 } # should "add top_level domain script" do