skip to main content

TAMU Webmaster's Blog


Information and insight from the A&M Webmasters

Crossing domains with Google Analytics

June 9th, 2010 by mdmcginnis

Webmasters are resigned to knowing little about their visitors before they arrive and losing track of them as soon as they leave. But we’ve recently found a way to use the new asynchronous Google Analytics code to track visits between websites that you own.

To make the code work, you’ll need to create a custom “Display Subdomain” filter for your Google Analytics reports, one that adds the host name to the file name. Otherwise, for example, you couldn’t distinguish aero.tamu.edu/index.php from nuclear.tamu.edu/index.php:
Subdomain tracking filter for Google Analytics

Sample cross-domain tracking code

This script will work for any site on the .tamu.edu domain. Just edit it to include your own tracking code. Because it’s asynchronous, you can place the script just before the ending </head> tag on each page of your site, and then it can track visitors who exit before the page finishes downloading. The new asynchronous code also works on secure sites without modification.

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(
  ['_setAccount', 'UA-your-tracking-code'],
  ['_setDomainName', '.tamu.edu'],
  ['_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 want to also track two or more sites separately as well as together, you’ll need to create separate profiles for each one, but using the same tracking code. You can reuse the “Display Subdomain” filter (above) for each of them, but you must create a second custom subdomain filter specifically for that subdomain. Here’s the filter we created for the university’s Web Accessibility website.
Cross-domain filter for Google Analytics
Let us know how it works for you, or if you have any questions.

Tags:

Wednesday, June 9th, 2010 Analytics
Share this article

No comments yet.

Leave a comment

Categories

Archives