Cookies

The gtag.js library uses cookies to identify unique users across browsing sessions. This page explains how to customize the cookie settings.

The following table shows the default cookie field values used by gtag.js:

Field name Value type Default values
cookie_domain string auto
cookie_expires integer For Analytics: 63072000 (two years, in seconds). For conversions: 7776000 (90 days, in seconds).
cookie_prefix string For conversions: _gcl. Note: Analytics cookies do not use a default prefix.
cookie_update boolean true

To change any of these values, update the gtag config command to specify them in the parameter list. For example:

gtag('config', '<TARGET_ID>', {
  'cookie_prefix': 'MyCookie',
  'cookie_domain': 'blog.example.com',
  'cookie_expires': 28 * 24 * 60 * 60  // 28 days, in seconds
});

By default, gtag.js has automatic cookie domain configuration enabled. When enabled, gtag.js will set cookies on the highest level domain it can. For example, if your website address is blog.example.com, gtag.js will set cookies on the example.com domain. If gtag.js detects that you're running a server locally (e.g. localhost), it automatically sets the cookie_domain to 'none', which will cause gtag.js to set cookies using the full domain from the document location.

To turn off automatic cookie domain configuration, add a cookie_domain parameter to config:

gtag('config', '<TARGET_ID>', {
  'cookie_domain': 'blog.example.com'
});

To avoid conflicts with other cookies, you may need to change the cookie prefix. For example, this code will prepend "example" to the cookie name (e.g. example_ga, example_aw, etc.:

gtag('config', '<TARGET_ID>', {
  cookie_prefix: 'example'
});

When a page loads, the cookie expiration time is updated to be the current time plus the value of the cookie_expires field. This means that if cookie_expires is set to one week, and a user visits using the same browser within five days, the cookie will be available for an additional week, and they will appear as the same visitor in Google Analytics. If that same user instead visited after the original cookie had expired, a new cookie will be created, and their first and second visits will appear as coming from distinct visitors in Google Analytics.

If you set the cookie_expires value to 0 (zero) seconds, the cookie turns into a session-based cookie and expires once the current browser session ends.

gtag('config', '<TARGET_ID>', {
  cookie_expires: 0
});

When cookie_update is set to true (the default value), gtag.js will update cookies on each page load. This will update the cookie expiration to be set relative to the most recent visit to the site. For example, if cookie expiration is set to one week, and a user visits using the same browser every five days, the cookie expiration will be updated on each visit and so will effectively never expire.

When set to false, cookies are not updated on each page load. This has the effect of cookie expiration being relative to the first time a user visited the site.

gtag('config', '<TARGET_ID>', {
  cookie_update: false
});