Brief overview of Google Analytics tracking code and how to set it up properly
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.
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>
|
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!
Anyone can tell the difference in reporting that the new code brings about?
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!