Skip to content

Commit 9bc3fcb

Browse files
committed
make criteo events more generic
* use only one loop in the .erb template * add global events (setSiteType, setAccount, setCustomerId) as Rack::Tracker::Criteo::Events
1 parent 851a18b commit 9bc3fcb

4 files changed

Lines changed: 35 additions & 66 deletions

File tree

lib/rack/tracker/criteo/criteo.rb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
class Criteo < Rack::Tracker::Handler
2+
3+
TRACKER_OPTIONS = {
4+
# option/event name => event key name, e.g. { event: 'setSiteType', type: '' }
5+
set_site_type: :type,
6+
set_account: :account,
7+
set_customer_id: :id
8+
}
9+
210
class Event < OpenStruct
311
def write
412
to_h.to_json
@@ -7,17 +15,16 @@ def write
715

816
self.position = :body
917

10-
def tracker_options
11-
@tracker_options ||= begin
12-
tracker_options = {}
13-
14-
user_id = options[:user_id].call(env) if options[:user_id]
15-
tracker_options[:user_id] = "#{user_id}" if user_id.present?
16-
17-
site_type = options[:site_type].call(env) if options[:site_type]
18-
tracker_options[:site_type] = "#{site_type}" if site_type.present?
19-
20-
tracker_options
18+
# global events for each tracker instance
19+
def tracker_events
20+
@tracker_events ||= begin
21+
tracker_events = []
22+
options.slice(*TRACKER_OPTIONS.keys).each do |key, value|
23+
if option_value = value.respond_to?(:call) ? value.call(env) : value
24+
tracker_events << Event.new(:event => "#{key}".camelize(:lower), TRACKER_OPTIONS[key] => "#{option_value}")
25+
end
26+
end
27+
tracker_events
2128
end
2229
end
2330

lib/rack/tracker/criteo/template/criteo.erb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,7 @@
22
<script type: 'text/javascript', src: '//static.criteo.net/js/ld/ld.js', async: 'true' />
33
<script type='text/javascript'>
44
window.criteo_q = window.criteo_q || [];
5-
6-
window.criteo_q.push({ event: 'setAccount', account: '<%= options[:account_id] %>' });
7-
8-
<% if tracker_options[:site_type] %>
9-
window.criteo_q.push({ event: 'setSiteType', type: '<%= tracker_options[:site_type] %>' });
10-
<% end %>
11-
12-
<% if tracker_options[:user_id] %>
13-
window.criteo_q.push({ event: 'setCustomerId', id: '<%= tracker_options[:user_id] %>' });
14-
<% end %>
15-
16-
<% events.each do |event| %>
5+
<% (tracker_events + events).each do |event| %>
176
window.criteo_q.push(<%= event.write %>);
187
<% end %>
198
</script>

spec/handler/criteo_spec.rb

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,41 +59,14 @@ def env
5959
end
6060
end
6161

62-
describe '#tracker_options' do
63-
describe 'with site_type' do
64-
context 'returning a value' do
65-
subject { described_class.new(env, { site_type: ->(env){ 'd' } }) }
66-
67-
it 'returns hash with userId' do
68-
expect(subject.tracker_options).to eql ({ site_type: 'd' })
69-
end
70-
end
71-
72-
context 'returning nil' do
73-
subject { described_class.new(env, { site_type: ->(env){ nil } }) }
74-
75-
it 'returns hash without userId' do
76-
expect(subject.tracker_options).to eql ({ })
77-
end
78-
end
79-
end
80-
81-
describe 'with user_id option' do
82-
context 'returning a value' do
83-
subject { described_class.new(env, { user_id: ->(env){ '123' } }) }
84-
85-
it 'returns hash with userId' do
86-
expect(subject.tracker_options).to eql ({ user_id: '123' })
87-
end
88-
end
89-
90-
context 'returning nil' do
91-
subject { described_class.new(env, { user_id: ->(env){ nil } }) }
92-
93-
it 'returns hash without userId' do
94-
expect(subject.tracker_options).to eql ({ })
95-
end
96-
end
62+
describe '#tracker_events' do
63+
subject { described_class.new(env, { set_account: '1234', set_site_type: ->(env){ 'd' }, set_customer_id: ->(env){ nil } }) }
64+
65+
specify do
66+
expect(subject.tracker_events).to match_array [
67+
Rack::Tracker::Criteo::Event.new(event: 'setSiteType', type: 'd'),
68+
Rack::Tracker::Criteo::Event.new(event: 'setAccount', account: '1234')
69+
]
9770
end
9871
end
9972

spec/integration/criteo_integration_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
before do
55
setup_app(action: :criteo) do |tracker|
66
tracker.handler(:criteo, {
7-
account_id: '1234',
8-
user_id: ->(env){ '4711' },
9-
site_type: ->(env) { 'm' }
7+
set_account: '1234',
8+
set_customer_id: ->(env){ '4711' },
9+
set_site_type: ->(env){ 'm' }
1010
})
1111
end
1212
visit '/'
@@ -15,22 +15,22 @@
1515
subject { page }
1616

1717
it 'should include all the basic pushes' do
18-
expect(page.find("body")).to have_content("window.criteo_q.push({ event: 'setAccount', account: '1234' });")
19-
expect(page.find("body")).to have_content("window.criteo_q.push({ event: 'setSiteType', type: 'm' });")
20-
expect(page.find("body")).to have_content("window.criteo_q.push({ event: 'setCustomerId', id: '4711' });")
18+
expect(page.find("body")).to have_content("window.criteo_q.push({\"event\":\"setAccount\",\"account\":\"1234\"});")
19+
expect(page.find("body")).to have_content("window.criteo_q.push({\"event\":\"setSiteType\",\"type\":\"m\"});")
20+
expect(page.find("body")).to have_content("window.criteo_q.push({\"event\":\"setCustomerId\",\"id\":\"4711\"});")
2121
end
2222

2323
describe 'adjust tracker position via options' do
2424
before do
2525
setup_app(action: :criteo) do |tracker|
26-
tracker.handler :criteo, { account_id: '1234', position: :head }
26+
tracker.handler :criteo, { set_account: '1234', position: :head }
2727
end
2828
visit '/'
2929
end
3030

3131
it "will be placed in the specified tag" do
3232
expect(page.find("body")).to_not have_content('criteo')
33-
expect(page.find("head")).to have_content("{ event: 'setAccount', account: '1234' }")
33+
expect(page.find("head")).to have_content("{\"event\":\"setAccount\",\"account\":\"1234\"}")
3434
end
3535

3636
end

0 commit comments

Comments
 (0)