Skip to content

Commit

Permalink
Add ab_test_chosen? helper and chosen? method on experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Salomon authored and sideshowbandana committed Oct 8, 2012
1 parent 067b8d1 commit 7194b70
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/vanity/adapters/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ def ab_add_participant(experiment, alternative, identity)
fail "Not implemented"
end

# Indicates which alternative has been picked for this participant. See #ab_add_participant.
def ab_chosen(experiment, identity)
false # TODO: default to false for now; should add to each adapter
end

# Records a conversion in this experiment for the given alternative.
# Associates a value with the conversion (default to 1). If implicit is
# true, add particpant if not already recorded for this experiment. If
Expand Down
6 changes: 6 additions & 0 deletions lib/vanity/adapters/active_record_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ def ab_add_participant(experiment, alternative, identity)
VanityParticipant.retrieve(experiment, identity, true, :seen => alternative)
end

# Indicates which alternative has been picked for this participant. See #ab_add_participant.
def ab_chosen(experiment, identity)
participant = VanityParticipant.retrieve(experiment, identity, false)
participant && participant.seen
end

# Records a conversion in this experiment for the given alternative.
# Associates a value with the conversion (default to 1). If implicit is
# true, add particpant if not already recorded for this experiment. If
Expand Down
12 changes: 12 additions & 0 deletions lib/vanity/experiment/ab_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ def showing?(alternative)
end
end

# True if this experiment has been selected for the current id (see #chooses).
def chosen?
# True if experiment is active and a value has been chosen for current identity
!!if @playground.collecting? # return a boolean value
active? && (connection.ab_showing(@id, identity()) ||
connection.ab_chosen(@id, identity()))
# TODO: implement ab_chosen on all vanity adapters!!
else
@showing && @showing[identity()]
end
end


# -- Reporting --

Expand Down
5 changes: 5 additions & 0 deletions lib/vanity/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def ab_test(name, &block)
end
end

# Check whether the specified experiment has had a value chosen yet
def ab_test_chosen?(id)
Vanity.playground.experiment(id).chosen?
end

# Tracks an action associated with a metric.
#
# @example
Expand Down

0 comments on commit 7194b70

Please sign in to comment.