Onclick Event as a Conversion With Google Website Optimizer

Often in testing scenarios the conversion is either gong to take place off site or the goal is a download and you therefore can’t install the tracking code.

To test that you can use the onclick event in the link(s) to record the click as the conversion. I’d tried Google’s suggestion previously and couldn’t get it to work for me.

With some experimentation I found the following code works using Google Website Optimizer and Google Analytics with one page being both the test experiment page and the conversion / success / goal page.

In this opening section we include the GWO control script, and a function for the onclick conversion count.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<!-- Google Website Optimizer Control Script -->
<script>
function utmx_section(){}function utmx(){}
(function(){var k='xxxxxxxxxx',d=document,l=d.location,c=d.cookie;function f(n){
if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.indexOf(';',i);return c.substring(i+n.
length+1,j<0?c.length:j)}}}var x=f('__utmx'),xx=f('__utmxx'),h=l.hash;
d.write('<sc'+'ript src="'+
'http'+(l.protocol=='https:'?'s://ssl':'://www')+'.google-analytics.com'
+'/siteopt.js?v=1&utmxkey='+k+'&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime='
+new Date().valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+
'" type="text/javascript" charset="utf-8"></sc'+'ript>')})();
</script>
<!-- End of Google Website Optimizer Control Script -->
<script type="text/javascript">
function ConversionCount() {
gwoTracker._trackPageview("/xxxxxxxxxx/goal");
return true;
}
</script>

The link we wish to count as the conversion has the onclick event added.
<p><a href="http://www.onlinesales.co.uk/" onclick="return ConversionCount();">Here's the link we're testing</a>.</p>

Finally at the bottom of the page we have the usual Google Analytics code, plus the modified GWO tracking code.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-yyyyyy-yy");
pageTracker._trackPageview();
</script>
<!-- Google Website Optimizer Tracking Script -->
<script type="text/javascript">
if(typeof(_gat)!='object')document.write('<sc'+'ript src="http'+
(document.location.protocol=='https:'?'s://ssl':'://www')+
'.google-analytics.com/ga.js"></sc'+'ript>')</script>
<script type="text/javascript">
// changed the tracker here
try {
var gwoTracker=_gat._getTracker("UA-zzzzzzz-zz");
gwoTracker._trackPageview("/xxxxxxxxxx/test");
}catch(err){}</script>
<!-- End of Google Website Optimizer Tracking Script -->
</body>
</html>

Notice that in the example above, xxxxxxxxxx is supplied by GWO as the goal, yyyyyy-yy is the UA code from Analytics and zzzzzzz-zz the account code from Website Optimizer, which for some reason differs for me.

One thought on “Onclick Event as a Conversion With Google Website Optimizer

Comments are closed.