forked from railslove/rack-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_global_spec.rb
More file actions
225 lines (185 loc) · 7.11 KB
/
google_global_spec.rb
File metadata and controls
225 lines (185 loc) · 7.11 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
RSpec.describe Rack::Tracker::GoogleGlobal 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 '#tracker_options' do
context 'with an allowed option configured with a static value' do
subject { described_class.new(env, user_id: 'value') }
it 'returns hash with option set' do
expect(subject.tracker_options).to eql ({ user_id: 'value' })
end
end
context 'with an allowed option configured with a block' do
subject { described_class.new(env, user_id: lambda { |env| return env[:misc] }) }
it 'returns hash with option set' do
expect(subject.tracker_options).to eql ({ user_id: 'foobar' })
end
end
context 'with an allowed option configured with a block returning nil' do
subject { described_class.new(env, user_id: 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 '#set_options' do
context 'with option configured with a static value' do
subject { described_class.new(env, set: { option: 'value' }) }
it 'returns hash with option set' do
expect(subject.set_options).to eql ({ option: 'value' })
end
end
context 'with option configured with a block' do
subject { described_class.new(env, set: lambda { |env| return { option: env[:misc] } }) }
it 'returns hash with option set' do
expect(subject.set_options).to eql ({ option: 'foobar' })
end
end
context 'with option configured with a block returning nil' do
subject { described_class.new(env, set: lambda { |env| return env[:non_existing_key] }) }
it 'returns nil' do
expect(subject.set_options).to be nil
end
end
end
describe "with custom domain" do
let(:tracker) { { id: 'somebody'}}
let(:options) { { cookie_domain: "railslabs.com", trackers: [tracker] } }
subject { described_class.new(env, options).render }
it "will show asyncronous tracker with cookie_domain" do
expect(subject).to match(%r{gtag\('config', 'somebody', {\"cookie_domain\":\"railslabs.com\"}\)})
end
end
describe "with user_id tracking" do
let(:tracker) { { id: 'somebody'}}
let(:options) { { user_id: lambda { |env| return env[:user_id] }, trackers: [tracker] } }
subject { described_class.new(env, options).render }
it "will show asyncronous tracker with userId" do
expect(subject).to match(%r{gtag\('config', 'somebody', {\"user_id\":\"123\"}\)})
end
end
describe "with link_attribution" do
let(:tracker) { { id: 'happy'}}
let(:options) { { link_attribution: true, trackers: [tracker] } }
subject { described_class.new(env, options).render }
it "will show asyncronous tracker with link_attribution" do
expect(subject).to match(%r{gtag\('config', 'happy', {\"link_attribution\":true}\)})
end
end
describe "with allow_display_features" do
let(:tracker) { { id: 'happy'}}
let(:options) { { allow_display_features: false, trackers: [tracker] } }
subject { described_class.new(env, options).render }
it "will disable display features" do
expect(subject).to match(%r{gtag\('config', 'happy', {\"allow_display_features\":false}\)})
end
end
describe "with anonymizeIp" do
let(:tracker) { { id: 'happy'}}
let(:options) { { anonymize_ip: true, trackers: [tracker] } }
subject { described_class.new(env, options).render }
it "will set anonymizeIp to true" do
expect(subject).to match(%r{gtag\('config', 'happy', {\"anonymize_ip\":true}\)})
end
end
describe "with dynamic tracker" do
let(:tracker) { { id: lambda { |env| return env[:misc] } }}
let(:options) { { trackers: [tracker] } }
subject { described_class.new(env, options).render }
it 'will call tracker lambdas to obtain tracking codes' do
expect(subject).to match(%r{gtag\('config', 'foobar', {}\)})
end
end
describe "with empty tracker" do
let(:present_tracker) { { id: 'present' }}
let(:empty_tracker) { { id: lambda { |env| return } }}
let(:options) { { trackers: [present_tracker, empty_tracker] } }
subject { described_class.new(env, options).render }
it 'will not render config' do
expect(subject).to match(%r{gtag\('config', 'present', {}\)})
expect(subject).not_to match(%r{gtag\('config', '', {}\)})
end
end
describe "with set options" do
let(:tracker) { { id: 'with_options' } }
let(:options) { { trackers: [tracker], set: { foo: 'bar' } } }
subject { described_class.new(env, options).render }
it 'will show set command' do
expect(subject).to match(%r{gtag\('set', {\"foo\":\"bar\"}\)})
end
end
describe "with virtual pages" do
subject { described_class.new(env, trackers: [{ id: 'somebody' }]).render }
describe "default" do
def env
{'tracker' => {
'google_global' => [
{ 'class_name' => 'Page', 'path' => '/virtual_page' }
]
}}
end
it "will show virtual page" do
expect(subject).to match(%r{gtag\('config', 'somebody', {\"page_path\":\"/virtual_page\"}\);})
end
end
describe "with a event value" do
def env
{'tracker' => {
'google_global' => [
{ 'class_name' => 'Page', 'path' => '/virtual_page', 'location' => 'https://example.com/virtual_page', 'title' => 'Virtual Page' }
]
}}
end
it "will show virtual page" do
expect(subject).to match(%r{gtag\('config', 'somebody', {\"page_title\":\"Virtual Page\",\"page_location\":\"https:\/\/example.com\/virtual_page\",\"page_path\":\"/virtual_page\"}\);})
end
end
end
describe "with events" do
subject { described_class.new(env, trackers: [{ id: 'somebody' }]).render }
describe "default" do
def env
{'tracker' => {
'google_global' => [
{ 'class_name' => 'Event', 'action' => 'login' }
]
}}
end
it "will show the event" do
expect(subject).to match(%r{gtag\('event', 'login', {}\);})
end
end
describe "with event parameters" do
def env
{'tracker' => {
'google_global' => [
{ 'class_name' => 'Event',
'action' => 'login',
'category' => 'engagement',
'label' => 'Github',
'value' => 5,
'transaction_id' => 1001,
}
]
}}
end
it "will show event" do
expect(subject).to match(%r{gtag\('event', 'login', {\"event_category\":\"engagement\",\"event_label\":\"Github\",\"value\":5,\"transaction_id\":1001}\);})
end
end
end
end