Server Side Testing with PHP


This is a sample of PHP code with the purpose to enable AB testing server side. With this code, you should be able to AB test changes, on the server side, before the page has been served to the browser.

Sample Code

This code will require that you create a normal Convert AB experiment, and do a small change on it, to be able to save it.

As a suggestion, go into the Visual Editor and under your Variation Menu > Custom Javascript enter the following:

// This is the variation.

With that change, you should be able to save it, and use that experiment to collect data, and analyze it using its report.

Make sure you enter a condition that wont be able to be triggered in the test. You could just use a JS Condition with the value false to never trigger it another way.

To start you will have to find out the Project Number and Experiment id. You need this to include it in the code.

This is way more efficient than getting them via an API, as this would delay the processing of the code. However, if this is what you need, we have an API available for you to use.

This code has been fully verified to be deployed with relative few effort.

// Set the project id and experiment id
$experimentid = '100122624';
$sel_variation = 0;
$unknownvseisitor = false;
$currentTime = time();
$defaultCookieData = "vi:1*sc:0*cs:$currentTime*fs:$currentTime*pv:0";

// echo 'Cookie Exists:'.(isset($_COOKIE['_conv_v'])).'<br/>';
if (!isset($_COOKIE['_conv_v']))

echo 'Convert Cookie named _conv_v is not set!';
$unknownvisitor = true;

// Get the variations from the Convert server
$projectdataURL = ''.PROJECTNUMBERID.'.json';
$output = file_get_contents($projectdataURL,0,null,null);
//echo $output;
// if request went well, extract the variations from the returned data and select the variation randomly
if ($output) {
$projectdata = json_decode($output,true);
$experiments = $projectdata['experiments'];
$ourexperiment = $experiments[$experimentid];
$variations = $ourexperiment['vars_sort'];
// Use array_rand to select the variation randomly
if (sizeof($variations) > 0) {
$sel_var_key = array_rand($variations, 1);
$sel_variation = $variations[$sel_var_key];
// echo "Variation Key Selected:'$sel_var_key\n";
// echo "Variation Selected:'$variations[$sel_var_key]\n";
$cookieData = $defaultCookieData;
// echo 'Convert Cookie is set! Lets read if we have <br />';
// Check if user is bucketed in experiment
$convertcookie = (string)$_COOKIE['_conv_v'];
// echo 'Convertcookie:'.$convertcookie.'<br/>';
// Extracts the experiment from the cookie
$sub = substr($convertcookie, strpos($convertcookie,'100122624.{v.')+strlen('100122624.{v.'),strlen($convertcookie));
$extractedvariation = substr($sub,0,strpos($sub,'-g.{'));
// echo 'Extracted Variation:'.$extractedvariation;
if ($extractedvariation){
$sel_variation = $extractedvariation;

$cookieData = $_COOKIE[$convertCookieName];

// Set the Convert Cookie to overcome the ITP cookie browser limits
setrawcookie("_conv_v",$cookieData, $currentTime + 15768000, "/", "");
// Set the Selected Variation on the cookie
$sptestcookiecontent = $experimentid.':'.$sel_variation;
setrawcookie('_conv_sptest',$sptestcookiecontent, $currentTime + 300, "/", "");

//Insert the Convert tracking code in case you have not done so.
echo '<!-- begin Convert Experiences code--><script type="text/javascript" src="//'.PROJECTNUMBERID.'.js"></script><!-- end Convert Experiences code -->';

// Execute changes depending on the variation
echo 'Variation:'.$sel_variation.'<br>';

if ($sel_variation == '1001176189'){

// Insert code for corresponding variation
echo 'First Variation Selected<br>';

elseif ($sel_variation == "1001176190"){
// Insert code for corresponding variation
echo 'Second Variation Selected<br>';


If you have any questions, let us know through the support channels available to you.

Need assistance? Start a conversation with our Support Team.

Reliably Convert More Site Visitors, Even If Everything Else is Changing in Your Business.

✓ No Credit Card  ✓ Easy Set-Up  ✓ Access All Features