process.env.BRANCH is returning undefined

Let’s take a step back. When is that code running?

//Sends branch name to GTM/GA for Netlify split-testing
window.ab_branch = process.env.BRANCH

It can ONLY run successfully during build. If you try to do it at browse time, yes, it will be undefined. BRANCH is only defined when BUILDING your site, so you have to interpolate at that time.

This is an article going into much more detail about how to use env vars, and it also explains some situations where even during build, your build pipeline “throws away” the environment and you’d have to fix that if it is what’s happening (that would cause “BRANCH” to be undefined even during build):