Data Models and A/B Testing
With Builder, A/B testing works out of the box for Page and Section models, but data models require a bit more set up. In this article we will learn about A/B testing structure in Data models, rendering A/B tests, and delivering the correct variant to users.
Prerequisites
To get the most out of this document, you should already be familiar with:
A/B test a data model in code with BuilderContent
A/B testing on a Data model follows the same process as testing a Page or Section model. Follow these steps to set up an A/B test for a Data model:
- Create a Data model entry.
- Add variants within the entry. Create as many variants as needed to test different content versions.
- Wrap the data model content in a
<BuilderContent/>component. This component selects the correct variant and persists the data across a user's session. It also supports server-side rendering (SSR). To enable SSR, pass the content to the content prop, just as you would with a<BuilderComponent/>.
The example below demonstrates an A/B test on a buy-button-cta Data model. The correct variant is selected and rendered as the call-to-action (CTA) text:
How this example works
variant: the selected data variant after the A/B test logic determines the correct variation.loading: a boolean that indicates whether the content is still being fetched.fullContent: represents the complete raw data from the server, including all variations.
When the content is still loading, a spinner displays. Once the correct variant is selected, its ctaText value displays inside a <button>.
This way the correct A/B test variation is delivered and persists across a user's session.
A/B testing structure in Data models
The JSON below represents the data returned by a Data model entry that contains an active A/B test. A/B testing is implemented with the standard data property for the default option and a variations property containing key/value pairs for each data object for a given variant of the A/B test.
Additionally, the testRatio sub-property indicates the percentage of time each variation should be shown.
How Builder.io Tracks A/B Test Variants
Each content entry in Builder has a unique content ID, which can be one of the following:
- A standalone unique ID, for example,
bbbc29f3d2dc43b7ad820c7709749e42_654c3085cc514cab8d028a5ab581c768. - A combination of your Public API Key and a unique ID, formatted as
{API_KEY}_{UNIQUE_ID}.
To track A/B test variations, Builder uses a cookie named builder.tests.{builderId}. This cookie stores one of two values:
- The Builder ID, which indicates that the default content is being served.
- The variation key, which represents the selected A/B test variant.
For example, given the following Data model entry:
The selected variation would be 1111aaddca3843029a7c5fafaed58ba4, meaning the cookie builder.tests.{builderId} would store this value.
This cookie-based tracking method applies to all A/B tests in Builder, not just those for Data models.
Handle A/B tests in rendering Data model
When rendering data from a Data model within a Builder components (for example, when setting up blog templates), it's vital to wrap the <BuilderComponent> within a <BuilderContent> tag.
If you don't, the <BuilderComponent> won't be aware of the A/B test cookie for the given data and will only return the default data (from the data object in the JSON).
Get the correct variation
Consider the following example. Begin with raw data stored in the articleData variable, obtained through a builder.get() call. Pass this data to the content prop within the <BuilderContent/> component.
Inside, the Builder SDK processes the data and selects the winning variant based on the A/B test cookie, builder.tests.{builderDataId}. This selected variant becomes the data object containing only the winning variation, excluding the others.
This data object is now ready for rendering with the <BuilderComponent>. The articleTemplate variable specifies the desired Section model, indicating that you want to render that specific segment with the winning variation provided as the data field.