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

Switched the tracker to always be stored in the session instead of it being swapped in and out of the env['tracker']. #153

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
13 changes: 4 additions & 9 deletions lib/rack/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,16 @@ def call(env)
def _call(env)
status, headers, body = @app.call(env)
return [status, headers, body] unless headers['Content-Type'] =~ /html/

response = Rack::Response.new([], status, headers)
env[EVENT_TRACKING_KEY] ||= {}

if session = env["rack.session"]
env[EVENT_TRACKING_KEY].deep_merge!(session.delete(EVENT_TRACKING_KEY) || {}) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
end

if response.redirection? && session
session[EVENT_TRACKING_KEY] = env[EVENT_TRACKING_KEY]
end
session = env['rack.session']
session[EVENT_TRACKING_KEY] ||= {} if session

body.each { |fragment| response.write inject(env, fragment) }
body.close if body.respond_to?(:close)

session.delete(EVENT_TRACKING_KEY) if session && !response.redirection?

response.finish
end

Expand Down
11 changes: 6 additions & 5 deletions lib/rack/tracker/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ def initialize(env, options = {})
self.env = env
self.options = options
self.position = options[:position] if options.has_key?(:position)
@session = env['rack.session'] ||= {}
end

def events
events = env.fetch('tracker', {})[handler_name] || []
events = @session.fetch('tracker', {})[handler_name] || []
events.map { |ev| "#{self.class}::#{ev['class_name']}".constantize.new(ev.except('class_name')) }
end

Expand All @@ -54,10 +55,10 @@ def inject(response)

def write_event(event)
event.deep_stringify_keys! # for consistent hash access use strings (keys from the session are always strings anyway)
if env.key?('tracker')
self.env['tracker'].deep_merge!(event) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
if @session.key?('tracker')
@session['tracker'].deep_merge!(event) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
else
self.env['tracker'] = event
@session['tracker'] = event
end
end

Expand All @@ -69,7 +70,7 @@ def tracker_options
@_tracker_options ||= {}.tap do |tracker_options|
options.slice(*allowed_tracker_options).each do |key, value|
option_value = value.respond_to?(:call) ? value.call(env) : value
unless option_value.nil?
unless option_value.nil?
tracker_options[tracker_option_key(key)] = tracker_option_value(option_value)
end
end
Expand Down
2 changes: 2 additions & 0 deletions rack-tracker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec", "~> 3.2"
spec.add_development_dependency "capybara", "~> 2.4"
spec.add_development_dependency "pry"
spec.add_development_dependency "fuubar"

if RUBY_VERSION < "2.0"
spec.add_development_dependency "mime-types", "< 3.0"
spec.add_development_dependency "addressable", "< 2.5"
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/views/metal/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h1>welcome to metal#index</h1>
<h1>welcome to metal#index</h1>
10 changes: 5 additions & 5 deletions spec/handler/bing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

describe "default" do
def env
{'tracker' => {
{ 'rack.session' => {'tracker' => {
'bing' => [
{ 'class_name' => 'Conversion', 'category' => 'Users', 'action' => 'Login', 'label' => 'Standard', 'value' => 10 }
]
}}
}}}
end

it "will show event initialiser" do
Expand All @@ -31,12 +31,12 @@ def env

describe "default" do
def env
{'tracker' => {
{ 'rack.session' => {'tracker' => {
'bing' => [
{ 'class_name' => 'Conversion', 'category' => 'Users', 'action' => 'Login', 'label' => 'Standard', 'value' => 10 },
{ 'class_name' => 'Conversion', 'category' => 'Users', 'action' => 'Logout', 'label' => 'Standard', 'value' => 5 }
]
}}
}}}
end

it "will show event initialiser" do
Expand All @@ -50,4 +50,4 @@ def env
end
end

end
end
24 changes: 13 additions & 11 deletions spec/handler/criteo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end

def env
{}
{ 'rack.session': {} }
end

it 'will be placed in the body' do
Expand All @@ -22,15 +22,17 @@ def env
context 'with events' do
let(:env) {
{
'tracker' => {
'criteo' =>
[
{
'event' => 'viewItem',
'item' => 'P001',
'class_name' => 'Event'
}
]
'rack.session' => {
'tracker' => {
'criteo' =>
[
{
'event' => 'viewItem',
'item' => 'P001',
'class_name' => 'Event'
}
]
}
}
}
}
Expand Down Expand Up @@ -119,4 +121,4 @@ def env
end
end

end
end
44 changes: 23 additions & 21 deletions spec/handler/facebook_pixel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,29 @@ def env
describe 'with events' do
def env
{
'tracker' => {
'facebook_pixel' =>
[
{
'type' => 'Purchase',
'class_name' => 'Track',
'options' =>
{
'value' => '23',
'currency' => 'EUR'
}
},{
'type' => 'FrequentShopper',
'class_name' => 'TrackCustom',
'options' =>
{
'purchases' => 8,
'category' => 'Sport'
}
}
]
'rack.session' => {
'tracker' => {
'facebook_pixel' =>
[
{
'type' => 'Purchase',
'class_name' => 'Track',
'options' =>
{
'value' => '23',
'currency' => 'EUR'
}
},{
'type' => 'FrequentShopper',
'class_name' => 'TrackCustom',
'options' =>
{
'purchases' => 8,
'category' => 'Sport'
}
}
]
}
}
}
end
Expand Down
22 changes: 12 additions & 10 deletions spec/handler/facebook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ def env
describe 'with events' do
def env
{
'tracker' => {
'facebook' =>
[
{
'id' => '123456789',
'value' => '23',
'currency' => 'EUR',
'class_name' => 'Event'
}
]
'rack.session' => {
'tracker' => {
'facebook' =>
[
{
'id' => '123456789',
'value' => '23',
'currency' => 'EUR',
'class_name' => 'Event'
}
]
}
}
}
end
Expand Down
8 changes: 4 additions & 4 deletions spec/handler/go_squared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def env
describe "with events" do
describe "visitor name" do
def env
{'tracker' => {
{ 'rack.session' => {'tracker' => {
'go_squared' => [
{ 'class_name' => 'VisitorName', 'name' => 'John Doe' }
]
}}
}}}
end

subject { described_class.new(env, tracker: '12345').render }
Expand All @@ -28,11 +28,11 @@ def env

describe "visitor details" do
def env
{'tracker' => {
{ 'rack.session' => {'tracker' => {
'go_squared' => [
{ 'class_name' => 'VisitorInfo', 'age' => 35, 'favorite_food' => 'pizza' }
]
}}
}}}
end

subject { described_class.new(env, tracker: '12345').render }
Expand Down
14 changes: 8 additions & 6 deletions spec/handler/google_adwords_conversion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def env
describe "with events" do
describe "default" do
def env
{'tracker' => {
{ 'rack.session' => {'tracker' => {
'google_adwords_conversion' => [
{ 'class_name' => 'Conversion', 'id' => 123456, 'language' => 'en', 'format' => '3', 'color' => 'ffffff', 'label' => 'Conversion Label' }
]
}}
}}}
end

subject { described_class.new(env).render }
Expand All @@ -44,10 +44,12 @@ def env
label: 'Conversion Label' } }
let(:env) do
{
'tracker' => {
'google_adwords_conversion' => [
{ 'class_name' => 'Conversion', 'value' => '10.0' }
]
'rack.session' => {
'tracker' => {
'google_adwords_conversion' => [
{ 'class_name' => 'Conversion', 'value' => '10.0' }
]
}
}
}
end
Expand Down
20 changes: 10 additions & 10 deletions spec/handler/google_analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ def env

describe "default" do
def env
{'tracker' => {
{ 'rack.session' => {'tracker' => {
'google_analytics' => [
{ 'class_name' => 'Send', 'category' => 'Users', 'action' => 'Login', 'label' => 'Standard' }
]
}}
}}}
end

it "will show events" do
Expand All @@ -105,9 +105,9 @@ def env

describe "with a event value" do
def env
{'tracker' => { 'google_analytics' => [
{ 'rack.session' => {'tracker' => { 'google_analytics' => [
{ 'class_name' => 'Send', category: "Users", action: "Login", label: "Standard", value: "5" }
]}}
]}}}
end

it "will show events with values" do
Expand All @@ -119,12 +119,12 @@ def env
describe 'with ecommerce events' do
describe "default" do
def env
{'tracker' => {
{ 'rack.session' => {'tracker' => {
'google_analytics' => [
{ 'class_name' => 'Ecommerce', 'type' => 'addItem', 'id' => '1234', 'name' => 'Fluffy Pink Bunnies', 'sku' => 'DD23444', 'category' => 'Party Toys', 'price' => '11.99', 'quantity' => '1' },
{ 'class_name' => 'Ecommerce', 'type' => 'addTransaction', 'id' => '1234', 'affiliation' => 'Acme Clothing', 'revenue' => 11.99, 'shipping' => '5', 'tax' => '1.29', 'currency' => 'EUR' }
]
}}
}}}
end

subject { described_class.new(env, tracker: 'somebody', ecommerce: true).render }
Expand All @@ -148,12 +148,12 @@ def env
describe 'with enhanced ecommerce events' do
describe "default" do
def env
{'tracker' => {
{ 'rack.session' => {'tracker' => {
'google_analytics' => [
{ 'class_name' => 'EnhancedEcommerce', 'type' => 'addProduct', 'id' => 'P12345', 'name' => 'Android Warhol T-Shirt', 'category' => 'Apparel', 'brand' => 'Google', 'variant' => 'black', 'price' => '29.20', 'coupon' => 'APPARELSALE', 'quantity' => 1 },
{ 'class_name' => 'EnhancedEcommerce', 'type' => 'setAction', 'label' => 'purchase' }
]
}}
}}}
end

subject { described_class.new(env, tracker: 'somebody', enhanced_ecommerce: true).render }
Expand All @@ -171,11 +171,11 @@ def env

describe 'with parameters events' do
def env
{'tracker' => {
{ 'rack.session' => {'tracker' => {
'google_analytics' => [
{ 'class_name' => 'Parameter', 'dimension1' => 'pink' },
]
}}
}}}
end

subject { described_class.new(env, tracker: 'somebody').render }
Expand Down
Loading