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:

Note: When you use automatic cookie domain configuration, activity is measured across subdomains; you don’t need to provide additional configuration.Important: The cookie_domain must be an ancestor of the current domain, otherwise the cookie will not get set. For example, if your domain is one.two.three.root.com, you may configure the cookie to be set on root.com, but not someotherdomain.com. If you set an incorrect cookie domain, no data will be sent to Google Analytics.

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'
});

Important: If cookie_prefix is changed after a user has already visited the site, the context stored in the non-prefixed cookies will be lost and that visitor will appear as a new visitor.

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
});

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: