Configure and customize cookies

This page is for developers that want to customize cookie settings with the Google tag or Google Tag Manager. If you aren't sure if you need to configure cookies, read Cookies and user identification.

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 (for example, localhost), cookie_domain is automatically set to 'none', and cookies will use the full domain from the document location.

If you only want to modify cookie expiration or cookie update settings, you can change default cookie settings using the Analytics interface.

For most websites and apps, cookie storage should also be controlled by user consent. User privacy overview introduces the available options for managing user consent.

When you use automatic cookie domain configuration, activity is measured across subdomains without any extra configuration.

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 the Google tag:

  1. In your workspace, open the Tags menu.
  2. Edit an existing Google tag or create a new one.
  3. In the Configuration settings, add a new parameter:

    • Name: cookie_domain
    • Value: my.example.com
  4. Save the tag and publish the container.

    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.

For Universal Analytics tags

  1. In Tag Manager, open the Tags menu.
  2. Edit and existing Universal Analytics tag or create a new one.
  3. Create a new Google Analytics settings variable. Select Variable Configuration > More Settings > Fields to Set.
  4. When the Fields to Set section is expanded, click Add Row.
  5. In Field Name, enter cookieDomain.
  6. For Value, enter the domain value, e.g. my.example.com.
  7. Save the tag and publish.

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 (for example _ga becomes example_ga, or _gcl_au becomes example_gcl_au):

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

Tag Manager

For the Google tag:

  1. In your workspace, open the Tags menu.
  2. Edit an existing Google tag or create a new one.
  3. In the Configuration settings, add a new parameter:

    • Name: cookie_prefix
    • Value: example
  4. Save the tag and publish the container.

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, for example, 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.
  1. Under Linker Options, click Override cookie settings (advanced)
  2. In the Name prefix field, enter the desired name prefix. Any tags that use these cookies (for example, 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 the Google tag:

  1. In your workspace, open the Tags menu.
  2. Edit an existing Google tag or create a new one.
  3. In the Configuration settings, add a new parameter:

    • Name: cookie_expires
    • Value: 2419200
  4. Save the tag and publish the container.

For Universal Analytics tags:

  1. In your workspace, open the Tags menu.
  2. Edit an existing Universal Analytics tag.
  3. Create a new Google Analytics settings variable. Select Variable Configuration > More Settings > Fields to Set.
  4. When the Fields to Set section is expanded, click Add Row.
  5. In Field Name, enter cookieExpires.
  6. For Value, enter the number of seconds before the cookie will expire, e.g. for 28 days, enter 2419200.
  7. Save the tag and publish the container.

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 the Google tag:

  1. In your workspace, open the Tags menu.
  2. Edit an existing Google tag or create a new one.
  3. In the Configuration settings, add a new parameter:

    • Name: cookie_update
    • Value: false
  4. Save the tag and publish the container.

For Universal Analytics tags:

  1. In your workspace, open the Tags menu.
  2. Edit an existing Universal Analytics tag.
  3. Create a new Google Analytics settings variable. Select Variable Configuration > More Settings > Fields to Set.
  4. When the Fields to Set section is expanded, click Add Row.
  5. In Field Name, enter cookieUpdate.
  6. For Value, enter a boolean value, e.g. false.
  7. Save the tag and publish the container.