Install New Google Analytics Tracking Code

Brief overview of Google Analytics tracking code and how to set it up properly


Google analytics tracking code

Google Analytics (GA) is a web analytics tool that works based on the page tagging method. To set up Google Analytics on your website, you need to tag individual web page with the tracking code (written in Javascript). When a tagged web page is open in the web browser, the tracking code is triggered and begins to collect web traffic data. The tracking code, thus, is the center of Google Analytics. Properly installing it on your site will be the first essential step in using this application.

Google Analytics tracking code changes over time

Like everything else, Google Analytics has changed over time, and so has the tracking code. The latest version of the tracking code is the asynchronous tracking code. Before that, we had the traditional tracking code, and even before that we had the Urchin tracking code.

If you have just started using Google Analytics, you will most likely use the latest asynchronous version. However, if you inherit the website from ex- web designer/employee, it’s possible your site was tagged with the traditional tracking code. If you’re not clear, continue reading to see where you can find the tracking code in the new Google Analytics interface, what tracking code your site is using, whether it’s easy to switch to the latest version and how to set it up.

Google Analytics tracking code versions and how to use

Asynchronous tracking code Traditional/legacy tracking code

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

<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-XXXXX-X”);
pageTracker._trackPageview();
</script>

Install asynchronous tracking code Install traditional/legacy tracking code

In the new Google Analytics interface, the tracking code can be found by clicking on the Admin located at the top right of the orange bar. In the past, Google used the Settings/Gear wheel icon for this.

Copy and paste the tracking code to right above the closing head tag </head> in all web pages that you want to track

Copy and paste the tracking code to right above the closing body tag </body> in all web pages that you want to track

Track event and pageview with asynchronous code Track event and pageview with traditional/legacy code

Track event
<a href=”URL” onclick=”_gaq.push([‘_trackEvent’, ‘category’, ‘action’, ‘opt_label’, opt_value]);”>Track event</a>

Track pageview
<a href=”URL” onclick=”_gaq.push([‘_trackPageview’, ‘virtual path’]);”>Track pageview</a>

Track event
<a href=”URL” onclick=”pageTracker._trackEvent(‘category’,’action’, ‘opt_label’);”>Track event</a>

Track pageview
<a href=”URL” onclick=”pageTracker._trackPageView(‘/virtual path’);”>Track pageview</a>


Should you switch to the latest tracking code?

As you can tell from above, the asynchronous and legacy tracking codes are coded differently. As a result, the javascript onclick event to track event and track pageview are also different. Thus, although it’s recommended to use the latest and greatest tracking code by switching to the asynchronous version, you need to make sure you’ll have time to update all onclick events accordingly. If you forget updating the onclicks, your event and pageview tracking will no longer work.

Also, the recommended place to put the asynchronous tracking code is above the closing </head> tag (not above the closing </body> tag as in the legacy version), this means that when switching, you’ll need to move the tracking from the bottom of the page to the top of the page. Depending on how your site is set up, this may require the permission/right to access the page template header and footer. Many times, it’s not as easy as it sounds.

Google has a wealth of information on its website. Here is the details on how to set up the tracking code (asynchronous), if you want more information.


Hope this is helpful!


Related Posts:




3 Responses to Install New Google Analytics Tracking Code

  1. I found this info a little too late. I changed from the legacy tracking code the asyn version and forgot changing the track event and track pageview onclicks and missed a month worth of data!

  2. Thanks for pointing out the difference between the new and old Google analytics tracker. My site is using the old tracker and I tried to tag my links as with the new one, so it didn’t work. Now I know why.
    Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *