├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── lib ├── google-analytics │ └── instance_methods.rb ├── rack-google-analytics.rb ├── rack │ ├── google-analytics.rb │ ├── google-analytics │ │ └── version.rb │ └── templates │ │ └── async.erb └── tracking │ ├── event.rb │ └── push.rb ├── rack-google-analytics.gemspec └── test ├── helper.rb ├── test_rack-google-analytics-events.rb ├── test_rack-google-analytics-instance-methods.rb └── test_rack-google-analytics.rb /.gitignore: -------------------------------------------------------------------------------- 1 | ## MAC OS 2 | .DS_Store 3 | 4 | ## TEXTMATE 5 | *.tmproj 6 | tmtags 7 | 8 | ## EMACS 9 | *~ 10 | \#* 11 | .\#* 12 | 13 | ## VIM 14 | *.swp 15 | 16 | ## PROJECT::GENERAL 17 | coverage 18 | rdoc 19 | pkg 20 | .bundle 21 | Gemfile.lock 22 | 23 | ## PROJECT::SPECIFIC 24 | ._* 25 | *.gem 26 | pkg/* 27 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 1.9.3 4 | - 2.0.0 5 | - 2.1.0 6 | - rbx-2 7 | branches: 8 | only: 9 | - master 10 | - ga-js 11 | matrix: 12 | allow_failures: 13 | - rvm: rbx-2 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 1.2.0 2 | 3 | * E-commerce tracking 4 | 5 | ## 1.1.0 6 | 7 | * Display Advertising 8 | 9 | ## 1.0.0 10 | 11 | * migrating to analytics.js 12 | 13 | ## 0.14.0 14 | 15 | * [#34][] adding proper dependencies to the gemspec 16 | * [#36][] lambda-based tracking code support 17 | * [#35][] Added to support inpage pageid plugin 18 | 19 | ## 0.13.0 20 | 21 | * [#25][] remove duplicated version file 22 | * [#26][] Avoid error on redirection when "rack.session" is nil 23 | * [#27][] support doubleclick to display advertising 24 | * [#37][] custom adjusted bounce rates 25 | 26 | 27 | ## 0.12.0 28 | 29 | ### Improvements 30 | 31 | * [#9][] Allow to track custom variables and events 32 | * [#22][] Allow to push generic stuff into the queue 33 | * [#10][] Removed deprecated `_trackPageLoadTime` adding `set_site_speed_sample_rate` as new configuration 34 | 35 | ### Bug fix 36 | 37 | - Change order of tracker vars 38 | 39 | ## 0.11.0 40 | 41 | ### Bug fix 42 | 43 | - [#11][] Fix an issue that was causing `(deadlock; recursive locking)` errors due to body not closed. ([@rymai][]) 44 | 45 | ### Improvements 46 | 47 | - [#11][] Usage of Bundler. ([@rymai][]) 48 | - [#11][] Addition of development dependencies. ([@rymai][]) 49 | - [#11][] Creation of `lib/rack-google-analytics.rb` so `:require => 'rack/google-analytics'` in the Gemfile shouldn't needed anymore. ([@rymai][]) 50 | 51 | ## 0.10.0 52 | 53 | ### Improvements 54 | 55 | - Include the Google pagespeed code. 56 | - `README` typos fixed. 57 | 58 | ## 0.9.2 59 | 60 | ### Bug fix 61 | 62 | - Fixed a bug with lots of missing files from the Gem... how silly! 63 | 64 | ## 0.9.1 65 | 66 | ### Improvement 67 | 68 | - Updated `README` to reflect 0.9.0 merge from achiu. 69 | 70 | ## 0.9.0 71 | 72 | ### Improvement 73 | 74 | - Name changed from 'rack-google-analytics' to 'rack/google-analytics' more inline with the norm. 75 | 76 | ## 0.6.0 77 | 78 | ### Improvement 79 | 80 | - Class now named `Rack::GoogleAnalytics`, in 0.5 and earlier this was incorrectly documented as `Rack::GoogleTracker`. 81 | 82 | ## 0.2.0 83 | 84 | ### Improvement 85 | 86 | - Asynchronous code is now the default. 87 | 88 | ## 22 Jul, 2010 89 | 90 | ### Improvement 91 | 92 | - Major re-write from Arthur Chiu, now correctly writes the `Content-Length` header, and comes with tests. ([@achiu][]) 93 | - 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). 94 | 95 | ## 19 Jan, 2010 96 | 97 | ### Improvement 98 | 99 | - Makes the default snippet the async version from Google. Use regular synchronous code with: `:async => false`. ([@ralph][]) 100 | 101 | ## 27 Dec, 2009 102 | 103 | - Initial release, extracted from the Capistrano-Website project. 104 | 105 | 106 | [#11]: https://github.com/jilion/rack/issues/11 107 | [#9]: https://github.com/leehambley/rack-google-analytics/pull/9 108 | [#10]: https://github.com/leehambley/rack-google-analytics/issues/10 109 | [#22]: https://github.com/leehambley/rack-google-analytics/pull/22 110 | [#25]: https://github.com/leehambley/rack-google-analytics/issues/25 111 | [@achiu]: https://github.com/achiu 112 | [@cimm]: https://github.com/cimm 113 | [@ralph]: https://github.com/ralph 114 | [@rymai]: https://github.com/rymai 115 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | 5 | gem 'rake' 6 | gem 'pry-debugger', platforms: :mri_19 7 | gem 'rubysl', platforms: :rbx 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009-2012 Lee Hambley 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## PLEASE READ: 2 | 3 | There is a new gem for various tracking and analytics services, which includes all the functionality of *rack-google-analytics*. 4 | So if you need more than just google, **checkout the [rack-tracker](https://github.com/railslove/rack-tracker) gem**. 5 | 6 | --- 7 | 8 | # Rack google Analytics 9 | 10 | [](https://travis-ci.org/kangguru/rack-google-analytics) 11 | 12 | Simple Rack middleware to help injecting the Google Analytics tracking code in your website. 13 | 14 | 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). 15 | 16 | ## Usage 17 | 18 | #### Gemfile 19 | 20 | ```ruby 21 | gem 'rack-google-analytics' 22 | ``` 23 | 24 | #### Sinatra 25 | 26 | ```ruby 27 | ## app.rb 28 | use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x' 29 | ``` 30 | 31 | #### Padrino 32 | 33 | ```ruby 34 | ## app/app.rb 35 | use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x' 36 | ``` 37 | 38 | #### Rails 3.X and Rails 4.X 39 | 40 | ```ruby 41 | ## application.rb: 42 | config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x' 43 | ``` 44 | 45 | ### Options 46 | 47 | * `: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. 48 | * `:domain` - sets the domain name for the GATC cookies. Defaults to `auto`. 49 | * `: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 50 | * `: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. 51 | * `:enhanced_link_attribution` - Enables [Enhanced Link Attribution](https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#enhancedlink). 52 | * `:advertising` - Enables [Display Features](https://developers.google.com/analytics/devguides/collection/analyticsjs/display-features). 53 | * `:ecommerce` - Enables [Ecommerce Tracking](https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce). 54 | 55 | If you are not sure what's best, go with the defaults, and read here if you should opt-out. 56 | 57 | ## Event Tracking 58 | 59 | In your application controller, you may track an event. For example: 60 | 61 | ```ruby 62 | ga_track_event("Users", "Login", "Standard") 63 | ``` 64 | 65 | See https://developers.google.com/analytics/devguides/collection/analyticsjs/events 66 | 67 | ## Custom Push 68 | 69 | In your application controller, you may push arbritrary data. For example: 70 | 71 | ```ruby 72 | ga_push("_addItem", "ID", "SKU") 73 | ``` 74 | 75 | ## Dynamic Tracking Code 76 | 77 | You may instead define your tracking code as a lambda taking the Rack environment, so that you may set the tracking code 78 | dynamically based upon information in the Rack environment. For example: 79 | 80 | ```ruby 81 | config.middleware.use Rack::GoogleAnalytics, :tracker => lambda { |env| 82 | return env[:site_ga].tracker if env[:site_ga] 83 | } 84 | ``` 85 | 86 | ## Special use case: Event tracking only 87 | 88 | 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. 89 | 90 | config.middleware.use Rack::GoogleAnalytics 91 | 92 | 93 | ## Thread Safety 94 | 95 | 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. 96 | 97 | ## Note on Patches/Pull Requests 98 | 99 | * Fork the project. 100 | * Make your feature addition or bug fix. 101 | * Add tests for it. This is important so I don't break it in a 102 | future version unintentionally. 103 | * Commit, do not mess with rakefile, version, or history. 104 | (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) 105 | * Send me a pull request. Bonus points for topic branches. 106 | 107 | ## Copyright 108 | 109 | Copyright (c) 2009-2012 Lee Hambley. See LICENSE for details. 110 | 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. 111 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require "bundler/gem_tasks" 3 | require 'rake' 4 | 5 | require 'rake/testtask' 6 | Rake::TestTask.new(:test) do |test| 7 | test.libs << 'lib' << 'test' 8 | test.pattern = 'test/**/test_*.rb' 9 | test.verbose = true 10 | end 11 | 12 | begin 13 | require 'rcov/rcovtask' 14 | Rcov::RcovTask.new do |test| 15 | test.libs << 'test' 16 | test.pattern = 'test/**/test_*.rb' 17 | test.verbose = true 18 | end 19 | rescue LoadError 20 | task :rcov do 21 | abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov" 22 | end 23 | end 24 | 25 | task :default => :test 26 | 27 | begin 28 | require 'yard' 29 | YARD::Rake::YardocTask.new 30 | rescue LoadError 31 | task :yardoc do 32 | abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard" 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/google-analytics/instance_methods.rb: -------------------------------------------------------------------------------- 1 | # This module holds all instance methods to be 2 | # included into ActionController::Base class 3 | # for enabling google analytics var tracking in a Rails app. 4 | # 5 | require "erb" 6 | 7 | module GoogleAnalytics 8 | module InstanceMethods 9 | 10 | private 11 | 12 | def ga_custom_vars 13 | self.env["google_analytics.custom_vars"] ||= [] 14 | end 15 | 16 | def ga_events 17 | self.env["google_analytics.event_tracking"] ||= [] 18 | end 19 | 20 | protected 21 | 22 | # Tracks an event or goal on a page load 23 | # 24 | # e.g. writes 25 | # ga.('send', 'event', 'Videos', 'Play', 'Gone With the Wind'); 26 | # 27 | def ga_track_event(category, action, label = nil, value = nil) 28 | ga_events.push(GoogleAnalytics::Event.new(category, action, label, value)) 29 | end 30 | 31 | def ga_push(*attributes) 32 | var = GoogleAnalytics::Push.new(attributes) 33 | ga_events.push(var) 34 | end 35 | 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /lib/rack-google-analytics.rb: -------------------------------------------------------------------------------- 1 | require "active_support/json" 2 | require "active_support/ordered_hash" 3 | 4 | require 'rack/google-analytics' 5 | 6 | require "tracking/event" 7 | require "tracking/push" 8 | 9 | require "google-analytics/instance_methods" 10 | 11 | ActionController::Base.send(:include, GoogleAnalytics::InstanceMethods) if defined?(ActionController::Base) 12 | -------------------------------------------------------------------------------- /lib/rack/google-analytics.rb: -------------------------------------------------------------------------------- 1 | require 'rack' 2 | require 'erb' 3 | 4 | module Rack 5 | 6 | class GoogleAnalytics 7 | 8 | EVENT_TRACKING_KEY = "google_analytics.event_tracking" 9 | DEFAULT = { async: true, enhanced_link_attribution: false, advertising: false } 10 | 11 | def initialize(app, options = {}) 12 | @app, @options = app, DEFAULT.merge(options) 13 | end 14 | 15 | def call(env); dup._call(env); end 16 | 17 | def _call(env) 18 | @status, @headers, @body = @app.call(env) 19 | return [@status, @headers, @body] unless html? 20 | response = Rack::Response.new([], @status, @headers) 21 | @options[:tracker_vars] = env["google_analytics.custom_vars"] || [] 22 | 23 | if response.ok? 24 | # Write out the events now 25 | @options[:tracker_vars] += (env[EVENT_TRACKING_KEY]) unless env[EVENT_TRACKING_KEY].nil? 26 | 27 | # Get any stored events from a redirection 28 | session = env["rack.session"] 29 | stored_events = session.delete(EVENT_TRACKING_KEY) if session 30 | @options[:tracker_vars] += stored_events unless stored_events.nil? 31 | elsif response.redirection? && env["rack.session"] 32 | # Store the events until next time 33 | env["rack.session"][EVENT_TRACKING_KEY] = env[EVENT_TRACKING_KEY] 34 | end 35 | 36 | @options[:tracker] = expand_tracker(env, @options[:tracker]) 37 | 38 | @body.each { |fragment| response.write inject(fragment) } 39 | @body.close if @body.respond_to?(:close) 40 | 41 | response.finish 42 | end 43 | 44 | private 45 | 46 | def html?; @headers['Content-Type'] =~ /html/; end 47 | 48 | def inject(response) 49 | @tracker_options = { cookieDomain: @options[:domain] }.select{|k,v| v }.to_json 50 | @template ||= ::ERB.new ::File.read ::File.expand_path("../templates/async.erb",__FILE__) 51 | 52 | response.gsub(%r{}, @template.result(binding) + "") 53 | end 54 | 55 | def expand_tracker(env, tracker) 56 | tracker.respond_to?(:call) ? tracker.call(env) : tracker 57 | end 58 | 59 | end 60 | 61 | end 62 | -------------------------------------------------------------------------------- /lib/rack/google-analytics/version.rb: -------------------------------------------------------------------------------- 1 | module Rack 2 | class GoogleAnalytics 3 | VERSION = '1.2.0' 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/rack/templates/async.erb: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/tracking/event.rb: -------------------------------------------------------------------------------- 1 | require "active_support/json" 2 | require "active_support/ordered_hash" 3 | 4 | module GoogleAnalytics 5 | 6 | # A Struct that mirrors the structure of a custom var defined in Google Analytics 7 | # see https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide 8 | class Event < Struct.new(:category, :action, :label, :value) 9 | 10 | def write 11 | { hitType: 'event', eventCategory: self.category, eventAction: self.action, eventLabel: self.label, eventValue: self.value }.select{|k,v| v }.to_json 12 | end 13 | 14 | end 15 | end -------------------------------------------------------------------------------- /lib/tracking/push.rb: -------------------------------------------------------------------------------- 1 | require "active_support/json" 2 | 3 | module GoogleAnalytics 4 | class Push 5 | 6 | def initialize(attributes) 7 | @attributes = attributes 8 | end 9 | 10 | def write 11 | @attributes.to_json 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /rack-google-analytics.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $LOAD_PATH.unshift File.expand_path('../lib', __FILE__) 3 | require 'rack/google-analytics/version' 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "rack-google-analytics" 7 | s.license = "MIT" 8 | s.version = Rack::GoogleAnalytics::VERSION 9 | s.platform = Gem::Platform::RUBY 10 | s.authors = ["Lee Hambley", "Lars Brillert"] 11 | s.email = ["lee.hambley@gmail.com", "lars@railslove.com"] 12 | s.homepage = "https://github.com/kangguru/rack-google-analytics" 13 | s.summary = "Rack middleware to inject the Google Analytics tracking code into outgoing responses." 14 | 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." 15 | 16 | s.files = Dir.glob("lib/**/*") + %w(README.md LICENSE) 17 | s.require_path = 'lib' 18 | 19 | s.add_dependency 'actionpack' 20 | s.add_dependency 'activesupport' 21 | 22 | s.add_development_dependency 'bundler' 23 | s.add_development_dependency 'test-unit', '~> 2.5' 24 | s.add_development_dependency 'shoulda', '~> 2.11' 25 | s.add_development_dependency 'rack', '~> 1.2' 26 | s.add_development_dependency 'rack-test', '~> 0.5' 27 | end 28 | -------------------------------------------------------------------------------- /test/helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'test/unit' 3 | require 'shoulda' 4 | require 'rack/test' 5 | require 'active_support/core_ext/hash/slice' 6 | require File.expand_path('../../lib/rack/google-analytics', __FILE__) 7 | require File.expand_path('../../lib/tracking/event', __FILE__) 8 | 9 | $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) 10 | $LOAD_PATH.unshift(File.dirname(__FILE__)) 11 | 12 | class Test::Unit::TestCase 13 | include Rack::Test::Methods 14 | 15 | def app; 16 | Rack::Lint.new(@app); 17 | end 18 | 19 | def main_app(options) 20 | lambda { |env| 21 | 22 | env["google_analytics.event_tracking"] = options[:events] if options[:events] 23 | env["google_analytics.custom_vars"] = options[:custom_vars] if options[:custom_vars] 24 | env["misc"] = options[:misc] if options[:misc] 25 | 26 | request = Rack::Request.new(env) 27 | case request.path 28 | when '/' then 29 | [200, {'Content-Type' => 'application/html'}, ['Hello world']] 30 | when '/test.xml' then 31 | [200, {'Content-Type' => 'application/xml'}, ['Xml here']] 32 | when '/bob' then 33 | [200, {'Content-Type' => 'application/html'}, ['bob here']] 34 | when '/redirect' then 35 | [302, {'Content-Type' => 'application/html'}, ['redirection']] 36 | else 37 | [404, 'Nothing here'] 38 | end 39 | } 40 | end 41 | 42 | def mock_app(options) 43 | app_options = options.slice(:events, :custom_vars, :misc) 44 | 45 | builder = Rack::Builder.new 46 | builder.use Rack::GoogleAnalytics, options 47 | builder.run main_app(app_options) 48 | @app = builder.to_app 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /test/test_rack-google-analytics-events.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../helper',__FILE__) 2 | 3 | class TestRackGoogleAnalyticsEvents < Test::Unit::TestCase 4 | 5 | context "Asyncronous With Events" do 6 | context "default" do 7 | setup do 8 | events = [GoogleAnalytics::Event.new("Users", "Login", "Standard")] 9 | mock_app :async => true, :tracker => 'somebody', :events => events 10 | end 11 | 12 | should "show events" do 13 | get "/" 14 | 15 | assert_match %r{ga\('send', {\"hitType\":\"event\",\"eventCategory\":\"Users\",\"eventAction\":\"Login\",\"eventLabel\":\"Standard\"}\)}, last_response.body 16 | end 17 | end 18 | 19 | context "with a event value" do 20 | setup do 21 | events = [GoogleAnalytics::Event.new("Users", "Login", "Standard", 5)] 22 | mock_app :async => true, :tracker => 'somebody', :events => events 23 | end 24 | should "show events with values" do 25 | get "/" 26 | 27 | assert_match %r{ga\('send', {\"hitType\":\"event\",\"eventCategory\":\"Users\",\"eventAction\":\"Login\",\"eventLabel\":\"Standard\",\"eventValue\":5}\)}, last_response.body 28 | end 29 | end 30 | end 31 | 32 | # context "Asyncronous With Push" do 33 | # context "default" do 34 | # setup do 35 | # events = [GoogleAnalytics::Push.new(["_addItem", "ID", "SKU"])] 36 | # mock_app :async => true, :tracker => 'somebody', :events => events 37 | # end 38 | # should "show events" do 39 | # get "/" 40 | 41 | # assert_match %r{\_gaq\.push}, last_response.body 42 | # assert_match %r{_addItem.*_trackPageview}m, last_response.body 43 | # assert_match %r{ID}, last_response.body 44 | # assert_match %r{SKU}, last_response.body 45 | # assert_match %r{ga('send', )}, last_response.body 46 | # end 47 | 48 | # end 49 | # end 50 | 51 | # context "Asyncronous With Custom Vars" do 52 | # context "default" do 53 | # setup do 54 | # custom_vars = [GoogleAnalytics::CustomVar.new(1, "Items Removed", "Yes", GoogleAnalytics::CustomVar::SESSION_LEVEL)] 55 | # mock_app :async => true, :tracker => 'somebody', :custom_vars => custom_vars 56 | # end 57 | # should "show events" do 58 | # get "/" 59 | 60 | # # assert_match %r{\_gaq\.push}, last_response.body 61 | # # assert_match %r{_setCustomVar.*_trackPageview}m, last_response.body 62 | # # assert_match %r{Items Removed}, last_response.body 63 | # # assert_match %r{Yes}, last_response.body 64 | # assert_match %r{ga('set', 'Items removed', 'Yes')}, last_response.body 65 | # end 66 | 67 | # end 68 | # end 69 | 70 | end 71 | -------------------------------------------------------------------------------- /test/test_rack-google-analytics-instance-methods.rb: -------------------------------------------------------------------------------- 1 | $:.unshift File.join(File.dirname(__FILE__), '..', 'lib') 2 | 3 | require 'rubygems' 4 | require 'test/unit' 5 | require 'shoulda' 6 | require 'rack' 7 | require 'rack/test' 8 | require 'active_support/core_ext/hash/slice' 9 | require "action_controller" 10 | require File.expand_path('../../lib/rack-google-analytics', __FILE__) 11 | 12 | class TestRackGoogleAnalyticsInstanceMethods < Test::Unit::TestCase 13 | 14 | include Rack::Test::Methods 15 | 16 | class MockController < ActionController::Base 17 | def index 18 | ga_track_event("Users", "Login", "Standard") 19 | ga_push("_addItem", "ID", "SKU") 20 | render :inline => "