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.
Change cookie domain
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:
- In your workspace, open the Tags menu.
- Edit an existing Google tag or create a new one.
In the Configuration settings, add a new parameter:
- Name:
cookie_domain
- Value:
my.example.com
- Name:
Save the tag and publish the container.
For Google Ads and Floodlight tags:
Domain settings can be modified from the Conversion Linker tag:
- In Tag Manager, open your existing Conversion Linker tag, or create one if the tag doesn't yet exist in your container.
- Under Linker Options, click Override cookie settings (advanced)
- 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
- In Tag Manager, open the Tags menu.
- Edit and existing Universal Analytics tag or create a new one.
- Create a new Google Analytics settings variable. Select Variable Configuration > More Settings > Fields to Set.
- When the Fields to Set section is expanded, click Add Row.
- In Field Name, enter
cookieDomain
. - For Value, enter the domain value, e.g.
my.example.com
. - 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:
- In your workspace, open the Tags menu.
- Edit an existing Google tag or create a new one.
In the Configuration settings, add a new parameter:
- Name:
cookie_prefix
- Value:
example
- Name:
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.
- In Tag Manager, open a Google Analytics settings variable and select Variable Configuration > More Settings > Fields to Set.
- When the Fields to Set section is expanded, click Add Row.
- For Field Name, enter enter
cookieName
. - For Value, enter the name of the cookie, for example,
myCookie
. - 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.
- Under Linker Options, click Override cookie settings (advanced)
- 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.
Cookie expiration
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:
- In your workspace, open the Tags menu.
- Edit an existing Google tag or create a new one.
In the Configuration settings, add a new parameter:
- Name:
cookie_expires
- Value:
2419200
- Name:
Save the tag and publish the container.
For Universal Analytics tags:
- In your workspace, open the Tags menu.
- Edit an existing Universal Analytics tag.
- Create a new Google Analytics settings variable. Select Variable Configuration > More Settings > Fields to Set.
- When the Fields to Set section is expanded, click Add Row.
- In Field Name, enter
cookieExpires
. - For Value, enter the number of seconds before the cookie will expire,
e.g. for 28 days, enter
2419200
. - Save the tag and publish the container.
Cookie update
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:
- In your workspace, open the Tags menu.
- Edit an existing Google tag or create a new one.
In the Configuration settings, add a new parameter:
- Name:
cookie_update
- Value:
false
- Name:
Save the tag and publish the container.
For Universal Analytics tags:
- In your workspace, open the Tags menu.
- Edit an existing Universal Analytics tag.
- Create a new Google Analytics settings variable. Select Variable Configuration > More Settings > Fields to Set.
- When the Fields to Set section is expanded, click Add Row.
- In Field Name, enter
cookieUpdate
. - For Value, enter a boolean value, e.g.
false
. - Save the tag and publish the container.