Skip to content

Commit

Permalink
more generic tracker_options
Browse files Browse the repository at this point in the history
  • Loading branch information
salimhb committed Jan 22, 2015
1 parent 621731f commit 17000d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
19 changes: 13 additions & 6 deletions lib/rack/tracker/google_analytics/google_analytics.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Rack::Tracker::GoogleAnalytics < Rack::Tracker::Handler

ALLOWED_TRACKER_OPTIONS = [:cookie_domain, :user_id]

class Send < OpenStruct
def initialize(attrs = {})
attrs.reverse_merge!(type: 'event')
Expand Down Expand Up @@ -31,12 +34,16 @@ def tracker
def tracker_options
@tracker_options ||= begin
tracker_options = {}

tracker_options[:cookieDomain] = options[:cookie_domain] if options[:cookie_domain]

user_id = options[:user_id].call(env) if options[:user_id]
tracker_options[:userId] = "#{user_id}" if user_id.present?

ALLOWED_TRACKER_OPTIONS.each do |option|

This comment has been minimized.

Copy link
@kangguru

kangguru Jan 22, 2015

options.slice(*ALLOWED_TRACKER_OPTIONS)

will remove one if

This comment has been minimized.

Copy link
@salimhb

salimhb Jan 22, 2015

Author Member

thanks for the tip e1b0f9e

if options[option]
if options[option].respond_to?(:call)
option_value = options[option].call(env)
else
option_value = options[option]
end
end
tracker_options["#{option}".camelize(:lower).to_sym] = "#{option_value}" if option_value.present?
end
tracker_options
end
end
Expand Down
30 changes: 16 additions & 14 deletions spec/handler/google_analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,31 @@ def env
end

describe '#tracker_options' do
describe 'with cookie_domain option' do
subject { described_class.new(env, { cookie_domain: 'railslabs.com' }) }
before do
stub_const("#{described_class}::ALLOWED_TRACKER_OPTIONS", [:some_option])
end

context 'an allowed option configured with a static value' do
subject { described_class.new(env, { some_option: 'value' }) }

it 'returns hash with cookieDomain' do
expect(subject.tracker_options).to eql ({ cookieDomain: 'railslabs.com' })
expect(subject.tracker_options).to eql ({ someOption: 'value' })
end
end

describe 'with user_id option' do
context 'returning a value' do
subject { described_class.new(env, { user_id: ->(env){ '123' } }) }
context 'an allowed option configured with a block' do
subject { described_class.new(env, { some_option: ->(env){ 'value' } }) }

it 'returns hash with userId' do
expect(subject.tracker_options).to eql ({ userId: '123' })
end
it 'returns hash with cookieDomain' do
expect(subject.tracker_options).to eql ({ someOption: 'value' })
end
end

context 'returning nil' do
subject { described_class.new(env, { user_id: ->(env){ nil } }) }
context 'a non allowed option' do
subject { described_class.new(env, { new_option: 'value' }) }

it 'returns hash without userId' do
expect(subject.tracker_options).to eql ({ })
end
it 'returns hash with cookieDomain' do
expect(subject.tracker_options).to eql ({})
end
end
end
Expand Down

0 comments on commit 17000d3

Please sign in to comment.