how to add google analytics tracking code to a website

Google Analytics is a tracking service from Google which allows you to track user behavior and interaction among other features in a website.  It just requires you to insert a few lines of custom JavaScript code or the Google Analytics tracking code as it is known, into your webpages that you need to track. You get plethora of statistics about your website performance and visitors.

As you might expect, you do need to sign up for Google Analytics if you haven’t already done so. I recommend using a Google account, as it makes it easier to include other marketing products such Adwords, Adsense and Webmaster tools.

Once you have signed up for the Analytics program, you get access to the analytics user interface. This interface provides you with all kinds of data and reports once you start tracking the pages in the website. This is also where you get access to the JavaScript code (the Google Analytics tracking code) that you need to insert into your webpages to get everything hooked up and working. To find the latest analytics code that you need, follow these steps.

GUI for Google analytics settings

Where to find the google analytics tracking code

  1. In the analytics user interface, click on the Analytics settings button. It is the gear icon that you find to the right of the menu bar.
  2. If you happen to have more than one account, then you will have to click on the appropriate account and the website/profile you are working on. The tracking code is profile and website specific, so make sure that you have correct website selected.
  3. Click on the “tracking code” tab on top
  4. Click on the “standard” tab next
  5. On the first section which says “What are you tracking“, select the appropriate selection for your scenario. Most likely you would select the first option, which is “a single domain“.
  6. In the second section, in the text box you will find the code that you will use on your website.
  7. Copy the code from the text box
Where to find Google Analytics tracking code in the UI

Where to insert the tracking code in the website

In your website, you have to insert the tracking code into each and every webpage that you would like to track. If you happen to have a lot of pages, it is not a easy task. Moreover there are some reasons why you should refrain from inserting the same piece of code into each of the webpages individually.

  • It is difficult to maintain in the long run as you will have to remember to insert the code into every new page you will create in future
  • If the code changes in future, as it most likely will, then you will have to change each and every page in your website
  • If you have many pages, then it is more likely that you will make a mistake and not realize it.

The best way to do it is to put it in a common or “include” file and then insert that file programmatically into every page.  Usually you should have an include file already where you put all the common code snippets, of course depending on how it is designed. This is probably the most efficient and easiest way to do it.

There are actually two versions of the code available, namely the synchronous/traditional version and the asynchronous version. The asynchronous version is the latest and the only version you should be using. There is no reason to use the older synchronous version and it is only supported for legacy reasons.  There are several advantages to using the latest asynchronous version.

  • Faster page loading compared to the previous version. The google tracking code itself is supposed to load faster without blocking the rest of the page.
  • Do not require additional code to track SSL or secure pages
  • Problems at the google servers should not affect the performance of your website
  • Better data collection even when the users leave early in the loading process.

Google recommends that you place this code just before the end of head tag (</head) in the webpage. It should still work wherever you put it, but it just gives a better performance and track even users who leave early before the page is fully loaded.

If you are using a CMS such as WordPress, then you do have other options available to insert tracking code such as plugins to make it a little more seamless. I still prefer to add the tracking code manually such it eliminates the need for a separate plugin in WordPress.