Openli
IntroductionGetting StartedTechnical DocumentationGet help
  • Getting started
    • Introduction
    • Dictionary
    • FAQ
      • IAB TCF v2.0
  • Privacy hub
    • How to create your privacy profile
    • How can I get an Openli Privacy Badge?
    • Vendor owners
    • Why are custom properties on vendors super valuable?
    • Setting up custom properties
  • Tasks
  • Cookie Consent Management
    • Quickstart
    • Checklist: Cookies
    • Cookie widget
      • Create a cookie policy
      • Create a cookie widget
      • Install the widget
      • Manually add any cookies
      • Customise your widget
      • How to get color codes
      • Set the widget language
      • Use custom link/button instead of Cookie Shield
      • Add company name to cookie pop-up
    • Categorise your cookies
    • Adding information to cookies
    • Adding information to your cookie providers
    • Blocking Cookies with Openli
    • Cookie expiration dates and lifespan
      • Changing expiration on Google Analytics cookies
  • Policy and agreement management
    • Generate your privacy policy
    • Changing an old cookie policy to a new on autopilot
    • Embed your policies and legal agreements
      • Embedding agreements without displaying a widget
    • Install the Privacy Badge
  • General Openli guides
    • Setting up SSO (Single Sign-On)
      • SSO with Azure AD (Microsoft)
      • SSO with Google
      • SSO with Okta
      • How to log in with SSO
    • Add a website to your Openli account
    • Using projects to handle multiple languages
    • Collect consents without storing IP addresses or user-agents
  • Technical documentation
    • Widget
      • Collect cookie consent
        • Blocking cookies
        • Blocking embedded media
        • Cookie-widget options
        • Consent state API
    • API
      • Services
        • Personal data
        • Subprocessors
    • Cookies set by Openli
  • System Specific Integration Guides
    • Cloudflare
  • Legacy integrations
    • Google Consent Mode
    • Google Tag Manager
      • Block cookies with GTM
      • Install Openli with GTM
      • Prevent triggers from firing in GTM
    • HubSpot
    • Pardot
    • Shopify
      • Install cookie widget
      • Integrate consent with checkout
    • Square Online
    • Squarespace
    • Webflow
    • Wix
Powered by GitBook
On this page
  • If you use the old google analytics script:
  • If you are using Google Tag Manager (GTM)
  • If you are using the new gtag js
  1. Cookie Consent Management
  2. Cookie expiration dates and lifespan

Changing expiration on Google Analytics cookies

PreviousCookie expiration dates and lifespanNextGenerate your privacy policy

Last updated 3 years ago

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','https://www.google-analytics.com/analytics.js','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:

https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference
https://developers.google.com/analytics/devguides/collection/gtagjs/cookies-user-id