Universal Conversion Code for Google Website Optimizer

I was reading a post over at ROIRevolution and found an awesome code that allows you to use one snippet of code to track all of your experiments, so I thought I’d share it here. This basically looks at your cookie and pulls out the proper conversion ID then inserts it into the code dynamically using javascript. Here’s the code:

<script type="text/javascript"> function readCookie(name) { // function to read cookie var nameRegex = RegExp("(?:; |^)" + name + "=([^;]+)"); nameValue = nameRegex.exec(document.cookie); if(nameValue) { return nameValue[1]; } else { return null; } } </script>  
  
<script type="text/javascript"> (function () { try { _gaq.push(['gwo._setAccount', 'UA-XXXXXXX-Y']); var utmx = readCookie("__utmx"); var pieces = utmx.split(/(?:^|:)[^.]*(?:.|$)/); for (i = 0; i < pieces.length; i += 1) { if (pieces[i]) { _gaq.push(['gwo._trackPageview', '/' + pieces[i].substring(10) + '/goal']); } } } catch (err) { } })();  
  
(function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script>

This code should replace all your current GWO-related code on your conversion page.


Recommended For You



© Convertify 2012

Mobile Analytics