April 23, 2026

A/B Testing with Google Tag Manager: The Free DIY Guide for 2026

Learn how to set up A/B tests using Google Tag Manager — from cookie-based traffic splitting to GA4 tracking — plus a simpler alternative that skips the JavaScript.

Since Google Optimize shut down in September 2023, marketers have scrambled for free alternatives. One approach that keeps surfacing: using Google Tag Manager (GTM) to run A/B tests directly. It works, it's free, and you probably already have GTM installed. But it comes with trade-offs most guides gloss over.

This guide walks you through how GTM-based A/B testing actually works, when it makes sense, and when you're better off using a purpose-built tool like PageDuel.

How GTM A/B Testing Works

The basic idea is simple: use GTM's Custom HTML tags and JavaScript to randomly assign visitors to a control or variant group, store that assignment in a cookie, and then modify the page accordingly. Here's the standard approach:

  1. Create a random assignment cookie. Use a Custom HTML tag or the community Cookie Creator template to generate a random number (e.g., 0–99). Values 0–49 go to the control group, 50–99 go to the variant.
  2. Read the cookie on subsequent visits. Set up a 1st-Party Cookie variable in GTM to read the assignment value so returning visitors always see the same version.
  3. Modify the page with JavaScript. Fire a second Custom HTML tag that checks the cookie value and applies DOM changes — swapping a headline, changing a CTA button color, or hiding an element.
  4. Push events to GA4. Use dataLayer.push() to send a custom event (like ab_split_test) with dimensions for the experiment name and variant. Then track conversions in GA4 to compare performance.

Tools like the free GTM Testing library from ABTestGuide.com streamline this process with a lightweight snippet that handles randomization and cookie management. Simo Ahava's popular GTM split test guide takes a similar cookie-and-redirect approach for URL-level tests.

What You Can Test with GTM

GTM-based A/B tests work best for simple, client-side changes:

  • Headlines and CTA copy (see our guide on A/B testing headline copy)
  • Button colors and placement
  • Hiding or showing page elements
  • Swapping images or banners

If you're just learning how to run an A/B test, GTM can be a decent starting point because the tooling is already on your site.

The Real Problems with GTM A/B Testing

Here's what most "GTM testing tutorials" skip: GTM loads asynchronously. That means your variant code fires after the page has already started rendering. Visitors see the original version flash before the variant kicks in — the dreaded flicker effect.

This isn't a minor annoyance. Flicker actively harms your test data. Visitors who see the original content — even for 200 milliseconds — form a first impression that skews their behavior. Your variant might actually be better, but flicker pollutes the signal.

Other practical limitations:

  • No visual editor. Every change requires writing JavaScript to target and modify DOM elements. For teams without developers, this is a blocker.
  • No built-in statistics. GTM doesn't calculate statistical significance. You have to export GA4 data and run your own analysis or rely on GA4's limited reporting.
  • No experiment management. There's no dashboard to start, stop, or monitor tests. You manage everything through GTM tags, triggers, and variables — which gets messy fast with multiple concurrent tests.
  • Cookie consent complications. Your split-test cookie requires consent under GDPR, adding another compliance layer to manage.

When GTM Testing Makes Sense

GTM-based A/B testing is a reasonable choice when:

  • You're running a one-off test and already have GTM and GA4 set up
  • You have a developer comfortable writing JavaScript DOM manipulation
  • You don't need real-time results or statistical significance calculations
  • You're on a team that already lives in the Google analytics ecosystem

For anything beyond a quick single-element test, the maintenance burden adds up quickly.

A Simpler Alternative: Skip the JavaScript

If you want free A/B testing without writing JavaScript, managing cookies, or fighting flicker, PageDuel gives you a visual editor that handles all of it. Drop a single script tag on your site — you can even deploy it through GTM itself — and use the point-and-click editor to create variants. PageDuel handles randomization, cookie management, anti-flicker protection, and statistical significance automatically.

The free tier includes unlimited experiments and has no pageview caps, which makes it a practical Google Optimize replacement for teams who don't want to cobble together a GTM-based testing stack.

Setting Up PageDuel via GTM (2 Minutes)

If you prefer to manage all your scripts through GTM, you can deploy PageDuel as a Custom HTML tag:

  1. Create a new Custom HTML tag in GTM
  2. Paste your PageDuel snippet (found in your dashboard under Site Settings)
  3. Set the trigger to "All Pages"
  4. Publish your GTM container

From there, everything — variant creation, traffic splitting, results tracking — happens in the PageDuel dashboard. No more JavaScript debugging in GTM preview mode.

Bottom Line

GTM can technically run A/B tests, and for a quick one-off experiment with developer support, it works. But for any team that wants to test regularly — especially without writing code — a dedicated tool saves hours of setup and avoids the flicker and analytics gaps that make GTM tests unreliable. Start with what you have, but know when to upgrade.

Related Reading