Cookies and user identification with gtag.js

gtag.js 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 value
cookie_domain string auto
cookie_expires integer 63072000 (two years, in seconds)
cookie_prefix string _ga
cookie_update boolean true
cookie_flags string

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

gtag('config', 'GA_MEASUREMENT_ID', {
  'cookie_prefix': 'MyCookie',
  'cookie_domain': '',
  '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, gtag.js will set cookies on the 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, update the config for your property to specify a value for the cookie_domain parameter:

gtag('config', 'GA_MEASUREMENT_ID', {
  'cookie_domain': ''

To avoid conflicts with other cookies, you may need to change the cookie prefix, which will be prepended to cookies set by gtag.js. For example, the default name of the ID cookie used by Google Analytics is _ga. This code will cause the cookie to be named example_ga instead:

gtag('config', 'GA_MEASUREMENT_ID', {
  'cookie_prefix': 'example'

On each page load, 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', 'GA_MEASUREMENT_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', 'GA_MEASUREMENT_ID', {
  'cookie_update': false

Appends additional flags to the cookie when set. Flags must be separated by semicolons.

gtag('set', {
  'cookie_flags': 'SameSite=None;Secure'

Set User ID

A User ID is a unique, persistent, and non-personally identifiable ID string that represents a user. It enables the analysis of groups of sessions across devices. To learn why you should implement the User ID, see Benefits of using the User ID feature.

To implement the User ID with gtag.js, update the config for your property to set the User ID:

gtag('config', 'GA_MEASUREMENT_ID', {
  'user_id': 'USER_ID'

You can configure gtag.js to not read or write cookies until consent is provided from the user. To learn more see Adjust tag behavior based on consent