Manage Google Analytics

Set up web analytics

How to set up the web tracking code


BEFORE YOU BEGIN: To set up tracking, you need to have access to the source code for your website. If you're already tracking your website using an older tracking snippet, remove that snippet from each page before you add the new one. If you have customizations you want to add back to your pages after you add the new snippet, you can use a text or HTML editor to open and save a copy of each file. (Looking for the traditional snippet? See Using the Traditional Tracking Code Snippet

The following instructions describe how to add the tracking snippet to your site. They assume two things:

  1. First, find the tracking code snippet for your property.
    1. From any Analytics page, click Admin.
    2. Select the property you want to track.
    3. Check that the URL at the top matches the one for your website.
      If the settings are showing the wrong web property, click the link in the breadcrumb trail for your account, and select the correct property from the list.
    4. Click the Tracking Info tab.

    You'll see something similar to the code snippet below, where XXXXX-Y indicates the property ID.

    <script type="text/javascript">
    
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXX-Y']);
      _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>
  2. Turn on the tracking options you want, then click Save.
  3. Copy and place the code snippet
    Once you find the code snippet, copy and paste it into your web page, just before the closing </head> tag*. If your website uses templates to generate pages, enter it just before the closing </head> tag in the file that contains the <head> section. (Most websites re-use one file for common content, so it's likely that you won't have to place the code snippet on every single page of your website.)

    For the best performance across all browsers we suggest you position other scripts in your site in one of these ways:
    • before the tracking code snippet in the <head> section of your HTML
    • after both the tracking code snippet and all page content (e.g. at the bottom of the HTML body)
  4. Verify and customize

    Verify that your tracking code contains the correct web property ID for your profile (it should if you copied it from the Profile Settings screen). Double-check that the tracking snippet installed on your website matches the code shown in the profile. For more details on verifying your setup, see Verifying your setup.

    Add any customizations back in using the asynchronous syntax. The Usage Guide and Migration Examples (English only) on Google Code provide many examples of customizations with asynchronous tracking.

*One of the main advantages of the asynchronous snippet is that you can position it at the top of the HTML document. This increases the likelihood that the tracking beacon will be sent before the user leaves the page. It is customary to place JavaScript code in the <head> section, and we recommend placing the snippet at the bottom of the <head> section for best performance.

For sites and apps using Universal Analytics, some information about data collection might be different. Find out more about Universal Analytics.