GWO Dynamic Section Variations Function

Here's a PHP function to speed up the entry of GWO server-side dynamic section variations

I've recently began working heavily with Google Website Optimiser, particularly on the technical and advanced side of testing, inline with improving the effectiveness our our e-commerce websites for clients.

On my travels I recently discovered the blog of Eric Vasilik, the techincal lead of GWO. Particular of interest is this article which demonstrates an ingenious method of being able to dynamically generate variations in GWOs multivariate tests (a common problem for many testers).

To both speed up and clean up the process of working with this method I wrote the following PHP script that you may find useful.

The script

<?php
/*
This function allows the insertion of dynamic sections as explained in this article: http://www.gwotricks.com/2009/05/server-side-dynamic-section-variations.html
It accepts a minimum of 3 arguments, the first MUST be the section name. All others thereafter are section values
*/
function insert_gwo_sections() {

	if (func_num_args() < 2) return false;
	
	$arguments = func_get_args();
	
	static $section_number;
	$section_number++;
	
	$section_name = $arguments[0];
	array_shift($arguments);
	
	ob_start();
	foreach ($arguments as $key=>$section_value) {
	
		if ($key==0) { ?>
			<!-- utmx section name="<?php echo $section_name; ?>" -->
			<script>
				var GWO_Section<?php echo $section_number; ?> = utmx("variation_number", "<?php echo $section_name; ?>");
				if (GWO_Section<?php echo $section_number; ?> != undefined && GWO_Section<?php echo $section_number; ?> != 0) document.write('<no' + 'script>');
			</script>
			<?php echo $section_value; ?>
			</noscript>
		<?php }
		else { ?>
			<script>
				if (GWO_Section<?php echo $section_number; ?> == <?php echo $key; ?>) document.write('</noscript a="');
			</script><!--">
			<?php echo $section_value; ?>
			<script>document.write('<'+'!'+'-'+'-')</script>-->
		<?php }
	}
	return ob_get_clean();
}
?>

Example Usage

This function accepts a minimum of 3 arguments. The first must be the name of the section. The following arguments (however many you require) are the variations you require. The function will sort everything out for you, leaving you more time to design your test and requiring less time to implement it. An example of a multivariate testing using the above function can be seen below:

<?php
$name = 'Steve';
echo insert_gwo_sections('headline','<h2>Hello '.$name.'</h2>','<h2>Howdie, '.$name.'!</h2>','<h2>Hi there '.$name.'</h2>');
?>

I originally posted this article on my business blog, found here.

Dated: 25/11/2010. Filed under: Google Website Optimiser.


Journal Archive

Search the archives of this website for design and web-related articles.

Search Archive:

Miscellaneous

Other fluff that doesn't fit within any other category.

XHTML | CSS | 508