phpA/B setup options

This documentation is under development. If you have a problem and find something missing here check back later.

Setting the name of your A/B test

All of phpA/B’s code examples use “my_test” as a test name, but that doesn’t mean you have to also. Use any name that makes sense for your test. Setting the name of your test is easy.

The first required parameter of phpab() is the name of your test. Only letters, numbers, and underlines are allowed (no spaces).

$text_color = new phpab('text_color'); // test name will be text_color

Running phpA/B in “trial mode” to test and play

phpA/B’s “trial mode” is a great way to see if your variations are working correctly. Each time you refresh your web page while in “trial mode” your browser will display a random variation. Additionally, Google Analytics tracking for your A/B test will be turned off to prevent skewing your test results.

// Setting the optional second parameter to TRUE will set the test in "trial mode"
$my_test = new phpab('my_test', TRUE);

Running more than 1 test at a time

Running more than 1 test works with phpA/B, but there is something additional you have to do. Set a specific Google Analytics slot number to each test (1-5) using set_ga_slot(). Otherwise, both tests will run in slot 1 and confuse Google.

$text_color = new phpab('text_color');
$text_color->set_ga_slot('1'); // 1-5
$subscribe_text = new phpab('subscribe_text');

Creating variations with phpA/B

Using phpA/B with HTML

Wrap the HTML you want to run A/B tests on with {phpab} tags. HTML wrapped with {phpab test_name} tags will be replaced with a variation or left alone if the user is in the control group.

{phpab my_test}

My control content

{/phpab my_test}

Using phpA/B with CSS

phpA/B will automatically add the name of the current variation as a class name to the <body> tag. Simply prepend the class name to your CSS rules.

  color: black; /* control styles */

.phpab-my_variation p
  color: red; /* variation styles */

Using phpA/B with PHP logic

The function get_user_segment() returns the current variation name. You can use this function with if statements to create variations with PHP logic.

<?php if($my_test->get_user_segment() == 'first_variation') : ?>
<?php elseif($my_test->get_user_segment() == 'second_variation') : ?>
<?php else : /* control */ ?>
<?php endif; ?>

Using phpA/B with javascript

phpA/B will automatically add the name of the current variation as a class name in the format
to the <body> tag. Just use your CSS prepend the class name to your CSS rules

// using jQuery
$('.phpab-my_test p');

// using plain javascript

For advanced Google Analytics users

Forcing _setCustomVar() to a different slot

Using set_ga_slot() you can set the specific slot used by _setCustomVar().

Manually placing the _setCustomVar() call

phpA/B automatically adds the _setCustomVar() call directly before _trackPageview(). If that doesn't work for you, you can manually place the _setCustomVar() call.

Writing your own _setCustomVar() call

Anywhere you put {phpab test_name current_variation}—replacing test_name with your test’s name—phpA/B will output the name of the user’s current variation. This can be used to make your own _setCustomVar() call like so:

// traditional tracking:
pageTracker._setCustomVar(2, "my_test" "{phpab my_test current_variation}" 2);

// asynchronous tracking:
_gaq.push(["_setCustomVar", 2, "my_test" "{phpab my_test current_variation}" 2]);
Looking for a hosted A/B testing service? Try Visual Website Optimizer free for 30 days