-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from railslove/criteo
Support for Criteo
- Loading branch information
Showing
7 changed files
with
249 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Criteo < Rack::Tracker::Handler | ||
|
||
TRACKER_EVENTS = { | ||
# event name => event key name, e.g. { event: 'setSiteType', type: '' } | ||
set_site_type: :type, | ||
set_account: :account, | ||
set_customer_id: :id | ||
} | ||
|
||
class Event < OpenStruct | ||
def write | ||
to_h.to_json | ||
end | ||
end | ||
|
||
self.position = :body | ||
|
||
# global events (setSiteType, setAccount, ...) for each tracker instance | ||
def tracker_events | ||
@tracker_events ||= [].tap do |tracker_events| | ||
options.slice(*TRACKER_EVENTS.keys).each do |key, value| | ||
if option_value = value.respond_to?(:call) ? value.call(env) : value | ||
tracker_events << Event.new(:event => "#{key}".camelize(:lower), TRACKER_EVENTS[key] => "#{option_value}") | ||
end | ||
end | ||
end | ||
end | ||
|
||
def render | ||
Tilt.new( File.join( File.dirname(__FILE__), 'template', 'criteo.erb') ).render(self) | ||
end | ||
|
||
def self.track(name, event_name, event_args = {}) | ||
{ name.to_s => [{ 'class_name' => 'Event', 'event' => event_name.to_s.camelize(:lower) }.merge(event_args)] } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<% if events.any? %> | ||
<script type='text/javascript' src='//static.criteo.net/js/ld/ld.js' async='true'></script> | ||
<script type='text/javascript'> | ||
window.criteo_q = window.criteo_q || []; | ||
<% (tracker_events + events).each do |event| %> | ||
window.criteo_q.push(<%= event.write %>); | ||
<% end %> | ||
</script> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
RSpec.describe Rack::Tracker::Criteo do | ||
|
||
describe Rack::Tracker::Criteo::Event do | ||
|
||
subject { described_class.new(event: "viewItem", item: 'P001') } | ||
|
||
describe '#write' do | ||
specify { expect(subject.write).to eq("{\"event\":\"viewItem\",\"item\":\"P001\"}") } | ||
end | ||
end | ||
|
||
def env | ||
{} | ||
end | ||
|
||
it 'will be placed in the body' do | ||
expect(described_class.position).to eq(:body) | ||
expect(described_class.new(env).position).to eq(:body) | ||
end | ||
|
||
describe '#render' do | ||
context 'with events' do | ||
let(:env) { | ||
{ | ||
'tracker' => { | ||
'criteo' => | ||
[ | ||
{ | ||
'event' => 'viewItem', | ||
'item' => 'P001', | ||
'class_name' => 'Event' | ||
} | ||
] | ||
} | ||
} | ||
} | ||
|
||
subject { described_class.new(env).render } | ||
|
||
it 'will push the tracking events to the queue' do | ||
expect(subject).to include 'window.criteo_q.push({"event":"viewItem","item":"P001"});' | ||
end | ||
end | ||
|
||
context 'without events' do | ||
let(:env) { | ||
{ | ||
'tracker' => { | ||
'criteo' => [] | ||
} | ||
} | ||
} | ||
|
||
subject { described_class.new(env, { user_id: ->(env){ '123' } }).render } | ||
|
||
it 'should render nothing' do | ||
expect(subject).to eql "" | ||
end | ||
end | ||
end | ||
|
||
describe '#tracker_events' do | ||
subject { described_class.new(env, options) } | ||
|
||
context 'nil value' do | ||
let(:options) { { set_account: nil } } | ||
|
||
it 'should ignore option' do | ||
expect(subject.tracker_events).to match_array [] | ||
end | ||
end | ||
|
||
context 'static string value' do | ||
let(:options) { { set_account: '1234' } } | ||
|
||
it 'should set the value' do | ||
expect(subject.tracker_events).to match_array [ | ||
Rack::Tracker::Criteo::Event.new(event: 'setAccount', account: '1234') | ||
] | ||
end | ||
end | ||
|
||
context 'static integer value' do | ||
let(:options) { { set_customer_id: 1234 } } | ||
|
||
it 'should set the value as string' do | ||
expect(subject.tracker_events).to match_array [ | ||
Rack::Tracker::Criteo::Event.new(event: 'setCustomerId', id: '1234') | ||
] | ||
end | ||
end | ||
|
||
context 'unsupported option' do | ||
let(:options) { { unsupported: "option" } } | ||
|
||
subject { described_class.new(env, options) } | ||
|
||
it 'should ignore the option' do | ||
expect(subject.tracker_events).to match_array [] | ||
end | ||
end | ||
|
||
context 'proc returning value' do | ||
let(:options) { { set_site_type: ->(env){ 'm' } } } | ||
|
||
it 'should set the value' do | ||
expect(subject.tracker_events).to match_array [ | ||
Rack::Tracker::Criteo::Event.new(event: 'setSiteType', type: 'm') | ||
] | ||
end | ||
end | ||
|
||
context 'proc returning nil' do | ||
let(:options) { { set_account: ->(env){ nil } } } | ||
|
||
it 'should ignore the option' do | ||
expect(subject.tracker_events).to match_array [] | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'support/capybara_app_helper' | ||
|
||
RSpec.describe "Criteo Integration" do | ||
before do | ||
setup_app(action: :criteo) do |tracker| | ||
tracker.handler(:criteo, { | ||
set_account: '1234', | ||
set_customer_id: ->(env){ '4711' }, | ||
set_site_type: ->(env){ 'm' } | ||
}) | ||
end | ||
visit '/' | ||
end | ||
|
||
subject { page } | ||
|
||
it 'should include all the events' do | ||
# tracker_events | ||
expect(page.find("body")).to have_content "window.criteo_q.push({\"event\":\"setAccount\",\"account\":\"1234\"});" | ||
expect(page.find("body")).to have_content "window.criteo_q.push({\"event\":\"setSiteType\",\"type\":\"m\"});" | ||
expect(page.find("body")).to have_content "window.criteo_q.push({\"event\":\"setCustomerId\",\"id\":\"4711\"});" | ||
|
||
# events | ||
expect(page.find("body")).to have_content "window.criteo_q.push({\"event\":\"viewItem\",\"item\":\"P001\"});" | ||
expect(page.find("body")).to have_content "window.criteo_q.push({\"event\":\"viewList\",\"item\":[\"P001\",\"P002\"]});" | ||
expect(page.find("body")).to have_content "window.criteo_q.push({\"event\":\"trackTransaction\",\"id\":\"id\",\"item\":{\"id\":\"P0038\",\"price\":\"6.54\",\"quantity\":1}});" | ||
expect(page.find("body")).to have_content "window.criteo_q.push({\"event\":\"viewBasket\",\"item\":[{\"id\":\"P001\",\"price\":\"6.54\",\"quantity\":1},{\"id\":\"P0038\",\"price\":\"2.99\",\"quantity\":1}]});" | ||
end | ||
|
||
describe 'adjust tracker position via options' do | ||
before do | ||
setup_app(action: :criteo) do |tracker| | ||
tracker.handler :criteo, { set_account: '1234', position: :head } | ||
end | ||
visit '/' | ||
end | ||
|
||
it "will be placed in the specified tag" do | ||
expect(page.find("body")).to_not have_content('criteo') | ||
expect(page.find("head")).to have_content("{\"event\":\"setAccount\",\"account\":\"1234\"}") | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters