forked from railslove/rack-tracker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgoogle_analytics_spec.rb
More file actions
266 lines (211 loc) · 9.49 KB
/
google_analytics_spec.rb
File metadata and controls
266 lines (211 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
RSpec.describe Rack::Tracker::GoogleAnalytics do
def env
{
misc: 'foobar',
user_id: '123'
}
end
it 'will be placed in the head' do
expect(described_class.position).to eq(:head)
expect(described_class.new(env).position).to eq(:head)
expect(described_class.new(env, position: :body).position).to eq(:body)
end
describe '#ecommerce_events' do
subject { described_class.new(env) }
describe 'with stored ecommerce events' do
before { allow(subject).to receive(:events).and_return([Rack::Tracker::GoogleAnalytics::Send.new, Rack::Tracker::GoogleAnalytics::Ecommerce.new]) }
it 'will just return the ecommerce events' do
expect(subject.ecommerce_events).to match_array(Rack::Tracker::GoogleAnalytics::Ecommerce)
end
end
describe 'without stored ecommerce events' do
before { allow(subject).to receive(:events).and_return([Rack::Tracker::GoogleAnalytics::Send.new]) }
it 'will be empty' do
expect(subject.ecommerce_events).to be_empty
end
end
end
describe '#enhanced_ecommerce_events' do
subject { described_class.new(env) }
describe 'with stored enhanced ecommerce events' do
before { allow(subject).to receive(:events).and_return([Rack::Tracker::GoogleAnalytics::Send.new, Rack::Tracker::GoogleAnalytics::EnhancedEcommerce.new]) }
it 'will just return the enhanced ecommerce events' do
expect(subject.enhanced_ecommerce_events).to match_array(Rack::Tracker::GoogleAnalytics::EnhancedEcommerce)
end
end
describe 'without stored enhanced ecommerce events' do
before { allow(subject).to receive(:events).and_return([Rack::Tracker::GoogleAnalytics::Send.new]) }
it 'will be empty' do
expect(subject.enhanced_ecommerce_events).to be_empty
end
end
end
describe '#tracker_options' do
before do
stub_const("#{described_class}::ALLOWED_TRACKER_OPTIONS", [:some_option])
end
context 'with an allowed option configured with a static value' do
subject { described_class.new(env, { some_option: 'value' }) }
it 'returns hash with option set' do
expect(subject.tracker_options).to eql ({ someOption: 'value' })
end
end
context 'with an allowed option configured with a block' do
subject { described_class.new(env, { some_option: lambda { |env| return env[:misc] } }) }
it 'returns hash with option set' do
expect(subject.tracker_options).to eql ({ someOption: 'foobar' })
end
end
context 'with an allowed option configured with a block returning nil' do
subject { described_class.new(env, { some_option: lambda { |env| return env[:non_existing_key] } }) }
it 'returns an empty hash' do
expect(subject.tracker_options).to eql ({})
end
end
context 'with a non allowed option' do
subject { described_class.new(env, { new_option: 'value' }) }
it 'returns an empty hash' do
expect(subject.tracker_options).to eql ({})
end
end
end
describe "with events" do
subject { described_class.new(env, tracker: 'somebody').render }
describe "default" do
def env
{'tracker' => {
'google_analytics' => [
{ 'class_name' => 'Send', 'category' => 'Users', 'action' => 'Login', 'label' => 'Standard' }
]
}}
end
it "will show events" do
expect(subject).to match(%r{ga\(\"send\",{\"hitType\":\"event\",\"eventCategory\":\"Users\",\"eventAction\":\"Login\",\"eventLabel\":\"Standard\"}\)})
end
end
describe "with a event value" do
def env
{'tracker' => { 'google_analytics' => [
{ 'class_name' => 'Send', category: "Users", action: "Login", label: "Standard", value: "5" }
]}}
end
it "will show events with values" do
expect(subject).to match(%r{ga\(\"send\",{\"hitType\":\"event\",\"eventCategory\":\"Users\",\"eventAction\":\"Login\",\"eventLabel\":\"Standard\",\"eventValue\":\"5\"}\)},)
end
end
end
describe 'with ecommerce events' do
describe "default" do
def env
{'tracker' => {
'google_analytics' => [
{ 'class_name' => 'Ecommerce', 'type' => 'addItem', 'id' => '1234', 'name' => 'Fluffy Pink Bunnies', 'sku' => 'DD23444', 'category' => 'Party Toys', 'price' => '11.99', 'quantity' => '1' },
{ 'class_name' => 'Ecommerce', 'type' => 'addTransaction', 'id' => '1234', 'affiliation' => 'Acme Clothing', 'revenue' => 11.99, 'shipping' => '5', 'tax' => '1.29', 'currency' => 'EUR' }
]
}}
end
subject { described_class.new(env, tracker: 'somebody', ecommerce: true).render }
it "will add items" do
attributes = { id: '1234', name: 'Fluffy Pink Bunnies', sku: 'DD23444', category: 'Party Toys', price: '11.99', quantity: '1' }.to_json
expect(subject).to match(%r{ga\(\"ecommerce:addItem\",#{attributes}\);})
end
it "will add transaction" do
attributes = { id: '1234', affiliation: 'Acme Clothing', revenue: '11.99', shipping: '5', tax: '1.29', currency: 'EUR' }.to_json
expect(subject).to match(%r{ga\(\"ecommerce:addTransaction\",#{attributes}\);})
end
it "will submit cart" do
expect(subject).to match(%r{ga\('ecommerce:send'\);})
end
end
end
describe 'with enhanced ecommerce events' do
describe "default" do
def env
{'tracker' => {
'google_analytics' => [
{ 'class_name' => 'EnhancedEcommerce', 'type' => 'addProduct', 'id' => 'P12345', 'name' => 'Android Warhol T-Shirt', 'category' => 'Apparel', 'brand' => 'Google', 'variant' => 'black', 'price' => '29.20', 'coupon' => 'APPARELSALE', 'quantity' => 1 },
{ 'class_name' => 'EnhancedEcommerce', 'type' => 'setAction', 'label' => 'purchase' }
]
}}
end
subject { described_class.new(env, tracker: 'somebody', enhanced_ecommerce: true).render }
it "will add product" do
attributes = { id: 'P12345', name: 'Android Warhol T-Shirt', category: 'Apparel', brand: 'Google', variant: 'black', price: '29.20', coupon: 'APPARELSALE', quantity: '1' }.to_json
expect(subject).to match(%r{ga\(\"ec:addProduct\",#{attributes}\);})
end
it "will add action" do
expect(subject).to match(%r{ga\(\"ec:setAction\",\"purchase\"\);})
end
end
end
describe 'with parameters events' do
def env
{'tracker' => {
'google_analytics' => [
{ 'class_name' => 'Parameter', 'dimension1' => 'pink' },
]
}}
end
subject { described_class.new(env, tracker: 'somebody').render }
it "will render dimension parameter" do
expect(subject).to match(%r{ga\('set', 'dimension1', 'pink'})
end
end
describe "with custom domain" do
subject { described_class.new(env, tracker: 'somebody', cookie_domain: "railslabs.com").render }
it "will show asyncronous tracker with cookieDomain" do
expect(subject).to match(%r{ga\('create', 'somebody', {\"cookieDomain\":\"railslabs.com\"}\)})
expect(subject).to match(%r{ga\('send', 'pageview', window\.location\.pathname \+ window\.location\.search\)})
end
end
describe "with user_id tracking" do
subject { described_class.new(env, tracker: 'somebody', user_id: lambda { |env| return env[:user_id] } ).render }
it "will show asyncronous tracker with userId" do
expect(subject).to match(%r{ga\('create', 'somebody', {\"userId\":\"123\"}\)})
expect(subject).to match(%r{ga\('send', 'pageview', window\.location\.pathname \+ window\.location\.search\)})
end
end
describe "with enhanced_link_attribution" do
subject { described_class.new(env, tracker: 'happy', enhanced_link_attribution: true).render }
it "will embedded the linkid plugin script" do
expect(subject).to match(%r{linkid.js})
end
end
describe "with advertising" do
subject { described_class.new(env, tracker: 'happy', advertising: true).render }
it "will require displayfeatures" do
expect(subject).to match(%r{ga\('require', 'displayfeatures'\)})
end
end
describe "with enhanced ecommerce" do
subject { described_class.new(env, tracker: 'happy', enhanced_ecommerce: true).render }
it "will require the enhanced ecommerce plugin" do
expect(subject).to match(%r{ga\('require', 'ec'\)})
end
end
describe "with ecommerce" do
subject { described_class.new(env, tracker: 'happy', ecommerce: true).render }
it "will require the ecommerce plugin" do
expect(subject).to match(%r{ga\('require', 'ecommerce', 'ecommerce\.js'\)})
end
end
describe "with anonymizeIp" do
subject { described_class.new(env, tracker: 'happy', anonymize_ip: true).render }
it "will set anonymizeIp to true" do
expect(subject).to match(%r{ga\('set', 'anonymizeIp', true\)})
end
end
describe "with dynamic tracker" do
subject { described_class.new(env, { tracker: lambda { |env| return env[:misc] }}).render }
it 'will call tracker lambdas to obtain tracking codes' do
expect(subject).to match(%r{ga\('create', 'foobar', {}\)})
end
end
describe 'adjusted bounce rate' do
subject { described_class.new(env, tracker: 'afake', adjusted_bounce_rate_timeouts: [15, 30]).render }
it "will add timeouts to push read events" do
expect(subject).to include %q{setTimeout(function() { ga('send', 'event', '15_seconds', 'read'); },15000)}
expect(subject).to include %q{setTimeout(function() { ga('send', 'event', '30_seconds', 'read'); },30000)}
end
end
end