rack-google-analytics 0.13.0 → 0.14.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.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 840b08567f04293f2ed02bb2dfc92c641e5e8a3e
4
- data.tar.gz: 1f634376e3d0b0383867e8526f69ca0594cb640a
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTVmMWNmY2UxNjEzMzhiOTEyMjY1NzA1NWY2MDg4MjdjZDhlNDM0Mg==
5
+ data.tar.gz: !binary |-
6
+ ZTlhZmVkNGJmNWMyZjc5ODMwNzkyN2JlODc3NDdhYzMwOGU4NDlmOQ==
5
7
  SHA512:
6
- metadata.gz: c613036c7f762929586faaa4e9428943d6f7e3b59440f3cfa8f4f022db2b19fafea29fc24ea9bf4d710998376268e2d766b0c29b53f86d3168f4422a9c9f41f5
7
- data.tar.gz: 3eb443013c95612cc8e8cd7b6697d8b888d2ebcfc50ea5b4724d4ea9bdd507b82ac6c736143cebe3c80e9b681bef41d5105853ec947a8de2da17124a9ecac758
8
+ metadata.gz: !binary |-
9
+ NDdiNDFmMTg5NDYxZjY0YzU0OTczMDZiMGNlOWJhYjI0OGE3ZWY4ZjYzOTc2
10
+ NjIwYTdmODIzZjcyOWVhN2ViZDdjOGM2NmVkMzlkYjIzZjYxZDNhN2Q2ZjE4
11
+ ZTgyYzRkZDljODg5MGViNjdiMGJiYWE4ODcwNDg5ZDdlNTY4MmM=
12
+ data.tar.gz: !binary |-
13
+ NzllNjIxZTEyNmE3OTYyNjBiMmYyZDc0MjMwOTQzMzZjMmI4OGI2ZDdjMmFi
14
+ OGUyM2JjZjE4N2UxNjUzNzExNjZkOGQzNGQ3N2MwOWJlZTA1MjZlOGVhODQz
15
+ M2JhZmQ0ZTI4NDAzMmI2Mjg0Y2RiNTI5MTJmMzc5OWQ1NDkzNWM=
data/README.md CHANGED
@@ -36,7 +36,7 @@ config.gem 'rack-google-analytics', :lib => 'rack/google-analytics'
36
36
  config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'
37
37
  ```
38
38
 
39
- #### Rails 3.X
39
+ #### Rails 3.X and 4.X
40
40
 
41
41
  ```ruby
42
42
  ## application.rb:
@@ -90,6 +90,17 @@ In your application controller, you may push arbritrary data. For example:
90
90
  ga_push("_addItem", "ID", "SKU")
91
91
  ```
92
92
 
93
+ ## Dynamic Tracking Code
94
+
95
+ You may instead define your tracking code as a lambda taking the Rack environment, so that you may set the tracking code
96
+ dynamically based upon information in the Rack environment. For example:
97
+
98
+ ```ruby
99
+ config.middleware.use Rack::GoogleAnalytics, :tracker => lambda { |env|
100
+ return env[:site_ga].tracker if env[:site_ga]
101
+ }
102
+ ```
103
+
93
104
 
94
105
  ## Thread Safety
95
106
 
@@ -7,10 +7,10 @@ module Rack
7
7
 
8
8
  EVENT_TRACKING_KEY = "google_analytics.event_tracking"
9
9
 
10
- DEFAULT = { :async => true, :advertising => false }
10
+ DEFAULT = { :async => true, :advertising => false, :inpage_pageid => false }
11
11
 
12
12
  def initialize(app, options = {})
13
- raise ArgumentError, "Tracker must be set!" unless options[:tracker] and !options[:tracker].empty?
13
+ raise ArgumentError, "Tracker must be set!" unless valid_tracker?(options[:tracker])
14
14
  @app, @options = app, DEFAULT.merge(options)
15
15
  end
16
16
 
@@ -34,6 +34,7 @@ module Rack
34
34
  # Store the events until next time
35
35
  env["rack.session"][EVENT_TRACKING_KEY] = env[EVENT_TRACKING_KEY]
36
36
  end
37
+ @tracker = tracker(env, @options[:tracker])
37
38
 
38
39
  @body.each { |fragment| response.write inject(fragment) }
39
40
  @body.close if @body.respond_to?(:close)
@@ -43,6 +44,12 @@ module Rack
43
44
 
44
45
  private
45
46
 
47
+ # tracker should be non-nil, non-empty string or a lambda
48
+ def valid_tracker?(tracker)
49
+ return false unless tracker
50
+ return (tracker.respond_to?(:call) && tracker.lambda?) || !tracker.empty?
51
+ end
52
+
46
53
  def html?; @headers['Content-Type'] =~ /html/; end
47
54
 
48
55
  def inject(response)
@@ -56,6 +63,12 @@ module Rack
56
63
  end
57
64
  end
58
65
 
66
+ # obtain tracking code dynamically if it's a lambda, use the string directly otherwise
67
+ def tracker(env, tracker)
68
+ return tracker unless tracker.respond_to?(:call)
69
+ return tracker.call(env)
70
+ end
71
+
59
72
  end
60
73
 
61
74
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class GoogleAnalytics
3
- VERSION = '0.13.0'
3
+ VERSION = '0.14.0'
4
4
  end
5
5
  end
@@ -1,7 +1,11 @@
1
1
  <script type="text/javascript">
2
2
 
3
3
  var _gaq = _gaq || [];
4
- _gaq.push(['_setAccount', <%= @options[:tracker].inspect %>]);
4
+ <% if @options[:inpage_pageid] %>
5
+ var pluginUrl = '//www.google-analytics.com/plugins/ga/inpage_linkid.js';
6
+ _gaq.push(['_require', 'inpage_linkid', pluginUrl]);
7
+ <% end %>
8
+ _gaq.push(['_setAccount', <%= @tracker.inspect %>]);
5
9
  <% if @options[:multiple] %>
6
10
  _gaq.push(['_setDomainName', <%= @options[:domain].inspect %>]);
7
11
  <% end %>
@@ -4,7 +4,7 @@ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.
4
4
  </script>
5
5
  <script type="text/javascript">
6
6
  try{
7
- var pageTracker = _gat._getTracker(<%= @options[:tracker].inspect %>);
7
+ var pageTracker = _gat._getTracker(<%= @tracker.inspect %>);
8
8
  <% if @options[:anonymize_ip] %>
9
9
  _gat._anonymizeIp();
10
10
  <% 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.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Hambley
@@ -11,45 +11,45 @@ cert_chain: []
11
11
  date: 2013-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: actionpack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
- type: :development
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: actionpack
28
+ name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ! '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
- type: :development
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: activesupport
42
+ name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ! '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ! '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
@@ -138,17 +138,17 @@ require_paths:
138
138
  - lib
139
139
  required_ruby_version: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - '>='
141
+ - - ! '>='
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  requirements:
146
- - - '>='
146
+ - - ! '>='
147
147
  - !ruby/object:Gem::Version
148
148
  version: '0'
149
149
  requirements: []
150
150
  rubyforge_project:
151
- rubygems_version: 2.0.6
151
+ rubygems_version: 2.1.5
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: Rack middleware to inject the Google Analytics tracking code into outgoing