markcatley-google_analytics 1.0.20080717 → 1.0.20080720
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 +7 -0
- data/lib/rubaidh/google_analytics.rb +1 -1
- data/lib/rubaidh/view_helpers.rb +12 -1
- data/test/view_helpers_test.rb +24 -0
- metadata +1 -1
data/README
CHANGED
@@ -51,6 +51,13 @@ link_to_tracked_if(condition, name, track_path = "/", options = {}, html_options
|
|
51
51
|
link_to_tracked_unless(condition, name, track_path = "/", options = {}, html_options = {}, &block)
|
52
52
|
link_to_tracked_unless_current(name, track_path = "/", options = {}, html_options = {}, &block)
|
53
53
|
|
54
|
+
To track outbound links, you should set
|
55
|
+
|
56
|
+
Rubaidh::GoogleAnalytics.defer_load = false
|
57
|
+
|
58
|
+
This will move the tracking javascript to the top of your page.
|
59
|
+
(see http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527&topic=11006)
|
60
|
+
|
54
61
|
Tracked links respect the legacy_mode flag.
|
55
62
|
|
56
63
|
Note: Link-tracking works by inserting onclick() code in the HTML. Because of this, it will overwrite
|
@@ -60,7 +60,7 @@ module Rubaidh # :nodoc:
|
|
60
60
|
# Set this to true if you want to load the Analytics javascript at the bottom of
|
61
61
|
# each page rather than at the top. This may result in faster page render times,
|
62
62
|
# but may break link_to_tracked functionality.
|
63
|
-
@@defer_load =
|
63
|
+
@@defer_load = true
|
64
64
|
cattr_accessor :defer_load
|
65
65
|
|
66
66
|
# Set this to true to use a local copy of the ga.js (or urchin.js) file.
|
data/lib/rubaidh/view_helpers.rb
CHANGED
@@ -1,22 +1,25 @@
|
|
1
1
|
module Rubaidh
|
2
2
|
module GoogleAnalyticsViewHelper #:nodoc:
|
3
3
|
def link_to_tracked(name, track_path = "/", options = {}, html_options = {})
|
4
|
-
|
4
|
+
raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
|
5
5
|
html_options.merge!({:onclick => tracking_call(track_path)})
|
6
6
|
link_to name, options, html_options
|
7
7
|
end
|
8
8
|
|
9
9
|
def link_to_tracked_if(condition, name, track_path = "/", options = {}, html_options = {}, &block)
|
10
|
+
raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
|
10
11
|
html_options.merge!({:onclick => tracking_call(track_path)})
|
11
12
|
link_to_unless !condition, name, options, html_options, &block
|
12
13
|
end
|
13
14
|
|
14
15
|
def link_to_tracked_unless(condition, name, track_path = "/", options = {}, html_options = {}, &block)
|
16
|
+
raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
|
15
17
|
html_options.merge!({:onclick => tracking_call(track_path)})
|
16
18
|
link_to_unless condition, name, options, html_options, &block
|
17
19
|
end
|
18
20
|
|
19
21
|
def link_to_tracked_unless_current(name, track_path = "/", options = {}, html_options = {}, &block)
|
22
|
+
raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
|
20
23
|
html_options.merge!({:onclick =>tracking_call(track_path)})
|
21
24
|
link_to_unless current_page?(options), name, options, html_options, &block
|
22
25
|
end
|
@@ -32,5 +35,13 @@ private
|
|
32
35
|
end
|
33
36
|
|
34
37
|
end
|
38
|
+
|
39
|
+
class AnalyticsError < StandardError
|
40
|
+
attr_reader :message
|
41
|
+
|
42
|
+
def initialize(message)
|
43
|
+
@message = message
|
44
|
+
end
|
45
|
+
end
|
35
46
|
end
|
36
47
|
|
data/test/view_helpers_test.rb
CHANGED
@@ -4,6 +4,11 @@ include ActionView::Helpers::UrlHelper
|
|
4
4
|
include ActionView::Helpers::TagHelper
|
5
5
|
|
6
6
|
class ViewHelpersTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
Rubaidh::GoogleAnalytics.defer_load = false
|
10
|
+
end
|
11
|
+
|
7
12
|
def test_link_to_tracked_should_return_a_tracked_link
|
8
13
|
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
14
|
end
|
@@ -13,6 +18,11 @@ class ViewHelpersTest < Test::Unit::TestCase
|
|
13
18
|
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
19
|
end
|
15
20
|
|
21
|
+
def test_link_to_tracked_should_error_if_defer_load
|
22
|
+
Rubaidh::GoogleAnalytics.defer_load = true
|
23
|
+
assert_raise(Rubaidh::AnalyticsError) { link_to_tracked('Link', '/sites/linked', "http://www.example.com" ) }
|
24
|
+
end
|
25
|
+
|
16
26
|
def test_link_to_tracked_if_with_true_should_return_a_tracked_link
|
17
27
|
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
28
|
end
|
@@ -21,6 +31,11 @@ class ViewHelpersTest < Test::Unit::TestCase
|
|
21
31
|
assert_equal "Link", link_to_tracked_if(false, 'Link', '/sites/linked', "http://www.example.com" )
|
22
32
|
end
|
23
33
|
|
34
|
+
def test_link_to_tracked_if_should_error_if_defer_load
|
35
|
+
Rubaidh::GoogleAnalytics.defer_load = true
|
36
|
+
assert_raise(Rubaidh::AnalyticsError) { link_to_tracked_if(false, 'Link', '/sites/linked', "http://www.example.com" ) }
|
37
|
+
end
|
38
|
+
|
24
39
|
def test_link_to_tracked_unless_with_false_should_return_a_tracked_link
|
25
40
|
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
41
|
end
|
@@ -29,4 +44,13 @@ class ViewHelpersTest < Test::Unit::TestCase
|
|
29
44
|
assert_equal "Link", link_to_tracked_unless(true, 'Link', '/sites/linked', "http://www.example.com" )
|
30
45
|
end
|
31
46
|
|
47
|
+
def test_link_to_tracked_unless_should_error_if_defer_load
|
48
|
+
Rubaidh::GoogleAnalytics.defer_load = true
|
49
|
+
assert_raise(Rubaidh::AnalyticsError) { link_to_tracked_unless(false, 'Link', '/sites/linked', "http://www.example.com" ) }
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_link_to_tracked_unless_current
|
53
|
+
#postponed
|
54
|
+
end
|
55
|
+
|
32
56
|
end
|