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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +1 -0
- data/README.markdown +27 -2
- data/lib/google-analytics-rails.rb +2 -2
- data/lib/google-analytics/async_tracking_queue.rb +8 -9
- data/lib/google-analytics/events/event.rb +18 -3
- data/lib/google-analytics/events/event_collection.rb +1 -1
- data/lib/google-analytics/events/event_renderer.rb +8 -2
- data/lib/google-analytics/events/events.rb +129 -37
- data/lib/google-analytics/rails/view_helpers.rb +56 -18
- data/lib/google-analytics/version.rb +1 -1
- data/test/async_tracking_queue_test.rb +55 -57
- data/test/event_collection_renderer_test.rb +8 -8
- data/test/event_collection_test.rb +4 -4
- data/test/event_renderer_test.rb +18 -4
- data/test/gaq_events_test.rb +81 -39
- data/test/rails/views_helper_test.rb +85 -132
- metadata +3 -3
@@ -7,16 +7,14 @@ class AsyncTrackingQueueTest < Test::Unit::TestCase
|
|
7
7
|
|
8
8
|
VALID_SNIPPET = <<-JAVASCRIPT
|
9
9
|
<script type="text/javascript">
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
(
|
16
|
-
|
17
|
-
ga
|
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('
|
28
|
-
gaq << GA::Event.new('
|
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('
|
32
|
-
gaq.push(GA::Event.new('
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
_gaq
|
42
|
-
_gaq.push(['
|
43
|
-
_gaq.push(['
|
44
|
-
(
|
45
|
-
|
46
|
-
|
47
|
-
var
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
(
|
75
|
-
|
76
|
-
ga
|
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('
|
88
|
-
gaq << GA::Event.new('
|
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('
|
92
|
-
gaq.push(GA::Event.new('
|
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('
|
7
|
-
event_collection << GA::Event.new('
|
8
|
-
event_collection << GA::Event.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
9
|
|
10
10
|
ecr = GA::EventCollectionRenderer.new(event_collection, nil)
|
11
|
-
assert_equal("
|
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('
|
17
|
-
event_collection << GA::Event.new('
|
18
|
-
event_collection << GA::Event.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
19
|
|
20
20
|
ecr = GA::EventCollectionRenderer.new(event_collection, 't2')
|
21
|
-
assert_equal("
|
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])
|
data/test/event_renderer_test.rb
CHANGED
@@ -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('
|
6
|
-
assert_equal("
|
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('
|
11
|
-
assert_equal("
|
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
|
data/test/gaq_events_test.rb
CHANGED
@@ -1,105 +1,147 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class GAEventsTest < Test::Unit::TestCase
|
4
|
-
def
|
5
|
-
event = GA::Events::
|
6
|
-
assert_equal('
|
7
|
-
assert_equal(
|
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
|
-
|
11
|
-
|
12
|
-
assert_equal('
|
13
|
-
assert_equal([
|
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
|
17
|
-
event = GA::Events::
|
18
|
-
assert_equal('
|
19
|
-
assert_equal(
|
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('
|
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('
|
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('
|
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('
|
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('
|
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('
|
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
|
59
|
-
event = GA::Events::
|
60
|
-
assert_equal('
|
61
|
-
assert_equal(
|
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
|
79
|
+
def test_set_custom_dimension_with_invalid_index
|
65
80
|
assert_raise ArgumentError do
|
66
|
-
GA::Events::
|
81
|
+
GA::Events::SetCustomDimension.new(6, 1)
|
67
82
|
end
|
68
83
|
end
|
69
84
|
|
70
|
-
def
|
71
|
-
event = GA::Events::
|
72
|
-
assert_equal('
|
73
|
-
assert_equal(
|
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
|
92
|
+
def test_set_custom_metric_with_invalid_index
|
77
93
|
assert_raise ArgumentError do
|
78
|
-
GA::Events::
|
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('
|
85
|
-
assert_equal(
|
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
|
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('
|
91
|
-
assert_equal(
|
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('
|
97
|
-
assert_equal(
|
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('
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
(
|
14
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
(
|
32
|
-
|
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
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
(
|
50
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
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
|
-
|
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 = "
|
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")
|