pax_global_header00006660000000000000000000000064123657470750014532gustar00rootroot0000000000000052 comment=60c23a0836b9cef16809772271172eeab4fb6f9b rack-google-analytics-1.2.0/000077500000000000000000000000001236574707500157115ustar00rootroot00000000000000rack-google-analytics-1.2.0/.gitignore000077500000000000000000000002671236574707500177110ustar00rootroot00000000000000## MAC OS .DS_Store ## TEXTMATE *.tmproj tmtags ## EMACS *~ \#* .\#* ## VIM *.swp ## PROJECT::GENERAL coverage rdoc pkg .bundle Gemfile.lock ## PROJECT::SPECIFIC ._* *.gem pkg/* rack-google-analytics-1.2.0/.travis.yml000066400000000000000000000002221236574707500200160ustar00rootroot00000000000000language: ruby rvm: - 1.9.3 - 2.0.0 - 2.1.0 - rbx-2 branches: only: - master - ga-js matrix: allow_failures: - rvm: rbx-2 rack-google-analytics-1.2.0/CHANGELOG.md000066400000000000000000000055321236574707500175270ustar00rootroot00000000000000## 1.2.0 * E-commerce tracking ## 1.1.0 * Display Advertising ## 1.0.0 * migrating to analytics.js ## 0.14.0 * [#34][] adding proper dependencies to the gemspec * [#36][] lambda-based tracking code support * [#35][] Added to support inpage pageid plugin ## 0.13.0 * [#25][] remove duplicated version file * [#26][] Avoid error on redirection when "rack.session" is nil * [#27][] support doubleclick to display advertising * [#37][] custom adjusted bounce rates ## 0.12.0 ### Improvements * [#9][] Allow to track custom variables and events * [#22][] Allow to push generic stuff into the queue * [#10][] Removed deprecated `_trackPageLoadTime` adding `set_site_speed_sample_rate` as new configuration ### Bug fix - Change order of tracker vars ## 0.11.0 ### Bug fix - [#11][] Fix an issue that was causing `(deadlock; recursive locking)` errors due to body not closed. ([@rymai][]) ### Improvements - [#11][] Usage of Bundler. ([@rymai][]) - [#11][] Addition of development dependencies. ([@rymai][]) - [#11][] Creation of `lib/rack-google-analytics.rb` so `:require => 'rack/google-analytics'` in the Gemfile shouldn't needed anymore. ([@rymai][]) ## 0.10.0 ### Improvements - Include the Google pagespeed code. - `README` typos fixed. ## 0.9.2 ### Bug fix - Fixed a bug with lots of missing files from the Gem... how silly! ## 0.9.1 ### Improvement - Updated `README` to reflect 0.9.0 merge from achiu. ## 0.9.0 ### Improvement - Name changed from 'rack-google-analytics' to 'rack/google-analytics' more inline with the norm. ## 0.6.0 ### Improvement - Class now named `Rack::GoogleAnalytics`, in 0.5 and earlier this was incorrectly documented as `Rack::GoogleTracker`. ## 0.2.0 ### Improvement - Asynchronous code is now the default. ## 22 Jul, 2010 ### Improvement - Major re-write from Arthur Chiu, now correctly writes the `Content-Length` header, and comes with tests. ([@achiu][]) - This patch also backs-out the changes from [@cimm][] - but they were un-tested (I intend to bring these back as soon as possible; this will probably constitute a 1.0 release when it happens). ## 19 Jan, 2010 ### Improvement - Makes the default snippet the async version from Google. Use regular synchronous code with: `:async => false`. ([@ralph][]) ## 27 Dec, 2009 - Initial release, extracted from the Capistrano-Website project. [#11]: https://github.com/jilion/rack/issues/11 [#9]: https://github.com/leehambley/rack-google-analytics/pull/9 [#10]: https://github.com/leehambley/rack-google-analytics/issues/10 [#22]: https://github.com/leehambley/rack-google-analytics/pull/22 [#25]: https://github.com/leehambley/rack-google-analytics/issues/25 [@achiu]: https://github.com/achiu [@cimm]: https://github.com/cimm [@ralph]: https://github.com/ralph [@rymai]: https://github.com/rymai rack-google-analytics-1.2.0/Gemfile000066400000000000000000000001701236574707500172020ustar00rootroot00000000000000source 'https://rubygems.org' gemspec gem 'rake' gem 'pry-debugger', platforms: :mri_19 gem 'rubysl', platforms: :rbx rack-google-analytics-1.2.0/LICENSE000077500000000000000000000020441236574707500167210ustar00rootroot00000000000000Copyright (c) 2009-2012 Lee Hambley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rack-google-analytics-1.2.0/README.md000077500000000000000000000106461236574707500172020ustar00rootroot00000000000000# Rack google Analytics [![Build Status](https://travis-ci.org/kangguru/rack-google-analytics.png?branch=analytics-js)](https://travis-ci.org/kangguru/rack-google-analytics) Simple Rack middleware to help injecting the Google Analytics tracking code in your website. This middleware injects the Google Analytics tracking code into the correct place of any request only when the response's `Content-Type` header contains `html` (therefore `text/html` and similar). ## Usage #### Gemfile ```ruby gem 'rack-google-analytics' ``` #### Sinatra ```ruby ## app.rb use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x' ``` #### Padrino ```ruby ## app/app.rb use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x' ``` #### Rails 3.X and Rails 4.X ```ruby ## application.rb: config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x' ``` ### Options * `: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 * `:adjusted_bounce_rate_timeouts` - An array of times in seconds that the tracker will use to set timeouts for adjusted bounce rate tracking. See http://analytics.blogspot.ca/2012/07/tracking-adjusted-bounce-rate-in-google.html for details. * `:enhanced_link_attribution` - Enables [Enhanced Link Attribution](https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#enhancedlink). * `:advertising` - Enables [Display Features](https://developers.google.com/analytics/devguides/collection/analyticsjs/display-features). * `:ecommerce` - Enables [Ecommerce Tracking](https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce). If you are not sure what's best, go with the defaults, and read here if you should opt-out. ## Event Tracking In your application controller, you may track an event. For example: ```ruby ga_track_event("Users", "Login", "Standard") ``` See https://developers.google.com/analytics/devguides/collection/analyticsjs/events ## Custom Push In your application controller, you may push arbritrary data. For example: ```ruby ga_push("_addItem", "ID", "SKU") ``` ## Dynamic Tracking Code You may instead define your tracking code as a lambda taking the Rack environment, so that you may set the tracking code dynamically based upon information in the Rack environment. For example: ```ruby config.middleware.use Rack::GoogleAnalytics, :tracker => lambda { |env| return env[:site_ga].tracker if env[:site_ga] } ``` ## Special use case: Event tracking only If you already set up your Google Analytics `analytics.js` tracker object with pageview tracking in your templates/frontend (inside the ``), the only thing you might want to use the `rack-google-analytics` middleware for is to track server-side events which you can't properly track in the forntend. In that case simply use the middleware without specifying the `:tracker` option, then it will only render the event tracking code (`ga('send', hitType: 'event', ..)`) and nothing else. config.middleware.use Rack::GoogleAnalytics ## Thread Safety This middleware *should* be thread safe. Although my experience in such areas is limited, having taken the advice of those with more experience; I defer the call to a shallow copy of the environment, if this is of consequence to you please review the implementation. ## Note on Patches/Pull Requests * Fork the project. * Make your feature addition or bug fix. * Add tests for it. This is important so I don't break it in a future version unintentionally. * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) * Send me a pull request. Bonus points for topic branches. ## Copyright Copyright (c) 2009-2012 Lee Hambley. See LICENSE for details. With thanks to [Ralph von der Heyden](https://github.com/ralph) and [Simon Schoeters](https://github.com/cimm) - And the biggest hand to [Arthur Chiu](https://github.com/achiu) for the huge work that went into the massive 0.9 re-factor. rack-google-analytics-1.2.0/Rakefile000077500000000000000000000013311236574707500173570ustar00rootroot00000000000000require 'rubygems' require "bundler/gem_tasks" require 'rake' require 'rake/testtask' Rake::TestTask.new(:test) do |test| test.libs << 'lib' << 'test' test.pattern = 'test/**/test_*.rb' test.verbose = true end begin require 'rcov/rcovtask' Rcov::RcovTask.new do |test| test.libs << 'test' test.pattern = 'test/**/test_*.rb' test.verbose = true end rescue LoadError task :rcov do abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov" end end task :default => :test begin require 'yard' YARD::Rake::YardocTask.new rescue LoadError task :yardoc do abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard" end end rack-google-analytics-1.2.0/lib/000077500000000000000000000000001236574707500164575ustar00rootroot00000000000000rack-google-analytics-1.2.0/lib/google-analytics/000077500000000000000000000000001236574707500217205ustar00rootroot00000000000000rack-google-analytics-1.2.0/lib/google-analytics/instance_methods.rb000066400000000000000000000015111236574707500255720ustar00rootroot00000000000000# This module holds all instance methods to be # included into ActionController::Base class # for enabling google analytics var tracking in a Rails app. # require "erb" module GoogleAnalytics module InstanceMethods private def ga_custom_vars self.env["google_analytics.custom_vars"] ||= [] end def ga_events self.env["google_analytics.event_tracking"] ||= [] end protected # Tracks an event or goal on a page load # # e.g. writes # ga.('send', 'event', 'Videos', 'Play', 'Gone With the Wind'); # def ga_track_event(category, action, label = nil, value = nil) ga_events.push(GoogleAnalytics::Event.new(category, action, label, value)) end def ga_push(*attributes) var = GoogleAnalytics::Push.new(attributes) ga_events.push(var) end end end rack-google-analytics-1.2.0/lib/rack-google-analytics.rb000066400000000000000000000004611236574707500231640ustar00rootroot00000000000000require "active_support/json" require "active_support/ordered_hash" require 'rack/google-analytics' require "tracking/event" require "tracking/push" require "google-analytics/instance_methods" ActionController::Base.send(:include, GoogleAnalytics::InstanceMethods) if defined?(ActionController::Base) rack-google-analytics-1.2.0/lib/rack/000077500000000000000000000000001236574707500173775ustar00rootroot00000000000000rack-google-analytics-1.2.0/lib/rack/google-analytics.rb000066400000000000000000000035131236574707500231670ustar00rootroot00000000000000require 'rack' require 'erb' module Rack class GoogleAnalytics EVENT_TRACKING_KEY = "google_analytics.event_tracking" DEFAULT = { async: true, enhanced_link_attribution: false, advertising: false } def initialize(app, options = {}) @app, @options = app, DEFAULT.merge(options) end def call(env); dup._call(env); end def _call(env) @status, @headers, @body = @app.call(env) return [@status, @headers, @body] unless html? response = Rack::Response.new([], @status, @headers) @options[:tracker_vars] = env["google_analytics.custom_vars"] || [] if response.ok? # Write out the events now @options[:tracker_vars] += (env[EVENT_TRACKING_KEY]) unless env[EVENT_TRACKING_KEY].nil? # Get any stored events from a redirection session = env["rack.session"] stored_events = session.delete(EVENT_TRACKING_KEY) if session @options[:tracker_vars] += stored_events unless stored_events.nil? elsif response.redirection? && env["rack.session"] # Store the events until next time env["rack.session"][EVENT_TRACKING_KEY] = env[EVENT_TRACKING_KEY] end @options[:tracker] = expand_tracker(env, @options[:tracker]) @body.each { |fragment| response.write inject(fragment) } @body.close if @body.respond_to?(:close) response.finish end private def html?; @headers['Content-Type'] =~ /html/; end def inject(response) @tracker_options = { cookieDomain: @options[:domain] }.select{|k,v| v }.to_json @template ||= ::ERB.new ::File.read ::File.expand_path("../templates/async.erb",__FILE__) response.gsub(%r{}, @template.result(binding) + "") end def expand_tracker(env, tracker) tracker.respond_to?(:call) ? tracker.call(env) : tracker end end end rack-google-analytics-1.2.0/lib/rack/google-analytics/000077500000000000000000000000001236574707500226405ustar00rootroot00000000000000rack-google-analytics-1.2.0/lib/rack/google-analytics/version.rb000066400000000000000000000001041236574707500246450ustar00rootroot00000000000000module Rack class GoogleAnalytics VERSION = '1.2.0' end end rack-google-analytics-1.2.0/lib/rack/templates/000077500000000000000000000000001236574707500213755ustar00rootroot00000000000000rack-google-analytics-1.2.0/lib/rack/templates/async.erb000066400000000000000000000023451236574707500232100ustar00rootroot00000000000000rack-google-analytics-1.2.0/lib/tracking/000077500000000000000000000000001236574707500202615ustar00rootroot00000000000000rack-google-analytics-1.2.0/lib/tracking/event.rb000066400000000000000000000010111236574707500217200ustar00rootroot00000000000000require "active_support/json" require "active_support/ordered_hash" module GoogleAnalytics # A Struct that mirrors the structure of a custom var defined in Google Analytics # see https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide class Event < Struct.new(:category, :action, :label, :value) def write { hitType: 'event', eventCategory: self.category, eventAction: self.action, eventLabel: self.label, eventValue: self.value }.select{|k,v| v }.to_json end end endrack-google-analytics-1.2.0/lib/tracking/push.rb000066400000000000000000000003051236574707500215630ustar00rootroot00000000000000require "active_support/json" module GoogleAnalytics class Push def initialize(attributes) @attributes = attributes end def write @attributes.to_json end end end rack-google-analytics-1.2.0/rack-google-analytics.gemspec000066400000000000000000000022761236574707500234440ustar00rootroot00000000000000# -*- encoding: utf-8 -*- $LOAD_PATH.unshift File.expand_path('../lib', __FILE__) require 'rack/google-analytics/version' Gem::Specification.new do |s| s.name = "rack-google-analytics" s.license = "MIT" s.version = Rack::GoogleAnalytics::VERSION s.platform = Gem::Platform::RUBY s.authors = ["Lee Hambley", "Lars Brillert"] s.email = ["lee.hambley@gmail.com", "lars@railslove.com"] s.homepage = "https://github.com/kangguru/rack-google-analytics" s.summary = "Rack middleware to inject the Google Analytics tracking code into outgoing responses." s.description = "Simple Rack middleware for implementing google analytics tracking in your Ruby-Rack based project. Supports synchronous and asynchronous insertion and configurable load options." s.files = Dir.glob("lib/**/*") + %w(README.md LICENSE) s.require_path = 'lib' s.add_dependency 'actionpack' s.add_dependency 'activesupport' s.add_development_dependency 'bundler' s.add_development_dependency 'test-unit', '~> 2.5' s.add_development_dependency 'shoulda', '~> 2.11' s.add_development_dependency 'rack', '~> 1.2' s.add_development_dependency 'rack-test', '~> 0.5' end rack-google-analytics-1.2.0/test/000077500000000000000000000000001236574707500166705ustar00rootroot00000000000000rack-google-analytics-1.2.0/test/helper.rb000077500000000000000000000030101236574707500204710ustar00rootroot00000000000000require 'rubygems' require 'test/unit' require 'shoulda' require 'rack/test' require 'active_support/core_ext/hash/slice' require File.expand_path('../../lib/rack/google-analytics', __FILE__) require File.expand_path('../../lib/tracking/event', __FILE__) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) class Test::Unit::TestCase include Rack::Test::Methods def app; Rack::Lint.new(@app); end def main_app(options) lambda { |env| env["google_analytics.event_tracking"] = options[:events] if options[:events] env["google_analytics.custom_vars"] = options[:custom_vars] if options[:custom_vars] env["misc"] = options[:misc] if options[:misc] request = Rack::Request.new(env) case request.path when '/' then [200, {'Content-Type' => 'application/html'}, ['Hello world']] when '/test.xml' then [200, {'Content-Type' => 'application/xml'}, ['Xml here']] when '/bob' then [200, {'Content-Type' => 'application/html'}, ['bob here']] when '/redirect' then [302, {'Content-Type' => 'application/html'}, ['redirection']] else [404, 'Nothing here'] end } end def mock_app(options) app_options = options.slice(:events, :custom_vars, :misc) builder = Rack::Builder.new builder.use Rack::GoogleAnalytics, options builder.run main_app(app_options) @app = builder.to_app end end rack-google-analytics-1.2.0/test/test_rack-google-analytics-events.rb000077500000000000000000000046001236574707500257400ustar00rootroot00000000000000require File.expand_path('../helper',__FILE__) class TestRackGoogleAnalyticsEvents < Test::Unit::TestCase context "Asyncronous With Events" do context "default" do setup do events = [GoogleAnalytics::Event.new("Users", "Login", "Standard")] mock_app :async => true, :tracker => 'somebody', :events => events end should "show events" do get "/" assert_match %r{ga\('send', {\"hitType\":\"event\",\"eventCategory\":\"Users\",\"eventAction\":\"Login\",\"eventLabel\":\"Standard\"}\)}, last_response.body end end context "with a event value" do setup do events = [GoogleAnalytics::Event.new("Users", "Login", "Standard", 5)] mock_app :async => true, :tracker => 'somebody', :events => events end should "show events with values" do get "/" assert_match %r{ga\('send', {\"hitType\":\"event\",\"eventCategory\":\"Users\",\"eventAction\":\"Login\",\"eventLabel\":\"Standard\",\"eventValue\":5}\)}, last_response.body end end end # context "Asyncronous With Push" do # context "default" do # setup do # events = [GoogleAnalytics::Push.new(["_addItem", "ID", "SKU"])] # mock_app :async => true, :tracker => 'somebody', :events => events # end # should "show events" do # get "/" # assert_match %r{\_gaq\.push}, last_response.body # assert_match %r{_addItem.*_trackPageview}m, last_response.body # assert_match %r{ID}, last_response.body # assert_match %r{SKU}, last_response.body # assert_match %r{ga('send', )}, last_response.body # end # end # end # context "Asyncronous With Custom Vars" do # context "default" do # setup do # custom_vars = [GoogleAnalytics::CustomVar.new(1, "Items Removed", "Yes", GoogleAnalytics::CustomVar::SESSION_LEVEL)] # mock_app :async => true, :tracker => 'somebody', :custom_vars => custom_vars # end # should "show events" do # get "/" # # assert_match %r{\_gaq\.push}, last_response.body # # assert_match %r{_setCustomVar.*_trackPageview}m, last_response.body # # assert_match %r{Items Removed}, last_response.body # # assert_match %r{Yes}, last_response.body # assert_match %r{ga('set', 'Items removed', 'Yes')}, last_response.body # end # end # end end rack-google-analytics-1.2.0/test/test_rack-google-analytics-instance-methods.rb000066400000000000000000000041551236574707500277030ustar00rootroot00000000000000$:.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'rubygems' require 'test/unit' require 'shoulda' require 'rack' require 'rack/test' require 'active_support/core_ext/hash/slice' require "action_controller" require File.expand_path('../../lib/rack-google-analytics', __FILE__) class TestRackGoogleAnalyticsInstanceMethods < Test::Unit::TestCase include Rack::Test::Methods class MockController < ActionController::Base def index ga_track_event("Users", "Login", "Standard") ga_push("_addItem", "ID", "SKU") render :inline => "TitleHello World" end def action_method?(name) true end end def controller MockController.action(:index) end # Build an app to call our MockController with GoogleAnalytics middleware def mock_app(options) builder = Rack::Builder.new builder.use Rack::GoogleAnalytics, options builder.run controller @app = builder.to_app end def app; Rack::Lint.new(@app); end context "Instance Methods" do setup { mock_app :async => true, :tracker => 'whatthe' } context "pass variables to rack" do should "have event tracking" do get "/" assert last_response.ok? assert_match %r{ga\('send', {\"hitType\":\"event\",\"eventCategory\":\"Users\",\"eventAction\":\"Login\",\"eventLabel\":\"Standard\"}\)}, last_response.body end # should "have custom vars" do # get "/" # assert last_response.ok? # assert_match %r{\_gaq\.push}, last_response.body # assert_match %r{_setCustomVar.*_trackPageview}m, last_response.body # assert_match %r{Items Removed}, last_response.body # assert_match %r{Yes}, last_response.body # end # should "have generic push" do # get "/" # assert last_response.ok? # assert_match %r{\_gaq\.push}, last_response.body # assert_match %r{_addItem.*_trackPageview}m, last_response.body # assert_match %r{ID}, last_response.body # assert_match %r{SKU}, last_response.body # end end end end rack-google-analytics-1.2.0/test/test_rack-google-analytics.rb000077500000000000000000000067171236574707500244510ustar00rootroot00000000000000require 'helper' class TestRackGoogleAnalytics < Test::Unit::TestCase context "Asyncronous" do context "default" do setup { mock_app tracker: 'somebody' } should "show asyncronous tracker" do get "/" assert_match %r{ga\('create', 'somebody', {}\)}, last_response.body assert_match %r{ga\('send', 'pageview'\)}, last_response.body assert_match %r{}, last_response.body end should "not add tracker to none html content-type" do get "/test.xml" assert_no_match %r{ga\('create', 'somebody', {}\)}, last_response.body assert_match %r{Xml here}, last_response.body end should "not add without " do get "/bob" assert_no_match %r{ga\('create', 'somebody', {}\)}, last_response.body assert_match %r{bob here}, last_response.body end should "redirects" do get "/redirect" assert_equal 302, last_response.status end end context "with custom domain" do setup { mock_app tracker: 'somebody', domain: "railslabs.com" } should "show asyncronous tracker with cookieDomain" do get "/" assert_match %r{ga\('create', 'somebody', {\"cookieDomain\":\"railslabs.com\"}\)}, last_response.body assert_match %r{ga\('send', 'pageview'\)}, last_response.body assert_match %r{}, last_response.body end end context "with enhanced_link_attribution" do setup { mock_app tracker: 'happy', enhanced_link_attribution: true } should "embedded the linkid plugin script" do get "/" assert_match %r{linkid.js}, last_response.body end end context "with advertising" do setup { mock_app tracker: 'happy', advertising: true } should "require displayfeatures" do get "/" assert_match %r{ga\('require', 'displayfeatures'\)}, last_response.body end end context "with e-commerce" do setup { mock_app tracker: 'happy', ecommerce: true } should "require ecommerce" do get "/" assert_match %r{ga\('require', 'ecommerce', 'ecommerce\.js'\)}, last_response.body end end context "with anonymizeIp" do setup { mock_app :async => true, :tracker => 'happy', :anonymize_ip => true } should "set anonymizeIp to true" do get "/" assert_match %r{ga\('set', 'anonymizeIp', true\)}, last_response.body end end context "with dynamic tracker" do setup do mock_app tracker: lambda { |env| return env["misc"] }, misc: "foobar" end should 'call tracker lambdas to obtain tracking codes' do get '/' assert_match %r{ga\('create', 'foobar', {}\)}, last_response.body end end context 'adjusted bounce rate' do setup do mock_app tracker: 'afake', adjusted_bounce_rate_timeouts: [15, 30] end should "add timeouts to push read events" do get "/" assert_match %r{ga\('send', 'event', '15_seconds', 'read'\)}, last_response.body assert_match %r{ga\('send', 'event', '30_seconds', 'read'\)}, 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 # get "/" # assert_match %r{'_setSiteSpeedSampleRate', 5}, last_response.body # end # end end end