gtag.js uses cookies to identify unique users across browsing sessions. This page explains how to customize cookie settings.
Configure cookie field 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 | 63072000 (two years, in seconds) |
cookie_prefix |
string | none |
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', 'MEASUREMENT_ID', {
'cookie_prefix': 'MyCookie',
'cookie_domain': 'blog.example.com',
'cookie_expires': 28 * 24 * 60 * 60 // 28 days, in seconds
});
Cookie domain configuration
By default, gtag.js has automatic cookie domain configuration enabled. When enabled, gtag.js will set cookies on the highest level domain it can set cookies on. 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, update the config for your property to specify a value for the cookie_domain parameter:
gtag('config', 'MEASUREMENT_ID', {
'cookie_domain': 'blog.example.com'
});
Cookie prefix
To avoid conflicts with other cookies, you may need to change the cookie prefix. This prefix will be prepended to cookies set by gtag.js. For example, the default name of the ID cookie used by Google Analytics is _ga
. For example, if you set the cookie_prefix
parameter value to "example", the cookie name will become example_ga
:
gtag('config', 'MEASUREMENT_ID', {
cookie_prefix: 'example'
});
Cookie expiration
On each page load, the cookie expiration time is updated to 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', 'MEASUREMENT_ID', {
cookie_expires: 0
});
cookie_update
parameter
When cookie_update
is set to true
(the default value), gtag.js updates cookies on each page load. This updates the cookie expiration to be set relative to the most recent visit to the site.
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', 'MEASUREMENT_ID', {
cookie_update: false
});
Cookie flags
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 Google Analytics uses to represent a user. It enables the analysis of groups of sessions across devices.
To implement the User ID with gtag.js, update the config for your property to set the User ID:
gtag('config', 'MEASUREMENT_ID', {
'user_id': 'USER_ID'
});
How cookies are used
gtag.js uses the _ga
and _gid
cookies to distinguish unique users:
Cookie name | Default expiration time | Can it be set? | Description |
---|---|---|---|
_ga |
2 years | Yes | Distinguishes users. |
_gid |
24 hours | No | Distinguishes users. |
_ga_<container-id> |
2 years | Yes | Persists session state. |