Cookies vs. localStorage vs. sessionStorage: What’s the Difference?
Sooner or later, every tracking project runs into the same question: where do we store this?
A click ID. The UTMs from the first visit. A “this purchase was already sent” flag. A consent choice. The browser gives you three common places to put them: cookies, localStorage and sessionStorage. They look similar until they don’t, and the difference usually shows up as a bug in your data.
Here’s the short version first, then the parts that actually matter for marketing and measurement. Grab a coffee. ☕
The quick comparison
| Cookies | localStorage | sessionStorage | |
|---|---|---|---|
| Who can read it | JavaScript (unless HttpOnly) and the server | JavaScript only | JavaScript only |
| Sent to the server automatically | Yes, with every request to the matching domain | No | No |
| How long it lasts | Until its expiry date, or until the browser closes if it has none | Until it’s deleted | Until the tab is closed |
| Where it’s available | A domain, optionally with its subdomains | One origin (protocol + domain + port) | One origin, in one tab |
| How much it holds | Usually around 4 KB per cookie | About 5 MiB per origin | About 5 MiB per origin |
Cookies: the only one the server sees
Cookies are the oldest of the three, and the only one that travels: the browser attaches them to every request it sends to the matching domain. That’s their superpower and their cost. The server (or your server-side tagging container) can read them without any JavaScript, but every cookie also adds weight to every request.
A few things worth knowing:
- They can be set by the server (with a
Set-Cookieheader) or by JavaScript (withdocument.cookie). - A cookie marked
HttpOnlycan’t be read by JavaScript at all, which includes your GTM tags. - With the
Domainattribute, one cookie can be shared betweenwww.shop.comandcheckout.shop.com. This is why analytics cookies can follow a user across subdomains. - They’re small. Around 4 KB per cookie is the usual limit, so they’re meant for IDs, not data dumps.
Most of the tracking you already use lives here. The GA4 client ID sits in the _ga cookie, and Google’s conversion linker keeps ad click information in first-party cookies such as _gcl_aw.
localStorage: persistent, but it never leaves the browser
localStorage is a simple key–value store that stays in the browser until someone (or something) deletes it. There’s no expiry date. It holds far more than a cookie, and nothing is sent to the server unless your own code sends it.
The classic gotcha is scope. localStorage belongs to one origin: protocol, domain and port together. So https://www.shop.com and https://shop.com have two completely separate storages. Save a value on one, try to read it on the other, and you get nothing. No error, just an empty result and a confused tag.
Two smaller details: it only stores strings (use JSON.stringify for objects), and if you need something to expire, you have to store a timestamp yourself and check it.
sessionStorage: one tab, one visit
sessionStorage works like localStorage with a much shorter memory. It survives reloads and navigation within the same tab, but opening the page in a new tab starts a fresh, empty storage, and closing the tab clears it.
Don’t confuse it with a session cookie. A session cookie is shared by all tabs and lasts until the browser closes. sessionStorage is per tab.
Where this gets interesting for tracking
The “already sent” purchase flag
A common way to stop a purchase from being counted twice is to save a flag like purchase_sent_1234 after the tag fires, and skip the tag if the flag already exists.
Store that flag in sessionStorage and it handles a page reload. But when the customer opens the thank-you page again tomorrow from the confirmation email, it’s a new tab, the storage is empty, and the purchase fires again. localStorage remembers longer, but only on that origin and in that browser.
So the flag helps, but it doesn’t replace sending the order ID as a transaction ID, so the platforms can drop duplicates on their side. Belt and suspenders.
Safari has opinions
Safari’s Intelligent Tracking Prevention deletes cookies created with JavaScript, along with localStorage, sessionStorage and other script-writeable storage, after 7 days without user interaction with the website. When a visitor arrives from a site Safari classifies as a tracker, through a link with extra parameters in the URL, cookies created with JavaScript on the landing page are capped at 24 hours.
In practice, someone who comes back after two weeks in Safari can look like a brand-new visitor. Cookies set by your own server tend to last longer, which is one reason server-side tagging comes up in these conversations. It’s not a magic fix, though: Safari also limits cookies set by servers that only look like they’re first-party.
localStorage isn’t a consent loophole
It’s a tempting idea: if cookies need consent, just use localStorage. It doesn’t work that way. In the EU, the ePrivacy rules cover storing or reading information on the user’s device, whatever the technology. If a cookie needs consent, the same data in localStorage or sessionStorage needs it too.
How to check what’s actually stored
In Chrome, open DevTools and go to Application → Storage. You’ll find Cookies, Local storage and Session storage, grouped by origin. Or type these in the Console:
document.cookie // cookies readable by JavaScript
localStorage.getItem('my_key') // one value from localStorage
Object.keys(sessionStorage) // everything in this tab's sessionStorage
One tip: check the exact origin you’re on. Half of the “it’s not working” moments with localStorage are really “it’s on www and I’m looking on the other one”.
So which one should you use?
- The server needs to read it, or it has to work across subdomains: a cookie.
- Only the browser needs it, and it should survive the next visit: localStorage.
- Only this tab, only this visit: sessionStorage.
- Your measurement depends on it: don’t rely on the browser alone. Send the IDs that matter to your backend and your platforms, where they can’t be cleared by a privacy setting.
Coffee finished? Then consider this event fired. ☕
Stay curious.
