Skip to main content
An A/B test shows one of two or more variants to each user and compares a conversion rate per variant. In Grain, a remote configuration value picks the variant. Two events measure it: one when the user sees the variant, one when the user converts. A/B tests use Analytics Web (@grainql/analytics-web). Grain Tag has no remote configuration. Read Remote configuration for the cache-first flow.

The pattern

A test has four parts. Both events carry the same test_name and variant properties. Without the variant on both events, the result cannot be split per variant.

The variant value

Create the key in the dashboard and add a rule that splits users into variants. Set the control variant as the default value in defaultConfigurations, so that a client with no response yet shows the control.
The value is a string. A test with more than two variants uses more values, for example control, price_24, and price_27. Read Remote configuration to create the key.

The exposure event

Send the exposure event one time, when the component that shows the variant mounts. In React, send it from an effect that depends on the variant.
The code maps every unknown value to A. Then a typo in the dashboard shows the control instead of an empty page.

The conversion event

The conversion event is the goal of the test: a click, a signup, a purchase. Send it with the same test_name and variant as the exposure event. When the goal is on another page, read the variant again on that page with useConfig. The cache returns the same value for the same user.
When the goal is a template event such as a signup or a purchase, add the variant property to that event instead of a second event. Read Template events.

Read the results

The conversion rate of a variant is the number of users with test_converted divided by the number of users with test_viewed. Both counts are filtered on the same variant. In the dashboard, build a track from test_viewed to test_converted and split it by the variant property. Read Tracks. To compute the rate outside the dashboard, count both events per variant with the Query API. Read Export and query data. Rules for a test:
  1. Change one element per test. Then the difference has one cause.
  2. Run the test for at least one full week, so that every weekday is in the sample.
  3. Do not read the result before each variant has about 1000 users.
  4. When a variant wins, move its share to 100% in the dashboard, then replace the code with the winning variant and delete the key.