Why is it important to track cookie size? In my previous article (What’s your cookie-size – are you on the safe side?), I demonstrated the situation when a third-party marketing script could damage your business: may prevent your returning visitors from coming back to your website. Obviously, sooner or later the situation can be discovered and fixed by IT, but this would be a reactive approach – not proactive. The best thing about Google Analytics is that we can be proactive and track almost anything!

The best thing about Google Analytics is that we can be proactive with and track almost anything!

Why you should track cookie size

I propose that it is important to track the cookie size associated with your website. If the percentage of users with a cookie size greater than 4 kilobytes goes up, have Google Analytics send you automated alert. This way, you will be able to address the situation long before it damages your business.

Technically, cookies are a small bit of information being transferred to your website with every request and there is no way you can access other website cookies (under normal circumstances), but cookies associated with your website are yours and you can do whatever you want with them. In this case, all we need is their size. To get cookie size using JavaScript, execute in your browser console:

document.cookie.length

This will print the size of the cookies associated with the current website. As you can see in the screenshot above, cookie size is 2597 bytes or 2.5 kilobytes. We can save this number as a Custom Metric in Google Analytics, however I don’t believe this would be useful. To make our analysis effective, I would assign current pageview (or Session) to a limited number of buckets:

  • Tiny: < 500 bytes of cookies
  • 500…1k: cookie size is between 500 bytes to 1 kilobyte of cookies
  • 1k..2k: cookie size is between one and two kilobytes
  • 2k..4k: cookie size is between two and four kilobytes
  • 4k..8k: cookie size is between four and eight kilobytes
  • More than 8 kilobytes

The last two buckets put a user into the red zone and a sudden increase of hits/sessions in these buckets should be a trigger for a Google Analytics Custom Alert.

Unfortunately, the method above will not include the HttpOnly cookies often used for authentication purpose. HttpOnly cookies are special in a way that JavaScript can’t access it and they are solely for the use of your webserver. Since we can’t access these cookies, we can’t track them. Well, we can using a server-side tracking but it is a completely different story.

Sounds good, how can I do this?

At Internetrix, we use Google Tag Manager. The following guide covers cookie size tracking setup step by step. If you are not using Google Tag Manager yet, or use another Tag Manager solution, this can easily be replicated using other tools. It is just JavaScript.

First, we need to create a Custom Dimension in Google Analytics Property settings. It does not matter how you name the Custom Dimension, but please note the assigned dimension index.

Let’s switch to Google Tag Manager and create a Variable which returns for the current hit which cookie size bucket it goes into. In Google Tag Manager, Click on Variables and click on New button. Name variable: Page Cookie Size Categorised and copy/paste the following into Custom JavaScript field

function(){
 var cookieSize = document.cookie.length;
 var ret = 'unknown';
 if(!isNaN(cookieSize)){
 if(cookieSize < 500)
 ret = '< 500';
 else if(cookieSize < 1024)
 ret = '500 b ... 1k';
 else if(cookieSize < 2048)
 ret = '1k...2k';
 else if(cookieSize < 4096)
 ret = '2k...4k';
 else if(cookieSize < 8192)
 ret = '4k...8k';
 else
 ret = 'more than 8k'; 
 }

 return ret; 
}

Click Save. As you can see the variable assigns cookie size to one of the buckets and returns it to us. All that is left to do is to save it as Custom Dimension in our Pageview tag.

In the screenshot above, the Variable IRX – Setting – Dimension Index – Cookie Size is a constant holding index of a Custom Dimension in your GA Property where you are sending cookie size and Page Cookie Size Categorised as a Variable we have created in the previous step.

Now, save the tag and publish your container (do not forget to test it!). That’s pretty much it.

Allow the data to be collected and create a Custom Report using this Cookie Size Custom Dimension and Sessions, Conversion Rate as metrics.

Well done!

Need help with your analytics to become more data-driven in your approach? Talk to us about your ideas. Internetrix combines digital consulting with winning website design, smart website development, and strong digital analytics and digital marketing skills to drive revenue or cut costs for our clients. We deliver web-based consulting, development and performance projects to customers across the Asia Pacific ranging from small business sole traders to ASX listed businesses and all levels of the Australian government.