A/B testing on WordPress: a framework for developers and tutorial

Some months ago, I changed one link in the menu in my website postpaycounter.com. After that, it looked to me more people were purchasing products, i.e. the conversion rate had increased. But how to check whther that was really the case, or if it was just an accident/impression? Use an A/B test, I told myself!

With an A/B test, half of the users are served one version of the page, the one with the old link, and half of them another version of it, the one with the new link in place. When a sale happens, we may then log that as a success for the kind of page that was used, be it the A version or the B one.

In my case, the two versions of the page simply consisted of two different links in the menu, while I wanted the success to be logged when the user purchased something (I use Easy Digital Downloads to handle purchases).

I could find a bunch of plugins that allowed to set up A/B tests, but they all seemed pretty difficult to customize from a developer perspective, and I was already seeing myself wrestling with someone else’s code that provide tons of features useless to me, but through which was nearly impossible to interact with Easy Digital Downloads. So I decided to build my own, simple implementation, with the aim of it being tailored to developers rather than users who needed an interface. 

An A/B test implementation example

This is an example of how to use the little framework. To set up a test, you only need to provide two functions:

  • A_function specifies what should happen to the page that is served to the first half of visitors. In my case, I attach a filter to edit the navigation menu with a custom function.
  • B_function works the same for specifying what should happen to the second version of the page. Again, I attach a filter to the navigation menu.

At this point, you can set up a test object, through which you will be able to retrieve the almost-unique alphanumeric test ID (it’s a MD5 hash of the setup arguments).

Finally, you need a third function that is going to log successes. You should probably have it attached to some action or filter. In my case, I used an action that fires when a purchase through Easy Digital Downloads happens. The only requirement about the test function is that it should call AB_log_success( $test_id ), that will handle the under-the-hood success logging, basically updating the wp_option (AB_tests) containing the stats.

For the full code, see the GitHub repo!

Leave a Reply

Your email address will not be published. Required fields are marked *