The global site tag (gtag.js) is now the Google tag. Learn more

Cookies and user identification

Google tags set and read cookies to identify unique users across browsing sessions. Cookies are small files saved on peoples' computers to help store preferences and other information that's used on web pages that they visit.

Google uses cookies in various ways. Remarketing and Google Analytics both use cookies to help do things like run your ads or measure your success. The conversion measurement features of Google Ads and Campaign Manager also use cookies. To help you measure sales and other conversions from your ad, a cookie is added to a person's computer when the person clicks an ad. Refer to the advertising and measurement cookie reference for a detailed list of cookies used by Google.

For most websites and apps, cookie storage should also be controlled by user consent. This page explains how to customize cookie settings with the Google tag and Google Tag Manager. Manage user privacy introduces the available options for managing user consent.

You can also manage cookie expiration and cookie update settings using the Analytics interface. To learn more, see Change default cookie settings.

By default, Google tags use automatic cookie domain configuration. Cookies are set on the highest level of domain possible. For example, if your website address is blog.example.com, cookies are set on the example.com domain. If it is detected that you're running a server locally (e.g. localhost), cookie_domain is automatically set to 'none', and cookies will use the full domain from the document location.

To make changes to the cookie domain configuration, follow these steps:

gtag.js

To turn off automatic cookie domain configuration, update the config for your tag to specify a value for the cookie_domain parameter:

gtag('config', 'TAG_ID', {
  'cookie_domain': 'blog.example.com'
});

Tag Manager

For Google Analytics tags:

  1. In Tag Manager, navigate to Fields to Set:
  2. When the Fields to Set section is expanded, click Add Row.
  3. For Field Name, enter the correct value:
    • For Google Analytics 4 Configuration tags, enter cookie_domain.
    • For Universal Analytics Google Analytics settings variables, enter cookieDomain.
  4. For Value, enter the domain value, e.g. my.example.com.
  5. Save the tag and publish.

For Google Ads and Floodlight tags:

Domain settings can be modified from the Conversion Linker tag:

  1. In Tag Manager, open your existing Conversion Linker tag, or create one if the tag doesn't yet exist in your container.
  2. Under Linker Options, click Override cookie settings (advanced)
  3. In the Domain field, enter the highest level domain for which a cookie should be allowed to be set. You can also specify a specific path in the Path field. Only use these settings if you need to limit cookies to a lower-level subdomain or subdirectory.

Rename cookies

To avoid conflicts with other cookies, you may need to change the cookie name.

gtag.js

This configuration adds example to the beginning of the cookie that it sets (e.g. _ga becomes example_ga, or _gcl_au becomes example_gcl_au):

gtag('config', 'TAG_ID', {
  cookie_prefix: 'example'
});

Tag Manager

For Google Analytics 4 Configuration tags:

  1. In Google Tag Manager, open a Google Analytics 4 Configuration tag and click Tag Configuration > Fields to Set.
  2. When the Fields to Set section is expanded, click Add Row.
  3. For Field Name, enter cookie_prefix.
  4. For Value, enter the prefix value, e.g. example.
  5. Save the tag and publish.

For Universal Analytics tags:

Universal Analytics tags in Tag Manager can replace the name of the cookie with cookieName via a Google Analytics settings variable.

  1. In Tag Manager, open a Google Analytics settings variable and select Variable Configuration > More Settings > Fields to Set.
  2. When the Fields to Set section is expanded, click Add Row.
  3. For Field Name, enter enter cookieName.
  4. For Value, enter the name of the cookie, e.g. myCookie.
  5. Save the tag and publish.

For Google Ads and Floodlight tags:

Name prefix settings can be modified from the Conversion Linker tag:

  1. In Tag Manager, open your existing Conversion Linker tag, or create one if the tag doesn't yet exist in your container.
  2. Under Linker Options, click Override cookie settings (advanced)
  3. In the Name prefix field, enter the desired name prefix. Any tags that use these cookies (e.g. Google Ads conversion tags) must also be configured to use the same prefix.

On each page load, the cookie expiration time is updated to be the current time plus the cookie expiration value set by the Google tag. This means that if cookie expiration is set to one week (604800 seconds), 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 your reports. 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 your reports.

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

gtag.js

gtag('config', 'TAG_ID', {
  cookie_expires: 28 * 24 * 60 * 60 // 28 days, in seconds
});

Tag Manager

For Google Analytics tags:

  1. In Tag Manager, navigate to Fields to Set:
  2. When the Fields to Set section is expanded, click Add Row.
  3. For Field Name, enter the correct value:
    • For Google Analytics 4 Configuration tags, enter cookie_expires.
    • For Universal Analytics Google Analytics settings variables, enter cookieExpires.
  4. For Value, enter the number of seconds before the cookie will expire, e.g. for 28 days, enter 2419200.
  5. Save the tag and publish.

When the cookie_update flag is set to true (the default value), Google tags may update cookies on each page load, and may 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.js

gtag('config', 'TAG_ID', {
  cookie_update: false
});

Tag Manager

For Google Analytics tags:

  1. In Tag Manager, navigate to Fields to Set:
  2. When the Fields to Set section is expanded, click Add Row.
  3. For Field Name, enter the correct value:
    • For Google Analytics 4 Configuration tags, enter cookie_update.
    • For Universal Analytics Google Analytics settings variables, enter cookieUpdate.
  4. For Value, enter a boolean value, e.g. false.
  5. Save the tag and publish.