How to Run an A/B Test on Your WordPress Website Homepage
Using Google Tag Manager and Google Analytics 4
No plugins required. Full control over tracking and results.
A/B testing is one of the most reliable ways to improve website performance. Rather than guessing which version of a page will work better, you can split your traffic between two versions and let the data decide.
This guide walks through a complete split test setup using Google Tag Manager and GA4. It does not require any WordPress plugins and gives you full control over variant assignment, tracking, and results.
What Is an A/B Test?
An A/B test (also called a split test) sends visitors to two different versions of a page and measures which one generates more conversions — whether that means form submissions, enquiries, bookings, or sales.
For this example we will test two versions of a homepage:
- / (Version A — existing homepage)
- /new (Version B — new landing page)
Visitors arriving at the homepage are randomly assigned to one version and remain on that version across all future visits until the test ends or their browser data is cleared.
Important
localStorage vs sessions This setup uses localStorage to store the variant assignment. Unlike session storage, this persists across browser sessions — meaning a returning visitor will always see the same version they were originally assigned. This is intentional and produces more reliable results.
Step 1: Create the Two Landing Pages
Before setting up tracking, you need both versions of your page ready in WordPress.
Your URL structure should be:
- / Version A — your current homepage
- /new Version B — the page you want to test
Version B should test one meaningful change. Common things to test include:
- A different headline or subheading
- A stronger or repositioned call-to-action
- A simplified layout with fewer distractions
- Additional social proof or testimonials
- A new hero image or video
Best practice
Change only one major element at a time. If you change multiple things and Version B wins, you won’t know what caused the improvement. Isolating one variable gives you a clear, actionable result.
Step 2: Set Up the Split Test Script in GTM
In Google Tag Manager, create a new Custom HTML tag and paste in the following script.
This version includes variant tracking via the dataLayer, which is required for reporting in GA4.
The Script
<script>
(function() {
try {
var variant = localStorage.getItem('homepage_test');
if (!variant) {
variant = Math.random() < 0.5 ? 'A' : 'B';
localStorage.setItem('homepage_test', variant);
}
// Push variant to dataLayer for GA4 tracking
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'ab_test_assigned',
ab_variant: variant
});
// Redirect B visitors on the homepage only
if (variant === 'B' && window.location.pathname === '/') {
window.location.replace('/new/');
}
} catch(e) {}
})();
</script>
What this script does
- Checks whether the visitor has already been assigned a variant
- If not, randomly assigns them to A or B (50/50) and stores the result in localStorage
- Pushes an ab_test_assigned event to the dataLayer so GA4 can record the variant
- Redirects B visitors from / to /new/ using location.replace (prevents back-button loops)
- Wrapped in try/catch to handle rare cases where localStorage is unavailable (some private browsers)
GTM tag settings
- Tag type: Custom HTML
- Trigger: All Pages
- Fire on: DOM Ready — not Page View or Window Loaded
Why DOM Ready matters
If the tag fires on Window Loaded, visitors assigned to Variant B will briefly see the original homepage before being redirected. Firing on DOM Ready executes the script earlier and minimises any visible flash.
Prevent /new/ from triggering the redirect
Make sure this tag only fires on the homepage, not on /new/ itself. In GTM, set the trigger to:
- Trigger type: Page View — DOM Ready
- This trigger fires on: Some Page Views
- Condition: Page Path equals /
This prevents visitors who land directly on /new/ from being caught by the redirect logic.
Step 3: Set Up GA4 Tracking
The dataLayer push in the script fires an event called ab_test_assigned with a parameter ab_variant containing either A or B. You need to capture this in GA4.
In Google Tag Manager
Create a Data Layer Variable:
- Variable type: Data Layer Variable
- Data Layer Variable Name: ab_variant
- Name it: DLV – ab_variant
Create a GA4 Event tag:
- Tag type: Google Analytics: GA4 Event
- Event name: ab_test_assigned
- Add event parameter — Parameter name: ab_variant, Value: {{DLV – ab_variant}}
- Trigger: Custom Event — Event name: ab_test_assigned
In Google Analytics 4
Register ab_variant as a custom dimension so it appears in your reports:
- Go to Admin > Custom Definitions > Custom Dimensions
- Create new dimension: Name: AB Variant, Scope: Event, Parameter: ab_variant
Track a meaningful conversion event
Set up a conversion event based on a real business outcome. Common examples:
- Visit to /thank-you (form submission confirmation)
- Click on a phone number or email link
- Booking confirmation page
In GA4, mark your chosen event as a conversion under Admin > Conversions.
Why this step is essential
Without the dataLayer push and the GA4 custom dimension, you have no way to segment your conversion data by variant. The redirect alone tells you nothing — you need GA4 to record which version each visitor saw.
Step 4: Run the Test Long Enough
One of the most common mistakes is stopping a test too early. Random variation in traffic and conversions can make one version look better in the first few days even when there is no real difference.
Minimum thresholds
- At least 2 to 4 weeks of data
- A minimum of 100 conversions per variant where possible
- Check a statistical significance calculator before declaring a winner
A free tool such as abtestguide.com/calc lets you enter your visitor and conversion numbers to confirm whether results are statistically significant.
Step 5: Declare a Winner and End the Test
Once you have enough data, compare performance in GA4 by filtering on the ab_variant dimension.
What to compare
- Conversion rate per variant
- Engagement rate and average session duration
- Bounce rate
- Revenue or lead value per visitor if applicable
If Version B wins
- Make /new/ your permanent homepage
- Set up a 301 redirect from /new/ to / once promoted
After the test ends — important cleanup steps
- Remove or pause the GTM split test tag
- Delete or archive the old homepage variant
- Clear localStorage if running a follow-up test with a different key name
Do not leave the split test tag running after the test ends.
If the tag keeps firing, visitors will continue being randomly assigned and redirected even when the test is over.
Always pause or delete the tag once you have a result.
SEO Considerations
Running a redirect-based split test carries some SEO risk if not handled carefully.
- Google may crawl /new/ and index it as a separate page, creating duplicate content
- Repeated 302 redirects from / to /new/ may affect how Google perceives your homepage
Recommended precautions
- Add a canonical tag to /new/ pointing to / so Google treats it as a variant, not a separate page
- Keep the test duration reasonable — extended tests of several months increase risk
- Monitor Google Search Console for any coverage issues during the test
For most short-duration tests on low-traffic sites, the SEO impact is minimal. For high-traffic homepages or longer tests, consider server-side splitting instead.
Best Practices Summary
| Principle | Guidance |
| Test one change at a time | Changing multiple elements makes it impossible to identify what caused any improvement. |
| Use meaningful conversions | Track actual business outcomes — leads, bookings, purchases — not just page views. |
| 50/50 traffic split | Equal splits produce the clearest results and reach significance fastest. |
| Run for at least 2–4 weeks | Short tests are often misleading due to day-of-week traffic variation. |
| Check statistical significance | Use a calculator to confirm results before making decisions. |
| Clean up after the test | Remove the GTM tag and redirect logic as soon as the test is concluded. |
| Monitor SEO during the test | Watch Search Console for unexpected indexing of the variant URL. |
What to Test Next
Once you are comfortable with the process, you can use the same setup to test any page on your site:
- Pricing pages
- Service landing pages
- Lead generation pages
- Campaign pages from paid advertising
Over time, consistently testing and improving your pages turns your website into a system that progressively improves its conversion rate rather than staying static.