Skip to content

Conversation

ndelage
Copy link

@ndelage ndelage commented Jun 8, 2016

Update Split::User to accept an optional persistence adapter, overriding the default.

This allows for running experiments outside a web request that are tied to a particular "user". By making use of Split::User when running the experiment, you're able to initiate the experiment one day and record a completion at a later time (by retrieving the earlier trial).

Running an experiment

experiment_name = :welcome_message

key = "thing-#{thing.id}"
# since we're outside the context of a web request, the first arg `context` is nil
ab_user = Split::User.new(Split::Persistence.adapter.new(nil, key))
experiment = Split::ExperimentCatalog.find_or_create(experiment_name)
trial = Split::Trial.new(user: ab_user, experiment: experiment)
trial.choose!

if trial.alternative.name == "short_message"
  send_short_message
elsif trial.alternative.name == "long_message"
  send_long_message
end

Recording completion at later date

experiment_name = :welcome_message

if conversion?
  # using the same key to retrieve our earlier trial
  key = "thing-#{thing.id}"
  ab_user = Split::User.new(Split::Persistence.adapter.new(nil, key))
  experiment = Split::ExperimentCatalog.find_or_create(experiment_name)
  trial = Split::Trial.new(user: ab_user, experiment: experiment)

  trial.complete!
end

Fixes #410

@coveralls
Copy link

coveralls commented Jun 8, 2016

Coverage Status

Coverage increased (+0.004%) to 98.308% when pulling 075fbe7 on ndelage:custom-persistence-adapter into 7df99d8 on splitrb:master.

@andrew andrew merged commit f4f33d3 into splitrb:master Jun 8, 2016
@andrew
Copy link
Member

andrew commented Jun 8, 2016

👍 Thanks @ndelage

@ndelage
Copy link
Author

ndelage commented Jun 8, 2016

💯 great maintainer, would contribute again.

@jakeonrails
Copy link

@andrew Do you know when a new release including this patch will be live on Rubygems? We are either going to monkey patch to add support or wait til the release if it's in the next day or two.

Thanks, and also thanks @ndelage for adding this support!@

@andrew
Copy link
Member

andrew commented Jun 8, 2016

Just released v1.5.0 for you: https://github.com/splitrb/split/releases/tag/v1.5.0

@jakeonrails
Copy link

💥 Nice! 💥
Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants