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:
1
ga('create', 'UA-XXXX-Y', {'cookieExpires': 31536000});
Copied!
Making the script look something like this:
1
<script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-XXXX-Y', {'cookieExpires': 31536000,'cookieUpdate': 'false'}); ga('send', 'pageview');</script>
Copied!
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:
1
ga('create', 'UA-XXXX-Y', {'cookieExpires': 31536000,'cookieUpdate': 'false'});
Copied!
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:
1
gtag('config', 'GA_MEASUREMENT_ID', { 'cookie_expires': 31536000});
Copied!
Last modified 2mo ago