Optimizing Google Analytics Cookies at Illinois
This content was originally presented at a session at the IT Professionals Forum Spring 2025. Note to the presenter: a slide-like display can be started by adding ?mode=present to the end of the page URL. Set the zoom between 175% and 200% and use the up/down arrow keys to navigate through the slides.
Table of Contents
- Introduction
- Google Analytics 4 and Google Tag Manager
- Google Analytics 4 Cookies
- Cookie Accumulation and Browser Errors
- Who Can This Affect?
- Possible Solutions
- Solution: Clearing Browser Cookies
- Solution: Increasing Max Request Header Size
- Solution: Cookie Clean Up Script
- Solution: Setting the Cookie Domain
- Solution: Setting the Cookie Domain (GTM config)
- Solution: Setting the Cookie Domain (gtag config)
Introduction
- Topic: Optimizing Google Analytics Cookies at Illinois
- Addressing 400-level browser errors seen by end users which can prevent website access; in part, caused by an accumulation of cookies used by Google Analytics 4 (GA4)
- Shout out to the UMN blog post that helped bring this to our campus' attention
- Presenter: Dan Dalpiaz
- Web Developer in the University Library at the University of Illinois Urbana-Champaign
- Not an expert in Google Analytics itself, but...
- Have been tracking a particular cookie issue for a while now
Google Analytics 4 and Google Tag Manager
- Google Analytics 4
- Google's latest version of their web analytics service, replaces Universal Analytics (UA).
- Within the GA4 platform, there are 'Accounts' which contain 'Properties' which contain 'Data Streams'
- Typically only one Data Stream is used within a property when used for a website.
- Creating a Data Stream generates an associated 'Google Tag'. The tag created for a web Data Stream will have two tag IDs: a 'Measurement ID' (e.g.
G-5WMFPCS43H
) and a 'Tag ID' (e.g.GT-WK5N5G5J
). - Other Google services can also use Google tags, but GA4 seems to be the most common on our campus.
- Google Tag Manager
- Google's tag management system that can be used to manage various tags, including GA4 tags.
- Not required to use for GA4, but can provide more options for managing tags and tracking.
Google Analytics 4 Cookies
- Google Tags use a browser cookie to establish sessions and track activity.
- GA4 tags use cookies with names like:
_ga_<TAG-ID>
, e.g._ga_5WMFPCS43H
- Each cookie is usually about 50-100 bytes in size.
- GA4 tags use cookies with names like:
- By default, GA4 tags will set the cookie's domain as the base ".illinois.edu" domain and not the subdomain for the site in question (reference: Google's Tag Platform documentation).
- Cookies set with the .illinois.edu domain will be included in the request headers for requests made to any subdomain site (example.illinois.edu).
Cookie Accumulation and Browser Errors
- Over time, as a user visits more and more campus websites that use Google Analytics, the number of
_ga_ cookies
can accumulate in their browser. - Web servers like Apache and Nginx set a maximum allowable size for the request headers they are sent. The default is typically around 8000 bytes.
- If enough cookies accumulate in the browser (along with other information sent in request headers), the total size of the request headers can exceed the server's maximum allowable size and will result in a browser error — usually HTTP status code 400 (Bad Request) or 431 (Request Header Fields Too Large).
Who Can This Affect?
- No individual user or campus website is completely immune to this issue. If certain conditions are met, any user visiting any website on the illinois.edu domain could run into this problem.
- Can happen on websites that aren't running Google Analytics or using any other Google tags
- This is most likely to be encountered by users who visit a large number of campus websites and do not clear their browser's cookies on a regular basis.
Possible Solutions
- Short-Term
- Clear the user's browser cookies (End User can implement)
- Can help fix issues for an individual user
- Increase the maximum allowable size of request headers on the web server (IT Pro can implement)
- Can help prevent issues on individual sites
- Implement a cookie clean up script on your website (IT Pro can implement)
- Can help prevent issues for groups of users
- Clear the user's browser cookies (End User can implement)
- Long-Term
- Setting the GA cookie domain to the relevant subdomain
- Using Google Tag Manager configuration (GTM User can implement)
- Using gtag.js configuration (IT Pro can implement)
- Setting the GA cookie domain to the relevant subdomain
Solution: Clearing Browser Cookies
- If a user starts seeing 400/431 errors on one or more campus sites, you can assist them in clearing their browser's cookies. See Browsers, Clearing Cache and Cookies.
- This will clear all cookies, not just GA4 cookies. The user will need to reauthenticate with sites that use cookies to manage user sessions.
- Alternatively, if comfortable with the browser's developer tools, you can assist the user in manually deleting just the
_ga_
cookies on the .illinois.edu domain.
- This is a short-term solution and may need to be repeated if the user continues to visit a large number of campus websites.
Solution: Increasing Max Request Header Size
- IT Pros that have access to a site's web server configuration (Apache, Nginx, IIS, etc.) may be able to increase the maximum allowable size of request headers. Note, this will only help prevent user errors for a specific site.
- Apache, for example, has the
LimitRequestFieldSize
directive.
- Apache, for example, has the
- Should be used cautiously — allowing larger request headers to be sent to the server could potentially be abused by bad actors.
- This is likely not an option for vended services that use an illinois.edu subdomain, unless the vendor is willing to make this change.
Solution: Cookie Clean Up Script
- It is possible to automatically clear
_ga_
cookies using JavaScript. Permissions are such that a subdomain site can modify cookies set at the higher level ".illinois.edu" domain. - A script has been made available on GitHub: https://github.com/UIUCLibrary/ga-cookie-script. By default, the script will clear a user's
_ga_
cookies if there are more than 30 accumulated. A higher threshold can be set by passing amax
parameter to the script, example below:<script src="https://cdn.jsdelivr.net/gh/UIUCLibrary/ga-cookie-script@1.0.1/ga-cookie-script.min.js?max=40"></script>
- This can be effective when implemented on high traffic pages to 'catch' the most amount of users. For users currently experiencing 400 errors, you could try directing them to a page running the script.
- Shout out to the campus web community for suggesting a script like this as a possibility.
Solution: Setting the Cookie Domain
- The previous solutions have some downsides and are best used as a short-term fix. Clearing cookies is often inconvenient and even if done automatically, is not ideal.
- Sites using GA4 have the ability to set the domain for its
_ga_
cookie to the site's own subdomain. This would prevent the cookie from being included in requests to other subdomains.- We'll go over some different ways of explictly setting the cookie's domain, depending on how the analytics were set up.
- Technically, this could temporarily makes things worse since this will generate a new cookie with the relevant subdomain, but the original cookie may not expire for some time.
Solution: Setting the Cookie Domain (GTM config)
- If you are using Google Tag Manager to manage your GA4 tags, you can set the cookie domain in the tag configuration:
- In Google Tag Manager, locate the container and GA4 tag you want to modify.
- Edit the 'Tag Configuration' and add a new 'Configuration Parameter' with a name of
cookie_domain
and a value of the site's subdomain, e.g.example.illinois.edu
. - Save the changes and publish the container.
- See Google's Tag Platform documentation for more information.
Solution: Setting the Cookie Domain (gtag config)
- If you are using Google Analytics without Google Tag Manager, configuration can be added to the existing JavaScript snippet:
gtag('config', 'TAG_ID', {'cookie_domain': 'example.illinois.edu'});
- If you don't have access to edit the JavaScript snippet, you could consider using Google Tag Manager to manage the GA4 tag. Steps to add an existing GA4 tag to GTM can be found on the Set up Google Analytics in Tag Manager support page. After adding the GA4 tag to GTM and adding the cookie domain configuration as described in the previous slide, you can switch the site to use the GTM snippets instead of the gtag.js snippet.
- In all cases, after making changes, it is recommended to check that the cookie is being set correctly in the browser's developer tools and that analytics data is being collected as expected.
Questions?
- Contact: dalpiaz1@illinois.edu
- Campus-related Google Analytics information also available on the WIGG Analytics website