mixcloud / django-experiments

Django AB testing module

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

get and set enrollments from views.

chrisvxd opened this issue · comments

Currently lacking an easy way of handling experiments in views. The easiest existing method is:

import random
from experiments.models import Experiment
from experiments.utils import WebUser

experiment = Experiment.objects.get(name="helloworld")
user = WebUser(req)

# to get an enrollment
user.get_enrollment(experiment)

# to set a random enrollment
user.set_enrollment(experiment, random.choice(experiment.alternatives.keys()) 

Want a dedicated function in views, or somewhere. Like record_goal()

from experiments.views import get_enrollment, set_enrollment
get_enrollment("helloworld", request)
set_enrollment("helloworld", request)

Could also supply an is_enrolled() function:

from experiments.views import is_enrolled
is_enrolled(request, 'helloworld', 'control')

This should blow up loudly if the alternative doesn't exist.

@matclayton what are your thoughts on this? Theo was interested in this functionality, especially is_enrolled.

I've written a similar set_enrollment function to put a user into a specific alternative. I agree that this would belong in experiments.utils

def set_enrollment(request, experiment, alternative):
    experiment_user = WebUser(request)
    experiment_user.set_enrollment(experiment, alternative)

The new python accessible API resolves this.