A Primer on Cookies in Web Analytics, Part 2: What about Web Storage?
Last week I wrote about cookies, and how they are used in web analytics. That post was really just the first in a series of posts I wanted to write about how cookies are used by web analytics tools to identify visitors to your site, and I’ll get to that shortly. But a few readers reached out to me and asked why I hadn’t mentioned web storage, so I wanted to mention it briefly before moving on.
Web storage is a feature built into modern browsers that allows a developer to store information in the browser about the site and the user, without using a cookie. This storage can be persistent (via the localStorage JavaScript object) or session-based (via the sessionStorage JavaScript object). It was included in the HTML5 spec by the W3C years ago, and any browser versions released in the last 5 years or so include it – even Internet Explorer 8, the current bane of web developers everywhere! Note that there are a few exceptions (like Compatibility Mode in IE8), but suffice to say that web storage has been available to developers for quite awhile. However, while it is an incredibly cool idea in theory, my experience has been that it is less widely used in practice – especially when it comes to web analytics. Why is this?
- Cookies are included with every HTTP request, but web storage is only available to the browser. So for tools that need to communicate with the server and give it access to client-side data (like many analytics tools), cookies are still the best solution – even though they clutter up the network as they get passed back and forth, and are limited to about 4KB of data.
- Cookies can be shared across subdomains of the same website, but web storage is subdomain-specific. That means that a cookie with scope of analyticsdemystified.com can be read by my blog or any of my partners – but a value stored in web storage is available only to my blog, josh.analyticsdemystified.com. For a company that operates many subdomains and tries to track their users across each one, this is a major limitation of web storage.
When I worked at salesforce.com, that second reason had a major impact on the reason we used a cookie to store all sorts of cool personalization data about our visitors, rather than local storage. In fact, at one point I was challenged by my manager with migrating that cookie to local storage – a really exciting project for me because I got to try out some really new technology. But after all our initial testing passed, I remember frantically having to revert all my code prior to our next code release because my own visitor history disappeared once I started browsing our staging site instead of our development site!
So keep that in mind when thinking about whether to use local storage or cookies. Web storage is much more considerate of your users, and much for forward thinking, since it’s utilizing the latest and greatest technology built into browsers. But there are some things it can’t do, so keep it in your toolbelt but remember that there are many times when cookies are still the best way to solve your analytics challenges.