A/B testing

Simple A/B testing in WordPress with Google Analytics site experiments

As we’re about to make some significant tweaks and changes to the main product pages on the Smart Insights site, I’ve looked at a lightweight way to do simple A/B testing in WordPress.

What is A/B testing?

You’ll probably be aware of the concept, I’m sure!

A/B testing, also called split testing or multivariate testing, involves your site randomly selecting one of several versions of a page to show to a site visitor. The aim is to find out which one is most successful at converting your visitors.

What is A/B Testing?

Why do you need it?

Put simply, how do you know if the changes you made to a page are working better than the earlier version unless you can compare them side by side?

A/B testing allows you to make changes from a position of intelligence – knowing the impact of the changes you make allows you to learn what works and what doesn’t, and make sure that you repeat the successes and not the failures.

A/B testing in WordPress

After doing the usual Google searches, I discovered several A/B testing WordPress plugins, the best of which seemed to be MaxA/B. It’s a comprehensive plugin, and as a result pretty complicated.

The problem – MaxA/B seemed to me to duplicate a lot of the metrics and data capture that is really the job of an analytics tool and the fact is, mostly all of us already have a system installed on our sites that is specifically geared around telling us how our site performs – Google Analytics.

It just didn’t seem to make much sense to install something else that duplicates something I already have – so I wanted to see if there was a simpler method to allow us to use Google Analytics and without installing a plugin. I discovered that there is – and it’s provided directly within Google Analytics.

Introducing Google Site Experiments

I use Google Analytics a lot, but this particular feature has completely passed me by. Apparently it used to be a separate system called Google Content Experiments but has been merged into Google Analytics back in August 2012.

Content Experiments is a somewhat different approach from either standard A/B or multivariate testing. Content Experiments is more A/B/N. You’re not testing just two versions of a page as in A/B testing, and you’re not testing various combinations of components on a single page as in multivariate testing. Instead, you are testing up to five full versions of a single page, each delivered to visitors from a separate URL.

More at the Google Analytics support site

Great – perfect for what we wanted to do, and easy enough to make it work with WordPress on the surface. It recognises that a user is on the page that you’re wanted to experiment with, and then randomly sends them off to one of the other pages you’ve got in the experiment. As a result we can set each version of the page up as a separate page within WordPress, and add each URL to the experiment within Google Analytics.

When you set up your experiment (full instructions here) you get some javascript code that you have to add to the <head> tag of the original page of your experiment;

Create a new experiment   Google Analytics
Creating a new experiment in Google Analytics

The problem is that the <head> section of every page in WordPress is served from a single PHP file – header.php. Adding the Google experiments javascript code to this means it will fire on every page, and break your site as a result. So how do we use experiments with WordPress pages? There’s an easy solution.

Outputting the experiments code on your original page only

Firstly, log in to your WordPress site as an administrator and browse to the edit screen for the original page. Once there, look in your browser’s URL bar to get the page ID from the URL.

Getting the WordPress page ID
Getting the WordPress page ID

Now fire up your favourite code editor and open the header.php file in your active theme directory. Below the first <head> line add the following code, substituting your page ID instead of ***PAGEID***, and the experiment javascript code provided by Google Analytics in place of ***GOOGLECODE***;

<?php if (is_page(***PAGE ID***) ):?>


<?php endif; ?>

Simple as that! Using the is_page function allows us to surround the output of our experiment code in a conditional statement – if the current page ID matches the ID of your original experiment page, the javascript will fire and the Google experiment will work.

Once installed on your site, Google will automatically randomise which of your test pages your users see and start to measure the success of each, and depending on the length and configuration of the experiment will eventually declare a ‘winner’.

I’m keen to hear what other WordPress developers are using for doing simple A/B testing in WordPress – are you using a plugin such as MaxA/B or have you had any experience of Google content experiments?

Published by

Stu Miller

Web consultant and specialist, WordPress developer and PHP developer based in Leeds, UK. 15 years experience in architecting web sites and applications. Co-founder and Technical Director of SmartInsights.com, formerly the same of First 10 Digital

23 thoughts on “Simple A/B testing in WordPress with Google Analytics site experiments”

  1. Awesome! Thanks for this! I was getting ready to start a hardcore search on WP plugins for A/B testing – this makes it WAY easier! (=^_^=)

    1. Hi Bart,

      Yes – it would, but when you’re setting up an experiment on the second page you’d have to;

      1) Find the page ID of the second original page you want to test
      2) Amend the header.php code I provided as follows;

      <?php if (is_page(***PAGE ID FOR FIRST PAGE***) ):?>
      <?php elseif (is_page(***PAGE ID FOR SECOND PAGE***) ):?>
      <?php endif; ?>

      This should work for you!

  2. I’m an intern at a company using WordPress and my boss told me to figure out how to do A/B testing. This was a huge help. Thanks a bunch.

  3. i’m trying to do this with a post, so is_page wasn’t the way to go.
    Here’s what i came up with when A/B-testing posts:

    if ($the_current_postid == *** yourpostid ***):?>
    *** GOOGLE CODE ***

  4. Stu, this is a great post. Thank you for sharing your insights. My big question that I am having trouble finding an answer for: how to A/B test a homepage. Everything I am seeing on A/B is testing a page or landing pages. I want to test my website’s homepage. Is that even possible? Seems like the only solution is optimizely, which is awesome, but very expensive especially when there are several sites to test.

  5. Hi Patrick – glad it helped you.

    In regard to A/B testing a homepage, though I haven’t tested this myself, it shouldn’t be a problem. Remember that if you set a static homepage then you can identify your homepage in one of two ways for the code I provided to work;

    – By getting the page ID for the static page from the admin as above
    – By using the function to decide when/if to output the Google code for the A/B test

    Either of these should allow you to A/B test the homepage. Let me know how you get on!

  6. Hey Stu, very helpful post about using Google Experiments. I am not sure that tool or Max A/b can solve my problem though. I want to test the banner calls to action on my page. They are in the side columns on every page. Is there a way to measure the effectiveness of a banner that is on everypage? I have a goal landing page too. The goal on the landing page is to fill out a form or call us. But neither of those go to another page so I am not sure how to capture that those goals are achieved either. Thanks! David.

    1. Hi David. A/B testing of certain page elements is beyond the scope of what Google offer. To test banners you’d need to find a plugin that offers this, or of course you could get familiar with the Google Doubleclick for advertisers service. It’s very complex but is built to do what you need. For popups/newsletter subscriptions you could try the Pippity plugin. It’s not free but has A/B testing built in.

  7. Well hello Stu, thanks for giving a great step by step way to achieve this without using plugins. Many of the plugin options work fine on my development/test sites but since WP3.6 update the live sites have all become very twitchy and sensitive to new plugins, with many causing the white screen of death.

    Will test this on the development site and then bring it across to my neurotic live WP installation.

    1. Hello Joshua,
      Any progress on the plugin? I am about to go down the same path, and wondered if you would like to combine forces.

Leave a Reply