-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Criteo #17
Merged
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3df3c46
integrate criteo tracking
jhilden cd0233e
only render criteo JS when there are events present
jhilden 851a18b
syntax error in travis.yml
jhilden 9bc3fcb
make criteo events more generic
jhilden 9fd6775
Merge branch 'master' into criteo
jhilden ee648ea
refactoring: use tap instead of temporary variables and renamend con…
jhilden d37c486
improve spec
jhilden 9255118
syntax error
jhilden 229cc7b
make track API for criteo a bit more human-friendly and added more specs
jhilden 386ac05
invalid HTML
jhilden 794c775
Merge branch 'stringify_keys' into criteo
jhilden eb82e1c
stringify keys
jhilden 99b6e50
whitespace
jhilden a3b3a0a
Merge branch 'stringify_keys' into criteo
jhilden 5a3cb5d
stringification now happens in central place
jhilden 14b6816
fix invalid HTML in criteo template
jhilden 0052e0b
remove pry-byebug
jhilden a1a5204
Merge branch 'master' into criteo
jhilden bbd5c7f
updated README with criteo documentation
jhilden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ rvm: | |
- 2.0.0 | ||
- 2.1.2 | ||
- jruby | ||
bundler_args: --without debug |
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,38 @@ | ||
class Criteo < Rack::Tracker::Handler | ||
|
||
TRACKER_OPTIONS = { | ||
# option/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 for each tracker instance | ||
def tracker_events | ||
@tracker_events ||= begin | ||
tracker_events = [] | ||
options.slice(*TRACKER_OPTIONS.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_OPTIONS[key] => "#{option_value}") | ||
end | ||
end | ||
tracker_events | ||
end | ||
end | ||
|
||
def render | ||
Tilt.new( File.join( File.dirname(__FILE__), 'template', 'criteo.erb') ).render(self) | ||
end | ||
|
||
def self.track(name, *event) | ||
{ name.to_s => [event.last.merge(class_name: 'Event')] } | ||
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 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,73 @@ | ||
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, { set_account: '1234', set_site_type: ->(env){ 'd' }, set_customer_id: ->(env){ nil } }) } | ||
|
||
specify do | ||
expect(subject.tracker_events).to match_array [ | ||
Rack::Tracker::Criteo::Event.new(event: 'setSiteType', type: 'd'), | ||
Rack::Tracker::Criteo::Event.new(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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 basic pushes' do | ||
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\"});") | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add another spec where the parameters are not Procs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a couple more specs in d37c486