JavaScript Examples - A/B Testing
Configure an A/B test by regulating the type of response based on cookies.
Configure an A/B test on Azion’s global network by serving one of two responses and remembering each visitor’s group in a cookie. Use this pattern to run randomized experiments, such as comparing two page variants, without sending traffic to an origin to decide which version to show.
How it works
The fetch handler reads the incoming Cookie header with request.headers.get("cookie"). If the visitor already has the test cookie, the function returns the matching variant so their experience stays consistent across requests. For new visitors, Math.random() assigns the test or control group at roughly 50/50, and response.headers.append("Set-Cookie", ...) persists that choice so the same variant is served next time.