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.
<!-- Google Website Optimizer Control 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="'+
+new Date().valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+
'" type="text/javascript" charset="utf-8"></sc'+'ript>')})();
<!-- End of Google Website Optimizer Control Script -->
<script type="text/javascript">
function ConversionCount() {
return true;

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 type="text/javascript">
var pageTracker = _gat._getTracker("UA-yyyyyy-yy");
<!-- Google Website Optimizer Tracking Script -->
<script type="text/javascript">
if(typeof(_gat)!='object')document.write('<sc'+'ript src="http'+
<script type="text/javascript">
// changed the tracker here
try {
var gwoTracker=_gat._getTracker("UA-zzzzzzz-zz");
<!-- End of Google Website Optimizer Tracking Script -->

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.

Author: Online Sales

Owner of Mega Products Ltd. Everyday superhero web-bloke.

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

Comments are closed.