skip to main content

Division of Marketing
& Communications

Advanced Google Analytics Tracking Codes

Visitors often assume that all our campus websites are part of one unified site. By adding these new tracking codes, even webmasters who already use Google Analytics can work with other webmasters and the Division of Marketing & Communications Web Services to better serve and refer visitors as they move between individual websites (sample report).Your existing Google Analytics profiles and reports will not be affected.

In addition to your own website information, your reports can be enhanced with data gathered from the main A&M website, www.tamu.edu. If you manage multiple sites, you can study the traffic between them and compare the effectiveness of each site. To set up your custom analytics profile, simply provide the Division of Marketing & Communications's Web Services team with a list of the websites that you want to track, as well as the Google Account that you will be using to log into your reports.

Tracking codes should be placed on each page of your website, ideally just before the ending </head> tag, or as near the top of the page as possible. This code won't slow down the loading of your page since it's asynchronous, so it doesn't need to be put near the bottom of the page, something which is otherwise recommended for better-performing Javascript code.

The generic code will track visitors to any website on the tamu.edu domain - no customization or Google Analytics account required.

The subdomain code and subdirectory code allow university webmasters to continue tracking visitors under their existing Google Analytics account, while still participating in the cross-site tracking project. Like the generic code, these codes will work for any university site: yourcollege.tamu.edu, yourcenter.tamu.edu, or yourcollege.tamu.edu/yourdepartment.

Subdomain Code

(http://yourcollege.tamu.edu/)
For websites that already have a Google Analytics account. Enter your tracking code and your domain. Place the code just before the ending </head> tag on each page.

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(
  ['_setAccount', 'UA-your-tracking-code'],
  ['_setDomainName', 'yourcollege.tamu.edu'],
  ['_trackPageview'],
  ['b._setAccount', 'UA-1617721-2'],
  ['b._setDomainName', '.tamu.edu'],
  ['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>

Subdirectory Code

(http://yourcollege.tamu.edu/yourdepartment/)
For websites that already have a Google Analytics account. Enter your tracking code, your domain, and your subdirectory. Place the code just before the ending </head> tag on each page. This code is identical to the subdomain code, except that the setCookiePath variable must be set to track subdirectories.

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(
  ['_setAccount', 'UA-your-tracking-code'],
  ['_setDomainName', 'yourcollege.tamu.edu'],
  ['_setCookiePath', '/your-department/'],
  ['_trackPageview'],
  ['b._setAccount', 'UA-1617721-2'],
  ['b._setDomainName', '.tamu.edu'],
  ['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>

Traditional Code

If you use the older, non-asynchronous tracking code, you can add a "bTracker" or "otherTracker" section, so you can participate in the cross-site tracking project.

<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">
var pageTracker = _gat._getTracker("UA-your-tracking-code");
pageTracker._setDomainName("yourcollege.tamu.edu");
pageTracker._trackPageview();

var bTracker = _gat._getTracker("UA-1617721-2");
bTracker._setDomainName(".tamu.edu");
bTracker._trackPageview();
</script>

Other Codes

More complex cross-domain tracking is also possible, such as multiple subdomains or even domains outside tamu.edu.

For more information, contact the Division of Marketing & Communications's Web Services team.