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:

ga('create', 'UA-XXXX-Y', {'cookieExpires': 31536000});

Making the script look something like this:

<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','','ga'); ga('create', 'UA-XXXX-Y', {'cookieExpires': 31536000,'cookieUpdate': 'false'}); ga('send', 'pageview');</script>

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:

ga('create', 'UA-XXXX-Y', {'cookieExpires': 31536000,'cookieUpdate': 'false'});

You can read more about modifying your analytics script in googles documentation here:

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:

gtag('config', 'GA_MEASUREMENT_ID', { 'cookie_expires': 31536000});

Check the documentation for gtag here:

Last updated