ua-google-analytics-rails 1.0.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.
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+
3
+ module GoogleAnalytics
4
+ # Gem version
5
+ VERSION = "1.0.0"
6
+ end
@@ -0,0 +1 @@
1
+ require 'google-analytics'
@@ -0,0 +1,94 @@
1
+ require 'test_helper'
2
+
3
+ class AsyncTrackingQueueTest < Test::Unit::TestCase
4
+ def teardown
5
+ GoogleAnalytics.script_source = nil
6
+ end
7
+
8
+ VALID_SNIPPET = <<-JAVASCRIPT
9
+ <script type="text/javascript">
10
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
11
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
12
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
13
+ })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
14
+ ga('send','event',1);
15
+ ga('send','event',2);
16
+ ga('t2.send','event',1);
17
+ ga('t2.send','event',2);
18
+ </script>
19
+ JAVASCRIPT
20
+
21
+ def test_queue_renders_valid_javascript_snippit
22
+ gaq = GAQ.new
23
+
24
+ # Add 2 events to the default tracker
25
+ gaq << GA::Event.new('send', 'event', 1)
26
+ gaq << GA::Event.new('send', 'event', 2)
27
+
28
+ # Add 2 events for an alternate tracker
29
+ gaq.push(GA::Event.new('send', 'event', 1), 't2')
30
+ gaq.push(GA::Event.new('send', 'event', 2), 't2')
31
+
32
+ assert_equal(VALID_SNIPPET, gaq.to_s)
33
+ end
34
+
35
+ # TODO: Add Double Click Snippet Support
36
+ #
37
+ # VALID_DOUBLECLICK_SNIPPET = <<-JAVASCRIPT
38
+ # <script type="text/javascript">
39
+ # var _gaq = _gaq || [];
40
+ # _gaq.push(['event1',1]);
41
+ # _gaq.push(['event2',2]);
42
+ # _gaq.push(['t2.event1',1]);
43
+ # _gaq.push(['t2.event2',2]);
44
+ # (function() {
45
+ # var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
46
+ # ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
47
+ # var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
48
+ # })();
49
+ # </script>
50
+ # JAVASCRIPT
51
+
52
+ # def test_queue_renders_valid_javascript_snippit_with_doubleclick_src
53
+ # GoogleAnalytics.script_source = :doubleclick
54
+ # gaq = GAQ.new
55
+
56
+ # # Add 2 events to the default tracker
57
+ # gaq << GA::Event.new('event1', 1)
58
+ # gaq << GA::Event.new('event2', 2)
59
+
60
+ # # Add 2 events for an alternate tracker
61
+ # gaq.push(GA::Event.new('event1', 1), 't2')
62
+ # gaq.push(GA::Event.new('event2', 2), 't2')
63
+
64
+ # assert_equal(VALID_DOUBLECLICK_SNIPPET, gaq.to_s)
65
+ # end
66
+
67
+ VALID_CUSTOM_SNIPPET = <<-JAVASCRIPT
68
+ <script type="text/javascript">
69
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
70
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
71
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
72
+ })(window,document,'script','http://127.0.0.1/custom.js','ga');
73
+ ga('send','event',1);
74
+ ga('send','event',2);
75
+ ga('t2.send','event',1);
76
+ ga('t2.send','event',2);
77
+ </script>
78
+ JAVASCRIPT
79
+
80
+ def test_queue_renders_valid_javascript_snippit_with_custom_src
81
+ GoogleAnalytics.script_source = "'http://127.0.0.1/custom.js'"
82
+ gaq = GAQ.new
83
+
84
+ # Add 2 events to the default tracker
85
+ gaq << GA::Event.new('send', 'event', 1)
86
+ gaq << GA::Event.new('send', 'event', 2)
87
+
88
+ # Add 2 events for an alternate tracker
89
+ gaq.push(GA::Event.new('send', 'event', 1), 't2')
90
+ gaq.push(GA::Event.new('send', 'event', 2), 't2')
91
+
92
+ assert_equal(VALID_CUSTOM_SNIPPET, gaq.to_s)
93
+ end
94
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class EventCollectionRendererTest < Test::Unit::TestCase
4
+ def test_event_collection_renderer_yield_proper_javascript_snippit_for_default_tracker
5
+ event_collection = GA::EventCollection.new
6
+ event_collection << GA::Event.new('send', 'evt', '1')
7
+ event_collection << GA::Event.new('send', 'evt', '2')
8
+ event_collection << GA::Event.new('send', 'evt', '3')
9
+
10
+ ecr = GA::EventCollectionRenderer.new(event_collection, nil)
11
+ assert_equal("ga('send','evt','1');\nga('send','evt','2');\nga('send','evt','3');", ecr.to_s)
12
+ end
13
+
14
+ def test_event_collection_renderer_yield_proper_javascript_snippit_for_custom_tracker
15
+ event_collection = GA::EventCollection.new
16
+ event_collection << GA::Event.new('send', 'evt', 1)
17
+ event_collection << GA::Event.new('send', 'evt', 2)
18
+ event_collection << GA::Event.new('send', 'evt', 3)
19
+
20
+ ecr = GA::EventCollectionRenderer.new(event_collection, 't2')
21
+ assert_equal("ga('t2.send','evt',1);\nga('t2.send','evt',2);\nga('t2.send','evt',3);", ecr.to_s)
22
+ end
23
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class EventCollectionTest < Test::Unit::TestCase
4
+ def test_event_collection_raises_on_non_event_insertion
5
+ ec = GA::EventCollection.new
6
+ assert_raise(GA::EventCollection::InvalidEventError) { ec << "This is invalid" }
7
+ end
8
+
9
+ def test_event_collection_is_enumerable_and_iterates_in_insertion_order
10
+ ec = GA::EventCollection.new
11
+
12
+ assert(ec.respond_to?(:each))
13
+
14
+ ec << (event0 = GA::Event.new('sample', 'test'))
15
+ ec << (event1 = GA::Event.new('sample2', 'test2'))
16
+ ec << (event3 = GA::Event.new('sample3', 'test3'))
17
+
18
+ items = ec.map { |e| e }
19
+
20
+ assert_equal(event0, items[0])
21
+ assert_equal(event1, items[1])
22
+ assert_equal(event3, items[2])
23
+ end
24
+
25
+ def test_event_collection_delegates_size_and_length
26
+ ec = GA::EventCollection.new
27
+
28
+ assert(ec.respond_to?(:size))
29
+ assert(ec.respond_to?(:length))
30
+
31
+ assert_equal(0, ec.size)
32
+ assert_equal(ec.size, ec.length)
33
+
34
+ ec << GA::Event.new('sample', 'test')
35
+ assert_equal(1, ec.size)
36
+ assert_equal(ec.size, ec.length)
37
+
38
+ ec << GA::Event.new('sample2', 'test2')
39
+ assert_equal(2, ec.size)
40
+ assert_equal(ec.size, ec.length)
41
+ end
42
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class EventRendererTest < Test::Unit::TestCase
4
+ def test_event_renderer_yield_proper_javascript_snippit_for_default_tracker
5
+ er = GA::EventRenderer.new(GA::Event.new('send', 'something', 1, 2, 3), nil)
6
+ assert_equal("ga('send','something',1,2,3);", er.to_s)
7
+ end
8
+
9
+ def test_event_renderer_yield_proper_javascript_snippit_for_custom_tracker
10
+ er = GA::EventRenderer.new(GA::Event.new('send', 'something', 1, 2, 3), 't2')
11
+ assert_equal("ga('t2.send','something',1,2,3);", er.to_s)
12
+ end
13
+ def test_event_renderer_yield_proper_javascript_snippit_for_custom_tracker_creation
14
+ er = GA::EventRenderer.new(GA::Events::SetupAnalytics.new('TEST', {:name => 't2'}), 't2')
15
+ assert_match(Regexp.new('"cookieDomain":"auto"'), er.to_s)
16
+ assert_match(Regexp.new('"name":"t2"'), er.to_s)
17
+ end
18
+
19
+ def test_single_event_renderer_yield_proper_javascript_snippit_for_transaction_send
20
+ er = GA::EventRenderer.new(GA::Events::Ecommerce::TrackTransaction.new, nil)
21
+ assert_equal("ga('ecommerce:send');", er.to_s)
22
+ end
23
+ def test_single_event_renderer_yield_proper_javascript_snippit_for_transaction_send_with_custom_tracker
24
+ er = GA::EventRenderer.new(GA::Events::Ecommerce::TrackTransaction.new, 't2')
25
+ assert_equal("ga('t2.ecommerce:send');", er.to_s)
26
+ end
27
+ end
@@ -0,0 +1,147 @@
1
+ require 'test_helper'
2
+
3
+ class GAEventsTest < Test::Unit::TestCase
4
+ def test_default_account_creation
5
+ event = GA::Events::SetupAnalytics.new('ABC123')
6
+ assert_equal('create', event.action)
7
+ assert_equal('ABC123', event.name)
8
+ assert_equal(['auto'], event.params)
9
+ end
10
+ def test_account_creation_with_cookie_domain
11
+ event = GA::Events::SetupAnalytics.new('ABC123', 'example.com')
12
+ assert_equal('create', event.action)
13
+ assert_equal('ABC123', event.name)
14
+ assert_equal([{:cookieDomain=>"example.com"}], event.params)
15
+ end
16
+ def test_account_creation_with_no_cookie_domain
17
+ event = GA::Events::SetupAnalytics.new('ABC123', 'none')
18
+ assert_equal('create', event.action)
19
+ assert_equal('ABC123', event.name)
20
+ assert_equal(['none'], event.params)
21
+ end
22
+
23
+ def test_require_statement
24
+ event = GA::Events::Require.new('ecommerce', 'ecommerce.js')
25
+ assert_equal('require', event.action)
26
+ assert_equal('ecommerce', event.name)
27
+ assert_equal(['ecommerce.js'], event.params)
28
+ end
29
+
30
+ def test_track_pageview_event
31
+ event = GA::Events::TrackPageview.new
32
+ assert_equal('send', event.action)
33
+ assert_equal('pageview', event.name)
34
+ assert_equal([], event.params)
35
+ end
36
+
37
+ def test_track_pageview_event_with_virtual_page
38
+ event = GA::Events::TrackPageview.new('/foo/bar')
39
+ assert_equal('send', event.action)
40
+ assert_equal('pageview', event.name)
41
+ assert_equal(['/foo/bar'], event.params)
42
+ end
43
+
44
+ def test_track_event_without_category_or_label
45
+ event = GA::Events::TrackEvent.new('Search', 'Executed')
46
+ assert_equal('send', event.action)
47
+ assert_equal('event', event.name)
48
+ assert_equal(['Search', 'Executed'], event.params)
49
+ end
50
+
51
+ def test_track_event_with_label
52
+ event = GA::Events::TrackEvent.new('Search', 'Executed', 'Son of Sam')
53
+ assert_equal('send', event.action)
54
+ assert_equal('event', event.name)
55
+ assert_equal(['Search', 'Executed', 'Son of Sam', nil], event.params)
56
+ end
57
+
58
+ def test_track_event_with_value
59
+ event = GA::Events::TrackEvent.new('Search', 'Executed', nil, 1)
60
+ assert_equal('send', event.action)
61
+ assert_equal('event', event.name)
62
+ assert_equal(['Search', 'Executed', nil, 1], event.params)
63
+ end
64
+
65
+ def test_track_event_with_label_and_value
66
+ event = GA::Events::TrackEvent.new('Search', 'Executed', 'Son of Sam', 1)
67
+ assert_equal('send', event.action)
68
+ assert_equal('event', event.name)
69
+ assert_equal(['Search', 'Executed', 'Son of Sam', 1], event.params)
70
+ end
71
+
72
+ def test_set_custom_dimension
73
+ event = GA::Events::SetCustomDimension.new(1, 2)
74
+ assert_equal('set', event.action)
75
+ assert_equal("dimension1", event.name)
76
+ assert_equal(['2'], event.params)
77
+ end
78
+
79
+ def test_set_custom_dimension_with_invalid_index
80
+ assert_raise ArgumentError do
81
+ GA::Events::SetCustomDimension.new(6, 1)
82
+ end
83
+ end
84
+
85
+ def test_set_custom_metric
86
+ event = GA::Events::SetCustomMetric.new(1, 2)
87
+ assert_equal('set', event.action)
88
+ assert_equal("metric1", event.name)
89
+ assert_equal(['2'], event.params)
90
+ end
91
+
92
+ def test_set_custom_metric_with_invalid_index
93
+ assert_raise ArgumentError do
94
+ GA::Events::SetCustomMetric.new(6, 1)
95
+ end
96
+ end
97
+
98
+ def test_anonymize_ip_event
99
+ event = GA::Events::AnonymizeIp.new
100
+ assert_equal('set', event.action)
101
+ assert_equal('anonymizeIp', event.name)
102
+ assert_equal([true], event.params)
103
+ end
104
+
105
+ def test_ecommerce_add_transaction_event_old_format
106
+ event = GA::Events::Ecommerce::AddTransaction.new(1, 'ACME', 123.45, 13.27, 75.35, 'Dallas', 'TX', 'USA')
107
+ assert_equal('ecommerce:addTransaction', event.action)
108
+ assert_equal({
109
+ :id => '1',
110
+ :affiliation => 'ACME',
111
+ :revenue => '123.45',
112
+ :tax => '13.27',
113
+ :shipping => '75.35'
114
+ }, event.name)
115
+ end
116
+
117
+ def test_ecommerce_add_transaction_event_new_format
118
+ event = GA::Events::Ecommerce::AddTransaction.new(1, 'ACME', 123.45, 13.27, 75.35)
119
+ assert_equal('ecommerce:addTransaction', event.action)
120
+ assert_equal({
121
+ :id => '1',
122
+ :affiliation => 'ACME',
123
+ :revenue => '123.45',
124
+ :tax => '13.27',
125
+ :shipping => '75.35'
126
+ }, event.name)
127
+ end
128
+
129
+ def test_ecommerce_add_item_event
130
+ event = GA::Events::Ecommerce::AddItem.new(1, 123, 'Bacon', 'Chunky', 5.00, 42)
131
+ assert_equal('ecommerce:addItem', event.action)
132
+ assert_equal({
133
+ :id => '1',
134
+ :sku => '123',
135
+ :name => 'Bacon',
136
+ :category => 'Chunky',
137
+ :price => '5.0',
138
+ :quantity => '42'
139
+ }, event.name)
140
+ end
141
+
142
+ def test_ecommerce_track_trans_event
143
+ event = GA::Events::Ecommerce::TrackTransaction.new
144
+ assert_equal('ecommerce:send', event.action)
145
+ assert_equal([], event.params)
146
+ end
147
+ end
@@ -0,0 +1,133 @@
1
+ require 'test_helper'
2
+ require 'google-analytics/rails/view_helpers'
3
+
4
+ class ViewHelpersTest < Test::Unit::TestCase
5
+ include GoogleAnalytics::Rails::ViewHelpers
6
+
7
+ VALID_JS_INCLUDE = <<-JAVASCRIPT
8
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
9
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
10
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
11
+ })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
12
+ JAVASCRIPT
13
+
14
+ VALID_INIT = <<-JAVASCRIPT
15
+ <script type="text/javascript">
16
+ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
17
+ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
18
+ m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
19
+ })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
20
+ ga('create','TEST','auto');
21
+ ga('send','pageview');
22
+ </script>
23
+ JAVASCRIPT
24
+
25
+ def test_analytics_init
26
+ assert_equal(VALID_INIT, analytics_init)
27
+ end
28
+
29
+ def test_analytics_init_with_special_name
30
+ str = analytics_init(:name => 't2').to_s
31
+ assert(str.include?(VALID_JS_INCLUDE))
32
+ assert_match(/.+ga\('create','TEST',\{.+\}\);.+/m, str)
33
+ assert_match(/.+"cookieDomain":"auto".+/m, str)
34
+ assert_match(/.+"name":"t2".+/m, str)
35
+ assert_match(/.+ga\('t2.send','pageview'\);.+/m, str)
36
+ end
37
+
38
+ def test_analytics_init_with_virtual_pageview
39
+ str = analytics_init(:page => '/some/virtual/url').to_s
40
+ assert(str.include?(VALID_JS_INCLUDE))
41
+ assert_match(/.+ga\('create','TEST','auto'\);.+/m, str)
42
+ assert_match(/.+ga\('send','pageview','\/some\/virtual\/url'\);.+/m, str)
43
+ end
44
+
45
+ def test_analytics_init_with_virtual_pageview_and_custom_title
46
+ str = analytics_init(:page => '/some/virtual/url', :title => 'Hello World').to_s
47
+ assert(str.include?(VALID_JS_INCLUDE))
48
+ assert_match(/.+ga\('create','TEST','auto'\);.+/m, str)
49
+ assert_match(/.+ga\('send','pageview'.+/m, str)
50
+ assert_match(/.+"page":"\/some\/virtual\/url".+/m, str)
51
+ assert_match(/.+"title":"Hello World".+/m, str)
52
+ end
53
+
54
+ def test_analytics_init_with_custom_tracker
55
+ str = analytics_init(:tracker => 'UA-CUSTOM-XX').to_s
56
+ assert(str.include?(VALID_JS_INCLUDE))
57
+ assert_match(/.+ga\('create','UA-CUSTOM-XX','auto'\);.+/m, str)
58
+ assert_match(/.+ga\('send','pageview'\);.+/m, str)
59
+ end
60
+
61
+ def test_analytics_init_with_custom_domain
62
+ str = analytics_init(:domain => 'example.com').to_s
63
+ assert(str.include?(VALID_JS_INCLUDE))
64
+ assert_match(/.+ga\('create','TEST',\{"cookieDomain":"example.com"\}\);.+/m, str)
65
+ assert_match(/.+ga\('send','pageview'\);.+/m, str)
66
+ end
67
+
68
+ def test_local_analytics_init
69
+ str = analytics_init(:local => true).to_s
70
+ assert(str.include?(VALID_JS_INCLUDE))
71
+ assert_match(/.+ga\('create','TEST',\{.+\}\);.+/m, str)
72
+ assert_match(/.+"cookieDomain":"none".+/m, str)
73
+ assert_match(/.+"allowLinker":true.+/m, str)
74
+ assert_match(/.+ga\('send','pageview'\);.+/m, str)
75
+ end
76
+
77
+ def test_analytics_init_with_anonymized_ip
78
+ str = analytics_init(:anonymize => true).to_s
79
+ assert(str.include?(VALID_JS_INCLUDE))
80
+ assert_match(/.+ga\('create','TEST','auto'\);.+/m, str)
81
+ assert_match(/.+ga\('set','anonymizeIp',true\);.+/m, str)
82
+ assert_match(/.+ga\('send','pageview'\);.+/m, str)
83
+ end
84
+
85
+ def test_analytics_init_with_link_attribution
86
+ str = analytics_init(:enhanced_link_attribution => true).to_s
87
+ assert(str.include?(VALID_JS_INCLUDE))
88
+ assert_match(/.+ga\('create','TEST','auto'\);.+/m, str)
89
+ assert_match(/.+ga\('require','linkid'\);.+/m, str)
90
+ assert_match(/.+ga\('send','pageview'\);.+/m, str)
91
+ end
92
+
93
+ def test_analytics_init_with_events
94
+ str = analytics_init(:add_events => GA::Events::SetAllowLinker.new(true)).to_s
95
+ assert(str.include?(VALID_JS_INCLUDE))
96
+ assert_match(/.+ga\('create','TEST',\{.+\}\);.+/m, str)
97
+ assert_match(/.+"cookieDomain":"auto".+/m, str)
98
+ assert_match(/.+"allowLinker":true.+/m, str)
99
+ assert_match(/.+ga\('send','pageview'\);.+/m, str)
100
+ end
101
+
102
+ def test_analytics_init_with_samplerate_events
103
+ str = analytics_init(:add_events => GA::Events::SetSiteSpeedSampleRate.new(5)).to_s
104
+ assert(str.include?(VALID_JS_INCLUDE))
105
+ assert_match(/.+ga\('create','TEST',\{.+\}\);.+/m, str)
106
+ assert_match(/.+"cookieDomain":"auto".+/m, str)
107
+ assert_match(/.+"siteSpeedSampleRate":5.+/m, str)
108
+ assert_match(/.+ga\('send','pageview'\);.+/m, str)
109
+ end
110
+
111
+ def test_analytics_init_with_custom_vars
112
+ str = analytics_init(:custom_vars => GA::Events::SetCustomVar.new(1, 'test', 'hoge',1)).to_s
113
+ assert(str.include?(VALID_JS_INCLUDE))
114
+ assert_match(/.+ga\('create','TEST','auto'\);.+/m, str)
115
+ assert_match(/.+ga\('set','dimension1','hoge'\);.+/m, str)
116
+ assert_match(/.+ga\('send','pageview'\);.+/m, str)
117
+ end
118
+
119
+ def test_analytics_init_with_custom_dimension
120
+ str = analytics_init(:custom_vars => GA::Events::SetCustomDimension.new(1, 'hoge')).to_s
121
+ assert(str.include?(VALID_JS_INCLUDE))
122
+ assert_match(/.+ga\('create','TEST','auto'\);.+/m, str)
123
+ assert_match(/.+ga\('set','dimension1','hoge'\);.+/m, str)
124
+ assert_match(/.+ga\('send','pageview'\);.+/m, str)
125
+ end
126
+
127
+ VALID_TRACK_EVENT = "ga('send','event','Videos','Play','Gone With the Wind',null);"
128
+
129
+ def test_analytics_track_event
130
+ event = analytics_track_event("Videos", "Play", "Gone With the Wind")
131
+ assert_equal(VALID_TRACK_EVENT, event)
132
+ end
133
+ end