Prevent Duplication of ga.js for Google Analytics (GA) and Google Website Optimizer (GWO)

252 Views Asked by At

It looks like the Google Analytics code is identical to Google Website Optimizer (GWO)*; both are sourced from http://www.google-analytics.com/ga.js. However, on GA and GWO admin panels, Google asks us to include (i.e. copy-paste) its respective tracking codes that both include the same reference to ga.js. Maybe just for instruction simplicity? So, I've seen multiple pages have redundant references to ga.js when they want to enable GA tracking and an A/B test at the same time.

My question is can we do the following? Hopefully, this would be useful for those who aren't really versed with JavaScript but need to configure GA and GWO.

<head>
<script>
var _gaq = _gaq || [];
_gaq.push(['gwo._setAccount', 'UA-xxxxxxx-x']);
_gaq.push(['gwo._trackPageview', '/yyyyyyyyyy/test']);
</script>
<!-- All other meta tags. -->

<!-- Let this script serves both for the regular GA tracking and GWO A/B test. -->
<script>
(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>
</head>

*GWO script is typically used for A/B testing.

1

There are 1 best solutions below

0
Open SEO On

Yes you can include the function snipet once, but you are missing the GA part of the configuration there.