Skip to content

Commit

Permalink
passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simianarmy committed Oct 4, 2011
1 parent ed0b88e commit 10d7876
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/rubaidh/google_analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ def self.google_analytics_code(ssl = false)
pageTracker._initData();
pageTracker._trackPageview(#{request_tracked_path});
} catch(err) {}
function recordOutboundLink(link, category, action, opt_label, opt_value) {
try {
pageTracker._trackEvent(category, action, opt_label, opt_value);
setTimeout('document.location = "' + link.href + '"', 100)
}catch(err){}
}
//--><!]]>
|)
end
Expand All @@ -214,6 +220,10 @@ def self.legacy_google_analytics_code(ssl = false)
# Construct the new asynchronous version of the Google Analytics code.
def self.asynchronous_google_analytics_code
JavaScriptTagHelper.new.javascript_tag %Q~
function recordOutboundLink(link, category, action, opt_label, opt_value) {
_gaq.push(['_trackEvent', category, action, opt_label, opt_value]);
setTimeout('document.location = "' + link.href + '"', 100);
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '#{request_tracker_id}']);
_gaq.push(['_trackPageview']);
Expand Down
14 changes: 14 additions & 0 deletions lib/rubaidh/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def link_to_tracked_unless_current(name, track_path = "/", options = {}, html_op
link_to_unless current_page?(options), name, options, html_options, &block
end

# Creates a link tag of the given +name+ using a URL created by the set of +url_options+,
# with event tracking created by +event_options+ in Google Analytics. The +html_options+
# will accept a hash of attributes for the link tag.
def link_to_tracked_event(name, category, action, url_options = {}, event_options={}, html_options = {})
raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use event tracking") if GoogleAnalytics.defer_load == true
html_options.merge!({:onclick =>event_tracking_call(
category, action, event_options[:label], event_options[:value])})
link_to name, url_options, html_options
end
private

def tracking_call(track_path)
Expand All @@ -54,6 +63,11 @@ def tracking_call(track_path)
end
end

def event_tracking_call(category, action, opt_label, opt_value)
unless GoogleAnalytics.legacy_mode
"javascript:recordOutboundLink(this, '#{category}', '#{action}', '#{opt_label}', '#{opt_value}');return false;"
end
end
end

# Error raised by tracking methods if Rubaidh::GoogleAnalytics.defer_load is not configured
Expand Down
26 changes: 24 additions & 2 deletions test/view_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
class ViewHelpersTest < Test::Unit::TestCase

def setup
Rubaidh::GoogleAnalytics.defer_load = false
Rubaidh::GoogleAnalytics.defer_load = false
Rubaidh::GoogleAnalytics.asynchronous_mode = false
Rubaidh::GoogleAnalytics.legacy_mode = false
end

def test_link_to_tracked_should_return_a_tracked_link
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" )
end
Expand Down Expand Up @@ -59,4 +60,25 @@ def test_link_to_tracked_unless_current
#postponed
end

def test_link_to_tracked_event_should_return_a_event_tracking_link
assert_equal "<a href=\"http://www.example.com\" onclick=\"javascript:recordOutboundLink(this, 'Event', 'example.com', '', '');return false;\">Link</a>",
link_to_tracked_event('Link', 'Event', 'example.com', "http://www.example.com")
end

def test_link_to_tracked_event_with_async_should_return_a_tracked_link
Rubaidh::GoogleAnalytics.asynchronous_mode = true
assert_equal "<a href=\"http://www.example.com\" onclick=\"javascript:recordOutboundLink(this, 'Event', 'example.com', '', '');return false;\">Link</a>",
link_to_tracked_event('Link', 'Event', 'example.com', "http://www.example.com")
end

def test_link_to_tracked_event_with_legacy_should_return_a_regular_link
Rubaidh::GoogleAnalytics.legacy_mode = true
assert_equal "<a href=\"http://www.example.com\">Link</a>",
link_to_tracked_event('Link', 'Event', 'example.com', "http://www.example.com")
end

def test_link_to_tracked_event_should_error_if_defer_load
Rubaidh::GoogleAnalytics.defer_load = true
assert_raise(Rubaidh::AnalyticsError) { link_to_tracked_event('Link', 'Event', 'example.com', "http://www.example.com") }
end
end

0 comments on commit 10d7876

Please sign in to comment.