There comes a time in all our projects where we have a bright idea we think will improve user interaction. Maybe we want more users to signup to our feed. Maybe we want more users to buy our products. Whatever the case, we want to role out our idea which we “know” will achieve our goal.
But wait….! What about social variables? Sometimes users just don’t want to buy things or sign up for stuff. Sometimes users are online less often or have other life factors which affect interactions. Month to month, quarter to quarter, we can’t guarantee consistent social variables. This is where A/B testing comes in. With A/B testing we can present both versions to the same social climate and track which one does a better job of accomplishing our goal.
Lets talk about our tracking tool, Google Analytics. Sure we could build our own system to track conversions or interactions. Sure there are other tools our there for this. But really, this is a one time use setup which is going to be in place for what.. 1 month, or maybe one quarter. Personally, I subscribe to the KISS principle. For one time use items I tend to reuse tools I already have in place.
For my particular use case I ended up with 4 variations of possible templates. For the sake of brevity I’m going to call them AA, AB, BA, BB. Each variation displays a different template(s) to the user viewing the site. The variations are shown at random.
The PHP code
The first thing you will notice is the
HTTP_USER_AGENT conditional at the beginning. This is an optional way to display a particular combination of templates to search engines. I added this to my implementation to be sure I have consistent results in SEO until I have concluded my test results and permanently implemented a set of templates.
Next I use a simple array and an array_rand() call to pick one at random. I experimented with different techniques for the best random set and array_rand() was the most consistently random for an array this size.
To output the template
Very basic implementation here. You may customize to fit your theme structure. This example will output the template which matches the A/B code -template.php. For instance, if the code is AB the template which will be called is AB-template.php in the root of the theme.
To track the results
Somewhere above the
get_template_part() call you will need this JS which taps into the existing Google Analytics (I assume GA is already setup on your site) and records the events. We can’t be 100% sure users will be provided with an exact ratio of template combinations so we also track the views while storing the same code as the interactions so we may compare them.
This example is using a html class “buy” to track the buttons which we are trying to get the users to click. When a user clicks a buy button, Google Analytics will record the event and which combination of templates where displayed at the time of click.
That’s it! Now we wait for a while (I recommend 1+ month) and we have a strong picture of which templates accomplish our goals and which do not. A/B testing has just prevented our ego from possibly rolling out something that worked against us. As well as giving us assurance that we are truly working toward our goals.