Changing expiration on Google Analytics cookies
There are several ways to change the cookie lifespan on a cookie for Google analytics cookie. The default lifespan for the Google Analytics cookie is currently two years. The method you need depends on how your Google analytics is set up.
If you use the old google analytics script:
If you have an old Google Analytics script on your website, you can specify the expiration date directly in the script and overwrite of the default value of two years. The value sets the cookie expiration, in seconds. All you need to do is add the cookieExpires parameter to the line in the script that creates the cookie by adding {'cookieExpires': 31536000}, like this:
Making the script look something like this:
In some cases you might need to make sure cookie updates are turned off by setting the parameter cookieUpdate to false. This can be done by adding 'cookieUpdate': 'false' to the parameter setting so it looks like this:
You can read more about modifying your analytics script in googles documentation here: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference
If you are using Google Tag Manager (GTM)
In this case you can simply set the lifespan in the tag settings for your google analytics tag.
Go to the global Google Analytics Page View Tag.
Check the box: Enable overriding settings in this tag
open more settings
Open Fields to Set
Press: Add Field and fill out the two fields as shown below.
Don't forget to save the settings and you're good to go.
If you are using the new gtag js
You can configure the gtag to set the cookie expiration like this:
Check the documentation for gtag here: https://developers.google.com/analytics/devguides/collection/gtagjs/cookies-user-id
Last updated