Split Test and Log Data With PHP/MySQL

0 replies
I just wanted to share an easy way to split test something on a Wordpress site using PHP/MySQL and log the data so you can see which one does better. I used this to test two different subscribe buttons on my site that redirect the user to my YouTube Channel to subscribe. You don't need Wordpress to do this, it just happens to be what I use.


Set up Custom Split Test Wordpress Shortcode

There are a few steps to this process, but once you do it once, you can replicate it very easily for other types of split testing.

Since I use Wordpress on my site, the first thing I did was create a custom function in my functions.php file to implement a custom shortcode that I could use on my blog posts. functions.php is a file that gets called on every wordpress page, so you can use it to put your own custom functions. If you use the Thesis Theme, there's a separate file called custom_functions.php I believe...

Inside functions.php, I put this:
PHP Code:
<? //Custom YouTube Subscribe Button
function youtubesub(){
    if (rand(1, 100) > 50) { // display this 50% of the time
        ob_start(); ?>
        <a href="/wp-content/custom/youtube.php?button=1"><img src="/youtube_subscribe_button_1.jpeg" alt="Subscribe to me on YouTube" class="aligncenter" /></a><img src="//www.youtube-nocookie.com/youtubesubscribelink" style="display: none" />
        <? return ob_get_clean();
        exit();
    } else { // display this the other 50% of the time
        ob_start(); ?>
        <a href="/wp-content/custom/youtube.php?button=1"><img src="/youtube_subscribe_button_2.jpeg" alt="Subscribe to me on YouTube" class="aligncenter" /></a><img src="//www.youtube-nocookie.com/youtubesubscribelink" style="display: none" />        
                <?    return ob_get_clean();
        exit();
    }
}

add_shortcode( 'youtubesub', 'youtubesub');
?>

This is a simple IF ELSE statement that uses the rand() function to decide which block of code to use. Over time, it will show the first bit of code 50% of the time, and the second bit of code the other 50% of the time.

Put your custom code within ob_start(); ?> //Custom code to run <? return ob_get_clean();

In this case, I've created the shortcode [youtubesub] for wordpress. So wherever I want this button to show up in my wordpress blog post, I just write [youtubesub]. It will automatically randomize which button shows up each time the page is refreshed.

You need to start ob_start() in order to capture whatever code you want passed on to the browser, and return it to the function where you place the shortcode. This could be literally anything. A picture, text, HTML, more PHP, javascript, etc. If you want to track clicks, you'll need to have a link of some sort in there obviously. In this case, I want to figure out which subscribe button is more effective, so I'll be using an image, linked to my Youtube Subscribe page.

Create Custom PHP file to Log Clicks and Forward User to Page

Now you could leave it at that, if you had a way to track clickthroughs on whatever page you're linking to for each button. For instance, you could do this method for adsense ads, and just track the clicks through your adsense dashboard. (But I wanted another metric to see how many people click the button because even if they do, it doesn't mean they necessarily click subscribe once they get to the Youtube page.)

You'll notice, instead of linking directly to Youtube in the code above, I link to a custom PHP file I created for this split test : Youtube.php?button=1 -OR- Youtube.php?button=2

This file figures out which button was pressed by looking at the variable passed to it in the URL, (button=1 or button=2) , saves the info in the database, then forwards the user to the destination page. The user doesn't see anything other than the result page. The script basically executes on the web server while redirecting them to the destination page.

Youtube.php is below:
PHP Code:
<? 
// Get database connection
include $_SERVER['DOCUMENT_ROOT'].'/wp-content/custom/database.php';

//Figure out which button was pressed, and create a log of the impressions, vs clicks.
$button = $_GET['button'];

if($button == 1){
    //redirect to the Youtube Subscribe
    header( 'Location: http://www.youtube.com/subscription_center?user_id' ) ;
    
    // log button #1
    $sql_go = mysql_query('INSERT INTO custom_testing (test_id, value) VALUES (1, 1) ');
    
} elseif($button == 2){
    //redirect to the Youtube Subscribe
    header( 'Location: //www.youtube.com/subscription_center?user_id' ) ;

    // Log Button #2
    $sql_go = mysql_query('INSERT INTO custom_testing (test_id, value) VALUES (1, 2) ');
}
?>
The buttons I created link to this file, and use GET to pass the variable "button" to this script. So if it's button 1 it does one thing, and if it's button # 2 it does something else. In this case, they both get forwarded to the same page, I just log it into the database as which button was clicked. Now you have plenty of flexibility here, you could have each button go to a different page altogether, its up to you. But the whole point of this page is to log in the database which one was clicked, and forward the user to the destination page.

This is a real bare bones approach, you could definitely expand on this. But it's a good starting point for anyone wanting to split test stuff. Also, you'd have to have already set up a table in your MySQL database and know how to connect to your database, but that's beyond the scope of this post, lol.

I was thinking you could get really fancy and set up an "automated optimizer." You could create lets say 10 different options to show, but have it only test 2 at a time. After one week, you could run a CRON script that read the database to determine which one performed better, and swap the bad one for another option. It would test that for another week, then read the data again. After 10 weeks it would have tested all 10 against each other automatically and picked the best performer.

Hope you found this useful!
Thanks,
-Matt
#data #log #php or mysql #split

Trending Topics