Builder.io is easy to customize to sync with another a/b testing system - for instance Optimizely, LaunchDarkly, Dynamic Yield, or a custom one. In this post we’ll walk you through a few options to set this up, based on your need can set this up.
Option 1 - custom field & query
One of the simplest options is just to create a custom field to choose/enter an a/b test group set up in your test provider (Optimizely, LaunchDarkly, etc). This can either be a value you can enter manually, or create a custom field plugin for fetching the list possible options and presenting the choices.
You can then use queries to filter content matching the correct test groups.
Option 2 - targeting
This option is similar to the above option, but can be simpler for some use cases, esp when matching content to test groups is purely optional.
For instance, you can make a custom targeting field called “testGroup”. Here you can use targeting to assign pieces of content to be chosen when a given user is assigned to a specific test group, e.g. in the Builder.io by choosing “test groups” “contains” [choose test group ID]. You can also use a custom plugin to make choosing the group in the targeting UI easy as well.
Then just send test groups as user attributes, e.g.
builder.setUserAttributes({
testGroups: ['abc123'].sort().join(',') // Sorting ensures best cache efficiency
});
Option 3 - webhooks
Use this option if you want the tests to be created directly in Builder as A/B test variations in our UI, as opposed to separate content entries that have to be connected to test groups also manually connected in your A/B testing system. This can be the most seamless for editors in Builder.
To use this option, you’ll want to use webhooks to listen to content changes in Builder. Anytime content in Builder has A/B tests there will be a variations
object on the JSON we send.
{
/* Main content ID */
"id": "abc123",
/* Main content data */
"data": { /* ... */ },
/* Map of variation ID to variation content */
"variations": {
/* Test group ID */
"xyz789": {
/* Test group ID */
"id": "xyz789",
/* Test variation name */
"name": "My test name",
/* Test variation tarffic percent */
"testRatio": "0.5",
/* Variation data */
"data": { /* ... */ }
}
}
}
You can read this to see that the above content has a 50/50 test of the default content vs one variation. You can then sync this to your A/B testing system writing the variation names and IDs.
Then, when users load your site, you can hit your A/B testing system for which groups a user should be in. You can instruct Builder on which test groups to load by setting a cookie of the format
setCookie(`builder.tests.${contentId}`, variationId)
Builder will read this cookie and use the provided test variation.
Note that for the default variation, the variation ID is the content ID - so to say load the default variation set
setCookie(`builder.tests.${contentId}`, contentId)