Google Analytics

Domains & Cookies - Web Tracking (analytics.js)

This guide describes how analytics.js uses cookies and how to set the configuration for multi-domain sites.

  1. Overview
  2. Implementation
    1. Configuring Default Cookie Settings
    2. Automatic Cookie Domain Configuration
    3. Setting cookies on localhost
    4. Multiple Trackers on The Same Domain
    5. Modifying Cookie Expiration
    6. Getting the Client ID
    7. Disabling Cookies

Overview

By default, the analytics.js library uses a single cookie named _ga to store a unique client identifier (Client ID), which is a randomly generated number. Once the ID is generated it's stored in the cookie and is included with each hit / request sent to Google Analytics. This ID is later used by Google Analytics servers to calculate user, session, and campaign data.

The _ga cookie is a first-party cookie that can only be accessed by the domain on which the JavaScript is run. For certain implementations you might need to modify how Google Analytics sets it's cookie – this document will show you how.

Implementation

The default JavaScript snippet for analytics.js creates a tracker using the create method:

ga('create', 'UA-XXXX-Y');

This creates a new cookie where:

  • The cookie name is _ga.
  • The cookie domain is the website domain, with the www. prefix removed.
  • The cookie is set to expire 24 months (2 years) and the expiration is refreshed every time a hit is sent.

Configuring Default Cookie Settings

To modify any of these values, you pass the create method a configuration object as the last parameter. The configuration object is a plain old JavaScript object where each property is the name of the field you want to override. For example:

ga('create', 'UA-XXXX-Y', {
  'cookieName': 'new_cookie_name',
  'cookieDomain': 'mynew.domain.com',
  'cookieExpires': 60 * 60 * 24 * 28  // Time in seconds.
});

This will change the default cookie settings to their respective values.

Automatic Cookie Domain Configuration

ga('create', 'UA-XXXX-Y', 'auto');

Automatic Cookie Domain Configuration simplifies cross domain tracking implementations by automatically writing cookies to the highest level domain possible when the auto parameter is used. When used on the domain www.example.co.uk, it will try to write cookies in the following order:

  1. co.uk
  2. example.co.uk
  3. www.example.co.uk

Analytics.js will fail to write a cookie on co.uk but will succeed on example.co.uk. Since a cookie was succesfully written on a higher level domain, www.example.co.uk will be skipped.

Setting cookies on localhost

In some cases you might want to run analytics.js from a webserver running on localhost. To set analytics.js cookies, you need to disable the default cookie domain using:

ga('create', 'UA-XXXX-Y', {
  'cookieDomain': 'none'
});

Or more simply:

ga('create', 'UA-XXXX-Y', 'none');

Multiple Trackers on The Same Domain

The analytics.js library sets a single cookie with the unique Client ID so all trackers on the page should share this same cookie. If you are implementing analytics.js on multiple subdomains, or on different subdirectories on the same domain, you should:

  • Configure all trackers to set a cookie at the highest domain possible.
  • Omit the www and . (dot) prefix of the domain.

So if you have a site with 2 subdomains: one.example.com and two.example.com, you would configure both trackers as follows:

// Configuration for one.example.com
ga('create', 'UA-XXXX-Y', {'cookieDomain': 'example.com'});
// Configuration for two.example.com
ga('create', 'UA-XXXX-Y', {'cookieDomain': 'example.com'});

Modifying Cookie Expiration

Every time a hit / request is sent to Google Analytics servers, the cookie expiration time is updated to be the current time plus the value in the cookieExpires time (in seconds) into the future. For example setting:

ga('create', 'UA-XXXX-Y', {
  'cookieExpires': 60
});

Would set the cookie expiration time to 60 seconds (1 minute) into the future.

If you set the cookieExpires time to 0 (zero) seconds, the cookie turns into a session based cookie and expires once the current browser session ends:

ga('create', 'UA-XXXX-Y', {
  'cookieExpires': 0
});

Getting the Client ID

Developers should not directly access the cookie analytics.js sets as the cookie format might change without warning. This could lead to script errors and incorrect data. Instead, developers should use the get command to retrieve the clientId value when needed. Here's how to do this:

ga(function(tracker) {
  var clientId = tracker.get('clientId');
});

Once the analytics.js library has loaded, the function passed to ga will execute and the tracker parameter will contain a reference to the default tracking object. The tracker object is then used to get the clientId.

Disabling Cookies

By default, analytics.js uses a single cookie to persist a unique client identifier across pages. In some cases you might want to use your own storage mechanism and send data directly to Google Analytics without the use of cookies.

You can disable analytics.js from setting cookies using the following:

ga('create', 'UA-XXXX-Y', {
  'storage': 'none',
  'clientId': '35009a79-1a05-49d7-b876-2b884d0f825b'
});

When you disable cookie storage, you will have to supply your own clientId parameter except for the special case where you are using cross-domain linking parameters.







Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.