How to point users with url to load specific branch while split testing

Hi. I am trying to figure out what is best way to point users to the branch of choice when split testing is running. For example for users that are coming from google ads?

Right now, I added a url param like “branch=branch-b” and added a script to the header of my index.html to check for branch param. If exists, script sets nf_ab cookie to param value. Next, I need to reload in order to fetch from the branch I want. However this is far from being perfect because page flash with master branch content and only after a moment re-loads with branch-b.

    const urlParams = new URLSearchParams(window.location.search);
    const branch = urlParams.get('branch');

    if(branch) {
      function getCookie(name) {
        const value = `; ${document.cookie}`;
        const parts = value.split(`; ${name}=`);
        if (parts.length === 2) return parts.pop().split(';').shift();
      }

      if(getCookie('nf_ab') !== branch) {
        document.cookie=`nf_ab=${branch}`;
      }

      location.replace(document.location.origin);
    }

Is there a way to force Netlify to load from branch-b at a first request? Some magic url param perhaps? Or is there something I am missing with my thinking?

Hi, @pawelw, the branch is controlled on the Netlify side with the nf_ab cookie value.

Normally, if no cookie is chosen, one is randomly assigned according to whatever the ratios are for the split test branches.

I wish I had a workaround to go directly to a specific branch of the test but I don’t. Unless you have a way of “presetting” the cookie (which I don’t believe is possible), it won’t be possible to specific the branch.

If there are other questions about this, please let us know.

Hi Luke

Thanks for your reply.

In case if anyone else has similar problem, I was able to sort the issue by setting the cookie in cloudflare service worker. Turned out we were using cloudflare already, so it was just a matter of adding short script that modifies request.

1 Like