google-analytics-rails 0.0.6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,5 +2,5 @@
2
2
 
3
3
  module GoogleAnalytics
4
4
  # Gem version
5
- VERSION = "0.0.6"
5
+ VERSION = "1.0.0"
6
6
  end
@@ -7,16 +7,14 @@ class AsyncTrackingQueueTest < Test::Unit::TestCase
7
7
 
8
8
  VALID_SNIPPET = <<-JAVASCRIPT
9
9
  <script type="text/javascript">
10
- var _gaq = _gaq || [];
11
- _gaq.push(['event1',1]);
12
- _gaq.push(['event2',2]);
13
- _gaq.push(['t2.event1',1]);
14
- _gaq.push(['t2.event2',2]);
15
- (function() {
16
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
17
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
18
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
19
- })();
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);
20
18
  </script>
21
19
  JAVASCRIPT
22
20
 
@@ -24,58 +22,58 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga
24
22
  gaq = GAQ.new
25
23
 
26
24
  # Add 2 events to the default tracker
27
- gaq << GA::Event.new('event1', 1)
28
- gaq << GA::Event.new('event2', 2)
25
+ gaq << GA::Event.new('send', 'event', 1)
26
+ gaq << GA::Event.new('send', 'event', 2)
29
27
 
30
28
  # Add 2 events for an alternate tracker
31
- gaq.push(GA::Event.new('event1', 1), 't2')
32
- gaq.push(GA::Event.new('event2', 2), 't2')
29
+ gaq.push(GA::Event.new('send', 'event', 1), 't2')
30
+ gaq.push(GA::Event.new('send', 'event', 2), 't2')
33
31
 
34
32
  assert_equal(VALID_SNIPPET, gaq.to_s)
35
33
  end
36
34
 
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
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
66
 
67
67
  VALID_CUSTOM_SNIPPET = <<-JAVASCRIPT
68
68
  <script type="text/javascript">
69
- var _gaq = _gaq || [];
70
- _gaq.push(['event1',1]);
71
- _gaq.push(['event2',2]);
72
- _gaq.push(['t2.event1',1]);
73
- _gaq.push(['t2.event2',2]);
74
- (function() {
75
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
76
- ga.src = 'http://127.0.0.1/custom.js';
77
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
78
- })();
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);
79
77
  </script>
80
78
  JAVASCRIPT
81
79
 
@@ -84,12 +82,12 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga
84
82
  gaq = GAQ.new
85
83
 
86
84
  # Add 2 events to the default tracker
87
- gaq << GA::Event.new('event1', 1)
88
- gaq << GA::Event.new('event2', 2)
85
+ gaq << GA::Event.new('send', 'event', 1)
86
+ gaq << GA::Event.new('send', 'event', 2)
89
87
 
90
88
  # Add 2 events for an alternate tracker
91
- gaq.push(GA::Event.new('event1', 1), 't2')
92
- gaq.push(GA::Event.new('event2', 2), 't2')
89
+ gaq.push(GA::Event.new('send', 'event', 1), 't2')
90
+ gaq.push(GA::Event.new('send', 'event', 2), 't2')
93
91
 
94
92
  assert_equal(VALID_CUSTOM_SNIPPET, gaq.to_s)
95
93
  end
@@ -3,21 +3,21 @@ require 'test_helper'
3
3
  class EventCollectionRendererTest < Test::Unit::TestCase
4
4
  def test_event_collection_renderer_yield_proper_javascript_snippit_for_default_tracker
5
5
  event_collection = GA::EventCollection.new
6
- event_collection << GA::Event.new('event1', 1)
7
- event_collection << GA::Event.new('event2', 2)
8
- event_collection << GA::Event.new('event3', 3)
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
9
 
10
10
  ecr = GA::EventCollectionRenderer.new(event_collection, nil)
11
- assert_equal("_gaq.push(['event1',1]);\n_gaq.push(['event2',2]);\n_gaq.push(['event3',3]);", ecr.to_s)
11
+ assert_equal("ga('send','evt','1');\nga('send','evt','2');\nga('send','evt','3');", ecr.to_s)
12
12
  end
13
13
 
14
14
  def test_event_collection_renderer_yield_proper_javascript_snippit_for_custom_tracker
15
15
  event_collection = GA::EventCollection.new
16
- event_collection << GA::Event.new('event1', 1)
17
- event_collection << GA::Event.new('event2', 2)
18
- event_collection << GA::Event.new('event3', 3)
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
19
 
20
20
  ecr = GA::EventCollectionRenderer.new(event_collection, 't2')
21
- assert_equal("_gaq.push(['t2.event1',1]);\n_gaq.push(['t2.event2',2]);\n_gaq.push(['t2.event3',3]);", ecr.to_s)
21
+ assert_equal("ga('t2.send','evt',1);\nga('t2.send','evt',2);\nga('t2.send','evt',3);", ecr.to_s)
22
22
  end
23
23
  end
@@ -1,14 +1,14 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class EventCollectionTest < Test::Unit::TestCase
4
- def test_event_collection_raises_on_non_event_insertion
4
+ def test_event_collection_raises_on_non_event_insertion
5
5
  ec = GA::EventCollection.new
6
6
  assert_raise(GA::EventCollection::InvalidEventError) { ec << "This is invalid" }
7
7
  end
8
-
8
+
9
9
  def test_event_collection_is_enumerable_and_iterates_in_insertion_order
10
10
  ec = GA::EventCollection.new
11
-
11
+
12
12
  assert(ec.respond_to?(:each))
13
13
 
14
14
  ec << (event0 = GA::Event.new('sample', 'test'))
@@ -16,7 +16,7 @@ class EventCollectionTest < Test::Unit::TestCase
16
16
  ec << (event3 = GA::Event.new('sample3', 'test3'))
17
17
 
18
18
  items = ec.map { |e| e }
19
-
19
+
20
20
  assert_equal(event0, items[0])
21
21
  assert_equal(event1, items[1])
22
22
  assert_equal(event3, items[2])
@@ -2,12 +2,26 @@ require 'test_helper'
2
2
 
3
3
  class EventRendererTest < Test::Unit::TestCase
4
4
  def test_event_renderer_yield_proper_javascript_snippit_for_default_tracker
5
- er = GA::EventRenderer.new(GA::Event.new('_someEvent', 1, 2, 3), nil)
6
- assert_equal("_gaq.push(['_someEvent',1,2,3]);", er.to_s)
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
7
  end
8
8
 
9
9
  def test_event_renderer_yield_proper_javascript_snippit_for_custom_tracker
10
- er = GA::EventRenderer.new(GA::Event.new('_someEvent', 1, 2, 3), 't2')
11
- assert_equal("_gaq.push(['t2._someEvent',1,2,3]);", er.to_s)
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)
12
26
  end
13
27
  end
@@ -1,105 +1,147 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class GAEventsTest < Test::Unit::TestCase
4
- def test_set_account_event
5
- event = GA::Events::SetAccount.new('ABC123')
6
- assert_equal('_setAccount', event.name)
7
- assert_equal(['ABC123'], event.params)
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)
8
9
  end
9
-
10
- def test_set_domain_name_event
11
- event = GA::Events::SetDomainName.new('foo.com')
12
- assert_equal('_setDomainName', event.name)
13
- assert_equal(['foo.com'], event.params)
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)
14
21
  end
15
22
 
16
- def test_set_site_speed_sample_rate_event
17
- event = GA::Events::SetSiteSpeedSampleRate.new(5)
18
- assert_equal('_setSiteSpeedSampleRate', event.name)
19
- assert_equal([5], event.params)
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)
20
28
  end
21
29
 
22
30
  def test_track_pageview_event
23
31
  event = GA::Events::TrackPageview.new
24
- assert_equal('_trackPageview', event.name)
32
+ assert_equal('send', event.action)
33
+ assert_equal('pageview', event.name)
25
34
  assert_equal([], event.params)
26
35
  end
27
36
 
28
37
  def test_track_pageview_event_with_virtual_page
29
38
  event = GA::Events::TrackPageview.new('/foo/bar')
30
- assert_equal('_trackPageview', event.name)
39
+ assert_equal('send', event.action)
40
+ assert_equal('pageview', event.name)
31
41
  assert_equal(['/foo/bar'], event.params)
32
42
  end
33
43
 
34
44
  def test_track_event_without_category_or_label
35
45
  event = GA::Events::TrackEvent.new('Search', 'Executed')
36
- assert_equal('_trackEvent', event.name)
46
+ assert_equal('send', event.action)
47
+ assert_equal('event', event.name)
37
48
  assert_equal(['Search', 'Executed'], event.params)
38
49
  end
39
50
 
40
51
  def test_track_event_with_label
41
52
  event = GA::Events::TrackEvent.new('Search', 'Executed', 'Son of Sam')
42
- assert_equal('_trackEvent', event.name)
53
+ assert_equal('send', event.action)
54
+ assert_equal('event', event.name)
43
55
  assert_equal(['Search', 'Executed', 'Son of Sam', nil], event.params)
44
56
  end
45
57
 
46
58
  def test_track_event_with_value
47
59
  event = GA::Events::TrackEvent.new('Search', 'Executed', nil, 1)
48
- assert_equal('_trackEvent', event.name)
60
+ assert_equal('send', event.action)
61
+ assert_equal('event', event.name)
49
62
  assert_equal(['Search', 'Executed', nil, 1], event.params)
50
63
  end
51
64
 
52
65
  def test_track_event_with_label_and_value
53
66
  event = GA::Events::TrackEvent.new('Search', 'Executed', 'Son of Sam', 1)
54
- assert_equal('_trackEvent', event.name)
67
+ assert_equal('send', event.action)
68
+ assert_equal('event', event.name)
55
69
  assert_equal(['Search', 'Executed', 'Son of Sam', 1], event.params)
56
70
  end
57
71
 
58
- def test_set_custom_var
59
- event = GA::Events::SetCustomVar.new(1, 'VarName1', 'VarVal1', 1)
60
- assert_equal('_setCustomVar', event.name)
61
- assert_equal([1, 'VarName1', 'VarVal1', 1], event.params)
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)
62
77
  end
63
78
 
64
- def test_set_custom_var_with_invalid_index
79
+ def test_set_custom_dimension_with_invalid_index
65
80
  assert_raise ArgumentError do
66
- GA::Events::SetCustomVar.new(6, 'VarName1', 'VarVal1', 1)
81
+ GA::Events::SetCustomDimension.new(6, 1)
67
82
  end
68
83
  end
69
84
 
70
- def test_delete_custom_var
71
- event = GA::Events::DeleteCustomVar.new(1)
72
- assert_equal('_deleteCustomVar', event.name)
73
- assert_equal([1], event.params)
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)
74
90
  end
75
91
 
76
- def test_delete_custom_var_with_invalid_index
92
+ def test_set_custom_metric_with_invalid_index
77
93
  assert_raise ArgumentError do
78
- GA::Events::DeleteCustomVar.new(6)
94
+ GA::Events::SetCustomMetric.new(6, 1)
79
95
  end
80
96
  end
81
97
 
82
98
  def test_anonymize_ip_event
83
99
  event = GA::Events::AnonymizeIp.new
84
- assert_equal('_gat._anonymizeIp', event.name)
85
- assert_equal([], event.params)
100
+ assert_equal('set', event.action)
101
+ assert_equal('anonymizeIp', event.name)
102
+ assert_equal([true], event.params)
86
103
  end
87
104
 
88
- def test_ecommerce_add_transaction_event
105
+ def test_ecommerce_add_transaction_event_old_format
89
106
  event = GA::Events::Ecommerce::AddTransaction.new(1, 'ACME', 123.45, 13.27, 75.35, 'Dallas', 'TX', 'USA')
90
- assert_equal('_addTrans', event.name)
91
- assert_equal(['1', 'ACME', '123.45', '13.27', '75.35', 'Dallas', 'TX', 'USA'], event.params)
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)
92
127
  end
93
128
 
94
129
  def test_ecommerce_add_item_event
95
130
  event = GA::Events::Ecommerce::AddItem.new(1, 123, 'Bacon', 'Chunky', 5.00, 42)
96
- assert_equal('_addItem', event.name)
97
- assert_equal(['1', '123', 'Bacon', 'Chunky', '5.0', '42'], event.params)
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)
98
140
  end
99
141
 
100
142
  def test_ecommerce_track_trans_event
101
143
  event = GA::Events::Ecommerce::TrackTransaction.new
102
- assert_equal('_trackTrans', event.name)
144
+ assert_equal('ecommerce:send', event.action)
103
145
  assert_equal([], event.params)
104
146
  end
105
147
  end
@@ -4,17 +4,21 @@ require 'google-analytics/rails/view_helpers'
4
4
  class ViewHelpersTest < Test::Unit::TestCase
5
5
  include GoogleAnalytics::Rails::ViewHelpers
6
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
+
7
14
  VALID_INIT = <<-JAVASCRIPT
8
15
  <script type="text/javascript">
9
- var _gaq = _gaq || [];
10
- _gaq.push(['_setAccount','TEST']);
11
- _gaq.push(['_setDomainName','auto']);
12
- _gaq.push(['_trackPageview']);
13
- (function() {
14
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
15
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
16
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
17
- })();
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');
18
22
  </script>
19
23
  JAVASCRIPT
20
24
 
@@ -22,156 +26,105 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga
22
26
  assert_equal(VALID_INIT, analytics_init)
23
27
  end
24
28
 
25
- VALID_INIT_WITH_VIRTUAL_PAGEVIEW = <<-JAVASCRIPT
26
- <script type="text/javascript">
27
- var _gaq = _gaq || [];
28
- _gaq.push(['_setAccount','TEST']);
29
- _gaq.push(['_setDomainName','auto']);
30
- _gaq.push(['_trackPageview','/some/virtual/url']);
31
- (function() {
32
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
33
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
34
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
35
- })();
36
- </script>
37
- JAVASCRIPT
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
38
37
 
39
38
  def test_analytics_init_with_virtual_pageview
40
- assert_equal(VALID_INIT_WITH_VIRTUAL_PAGEVIEW, analytics_init(:page => '/some/virtual/url'))
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)
41
43
  end
42
44
 
43
- VALID_INIT_WITH_CUSTOM_TRACKER = <<-JAVASCRIPT
44
- <script type="text/javascript">
45
- var _gaq = _gaq || [];
46
- _gaq.push(['_setAccount','UA-CUSTOM-XX']);
47
- _gaq.push(['_setDomainName','auto']);
48
- _gaq.push(['_trackPageview']);
49
- (function() {
50
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
51
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
52
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
53
- })();
54
- </script>
55
- JAVASCRIPT
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
56
53
 
57
54
  def test_analytics_init_with_custom_tracker
58
- assert_equal(VALID_INIT_WITH_CUSTOM_TRACKER, analytics_init(:tracker => 'UA-CUSTOM-XX'))
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
59
  end
60
60
 
61
- VALID_INIT_WITH_CUSTOM_DOMAIN = <<-JAVASCRIPT
62
- <script type="text/javascript">
63
- var _gaq = _gaq || [];
64
- _gaq.push(['_setAccount','TEST']);
65
- _gaq.push(['_setDomainName','example.com']);
66
- _gaq.push(['_trackPageview']);
67
- (function() {
68
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
69
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
70
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
71
- })();
72
- </script>
73
- JAVASCRIPT
74
-
75
61
  def test_analytics_init_with_custom_domain
76
- assert_equal(VALID_INIT_WITH_CUSTOM_DOMAIN, analytics_init(:domain => 'example.com'))
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)
77
66
  end
78
67
 
79
- VALID_LOCAL_INIT = <<-JAVASCRIPT
80
- <script type="text/javascript">
81
- var _gaq = _gaq || [];
82
- _gaq.push(['_setAccount','TEST']);
83
- _gaq.push(['_setAllowLinker',true]);
84
- _gaq.push(['_setDomainName','none']);
85
- _gaq.push(['_trackPageview']);
86
- (function() {
87
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
88
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
89
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
90
- })();
91
- </script>
92
- JAVASCRIPT
93
-
94
68
  def test_local_analytics_init
95
- assert_equal(VALID_LOCAL_INIT, analytics_init(:local => true))
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)
96
75
  end
97
76
 
98
- VALID_INIT_WITH_ANONYMIZED_IP = <<-JAVASCRIPT
99
- <script type="text/javascript">
100
- var _gaq = _gaq || [];
101
- _gaq.push(['_setAccount','TEST']);
102
- _gaq.push(['_setDomainName','auto']);
103
- _gaq.push(['_gat._anonymizeIp']);
104
- _gaq.push(['_trackPageview']);
105
- (function() {
106
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
107
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
108
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
109
- })();
110
- </script>
111
- JAVASCRIPT
112
-
113
77
  def test_analytics_init_with_anonymized_ip
114
- assert_equal(VALID_INIT_WITH_ANONYMIZED_IP, analytics_init(:anonymize => true))
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)
115
83
  end
116
84
 
117
- VALID_INIT_WITH_LINK_ATTRIBUTION = <<-JAVASCRIPT
118
- <script type="text/javascript">
119
- var _gaq = _gaq || [];
120
- _gaq.push(['_require','inpage_linkid','//www.google-analytics.com/plugins/ga/inpage_linkid.js']);
121
- _gaq.push(['_setAccount','TEST']);
122
- _gaq.push(['_setDomainName','auto']);
123
- _gaq.push(['_trackPageview']);
124
- (function() {
125
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
126
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
127
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
128
- })();
129
- </script>
130
- JAVASCRIPT
131
-
132
85
  def test_analytics_init_with_link_attribution
133
- assert_equal(VALID_INIT_WITH_LINK_ATTRIBUTION, analytics_init(:enhanced_link_attribution => true))
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)
134
91
  end
135
92
 
136
- VALID_EVENT_INIT = <<-JAVASCRIPT
137
- <script type="text/javascript">
138
- var _gaq = _gaq || [];
139
- _gaq.push(['_setAccount','TEST']);
140
- _gaq.push(['_setDomainName','auto']);
141
- _gaq.push(['_trackPageview']);
142
- _gaq.push(['_setAllowLinker',true]);
143
- (function() {
144
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
145
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
146
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
147
- })();
148
- </script>
149
- JAVASCRIPT
150
-
151
93
  def test_analytics_init_with_events
152
- assert_equal(VALID_EVENT_INIT, analytics_init(:add_events => GA::Events::SetAllowLinker.new(true)))
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)
153
100
  end
154
101
 
155
- VALID_EVENT_INIT_WITH_CUSTOM_VARS = <<-JAVASCRIPT
156
- <script type="text/javascript">
157
- var _gaq = _gaq || [];
158
- _gaq.push(['_setAccount','TEST']);
159
- _gaq.push(['_setDomainName','auto']);
160
- _gaq.push(['_setCustomVar',1,'test','hoge',1]);
161
- _gaq.push(['_trackPageview']);
162
- (function() {
163
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
164
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
165
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
166
- })();
167
- </script>
168
- JAVASCRIPT
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
169
110
 
170
111
  def test_analytics_init_with_custom_vars
171
- assert_equal(VALID_EVENT_INIT_WITH_CUSTOM_VARS, analytics_init(:custom_vars => GA::Events::SetCustomVar.new(1, 'test', 'hoge',1)))
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)
172
125
  end
173
126
 
174
- VALID_TRACK_EVENT = "_gaq.push(['_trackEvent','Videos','Play','Gone With the Wind',null]);"
127
+ VALID_TRACK_EVENT = "ga('send','event','Videos','Play','Gone With the Wind',null);"
175
128
 
176
129
  def test_analytics_track_event
177
130
  event = analytics_track_event("Videos", "Play", "Gone With the Wind")