`ab_test` does not return the manually assigned variant
Describe the bug
Having assigned a user to a variant for an experiment, I would expect a call to ab_test for that user to return the assigned variant. However, it does not.
To Reproduce Steps to reproduce the behavior:
# In the console run:
user = User.find(...) # any user
experiment = Split::ExperimentCatalog.find('my_experiment')
alternative = Split::Alternative.new('variant_a', experiment.name)
Split::User.new(
nil,
Split::Persistence::RedisAdapter.new(nil, user.id)
)[experiment.key] = alternative.name
# Now, in Redis, the user has been assigned the desired variant for the experiment
# In a controller, run:
def show
assigned_variant = ab_test(
:my_experiment,
'control' => 1,
'variant_a' => 0
)
# assigned_variant will be 'control' even though we had already assigned the variant to 'variant_a'
experiment = Split::ExperimentCatalog.find('my_experiment')
variant = Split::User.new(
nil,
Split::Persistence::RedisAdapter.new(nil, user.id)
)[experiment.key]
# even now, variant will be 'variant_a' like we assigned it to be
end
Expected behavior
Running ab_test should return the variant assigned to the user for the given experiment.
What's your Split.configuration.persistence set to? I'm thinking it's possible that you're not setting the lookup_by. Your split persistence should be set to something like:
Split::Persistence::RedisAdapter.with_config(lookup_by: -> (context) { context.user.id })
@robin-phung, it is the Split::Persistence::RedisAdapter with lookup_by set to our tracking id:
Split.configure do |config|
config.persistence = Split::Persistence::RedisAdapter.with_config(
lookup_by: ->(_) { <USER_TRACKING_ID> }
)
config.redis = <REDIS_URL>
...
end
@TomasBarry, hmm I'm not able to reproduce it, perhaps I'm missing something?
Just to rule out lookup_by and ab_test is not being performed for a different user, your USER_TRACKING_ID returns what would be the user.id correct? This can be verified by running redis-cli monitor to ensure ab_test is being performed on the expected user.
Hi @TomasBarry
Have you configured allow_multiple_experiments? By default, Split will avoid users participating in multiple experiments at once. So, if your user is already participating in another experiment, that's why you are seeing control even if you manually assigned a variant.
That's the only way I was able to reproduce your issue.
@andrehjr, we do indeed have allow_multiple_experiments set to true.
Hello, @TomasBarry I've tried again and was not able to fully reproduce with your script. :(
user = Split::User.new(nil, Split::Persistence::RedisAdapter.new(nil, current_user.id))
user.active_experiments
=> {"my_experiment"=>"control"}
user['my_experiment'] = 'variant_a'
=> "variant_a"
# triggers "hset" "persistence:90" "my_experiment" "variant_a
# Made a request calling ab_test(
# :my_experiment,
# 'control' => 1,
# 'variant_a' => 0
# )
# It rendered variant_a as it should.
user = Split::User.new(nil, Split::Persistence::RedisAdapter.new(nil, current_user.id))
user.active_experiments
=> {"my_experiment"=>"variant_a"}
Other than that, the ab_test could be excluding the user. Split has some rules on that, to ignore bots, and such: https://github.com/splitrb/split/blob/595a59a78484af4ae7a844b05a1bbf6056015fbc/lib/split/helper.rb#L144 https://github.com/splitrb/split/blob/master/lib/split/trial.rb#L124
So If you could debug it further, that would be helpful. I'll close this, for now, feel free to reopen!