markcatley-google_analytics 1.0.20080720 → 1.0.20080822

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 CHANGED
@@ -18,7 +18,7 @@ If you want to disable the code insertion for particular pages, add the followin
18
18
 
19
19
  If you are running rails 2.1 or above add install this by adding:
20
20
 
21
- config.gem 'markcatley-google_analytics', :lib => 'rubaidh/google_analytics', :source => 'http://gems.github.com'
21
+ config.gem 'rubaidh-google_analytics', :lib => 'rubaidh/google_analytics', :source => 'http://gems.github.com'
22
22
 
23
23
  and run:
24
24
 
@@ -98,4 +98,22 @@ configuration:
98
98
  end
99
99
 
100
100
  The above Capistrano recipe will almost certainly need some adjustments based
101
- on how you run your deployments, but you should get the idea.
101
+ on how you run your deployments, but you should get the idea.
102
+
103
+ OVERRIDING APPLICATION-WIDE VALUES
104
+
105
+ If you're using one Rails application to serve pages across multiple domains, you
106
+ may wish to override the domain and tracker ID values on a controller-by-controller or
107
+ view-by-view basis. You can do this by setting the override_domain_name and
108
+ override_tracker_id properties. These properties are automatically reset after
109
+ each use, so the values you set for domain_name and tracker_id (usually in an
110
+ initializer) will apply to all other requests.
111
+
112
+ before_filter :local_analytics
113
+
114
+ def local_analytics
115
+ Rubaidh::GoogleAnalytics.override_domain_name = 'foo.com'
116
+ Rubaidh::GoogleAnalytics.override_tracker_id = 'UA-123456-7'
117
+ end
118
+
119
+ See the documentation for the GoogleAnalytics class for other configuration options.
@@ -5,10 +5,9 @@ module Rubaidh # :nodoc:
5
5
  end
6
6
 
7
7
  # An after_filter to automatically add the analytics code.
8
- # Add the code at the top of the page to support calls to _trackPageView
9
- # (see http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527&topic=11006)
10
- # If you're not going to use the link_to_tracked view helpers, you can set Rubaidh::GoogleAnalytics.defer_load = true
11
- # to load the codel at the bottom of the page
8
+ # If you intend to use the link_to_tracked view helpers, you need to set Rubaidh::GoogleAnalytics.defer_load = false
9
+ # to load the code at the top of the page
10
+ # (see http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527&topic=11006)
12
11
  def add_google_analytics_code
13
12
  if GoogleAnalytics.defer_load
14
13
  response.body.sub! '</body>', "#{google_analytics_code}</body>" if response.body.respond_to?(:sub!)
@@ -20,57 +19,126 @@ module Rubaidh # :nodoc:
20
19
 
21
20
  class GoogleAnalyticsConfigurationError < StandardError; end
22
21
 
22
+ # The core functionality to connect a Rails application
23
+ # to a Google Analytics installation.
24
+ #
25
+ # The +GoogleAnalytics+ class has a variety of class attributes for configuration:
26
+ #
27
+ # * tracker_id (required)
28
+ #
29
+ # Specify the Google Analytics ID for this web site. This can be found
30
+ # as the value of +_getTracker+ if you are using the new (ga.js) tracking
31
+ # code, or the value of +_uacct+ if you are using the old (urchin.js)
32
+ # tracking code.
33
+ #
34
+ # * domain_name
35
+ #
36
+ # Specify a different domain name from the default. You'll want to use
37
+ # this if you have several subdomains that you want to combine into
38
+ # one report. See the Google Analytics documentation for more
39
+ # information.
40
+ #
41
+ # * legacy_mode
42
+ #
43
+ # Specify whether the legacy Google Analytics code should be used. By
44
+ # default, the new Google Analytics code is used.
45
+ #
46
+ # * analytics_url
47
+ #
48
+ # I can't see why you'd want to do this, but you can always change the
49
+ # analytics URL. This is only applicable in legacy mode.
50
+ #
51
+ # * analytics_ssl_url
52
+ #
53
+ # I can't see why you'd want to do this, but you can always change the
54
+ # analytics URL (ssl version). This is only applicable in legacy mode.
55
+ #
56
+ # * environments
57
+ #
58
+ # The environments in which to enable the Google Analytics code. Defaults
59
+ # to 'production' only. Supply an array of environment names to change this.
60
+ #
61
+ # * formats
62
+ #
63
+ # The formats for which to add. Defaults to +:html+ only. Supply an array
64
+ # of formats to change this.
65
+ #
66
+ # * defer_load
67
+ #
68
+ # Set this to true (the default) if you want to load the Analytics javascript at
69
+ # the bottom of page. Set this to false if you want to load the Analytics
70
+ # javascript at the top of the page. The page will render faster if you set this to
71
+ # true, but that will break the linking functions in Rubaidh::GoogleAnalyticsViewHelper.
72
+ #
73
+ # * local_javascript
74
+ #
75
+ # Set this to true to use a local copy of the ga.js (or urchin.js) file.
76
+ # This gives you the added benefit of serving the JS directly from your
77
+ # server, which in case of a big geographical difference between your server
78
+ # and Google's can speed things up for your visitors. Use the
79
+ # 'google_analytics:update' rake task to update the local JS copies.
80
+ #
81
+ # * override_domain_name
82
+ #
83
+ # Set this to override the initialized domain name for a single render. Useful
84
+ # when you're serving to multiple hosts from a single codebase. Typically you'd
85
+ # set up a before filter in the appropriate controller:
86
+ # before_filter :override_domain_name
87
+ # def override_domain_name
88
+ # Rubaidh::GoogleAnalytics.override_domain_name = 'foo.com'
89
+ # end
90
+ #
91
+ # * override_tracker_id
92
+ #
93
+ # Set this to override the initialized tracker ID for a single render. Useful
94
+ # when you're serving to multiple hosts from a single codebase. Typically you'd
95
+ # set up a before filter in the appropriate controller:
96
+ # before_filter :override_tracker_id
97
+ # def override_tracker_id
98
+ # Rubaidh::GoogleAnalytics.override_tracker_id = 'UA-123456-7'
99
+ # end
100
+ #
101
+ # * override_trackpageview
102
+ #
103
+ # Set this to override the automatically generated path to the page in the
104
+ # Google Analytics reports for a single render. Typically you'd set this up on a
105
+ # controller-by-controller basis:
106
+ # def show
107
+ # Rubaidh::GoogleAnalytics.override_trackpageview = "path_to_report"
108
+ # ...
23
109
  class GoogleAnalytics
24
- # Specify the Google Analytics ID for this web site. This can be found
25
- # as the value of +_uacct+ in the Javascript excerpt
110
+
26
111
  @@tracker_id = nil
27
- cattr_accessor :tracker_id
112
+ cattr_accessor :tracker_id
28
113
 
29
- # Specify a different domain name from the default. You'll want to use
30
- # this if you have several subdomains that you want to combine into
31
- # one report. See the Google Analytics documentation for more
32
- # information.
33
114
  @@domain_name = nil
34
115
  cattr_accessor :domain_name
35
116
 
36
- # Specify whether the legacy Google Analytics code should be used.
37
117
  @@legacy_mode = false
38
118
  cattr_accessor :legacy_mode
39
119
 
40
- # I can't see why you'd want to do this, but you can always change the
41
- # analytics URL. This is only applicable in legacy mode.
42
120
  @@analytics_url = 'http://www.google-analytics.com/urchin.js'
43
121
  cattr_accessor :analytics_url
44
122
 
45
- # I can't see why you'd want to do this, but you can always change the
46
- # analytics URL (ssl version). This is only applicable in legacy mode.
47
123
  @@analytics_ssl_url = 'https://ssl.google-analytics.com/urchin.js'
48
124
  cattr_accessor :analytics_ssl_url
49
125
 
50
- # The environments in which to enable the Google Analytics code. Defaults
51
- # to 'production' only.
52
126
  @@environments = ['production']
53
127
  cattr_accessor :environments
54
128
 
55
- # The formats for which to add. Defaults
56
- # to :html only.
57
129
  @@formats = [:html]
58
130
  cattr_accessor :formats
59
131
 
60
- # Set this to true if you want to load the Analytics javascript at the bottom of
61
- # each page rather than at the top. This may result in faster page render times,
62
- # but may break link_to_tracked functionality.
63
132
  @@defer_load = true
64
133
  cattr_accessor :defer_load
65
134
 
66
- # Set this to true to use a local copy of the ga.js (or urchin.js) file.
67
- # This gives you the added benefit of serving the JS directly from your
68
- # server, which in case of a big geographical difference between your server
69
- # and Google's can speed things up for your visitors. Use the
70
- # 'google_analytics:update' rake task to update the local JS copies.
71
135
  @@local_javascript = false
72
136
  cattr_accessor :local_javascript
73
137
 
138
+ cattr_accessor :override_domain_name
139
+ cattr_accessor :override_tracker_id
140
+ cattr_accessor :override_trackpageview
141
+
74
142
  # Return true if the Google Analytics system is enabled and configured
75
143
  # correctly for the specified format
76
144
  def self.enabled?(format)
@@ -78,10 +146,16 @@ module Rubaidh # :nodoc:
78
146
  environments.include?(RAILS_ENV) && formats.include?(format.to_sym)
79
147
  end
80
148
 
149
+ # Construct the javascript code to be inserted on the calling page. The +ssl+
150
+ # parameter can be used to force the SSL version of the code in legacy mode only.
81
151
  def self.google_analytics_code(ssl = false)
82
152
  return legacy_google_analytics_code(ssl) if legacy_mode
83
153
 
84
154
  extra_code = domain_name.blank? ? nil : "pageTracker._setDomainName(\"#{domain_name}\");"
155
+ if !override_domain_name.blank?
156
+ extra_code = "pageTracker._setDomainName(\"#{override_domain_name}\");"
157
+ self.override_domain_name = nil
158
+ end
85
159
 
86
160
  code = if local_javascript
87
161
  <<-HTML
@@ -100,27 +174,33 @@ module Rubaidh # :nodoc:
100
174
  code << <<-HTML
101
175
  <script type="text/javascript">
102
176
  <!--//--><![CDATA[//><!--
103
- var pageTracker = _gat._getTracker('#{tracker_id}');
177
+ var pageTracker = _gat._getTracker('#{request_tracker_id}');
104
178
  #{extra_code}
105
179
  pageTracker._initData();
106
- pageTracker._trackPageview();
180
+ pageTracker._trackPageview(#{request_tracked_path});
107
181
  //--><!]]>
108
182
  </script>
109
183
  HTML
110
184
  end
111
185
 
112
- # Run the legacy version of the Google Analytics code.
186
+ # Construct the legacy version of the Google Analytics code. The +ssl+
187
+ # parameter specifies whether or not to return the SSL version of the code.
113
188
  def self.legacy_google_analytics_code(ssl = false)
114
189
  extra_code = domain_name.blank? ? nil : "_udn = \"#{domain_name}\";"
190
+ if !override_domain_name.blank?
191
+ extra_code = "_udn = \"#{override_domain_name}\";"
192
+ self.override_domain_name = nil
193
+ end
194
+
115
195
  url = legacy_analytics_js_url(ssl)
116
196
 
117
197
  code = <<-HTML
118
198
  <script src="#{url}" type="text/javascript">
119
199
  </script>
120
200
  <script type="text/javascript">
121
- _uacct = "#{tracker_id}";
201
+ _uacct = "#{request_tracker_id}";
122
202
  #{extra_code}
123
- urchinTracker();
203
+ urchinTracker(#{request_tracked_path});
124
204
  </script>
125
205
  HTML
126
206
  end
@@ -133,9 +213,24 @@ module Rubaidh # :nodoc:
133
213
  ssl ? analytics_ssl_url : analytics_url
134
214
  end
135
215
  end
216
+
217
+ # Determine the tracker ID for this request
218
+ def self.request_tracker_id
219
+ use_tracker_id = override_tracker_id.blank? ? tracker_id : override_tracker_id
220
+ self.override_tracker_id = nil
221
+ use_tracker_id
222
+ end
223
+
224
+ # Determine the path to report for this request
225
+ def self.request_tracked_path
226
+ use_tracked_path = override_trackpageview.blank? ? '' : "'#{override_trackpageview}'"
227
+ self.override_trackpageview = nil
228
+ use_tracked_path
229
+ end
230
+
136
231
  end
137
-
138
- class LocalAssetTagHelper
232
+
233
+ class LocalAssetTagHelper # :nodoc:
139
234
  # For helping with local javascripts
140
235
  include ActionView::Helpers::AssetTagHelper
141
236
  end
@@ -1,23 +1,41 @@
1
1
  module Rubaidh
2
- module GoogleAnalyticsViewHelper #:nodoc:
2
+ # Collection of methods similar to the ones in ActionView::Helpers::UrlHelper,
3
+ # with the addition of outbound link tracking. See the Google Analytics help
4
+ # at http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527
5
+ # for more information on outbound link tracking.
6
+ module GoogleAnalyticsViewHelper
7
+ # Creates a link tag of the given +name+ using a URL created by the set of +options+,
8
+ # with outbound link tracking under +track_path+ in Google Analytics. The +html_options+
9
+ # will accept a hash of attributes for the link tag.
3
10
  def link_to_tracked(name, track_path = "/", options = {}, html_options = {})
4
11
  raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
5
12
  html_options.merge!({:onclick => tracking_call(track_path)})
6
13
  link_to name, options, html_options
7
14
  end
8
15
 
16
+ # Creates a link tag of the given +name+ using a URL created by the set of +options+
17
+ # if +condition+ is true, with outbound link tracking under +track_path+ in Google Analytics.
18
+ # The +html_options+ will accept a hash of attributes for the link tag.
9
19
  def link_to_tracked_if(condition, name, track_path = "/", options = {}, html_options = {}, &block)
10
20
  raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
11
21
  html_options.merge!({:onclick => tracking_call(track_path)})
12
22
  link_to_unless !condition, name, options, html_options, &block
13
23
  end
14
24
 
25
+ # Creates a link tag of the given +name+ using a URL created by the set of +options+
26
+ # unless +condition+ is true, with outbound link tracking under +track_path+ in Google Analytics.
27
+ # The +html_options+ will accept a hash of attributes for the link tag.
15
28
  def link_to_tracked_unless(condition, name, track_path = "/", options = {}, html_options = {}, &block)
16
29
  raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
17
30
  html_options.merge!({:onclick => tracking_call(track_path)})
18
31
  link_to_unless condition, name, options, html_options, &block
19
32
  end
20
33
 
34
+ # Creates a link tag of the given +name+ using a URL created by the set of +options+
35
+ # unless the current request URI is the same as the link's, with outbound link tracking
36
+ # under +track_path+ in Google Analytics. If the request URI is the same as the link
37
+ # URI, only the name is returned, or the block is yielded, if one exists.
38
+ # The +html_options+ will accept a hash of attributes for the link tag.
21
39
  def link_to_tracked_unless_current(name, track_path = "/", options = {}, html_options = {}, &block)
22
40
  raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
23
41
  html_options.merge!({:onclick =>tracking_call(track_path)})
@@ -36,6 +54,8 @@ private
36
54
 
37
55
  end
38
56
 
57
+ # Error raised by tracking methods if Rubaidh::GoogleAnalytics.defer_load is not configured
58
+ # properly to enable tracking.
39
59
  class AnalyticsError < StandardError
40
60
  attr_reader :message
41
61
 
@@ -1,8 +1,117 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
1
2
  require 'test/unit'
3
+ require 'rubygems'
4
+ require 'mocha'
2
5
 
3
6
  class GoogleAnalyticsTest < Test::Unit::TestCase
4
- # Replace this with your real tests.
5
- def test_this_plugin
6
- flunk
7
+ def setup
8
+ @ga = Rubaidh::GoogleAnalytics.new
9
+ @ga.tracker_id = "the tracker id"
7
10
  end
11
+
12
+ def test_createable
13
+ assert_not_nil(@ga)
14
+ end
15
+
16
+ def test_domain_name_defaults_to_nil
17
+ assert_nil(@ga.domain_name)
18
+ end
19
+
20
+ def test_legacy_mode_defaults_to_false
21
+ assert_equal(false, @ga.legacy_mode)
22
+ end
23
+
24
+ def test_default_analytics_url
25
+ assert_equal("http://www.google-analytics.com/urchin.js", @ga.analytics_url)
26
+ end
27
+
28
+ def test_default_analytics_ssl_url
29
+ assert_equal('https://ssl.google-analytics.com/urchin.js', @ga.analytics_ssl_url)
30
+ end
31
+
32
+ def test_default_environments
33
+ assert_equal(false, @ga.environments.include?('test'))
34
+ assert_equal(false, @ga.environments.include?('development'))
35
+ assert_equal(true, @ga.environments.include?('production'))
36
+ end
37
+
38
+ def test_default_formats
39
+ assert_equal(false, @ga.formats.include?(:xml))
40
+ assert_equal(true, @ga.formats.include?(:html))
41
+ end
42
+
43
+ def test_defer_load_defaults_to_true
44
+ assert_equal(true, @ga.defer_load)
45
+ end
46
+
47
+ def test_local_javascript_defaults_to_false
48
+ assert_equal(false, @ga.local_javascript)
49
+ end
50
+
51
+ # test self.enabled
52
+ def test_enabled_requires_tracker_id
53
+ Rubaidh::GoogleAnalytics.stubs(:tracker_id).returns(nil)
54
+ assert_raise(Rubaidh::GoogleAnalyticsConfigurationError) { Rubaidh::GoogleAnalytics.enabled?(:html) }
55
+ end
56
+
57
+ def test_enabled_requires_analytics_url
58
+ Rubaidh::GoogleAnalytics.stubs(:analytics_url).returns(nil)
59
+ assert_raise(Rubaidh::GoogleAnalyticsConfigurationError) { Rubaidh::GoogleAnalytics.enabled?(:html) }
60
+ end
61
+
62
+ def test_enabled_returns_false_if_current_environment_not_enabled
63
+ assert_equal(false, Rubaidh::GoogleAnalytics.enabled?(:html))
64
+ end
65
+
66
+ def test_enabled_with_default_format
67
+ Rubaidh::GoogleAnalytics.stubs(:environments).returns(['test'])
68
+ assert_equal(true, Rubaidh::GoogleAnalytics.enabled?(:html))
69
+ end
70
+
71
+ def test_enabled_with_not_included_format
72
+ Rubaidh::GoogleAnalytics.stubs(:environments).returns(['test'])
73
+ assert_equal(false, Rubaidh::GoogleAnalytics.enabled?(:xml))
74
+ end
75
+
76
+ def test_enabled_with_added_format
77
+ Rubaidh::GoogleAnalytics.stubs(:environments).returns(['test'])
78
+ Rubaidh::GoogleAnalytics.stubs(:formats).returns([:xml])
79
+ assert_equal(true, Rubaidh::GoogleAnalytics.enabled?(:xml))
80
+ end
81
+
82
+ # test request_tracker_id
83
+ def test_request_tracker_id_without_override
84
+ Rubaidh::GoogleAnalytics.stubs(:tracker_id).returns("1234")
85
+ assert_equal("1234", Rubaidh::GoogleAnalytics.request_tracker_id)
86
+ end
87
+
88
+ def test_request_tracker_id_with_override
89
+ Rubaidh::GoogleAnalytics.stubs(:tracker_id).returns("1234")
90
+ Rubaidh::GoogleAnalytics.override_tracker_id = "4567"
91
+ assert_equal("4567", Rubaidh::GoogleAnalytics.request_tracker_id)
92
+ end
93
+
94
+ def test_request_tracker_id_resets_override
95
+ Rubaidh::GoogleAnalytics.override_tracker_id = "4567"
96
+ Rubaidh::GoogleAnalytics.stubs(:tracker_id).returns("1234")
97
+ foo = Rubaidh::GoogleAnalytics.request_tracker_id
98
+ assert_nil(Rubaidh::GoogleAnalytics.override_tracker_id)
99
+ end
100
+
101
+ # test request_tracked_path
102
+ def test_request_tracked_path_without_override
103
+ assert_equal('', Rubaidh::GoogleAnalytics.request_tracked_path)
104
+ end
105
+
106
+ def test_request_tracked_path_with_override
107
+ Rubaidh::GoogleAnalytics.override_trackpageview = "/my/path"
108
+ assert_equal("'/my/path'", Rubaidh::GoogleAnalytics.request_tracked_path)
109
+ end
110
+
111
+ def test_request_tracked_path_resets_override
112
+ Rubaidh::GoogleAnalytics.override_trackpageview = "/my/path"
113
+ foo = Rubaidh::GoogleAnalytics.request_tracked_path
114
+ assert_nil(Rubaidh::GoogleAnalytics.override_trackpageview)
115
+ end
116
+
8
117
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markcatley-google_analytics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.20080720
4
+ version: 1.0.20080822
5
5
  platform: ruby
6
6
  authors:
7
7
  - Graeme Mathieson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-08 00:00:00 -07:00
12
+ date: 2008-08-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -38,7 +38,8 @@ files:
38
38
  - test/view_helpers_test.rb
39
39
  - lib/rubaidh/google_analytics.rb
40
40
  - lib/rubaidh/view_helpers.rb
41
- has_rdoc: false
41
+ - task/google_analytics.rake
42
+ has_rdoc: true
42
43
  homepage: http://github.com/rubaidh/google_analytics/tree/master
43
44
  post_install_message:
44
45
  rdoc_options: []