How To Create an A/B Test
Creating A/B tests with LiteCart can be done very simple. Here is how you can randomize the testing of different variations and report the results to an analytics service like Google Analytics or Matomo.
// Determine if we need to assign a variation (A, B [, or C])
if (empty(session::$data['ab_tests']['test1'])) {
$variations = ['a', 'b', 'c'];
session::$data['ab_tests']['test1'] = $variations[array_rand($variations)];
}
// Do variation
switch (session::$data['ab_tests']['test1']) {
case 'a':
// Do something for A
break;
case 'b':
// Do something for B
break;
case 'c':
// Do something for C
break;
}
Report Variation with Google GTag:
<script>
gtag('event', 'experiment_impression', {
'experiment_id': 'test_1',
'variant_id': '<?php echo session::$data['ab_tests']['test1']; ?>'
});
</script>
Report Variation with Matomo:
<script>
_paq.push(['AbTesting::enter', {
experiment: 'test_1',
variation: '<?php echo session::$data['ab_tests']['test1']; ?>'
}]);
</script>