Currently, the ExperimentManager service is wired up to the current user, so $this->experimentManager->getExperiment( 'foo' )->send( 'bar' ); will send a bar event for the foo experiment for the current user. The event will be attributed to the current user, and will only be sent if the current user is enrolled in the foo experiment. In most cases, that's what we want, but in some cases the user subject to experimentation isn't the same as the current user. Test Kitchen currently has no officially supported way to attribute an event to a different user, or to look up experiment enrollment for a different user. The current user and their enrollments are looked up once, at the beginning of the request, and never change after that.
Use cases for this include:
- During regular account creation, the "current user" is anonymous; the newly created user doesn't become the current user until the next request
- When an admin creates an account on behalf of someone else, the account creator is the current user, but if we're going to send an account_created event we want to attribute that to the created user, not to the account creator
- If we ever want to experiment with notifications, we would want to vary the content of the notification based on which variant the recipient is enrolled in. But the current user is typically the user who performed the action that triggered the notification. One action can also send notifications to multiple different users (e.g. if user A posts a talk page comment that mentions both user B and user C), and we want each recipient to receive a notification that is correctly personalized to their experiment enrollment status, and is not influenced by the enrollment status of the performer or the other recipients.
- If we ever want to experiment with actions that are taken in bulk (e.g. certain types of emails we send to users), then there typically isn't a current user at all (because the context is a maintenance script, not a web request) and we would need to look up experiment enrollment and/or send events for many different users, not just one
The current workaround for the account creation case is here. It's basically:
// Change the ExperimentManager's user to the created user $this->experimentManager->updateUser( $createdUser ); // ... do stuff with $this->experimentManager ... // Change the user back to the current user $this->experimentManager->updateUser( RequestContext::getMain()->getUser() );
But ExperimentManager isn't designed to work this way. Some of the problems are:
- updateUser() does not accept an anonymous user, and silently does nothing when it receives one. So if the current user is anonymous, you can't change the ExperimentManager user back to that. Instead, any subsequent events are logged against the wrong user (in practice, this probably doesn't happen right now, at least not for the account creation use case)
- updateUser() does not clear the enrollments that have already accumulated in the EnrollmentResultBuilder object. This is probably intended behavior for the regular use case of updateUser() (where we want the current user's mw-user-based enrollments and their edge-unique-based enrollments should be added together), but for the purposes of this hack it's not. This means that any subsequent events or experiment enrollment checks will be based on a mix of the current user's and the created user's experiment enrollments, and that might lead to the current user being incorrectly temporarily enrolled into an experiment for the remainder of the request. In practice, this probably also doesn't happen right now for the account creation use case, but it's a big problem for a use case involving multiple users like notifications or bulk actions. (It also means that the current user's edge-unique enrollments are applied, which is probably also wrong, but less likely to cause problems in practice.)
What I think we need for this is a way to obtain a separate ExperimentManager instance for a different user, that computes enrollment only for that user and sends events attributed to that user. Code that needs to do experiment stuff for a different user would first obtain an ExperimentManager for that user, get Experiment objects for them, and then discard the ExperimentManager instance when it's done. @phuedx and I independently had the same 2 ideas for how this might work:
- Create an ExperimentManagerFactory service that has a newForUser() method that takes a User and returns an ExperimentManagerInterface for that user
- Add a method to ExperimentManagerInterface named forUser() that does the same thing
@phuedx prefers #1, and I have a weak preference for #1 as well
Acceptance Criteria
- Create the ExperimentManagerFactory service that has a newForUser() method that takes a User and returns an ExperimentManagerInterface for that user
- Add unit tests to cover new behaviour
- Documentation
- Case study (here's the weird thing that was needed, here's what we did to make it work)