Skip to content

Commit

Permalink
Merge pull request #34 from splitrb/fix-split-integration
Browse files Browse the repository at this point in the history
Make split-analytics work with the latest split version
  • Loading branch information
andrehjr authored Apr 3, 2019
2 parents bf0eb0d + e899407 commit 06191c0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/split/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def tracking_code(options = {})
end

def custom_variables
return nil if session.nil?
return nil if ab_user.nil?
arr = []

session.each_with_index do |h, i|
ab_user.active_experiments.each_with_index do |h, i|
arr << "_gaq.push([\"_setCustomVar\", #{i + 1}, \"#{h[0]}\", \"#{h[1]}\", 1]);"
end
arr.reverse[0..4].reverse.join("\n")
Expand Down
3 changes: 1 addition & 2 deletions spec/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

it 'should add custom variables for every test the user is involved in' do
first_alt = ab_test('link_colour', 'red', 'blue')

expect(session).to eql({'link_colour' => first_alt})
expect(ab_user.active_experiments).to eql({'link_colour' => first_alt})

tracking_code = tracking_code(account: 'UA-12345-6')
expect(tracking_code).to eql(%Q{ <script type=\"text/javascript\">\n var _gaq = _gaq || [];\n _gaq.push(["_setAccount", "UA-12345-6"]);\n \n _gaq.push(["_setCustomVar", 1, "link_colour", "#{first_alt}", 1]);\n _gaq.push(["_trackPageview"]);\n (function() {\n var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n ga.src = ('https:' == document.location.protocol ? 'https://ssl.google-analytics.com/ga.js' : 'http://www.google-analytics.com/ga.js');\n var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n })();\n </script>\n})
Expand Down
40 changes: 33 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,41 @@
require 'split/analytics'
require 'ostruct'

def session
@ab_user ||= {}
end
require "fakeredis"

RSpec.configure do |config|
config.order = 'random'
config.before(:each) do
G_fakeredis = Redis.new

module GlobalSharedContext
extend RSpec::SharedContext
let(:ab_user){ Split::User.new(double(session: {})) }

before(:each) do
Split.configuration = Split::Configuration.new
Split.redis = G_fakeredis
Split.redis.flushall
@ab_user = {}
@ab_user = ab_user
params = nil
end
end

RSpec.configure do |config|
config.order = 'random'
config.include GlobalSharedContext
end

def session
@session ||= {}
end

def params
@params ||= {}
end

def request(ua = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; de-de) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27')
@request ||= begin
r = OpenStruct.new
r.user_agent = ua
r.ip = '192.168.1.1'
r
end
end
3 changes: 2 additions & 1 deletion split-analytics.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']
gem.version = Split::Analytics::VERSION

gem.add_dependency('split', '>= 1.0.0')
gem.add_dependency('split', '>= 3.0.0')

gem.add_development_dependency('rspec', '~> 3.1')
gem.add_development_dependency('rake', '~> 12.3')
gem.add_development_dependency('fakeredis', '~> 0.7')
end

0 comments on commit 06191c0

Please sign in to comment.