A/B testing with same-URL direct access
Set up an A/B test by controlling what response is served based on cookies.
Last updated Oct 13, 2025
This version passes through requests for /test/* and /control/* URI paths to the origin server, bypassing random assignment.
const NAME = "myExampleABTest";
export default {
async fetch(request) {
// Clone the original URL
const url = new URL(request.url);
// Enable Passthrough to allow direct access to control and test routes.
if (url.pathname.startsWith("/control") || url.pathname.startsWith("/test"))
return fetch(request);
// Determine which group this requester is in.
const cookie = request.headers.get("cookie");
if (cookie && cookie.includes(`${NAME}=control`)) {
url.pathname = "/control" + url.pathname;
} else if (cookie && cookie.includes(`${NAME}=test`)) {
url.pathname = "/test" + url.pathname;
} else {
// If there is no cookie, this is a new client. Choose a group and set the cookie.
const group = Math.random() < 0.5 ? "test" : "control"; // 50/50 split
if (group === "control") {
url.pathname = "/control" + url.pathname;
} else {
url.pathname = "/test" + url.pathname;
}
// Reconstruct response to avoid immutability
let response = await fetch(url);
response = new Response(response.body, response);
// Set cookie to enable persistent A/B sessions.
response.headers.append("Set-Cookie", `${NAME}=${group}; path=/`);
return response;
}
return fetch(url);
},
};
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/snippets/examples/ab-testing-same-url/#page","headline":"A/B testing with same-URL direct access","description":"Set up an A/B test by controlling what response is served based on cookies.","url":"https://developers.cloudflare.com/rules/snippets/examples/ab-testing-same-url/","inLanguage":"en","image":"https://developers.cloudflare.com/rules/snippets/examples/ab-testing-same-url/og.png?v=52e357d84acb1e79","dateModified":"2025-10-13","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["A/B testing","Cookies","URL rewrite"]}
description: Dynamically set a cookie expiration and test group. title: Append dates to cookies to use with A/B testing image: https://developers.cloudflare.com/rules/snippets/examples/append-dates-to-cookies/og.png?v=72b4197b81f549c5
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/rules/llms.txt
Use this file to discover all available pages before exploring further.
Docs