rack-google-analytics 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -34,11 +34,20 @@ config.gem 'rack-google-analytics', :lib => 'rack/google-analytics'
34
34
  config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
35
35
  ```
36
36
 
37
+ #### Rails 3.X
38
+
39
+ ```ruby
40
+ ## application.rb:
41
+ config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
42
+ ```
43
+
37
44
  ### Options
38
45
 
39
46
  * `:async` - sets to use asynchronous tracker
40
47
  * `:multiple` - sets track for multiple subdomains. (must also set :domain)
41
48
  * `:top_level` - sets tracker for multiple top-level domains. (must also set :domain)
49
+ * `: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.
50
+ * `: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
42
51
 
43
52
  Note: since 0.2.0 this will use the asynchronous Google Analytics tracking code, for the traditional behaviour please use:
44
53
 
@@ -48,6 +57,35 @@ use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x', :async => false
48
57
 
49
58
  If you are not sure what's best, go with the defaults, and read here if you should opt-out.
50
59
 
60
+ ## Custom Variable Tracking
61
+
62
+ In your application controller, you may track a custom variable. For example:
63
+
64
+ ```ruby
65
+ set_ga_custom_var(1, "LoggedIn", value, GoogleAnalytics::CustomVar::SESSION_LEVEL)
66
+ ```
67
+
68
+ See https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables for details.
69
+
70
+ ## Event Tracking
71
+
72
+ In your application controller, you may track an event. For example:
73
+
74
+ ```ruby
75
+ track_ga_event("Users", "Login", "Standard")
76
+ ```
77
+
78
+ See https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
79
+
80
+ ## Custom Push
81
+
82
+ In your application controller, you may push arbritrary data. For example:
83
+
84
+ ```ruby
85
+ ga_push("_addItem", "ID", "SKU")
86
+ ```
87
+
88
+
51
89
  ## Thread Safety
52
90
 
53
91
  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.
@@ -0,0 +1,54 @@
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
+ # Sets a custom variable on a page load
23
+ #
24
+ # e.g. writes
25
+ # _gaq.push(['_setCustomVar',
26
+ # 2, // This custom var is set to slot #2. Required parameter.
27
+ # 'Shopping Attempts', // The name of the custom variable. Required parameter.
28
+ # 'Yes', // The value of the custom variable. Required parameter.
29
+ # // (you might set this value by default to No)
30
+ # 2 // Sets the scope to session-level. Optional parameter.
31
+ # ]);
32
+ def set_ga_custom_var(slot, name, value, scope = nil)
33
+ var = GoogleAnalytics::CustomVar.new(slot, name, value, scope)
34
+
35
+ ga_custom_vars.push(var)
36
+ end
37
+
38
+ # Tracks an event or goal on a page load
39
+ #
40
+ # e.g. writes
41
+ # _gaq.push(['_trackEvent', 'Videos', 'Play', 'Gone With the Wind']);
42
+ #
43
+ def track_ga_event(category, action, label = nil, value = nil, noninteraction = nil)
44
+ var = GoogleAnalytics::Event.new(category, action, label, value, noninteraction)
45
+ ga_events.push(var)
46
+ end
47
+
48
+ def ga_push(*attributes)
49
+ var = GoogleAnalytics::Push.new(attributes)
50
+ ga_events.push(var)
51
+ end
52
+
53
+ end
54
+ end
@@ -1 +1,11 @@
1
+ require "active_support/json"
2
+
1
3
  require 'rack/google-analytics'
4
+
5
+ require "tracking/custom_var"
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)
@@ -5,6 +5,8 @@ module Rack
5
5
 
6
6
  class GoogleAnalytics
7
7
 
8
+ EVENT_TRACKING_KEY = "google_analytics.event_tracking"
9
+
8
10
  DEFAULT = { :async => true }
9
11
 
10
12
  def initialize(app, options = {})
@@ -18,9 +20,24 @@ module Rack
18
20
  @status, @headers, @body = @app.call(env)
19
21
  return [@status, @headers, @body] unless html?
20
22
  response = Rack::Response.new([], @status, @headers)
23
+ @options[:tracker_vars] = env["google_analytics.custom_vars"] || []
24
+
25
+ if response.ok?
26
+ # Write out the events now
27
+ @options[:tracker_vars] += (env[EVENT_TRACKING_KEY]) unless env[EVENT_TRACKING_KEY].nil?
28
+
29
+ # Get any stored events from a redirection
30
+ session = env["rack.session"]
31
+ stored_events = session.delete(EVENT_TRACKING_KEY) if session
32
+ @options[:tracker_vars] += stored_events unless stored_events.nil?
33
+ elsif response.redirection?
34
+ # Store the events until next time
35
+ env["rack.session"][EVENT_TRACKING_KEY] = env[EVENT_TRACKING_KEY]
36
+ end
37
+
21
38
  @body.each { |fragment| response.write inject(fragment) }
22
39
  @body.close if @body.respond_to?(:close)
23
-
40
+
24
41
  response.finish
25
42
  end
26
43
 
@@ -30,6 +47,7 @@ module Rack
30
47
 
31
48
  def inject(response)
32
49
  file = @options[:async] ? 'async' : 'sync'
50
+
33
51
  @template ||= ::ERB.new ::File.read ::File.expand_path("../templates/#{file}.erb",__FILE__)
34
52
  if @options[:async]
35
53
  response.gsub(%r{</head>}, @template.result(binding) + "</head>")
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class GoogleAnalytics
3
- VERSION = '0.11.0'
3
+ VERSION = '0.12.0'
4
4
  end
5
5
  end
@@ -9,8 +9,21 @@
9
9
  _gaq.push(['_setDomainName', 'none']);
10
10
  _gaq.push(['_setAllowLinker', true]);
11
11
  <% end %>
12
+ <% if @options[:anonymize_ip] %>
13
+ _gaq.push(['_gat._anonymizeIp']);
14
+ <% end %>
15
+ <% if @options[:site_speed_sample_rate] %>
16
+ _gaq.push(['_setSiteSpeedSampleRate', <%= @options[:site_speed_sample_rate].to_i %>]);
17
+ <% end %>
18
+
19
+ <% if @options[:tracker_vars]
20
+ @options[:tracker_vars].each do |var|
21
+ %>
22
+ _gaq.push(<%= var.write() %>);
23
+ <% end
24
+ end
25
+ %>
12
26
  _gaq.push(['_trackPageview']);
13
- _gaq.push(['_trackPageLoadTime']);
14
27
 
15
28
  (function() {
16
29
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
@@ -5,5 +5,8 @@ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.
5
5
  <script type="text/javascript">
6
6
  try{
7
7
  var pageTracker = _gat._getTracker(<%= @options[:tracker].inspect %>);
8
+ <% if @options[:anonymize_ip] %>
9
+ _gat._anonymizeIp();
10
+ <% end %>
8
11
  pageTracker._trackPageview();
9
12
  } catch(err) {}</script>
data/lib/rack/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class GoogleAnalytics
3
- VERSION = '0.11.0'
3
+ VERSION = '0.12.0'
4
4
  end
5
5
  end
@@ -0,0 +1,14 @@
1
+ module GoogleAnalytics
2
+
3
+ # A Struct that mirrors the structure of a custom var defined in Google Analytics
4
+ # see https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables
5
+ class CustomVar < Struct.new(:index, :name, :value, :opt_scope)
6
+ VISITOR_LEVEL = 1
7
+ SESSION_LEVEL = 2
8
+ PAGE_LEVEL = 3
9
+
10
+ def write
11
+ ['_setCustomVar', self.index, self.name, self.value,self.opt_scope].to_json
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ require "active_support/json"
2
+
3
+ module GoogleAnalytics
4
+
5
+ # A Struct that mirrors the structure of a custom var defined in Google Analytics
6
+ # see https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
7
+ class Event < Struct.new(:category, :action, :label, :value, :noninteraction)
8
+
9
+ def write
10
+ ['_trackEvent', self.category, self.action, self.label,self.value, self.noninteraction].to_json
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-google-analytics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-17 00:00:00.000000000 Z
12
+ date: 2013-08-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: actionpack
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
30
62
  - !ruby/object:Gem::Dependency
31
63
  name: test-unit
32
64
  requirement: !ruby/object:Gem::Requirement
@@ -100,12 +132,16 @@ executables: []
100
132
  extensions: []
101
133
  extra_rdoc_files: []
102
134
  files:
135
+ - lib/google-analytics/instance_methods.rb
103
136
  - lib/rack/google-analytics/version.rb
104
137
  - lib/rack/google-analytics.rb
105
138
  - lib/rack/templates/async.erb
106
139
  - lib/rack/templates/sync.erb
107
140
  - lib/rack/version.rb
108
141
  - lib/rack-google-analytics.rb
142
+ - lib/tracking/custom_var.rb
143
+ - lib/tracking/event.rb
144
+ - lib/tracking/push.rb
109
145
  - README.md
110
146
  - LICENSE
111
147
  homepage: https://github.com/leehambley/rack-google-analytics
@@ -120,15 +156,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
156
  - - ! '>='
121
157
  - !ruby/object:Gem::Version
122
158
  version: '0'
159
+ segments:
160
+ - 0
161
+ hash: 4292920950575174798
123
162
  required_rubygems_version: !ruby/object:Gem::Requirement
124
163
  none: false
125
164
  requirements:
126
165
  - - ! '>='
127
166
  - !ruby/object:Gem::Version
128
167
  version: '0'
168
+ segments:
169
+ - 0
170
+ hash: 4292920950575174798
129
171
  requirements: []
130
172
  rubyforge_project:
131
- rubygems_version: 1.8.23
173
+ rubygems_version: 1.8.25
132
174
  signing_key:
133
175
  specification_version: 3
134
176
  summary: Rack middleware to inject the Google Analytics tracking code into outgoing