- Clone this repository.
- Run
npm install
oryarn install
. - Run
npm start
oryarn start
.
This repository reproduces some undesirable behaviour caused in situations where we want to force Optimizely experiment variations during local development.
The following is an outline of how we want to be able to force variations:
- Develop components as per usual without needing to add any special code in the components to handle the forcing of variations.
- Default behaviour is to use Optimizely's default behaviour for bucketing a user.
- If we want to force a variation, provide two URL parameters (
optimizelyExperiment
andoptimizelyVariation
) to specify the experiment/variation combination to render.
In src/index.tsx
you can see the logic for implementing the above behaviour. We hook into the SDK client's onReady
lifecycle and execute code to check for the presence of optimizelyExperiment
and optimizelyVariation
in the URL - if they are, we call setForcedVariation
to force that variation.
The problem: When setForcedVariation
is called, the screen is not rerendered, so the user doesn't actually see that variation.
The goal is to toggle the user's experiment status between the boring button (show_boring_button
) and the shiny button (show_shiny_button
). The user is bucketed to see the boring button by default.
- Run the project and navigate to
localhost:3000
. - Observe that the user sees the boring button.
- Navigate to
localhost:3000/?optimizelyExperiment=test_experiment&optimizelyVariation=show_shiny_button
. - Observe that the user still sees the boring button.
When diving into Optimizely's debug logs, you can see that the user has indeed been forced into the show_shiny_button
variation but they are seeing the boring button on the screen.
So far we've seen that there isn't the expected rerender behaviour when trying to force variations via the URL, however we can get this behaviour to semi-work if we do the following:
- Run the project and navigate to
localhost:3000/?optimizelyExperiment=test_experiment&optimizelyVariation=show_shiny_button
. - Observe that the user sees the boring button.
- Uncomment line 13 in
src/App.tsx
and save the file. - Go back to your browser (Note: our last navigation was
localhost:3000/?optimizelyExperiment=test_experiment&optimizelyVariation=show_shiny_button
). - Observe that the shiny button is displayed.
This is incredibly weird and may have you thinking "well, just use the hook then". However...
- Reload the browser.
- Notice that the boring button is now displayed despite the URL being
localhost:3000/?optimizelyExperiment=test_experiment&optimizelyVariation=show_shiny_button
.
At this point we're already in a weird state... but wait, it gets weirder!
- Comment line 13 in
src/App.tsx
and save the file. - Notice that the shiny button is being displayed!
How is this one possible? This is now back to our original code and the shiny button is being shown. To finish it all off...
- Reload the browser.
- Notice that the boring button is now displayed despite the URL being
localhost:3000/?optimizelyExperiment=test_experiment&optimizelyVariation=show_shiny_button
.
This seems like a bug where rerender isn't being triggered when it should be. We would like this to be fixed so that it's possible for us to deterministically toggle the state of our experiments. The benefits of this are:
- It allows us to run integration tests on code behind an experiment.
- It allows developers to easily toggle between experiment states when developing features locally.