Skip to content

Commit

Permalink
finished method accept goals that are not passed as arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
phoet committed Mar 27, 2013
1 parent 0ce952d commit cbd0354
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ end
To complete a goal conversion, you do it like:

```ruby
finished("link_color" => ["purchase"])
finished("link_color" => "purchase")
```

### DB failover solution
Expand Down
25 changes: 11 additions & 14 deletions lib/split/helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Split
module Helper

def ab_test(experiment_label, control=nil, *alternatives)
def ab_test(metric_descriptor, control=nil, *alternatives)
if RUBY_VERSION.match(/1\.8/) && alternatives.length.zero? && ! control.nil?
puts 'WARNING: You should always pass the control alternative through as the second argument with any other alternatives as the third because the order of the hash is not preserved in ruby 1.8'
end

begin
experiment_name_with_version, goals = normalize_experiment(experiment_label)
experiment_name_with_version, goals = normalize_experiment(metric_descriptor)
experiment_name = experiment_name_with_version.to_s.split(':')[0]
experiment = Split::Experiment.new(experiment_name, :alternatives => [control].compact + alternatives, :goals => goals)
control ||= experiment.control && experiment.control.name
Expand Down Expand Up @@ -67,10 +67,10 @@ def finish_experiment(experiment, options = {:reset => true})
end


def finished(metric_name, options = {:reset => true})
def finished(metric_descriptor, options = {:reset => true})
return if exclude_visitor? || Split.configuration.disabled?
metric_name, goals = normalize_experiment(metric_name)
experiments = Metric.possible_experiments(metric_name)
metric_descriptor, goals = normalize_experiment(metric_descriptor)
experiments = Metric.possible_experiments(metric_descriptor)

if experiments.any?
experiments.each do |experiment|
Expand Down Expand Up @@ -141,12 +141,13 @@ def is_ignored_ip_address?
end

protected
def normalize_experiment(experiment_label)
if Hash === experiment_label
experiment_name = experiment_label.keys.first
goals = experiment_label.values.first

def normalize_experiment(metric_descriptor)
if Hash === metric_descriptor
experiment_name = metric_descriptor.keys.first
goals = Array(metric_descriptor.values.first)
else
experiment_name = experiment_label
experiment_name = metric_descriptor
goals = []
end
return experiment_name, goals
Expand All @@ -156,10 +157,6 @@ def control_variable(control)
Hash === control ? control.keys.first : control
end

def load_and_start_trial(experiment_label, control, alternatives)
experiment_name, goals = normalize_experiment(experiment_label)
end

def start_trial(trial)
experiment = trial.experiment
if override_present?(experiment.name)
Expand Down
6 changes: 3 additions & 3 deletions spec/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
lambda { ab_test({'link_color' => ["purchase", "refund"]}, 'blue', 'red') }.should_not raise_error
end

it "should raise the appropriate error when passed string for goals" do
lambda { ab_test({'link_color' => "purchase"}, 'blue', 'red') }.should raise_error(ArgumentError)
it "should not raise error when passed just one goal" do
lambda { ab_test({'link_color' => "purchase"}, 'blue', 'red') }.should_not raise_error
end

it "should assign a random alternative to a new user when there are an equal number of alternatives assigned" do
Expand Down Expand Up @@ -852,7 +852,7 @@ def should_finish_experiment(experiment_name, should_finish=true)
it "should increment the counter for the specified-goal completed alternative" do
@previous_completion_count_for_goal1 = Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal1)
@previous_completion_count_for_goal2 = Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal2)
finished({"link_color" => ["purchase"]})
finished({"link_color" => "purchase"})
new_completion_count_for_goal1 = Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal1)
new_completion_count_for_goal1.should eql(@previous_completion_count_for_goal1 + 1)
new_completion_count_for_goal2 = Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal2)
Expand Down

0 comments on commit cbd0354

Please sign in to comment.