August 10th, 2010 → 7:00 am
by Robert Kingston
// No Comments
Displaying experiment data in Google Analytics from Google Website Optimizer is extremely straightforward to do. You might have found my other post on integrating Google Website Optimizer MVT with Google Analytics and were curious as to how you could integrate it with Google Analytics.
The truth is though, this may not be what you anticipated.
The short answer, you can get by without using fancy code.
So before you go ahead “willy-nilly” and implement random code about the place, be sure you know what you need to do first, then pick a method and stick with it.
On Version A of the page, you just need to modify the Analytics code as follows. From this:
<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">
try {
var pageTracker = _gat._getTracker("UA-1111111-1");
pageTracker._trackPageview();
} catch(err) {}</script>
To this:
<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">
try {
var pageTracker = _gat._getTracker("UA-1111111-1");
pageTracker._setCustomVar(1, "Test-1", "Original", 2);
pageTracker._trackPageview();
} catch(err) {}</script>
On Version B of the page, you would change the Analytics snippet as follows:
<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">
try {
var pageTracker = _gat._getTracker("UA-1111111-1");
pageTracker._setCustomVar(1, "Test-1", "Variation-1", 2);
pageTracker._trackPageview();
} catch(err) {}</script>
Simple as that. No fancy JavaScript, no mucking around.
One alternative that you can use on this is (which impacts your bounce rate metric) can be done by setting a custom variable and making a call to __utm.gif BEFORE the Google Website Optimizer Code conducts the redirect. It takes the experiment variable straight out of Google Website Optimizer and plugs it right into Google Analytics. Since the code redirects you to another page before your normal GA code will execute, we need to place more GA code within the GWO code.
You’ll notice that there are two separate scripts in the snippet of code you get from GWO’s Control Script it looks like:
<!-- Google Website Optimizer Control Script -->
<script>function utmx_section(){}function utmx(){}
(function(){var k='1111111111',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><script>utmx("url",'A/B');</script>
<!-- End of Google Website Optimizer Control Script -->
Therefore, all you need to do is insert some Google Analytics code in between these two scripts, like so:
<!-- Google Website Optimizer Control Script -->
<script>function utmx_section(){}function utmx(){}
(function(){var k='1111111111',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>
<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">
try {
var pageTracker = _gat._getTracker("UA-1111111-1");
if(typeof(utmx) == 'function'){
pageTracker._setCustomVar(1, "Test-1", "Variation-" + utmx('combination'), 2);
}
pageTracker._trackEvent("Experiment","Live","",1);
} catch(err) {}</script>
<script>utmx("url",'A/B');</script>
<!-- End of Google Website Optimizer Control Script -->
Note that I have changed lines, 21 through to 24 to track the custom variable and submit it with a call to _trackEvent, IN PLACE OF _trackPageview. Obviously, you can customise the custom variable code to suit your needs:
There you have it, three simple (well maybe they’re not all simple) and straight forward ways to integrate your tests with Google Analytics.
Show it to your friends:
Tweet