Skip to content
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
merged 19 commits into from
Mar 26, 2015
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ rvm:
- 2.0.0
- 2.1.2
- jruby
bundler_args: --without debug
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in rack-tracker.gemspec
gemspec

group :debug do
gem 'pry-byebug'
end
1 change: 1 addition & 0 deletions lib/rack/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require "rack/tracker/facebook/facebook"
require "rack/tracker/vwo/vwo"
require "rack/tracker/go_squared/go_squared"
require "rack/tracker/criteo/criteo"

module Rack
class Tracker
Expand Down
38 changes: 38 additions & 0 deletions lib/rack/tracker/criteo/criteo.rb
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
9 changes: 9 additions & 0 deletions lib/rack/tracker/criteo/template/criteo.erb
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 %>
73 changes: 73 additions & 0 deletions spec/handler/criteo_spec.rb
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 } }) }
Copy link
Member

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

Copy link
Contributor Author

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


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
37 changes: 37 additions & 0 deletions spec/integration/criteo_integration_spec.rb
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
7 changes: 7 additions & 0 deletions spec/support/metal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ def go_squared
end
render "metal/index"
end

def criteo
tracker do |t|
t.criteo :track, { event: 'viewItem', item: 'P001' }
end
render 'metal/index'
end
end