Skip to content

Commit

Permalink
Added user qualification functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-phung committed May 27, 2020
1 parent f9fc878 commit f06b16b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/split/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ def ab_test(metric_descriptor, control = nil, *alternatives)
alternative = if Split.configuration.enabled && !exclude_visitor?
experiment.save
raise(Split::InvalidExperimentsFormatError) unless (Split.configuration.experiments || {}).fetch(experiment.name.to_sym, {})[:combined_experiments].nil?
trial = Trial.new(:user => ab_user, :experiment => experiment,
:override => override_alternative(experiment.name), :exclude => exclude_visitor?,
:disabled => split_generically_disabled?)
trial = Trial.new(
:user => ab_user,
:experiment => experiment,
:override => override_alternative(experiment.name),
:exclude => !is_qualified?,
:disabled => split_generically_disabled?
)
alt = trial.choose!(self)
alt ? alt.name : nil
else
Expand Down Expand Up @@ -161,5 +165,11 @@ def normalize_metric(metric_descriptor)
def control_variable(control)
Hash === control ? control.keys.first.to_s : control.to_s
end

private

def is_qualified?
self.respond_to?(:ab_test_user_qualified?, true) ? self.send(:ab_test_user_qualified?) : true
end
end
end
24 changes: 24 additions & 0 deletions spec/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,30 @@
ab_test('link_color', 'blue', 'red')
expect(ab_user).to eq(finished_session)
end

context "with ab_test_user_qualified is set" do
context "ab_test_user_qualified returns true" do
def ab_test_user_qualified?
true
end

it "user is qualified to participate in experiment" do
ab_test('link_color', 'blue', 'red')
expect(['red', 'blue']).to include(ab_user['link_color'])
end
end

context "ab_test_user_qualified returns false" do
def ab_test_user_qualified?
false
end

it "user is not qualified to participate in experiment" do
ab_test('link_color', 'blue', 'red')
expect(ab_user['link_color']).to eq(nil)
end
end
end
end

describe 'metadata' do
Expand Down

0 comments on commit f06b16b

Please sign in to comment.