Skip to content

Programmable Tests

Overview

A/B testing (or split testing) is a method to compare multiple variations of web page design or user-website interaction, in order to find out which version achieves the largest profit. You can test phrases, page elements (their appearance, layout, etc.), and even entire website themes against each other.

Setting Up

  1. Download the ABTest.php file and place it inside any folder on the server.

  2. Create the tests.php file and list all your A/B tests there. Place it inside the folder where the ABTest.php file is located.

    See an example of the tests.php file below:

    <?php
    
    return
    array(
        array(
            'id'        => 'ab_form_color', // The title of an AB testing campaign to be displayed in Roistat
            'variants'  => array(
                array( 'id' => 'dark'),  // The title of a variant to be displayed in Roistat
                array( 'id' => 'blue'),  // The number of variants is not limited
            ),
        ),
        array(
            'id'        => 'ab_main_title', // The number of campaigns is not limited
            'variants'  => array(
                array( 'id' => 'small'),
                array( 'id' => 'big'),
            ),
        ),
    );
    
  3. Add the library for A/B testing at the beginning of the page file:

    <?php
    
    require_once __DIR__ . '/ABTest.php'; // Path to ABTest.php
    

    Please note:

    In case the code isn't included at the beginning of the page, errors may occur if any other php code is located above yours.

  4. The next steps depend on whether your site is single-page or multi-page.

    Single-Page Website

    E.g. you wish to evaluate all the changes occurred after a heading on the website is replaced by a new one. In this case:

    <?php require_once __DIR__ . '/ABTest.php'; ?> <!-- The library is now added -->
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    
    ...
    
    <div class="div1">
      <!-- A campaign and variant titles from tests.php -->
      <?php if (ABTest::instance()->getTestValue('ab_title') === 'old'): ?>
        <h2>Old heading!<h2>
      <?php else: ?>
        <h2>New heading!<h2>
      <?php endif; ?>
    </div>
    

    Multi-page website

    If different pages are to run different tests, all the tests are to be marked as inactive by default. To do this, set ‘active’ => false in the tests.php file:

    <?php
    
    return
    array(
        array(
            'id'        => 'ab_title',
            'active' => false, //  <---
            'variants'  => array(
                array( 'id' => 'old'),
                array( 'id' => 'new'),
                array( 'id' => 'newest'),
            ),
        ),
    );
    

    Then, you should manually enable all the necessary tests for each page:

    <?php
    
    // Include this code at the beginning of a page
    define('ROISTAT_AB_AUTO_SUBMIT', false);
    require_once __DIR__ . '/ABTest.php'; // Path to ABTest.php
    ABTest::instance()->activateTest('ab_title'); // A campaign title from tests.php
    ABTest::instance()->activateTest('ab_second_test'); // If there are several tests on a page
    ABTest::instance()->submit();
    
    ...
    
    // Example:
    
    <div class="div1">
      // A campaign and variant titles from tests.php
      <?php if (ABTest::instance()->getTestValue('ab_title') === 'old'): ?>
        <h2>Old heading!<h2>
      <?php elseif (ABTest::instance()->getTestValue('ab_title') === 'new'): ?>
        <h2>New heading!<h2>
      <?php else: ?>
        <h2>The newest heading!<h2>
      <?php endif; ?>
    </div>
    

Compare Two Variations of the Website

When your goal is to compare two website variations, e.g. two landing pages, you should follow theses 2 steps:

  1. To store the original referrer in a cookie;

  2. To redirect from the first page to the second one without losing any link labels.

Two pages under one domain

If you wish to compare two website variations available under one domain, e.g. site.ru/lp1 and site.ru/lp2, follow these steps:

  1. Configure the variant to test in the tests.php file:

    <?php
    
    return
    array(
        array(
            'id'        => 'ab_two_versions', // ab_two_versions - the title of a test to be displayed in Roistat
            'variants'  => array(
                array( 'id' => 'old'),  // old - the title of a variant to be displayed in Roistat
                array( 'id' => 'new'),
        ),
    );
    
  2. Insert the following code in the first page (this version is the original one):

    <?php
    
    // Include this code at the beginning of a page
    define('ROISTAT_AB_AUTO_SUBMIT', false);
    require_once __DIR__ . '/ABTest.php'; // Path to ABTest.php
    ABTest::instance()->activateTest('ab_two_versions'); // The title of a test from tests.php
    ?>
    
    // The redirection is to start before the web page content is loaded, therefore the redirection code is to be inserted in the beginning of the code
    <?php if (ABTest::instance()->getTestValue('ab_two_versions') === 'new'): ?> // If the value is new, the redirection starts
        $abVariant = "?b=1";
        $param = !empty($_SERVER['QUERY_STRING']) ? "&" . $_SERVER['QUERY_STRING'] : ""; // All the link labels are stored here
    
        setcookie("roistat_referer", isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "");
        header("Location: http://site.ru/{$abVariant}{$param}"); // Change the web address site.ru to a new one where the user is to be redirected to
        exit();
    <?php endif; ?>
    <?php ABTest::instance()->submit(); ?>  
    
  3. Insert the following code in the very beginning of the redirection page:

    <?php if ($_GET['b'] === '1'): ?>
    <?php  
        setcookie('ab_two_versions', 'new', time()+14*24*60*60, '/');
        setcookie('roistat_ab', 'ab_two_versions:new', time()+14*24*60*60, '/');
    ?>
    <?php endif; ?>
    

View the Results

To view the results, open the project, go to the A/B-tests page, and click on the test name.

The results screen will help you to understand which version is more profitable.

12.png