How to track website with multiple Google Analytics accounts

We have built the product that our clients can easily build website and feed content into it without requiring any technology background.  Our client is using Google Analytics to measure user interaction with website.  We also want to track  all our websites.  The question was how can we collect page views in multiple Google Analytics accounts, one is in our GA account and another is in our client’s GA account?  After doing some research we have found the solution and it is pretty easy.

Here is what Google Analytics tracking code looks like.

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  (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>

If you closely look at the code, Google Analytics account Id ‘UA-XXXXX-X’ and method name “_trackPageView” are pushed into array variable called _gaq.  Please refer this document if you want to understand how this tracking code works.    What we need to do here is that is to set another Google Analytics account Id and push into that array.  Here is how you could do that.

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  _gaq.push(['b._setAccount', 'UA-XXXXX-1']);  _gaq.push(['b._trackPageview']);

  (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>

That’s it. You will start seeing all page views being tracked in both Google Analytics account Ids. If you are aggregating user interactions in one Google Analytic account, you may need to filter content by domain name. You could do that by selecting secondary dimension: content -> hostname. Here is the screenshot how you could choose that.

secondary domain hostname

Hope this post helps you.

Posted in Microsoft Technology

Ads