Analytics Market is a one-stop resource for anything you can do with Google Analytics. Tips and tricks, product news, API tools, and more!
Analytics Market Chart

Adding and Testing Google Analytics Tracking Code

Get the code on your site right the first time--and double-check it immediately.

One reason that Google Analytics has gained such rapid popularity is its ease of implementation. Google has kept the process straightforward enough that even many nontechnical website owners have been able to use it.

For most websites, simply copying and pasting the code that Google provides is sufficient. It's important to be able to test that code implementation immediately, though. Sometimes it's even necessary to go back into the account to grab the code again.

Where is the Code?

When you first create a profile, you are presented right away with a screen that instructs you to copy and paste the code into your website. Below those instructions is a text box with JavaScript code.

If you need to get that code again after that initial setup process, you'll need to hunt. First, go to the edit screen for the profile in question. From there, click "Check Status" in the upper-right corner of the screen. This brings you right back to the page with code and instructions for implementing it.

Where Does the Tracking Code Go?

The tracking code needs to be pasted into the HTML code for each page of your website. If you can't edit the HTML on any pages of your site, Google Analytics wont be able to track them. If you are using some type of template or CMS, there is likely a plugin or module you can install that will insert the code automatically. If not, there is probably a global footer file that will affect every page of your site.

Paste this code into the footer directly above the closing body tag. The code can conceivably be placed anywhere in the page. The recommendation is to place it as the very last element of the page just in case the load time is ever longer than desired. That way it won't affect visitor experience.

Exceptions

The standard implementation works for most websites. There are a few very common scenarios, however, that require some type of modification to the tracking code before you put it into your site. If your site is an ecommerce site, uses multiple subdomains or if it crosses multiple domains (e.g., during a checkout process), you will need to make some changes.

Besides these required changes, there are some more advanced modifications that can be made using built-in JavaScript methods that are outside the scope of this article.

Our tracking code generator creates custom code for the most common scenarios.

Testing the Implementation

Google Analytics may display a warning icon next to your profile indicating that the code is not set up correctly until it begins to receive data. Since data can take up to 24 hours to populate, that icon might bother you. Not too much confidence should be placed in it, though. It doesn't do any sophisticated error checking. Mostly it just waits for data to feed into the account.

There are a few places to look to make sure your code is implemented correctly no matter how long you've had it on the site or what modifications you make to it. If you know what to look for, you can use a variety of different methods. Otherwise, the easiest way to check is to use addons for Firefox that check a couple of things.

UA Number

Before anything else, make sure the UA number in the code is the same as the UA number for your profile. View the source of a page on your site and look for a line that looks like this:

pageTracker = _gat._getTracker("UA-31144-17");

That UA number should be identical to the one for your profile. Confirm by going to the list of your profiles in your Google Analytics account. The UA number is listed next to each one.

Cookies

Google Analytics sets four cookies on each visitors' machine by default. They are named __utma, __utmb, __utmc and __utmz. There are multiple add-ons for Firefox that will allow you to read the cookies on your browser. If any of these cookies are missing, are blank or are attributed to a funny domain name, then you know something is wrong. With a standard installation, though, they are either there and correct or they aren't there at all. It's usually very straightforward.

__utm.gif Request

The tracking code sends all of the information to your Google Analytics account via a file request from www.google-analytics.com named __utm.gif. It will have a long query string (question mark followed by variables) that contains all the pageview and visit information. You can use ad-blocking addons for Firefox to see whether or not that file is being requested. If it is, your code is working fine. It's gathering all the information, setting cookies and sending it all to your account.

If that file is not being requested, it means the information is not being sent to your account at all. This indicates an implementation error.

If you can see that ga.js is being requested from the same server but not __utm.gif, it indicates a problem in the second block of JavaScript. If neither are being requested, it indicates a problem with the first block of JavaScript. In either case it's usually a syntax error. Usually this is the result of modifications to the code that were done incorrectly or the code being reformatted by an email client. If you copy and paste the tracking code into an email, it may get reformatted when it's sent. If you must email the code, try copying and pasting into a text document and attaching that to your email.