pax_global_header 0000666 0000000 0000000 00000000064 12365747075 0014532 g ustar 00root root 0000000 0000000 52 comment=60c23a0836b9cef16809772271172eeab4fb6f9b rack-google-analytics-1.2.0/ 0000775 0000000 0000000 00000000000 12365747075 0015711 5 ustar 00root root 0000000 0000000 rack-google-analytics-1.2.0/.gitignore 0000775 0000000 0000000 00000000267 12365747075 0017711 0 ustar 00root root 0000000 0000000 ## 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.yml 0000664 0000000 0000000 00000000222 12365747075 0020016 0 ustar 00root root 0000000 0000000 language: 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.md 0000664 0000000 0000000 00000005532 12365747075 0017527 0 ustar 00root root 0000000 0000000 ## 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/Gemfile 0000664 0000000 0000000 00000000170 12365747075 0017202 0 ustar 00root root 0000000 0000000 source 'https://rubygems.org' gemspec gem 'rake' gem 'pry-debugger', platforms: :mri_19 gem 'rubysl', platforms: :rbx rack-google-analytics-1.2.0/LICENSE 0000775 0000000 0000000 00000002044 12365747075 0016721 0 ustar 00root root 0000000 0000000 Copyright (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.md 0000775 0000000 0000000 00000010646 12365747075 0017202 0 ustar 00root root 0000000 0000000 # Rack google Analytics [](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/Rakefile 0000775 0000000 0000000 00000001331 12365747075 0017357 0 ustar 00root root 0000000 0000000 require '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/ 0000775 0000000 0000000 00000000000 12365747075 0016457 5 ustar 00root root 0000000 0000000 rack-google-analytics-1.2.0/lib/google-analytics/ 0000775 0000000 0000000 00000000000 12365747075 0021720 5 ustar 00root root 0000000 0000000 rack-google-analytics-1.2.0/lib/google-analytics/instance_methods.rb 0000664 0000000 0000000 00000001511 12365747075 0025572 0 ustar 00root root 0000000 0000000 # 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.rb 0000664 0000000 0000000 00000000461 12365747075 0023164 0 ustar 00root root 0000000 0000000 require "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/ 0000775 0000000 0000000 00000000000 12365747075 0017377 5 ustar 00root root 0000000 0000000 rack-google-analytics-1.2.0/lib/rack/google-analytics.rb 0000664 0000000 0000000 00000003513 12365747075 0023167 0 ustar 00root root 0000000 0000000 require '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/ 0000775 0000000 0000000 00000000000 12365747075 0022640 5 ustar 00root root 0000000 0000000 rack-google-analytics-1.2.0/lib/rack/google-analytics/version.rb 0000664 0000000 0000000 00000000104 12365747075 0024645 0 ustar 00root root 0000000 0000000 module Rack class GoogleAnalytics VERSION = '1.2.0' end end rack-google-analytics-1.2.0/lib/rack/templates/ 0000775 0000000 0000000 00000000000 12365747075 0021375 5 ustar 00root root 0000000 0000000 rack-google-analytics-1.2.0/lib/rack/templates/async.erb 0000664 0000000 0000000 00000002345 12365747075 0023210 0 ustar 00root root 0000000 0000000