markcatley-google_analytics 1.0.20080710 → 1.0.20080714

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
@@ -19,9 +19,39 @@ If you want to disable the code insertion for particular pages, add the followin
19
19
  If you are running rails 2.1 or above add install this by adding:
20
20
 
21
21
  config.gem 'markcatley-google_analytics', :lib => 'rubaidh/google_analytics', :source => 'http://gems.github.com'
22
-
22
+
23
23
  and run:
24
24
 
25
25
  rake gems:install
26
26
 
27
- Simple. :-)
27
+ Simple. :-)
28
+
29
+
30
+ NOTE
31
+
32
+ This version of the plugin uses the new Google Analytics code (ga.js) by
33
+ default. To use the legacy tracking code add the following line to your
34
+ `config/environment.rb`:
35
+
36
+ Rubaidh::GoogleAnalytics.legacy_mode = true
37
+
38
+ TRACKING OUTBOUND LINKS
39
+
40
+ Google Analytics only tracks intra-site links by default. To create an outbound link that is tracked use
41
+ the link_to_tracked helper:
42
+
43
+ link_to_tracked(name, track_path = "/", options = {}, html_options = {})
44
+
45
+ You can use the track_path parameter to group your outbound links into logical folders inside of
46
+ Google Analytics.
47
+
48
+ The other forms of link_to are also supported:
49
+
50
+ link_to_tracked_if(condition, name, track_path = "/", options = {}, html_options = {}, &block)
51
+ link_to_tracked_unless(condition, name, track_path = "/", options = {}, html_options = {}, &block)
52
+ link_to_tracked_unless_current(name, track_path = "/", options = {}, html_options = {}, &block)
53
+
54
+ Tracked links respect the legacy_mode flag.
55
+
56
+ Note: Link-tracking works by inserting onclick() code in the HTML. Because of this, it will overwrite
57
+ any onclick that you insert in the html_options hash.
@@ -1,18 +1,19 @@
1
1
  module Rubaidh # :nodoc:
2
2
  module GoogleAnalyticsMixin
3
- def google_analytics_code(request = nil)
4
- return unless GoogleAnalytics.enabled?
5
- GoogleAnalytics.google_analytics_code(request)
3
+ def google_analytics_code
4
+ GoogleAnalytics.google_analytics_code(request.ssl?) if GoogleAnalytics.enabled?(request.format)
6
5
  end
7
6
 
8
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)
9
10
  def add_google_analytics_code
10
- code = google_analytics_code(request)
11
- return if code.blank?
12
- response.body.gsub! '</body>', code + '</body>' if response.body.respond_to?(:gsub!)
11
+ response.body.sub! '</body>', "#{google_analytics_code}</body>" if response.body.respond_to?(:sub!)
13
12
  end
14
13
  end
15
14
 
15
+ class GoogleAnalyticsConfigurationError < StandardError; end
16
+
16
17
  class GoogleAnalytics
17
18
  # Specify the Google Analytics ID for this web site. This can be found
18
19
  # as the value of +_uacct+ in the Javascript excerpt
@@ -25,14 +26,18 @@ module Rubaidh # :nodoc:
25
26
  # information.
26
27
  @@domain_name = nil
27
28
  cattr_accessor :domain_name
29
+
30
+ # Specify whether the legacy Google Analytics code should be used.
31
+ @@legacy_mode = false
32
+ cattr_accessor :legacy_mode
28
33
 
29
34
  # I can't see why you'd want to do this, but you can always change the
30
- # analytics URL.
35
+ # analytics URL. This is only applicable in legacy mode.
31
36
  @@analytics_url = 'http://www.google-analytics.com/urchin.js'
32
37
  cattr_accessor :analytics_url
33
38
 
34
39
  # I can't see why you'd want to do this, but you can always change the
35
- # analytics URL (ssl version).
40
+ # analytics URL (ssl version). This is only applicable in legacy mode.
36
41
  @@analytics_ssl_url = 'https://ssl.google-analytics.com/urchin.js'
37
42
  cattr_accessor :analytics_ssl_url
38
43
 
@@ -40,21 +45,45 @@ module Rubaidh # :nodoc:
40
45
  # to 'production' only.
41
46
  @@environments = ['production']
42
47
  cattr_accessor :environments
48
+
49
+ # The formats for which to add. Defaults
50
+ # to :html only.
51
+ @@formats = [:html]
52
+ cattr_accessor :formats
43
53
 
44
54
  # Return true if the Google Analytics system is enabled and configured
45
- # correctly.
46
- def self.enabled?
47
- (environments.include?(RAILS_ENV) and
48
- not tracker_id.blank? and
49
- not analytics_url.blank?)
55
+ # correctly for the specified format
56
+ def self.enabled?(format)
57
+ raise Rubaidh::GoogleAnalyticsConfigurationError if tracker_id.blank? || analytics_url.blank?
58
+ environments.include?(RAILS_ENV) && formats.include?(format.to_sym)
50
59
  end
51
60
 
52
- def self.google_analytics_code(request = nil)
61
+ def self.google_analytics_code(ssl = false)
62
+ return legacy_google_analytics_code(ssl) if legacy_mode
63
+
64
+ extra_code = domain_name.blank? ? nil : "pageTracker._setDomainName(\"#{domain_name}\");"
65
+
66
+ code = <<-HTML
67
+ <script type="text/javascript">
68
+ var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
69
+ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
70
+ </script>
71
+ <script type="text/javascript">
72
+ <!--//--><![CDATA[//><!--
73
+ var pageTracker = _gat._getTracker('#{tracker_id}');
74
+ #{extra_code}
75
+ pageTracker._initData();
76
+ pageTracker._trackPageview();
77
+ //--><!]]>
78
+ </script>
79
+ HTML
80
+ end
81
+
82
+ # Run the legacy version of the Google Analytics code.
83
+ def self.legacy_google_analytics_code(ssl = false)
53
84
  extra_code = domain_name.blank? ? nil : "_udn = \"#{domain_name}\";"
54
- url = (not request.blank? and request.ssl?) ? analytics_ssl_url : analytics_url
85
+ url = ssl ? analytics_ssl_url : analytics_url
55
86
 
56
- # OK, I'm not very bright -- I tried to turn this into a partial and
57
- # failed miserably! So it'll have to live here for now.
58
87
  code = <<-HTML
59
88
  <script src="#{url}" type="text/javascript">
60
89
  </script>
@@ -64,7 +93,6 @@ module Rubaidh # :nodoc:
64
93
  urchinTracker();
65
94
  </script>
66
95
  HTML
67
- code
68
96
  end
69
97
  end
70
- end
98
+ end
@@ -0,0 +1,36 @@
1
+ module Rubaidh
2
+ module GoogleAnalyticsViewHelper #:nodoc:
3
+ def link_to_tracked(name, track_path = "/", options = {}, html_options = {})
4
+
5
+ html_options.merge!({:onclick => tracking_call(track_path)})
6
+ link_to name, options, html_options
7
+ end
8
+
9
+ def link_to_tracked_if(condition, name, track_path = "/", options = {}, html_options = {}, &block)
10
+ html_options.merge!({:onclick => tracking_call(track_path)})
11
+ link_to_unless !condition, name, options, html_options, &block
12
+ end
13
+
14
+ def link_to_tracked_unless(condition, name, track_path = "/", options = {}, html_options = {}, &block)
15
+ html_options.merge!({:onclick => tracking_call(track_path)})
16
+ link_to_unless condition, name, options, html_options, &block
17
+ end
18
+
19
+ def link_to_tracked_unless_current(name, track_path = "/", options = {}, html_options = {}, &block)
20
+ html_options.merge!({:onclick =>tracking_call(track_path)})
21
+ link_to_unless current_page?(options), name, options, html_options, &block
22
+ end
23
+
24
+ private
25
+
26
+ def tracking_call(track_path)
27
+ if GoogleAnalytics.legacy_mode
28
+ "javascript:urchinTracker('#{track_path}');"
29
+ else
30
+ "javascript:pageTracker._trackPageview('#{track_path}');"
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+
data/rails/init.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  require 'rubaidh/google_analytics'
2
+ require 'rubaidh/view_helpers'
2
3
  ActionController::Base.send :include, Rubaidh::GoogleAnalyticsMixin
3
- ActionController::Base.send :after_filter, :add_google_analytics_code
4
+ ActionController::Base.send :after_filter, :add_google_analytics_code
5
+ ActionView::Base.send :include, Rubaidh::GoogleAnalyticsViewHelper
@@ -0,0 +1,10 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
3
+
4
+ require 'test/unit'
5
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
6
+
7
+ #config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
8
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
9
+
10
+ db_adapter = ENV['DB']
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ include Rubaidh::GoogleAnalyticsViewHelper
3
+ include ActionView::Helpers::UrlHelper
4
+ include ActionView::Helpers::TagHelper
5
+
6
+ class ViewHelpersTest < Test::Unit::TestCase
7
+ def test_link_to_tracked_should_return_a_tracked_link
8
+ assert_equal "<a href=\"http://www.example.com\" onclick=\"javascript:pageTracker._trackPageview('/sites/linked');\">Link</a>", link_to_tracked('Link', '/sites/linked', "http://www.example.com" )
9
+ end
10
+
11
+ def test_link_to_tracked_with_legacy_should_return_a_tracked_link
12
+ Rubaidh::GoogleAnalytics.legacy_mode = true
13
+ assert_equal "<a href=\"http://www.example.com\" onclick=\"javascript:urchinTracker('/sites/linked');\">Link</a>", link_to_tracked('Link', '/sites/linked', "http://www.example.com" )
14
+ end
15
+
16
+ def test_link_to_tracked_if_with_true_should_return_a_tracked_link
17
+ assert_equal "<a href=\"http://www.example.com\" onclick=\"javascript:pageTracker._trackPageview('/sites/linked');\">Link</a>", link_to_tracked_if(true, 'Link', '/sites/linked', "http://www.example.com" )
18
+ end
19
+
20
+ def test_link_to_tracked_if_with_false_should_return_unlinked_text
21
+ assert_equal "Link", link_to_tracked_if(false, 'Link', '/sites/linked', "http://www.example.com" )
22
+ end
23
+
24
+ def test_link_to_tracked_unless_with_false_should_return_a_tracked_link
25
+ assert_equal "<a href=\"http://www.example.com\" onclick=\"javascript:pageTracker._trackPageview('/sites/linked');\">Link</a>", link_to_tracked_unless(false, 'Link', '/sites/linked', "http://www.example.com" )
26
+ end
27
+
28
+ def test_link_to_tracked_unless_with_true_should_return_unlinked_text
29
+ assert_equal "Link", link_to_tracked_unless(true, 'Link', '/sites/linked', "http://www.example.com" )
30
+ end
31
+
32
+ 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.20080710
4
+ version: 1.0.20080714
5
5
  platform: ruby
6
6
  authors:
7
7
  - Graeme Mathieson
@@ -34,7 +34,10 @@ files:
34
34
  - Rakefile
35
35
  - rails/init.rb
36
36
  - test/google_analytics_test.rb
37
+ - test/test_helper.rb
38
+ - test/view_helpers_test.rb
37
39
  - lib/rubaidh/google_analytics.rb
40
+ - lib/rubaidh/view_helpers.rb
38
41
  has_rdoc: false
39
42
  homepage: http://github.com/rubaidh/google_analytics/tree/master
40
43
  post_install_message: