Analytics Tracking Code

Peter Roesler, President - Web Marketing Pros

By Peter Roesler

President, Web Marketing Pros

google-tracking-code-changeThe new Analytics Tracking Code has the latest asynchronous syntax. With this version of tracking code, you can place the Analytics snippet higher in the page without delaying subsequent content from rendering.

The Analytics snippet is a small piece of JavaScript code that you paste into your pages. It activates Google Analytics tracking by inserting ga.js into the page. To use this on your pages, copy the code snippet below, replacing UA-XXXXX-X with your web property ID. Paste this snippet into your website template page so that it appears before the closing </head> tag. The _gaq object is the thing that makes the asynchronous syntax possible. It acts as a queue, which is a first-in-first-out data structure that collects API calls until ga.js is ready to execute them. To add something to the queue, use the _gaq.push method.

To push an API call onto the queue, you must convert it from the traditional JavaScript syntax into a command array. Command arrays are simply JavaScript arrays that conform to a certain format. The first element in a command array is the name of the tracker object method you want to call, which must be a string. The rest of the elements are the arguments you want to pass to the tracker object method. These can be any JavaScript value. In the asynchronous syntax, the creation of the tracker object is implied, but users still need a way to set the web property ID for the tracker. The _setAccount method has been added to provide this capability. All the other tracker object methods are the same in both asynchronous and traditional tracking with just the syntax being different.

In addition to command arrays, you can also push function objects onto the _gaq queue. The functions can contain any arbitrary JavaScript and like command arrays, they are executed in the order in which they are pushed onto _gaq. This technique is useful for calling the tracking APIs that return values. If you prefer to put the Analytics snippet at the bottom of the page, you should know that you don’t have to put the whole snippet at the bottom. You can still keep most of the benefits of asynchronous loading by splitting the snippet in half and keeping the first half at the top of the page and moving the rest to the bottom. Because the first part of the tracking snippet has little to no effect on page rendering, you can leave that part at the top and put the part of the snippet that inserts ga.js at the bottom.

This is a simple overview of the newly implemented tracking code and there are still much more in depth descriptions on each function. However, before it is looked over, there are some things to watch out for. There are common mistakes that can occur, such as people forgetting that method names are case-sensitive, entering the wrong method name (possibly due to a typo), not realizing that only strings should be passed with quotes, and not knowing that strings cannot contain leading or trailing whitespace.

The new implemented tracking code is a great bonus to the Analytics page.


Share This Article