PrivacyQuick

Cookies & tracking

Do localStorage and sessionStorage need consent?

Usually, yes. EU and UK cookie rules cover any information stored on or read from a visitor's device, so localStorage and sessionStorage follow the same consent rules as cookies, with the same exemption for strictly necessary uses.

  • Plain English
  • Written for website owners
  • Free to read

In the EU and the UK, localStorage and sessionStorage generally need consent on the same terms as cookies. The rule doesn't mention cookies by name. It covers storing information on a visitor's device, or reading information already stored there, whatever the technology. So a value an analytics or advertising script writes to localStorage needs prior consent. A value that is strictly necessary for something the visitor asked for, such as a shopping cart, does not.

This guide explains why the law treats web storage like cookies, goes through common examples, and shows how to keep your own code's storage writes behind consent.

Why the rule isn't limited to cookies

The consent requirement comes from Article 5(3) of the ePrivacy Directive (2002/58/EC, as amended in 2009). It applies to "the storing of information, or the gaining of access to information already stored, in the terminal equipment" of a user. Storing or reading is allowed only with consent, unless one of two exemptions applies:

  • Transmission: the storage is needed solely to carry out the transmission of a communication over a network.
  • Strictly necessary: the storage is strictly necessary to provide a service the user has explicitly requested.

Nothing in that wording depends on the storage being a cookie. A browser's localStorage, sessionStorage, IndexedDB and Cache Storage all hold information on the user's device, and a script reading them is "gaining access" to it. Regulators have said the same thing directly:

  • The European Data Protection Board's Guidelines 2/2023 on the technical scope of Art. 5(3) stress that the provision is technology-neutral and isn't limited to cookies.
  • France's CNIL frames its rules as applying to "cookies and other trackers", which includes HTML5 web storage.
  • In the UK, regulation 6 of PECR mirrors Art. 5(3), and the ICO's guidance on storage and access technologies lists local storage alongside cookies.

Member states transpose the directive into national law, so the exact wording varies by country. The guide to the ePrivacy Directive covers that in more detail.

The test is the purpose of the data, not the API that stores it. If a cookie with the same purpose would need consent, a localStorage key with that purpose needs consent too.

How web storage differs from cookies

The legal treatment is the same, but the mechanics differ in ways that matter when you audit and control them.

TopicCookieslocalStoragesessionStorageIndexedDB
Sent to the server automaticallyYes, with matching requestsNoNoNo
Who can set itServer or JavaScriptJavaScript onlyJavaScript onlyJavaScript only
LifetimeSession or an expiry dateUntil clearedUntil the tab closesUntil cleared
ScopeDomain and pathOrigin (scheme, host, port)Origin, per tabOrigin
Typical sizeAbout 4 KB per cookieSeveral MB per originSeveral MB per originMuch larger, browser-dependent

Because web storage isn't sent with requests, people sometimes assume it can't be used for tracking. It can. A script reads the stored identifier and adds it to the requests it sends. Many product analytics and session replay tools work exactly this way.

One detail is easy to miss: a third-party script that runs in your page writes to your origin's storage, not its own. A tracking library loaded from a vendor's CDN still ends up storing its identifiers under your domain. A third-party iframe, by contrast, uses its own origin's storage, which your page can't read or clear.

Classification works the same way as for cookies. Ask whether the storage is strictly necessary for something the visitor explicitly asked for. The guide to strictly necessary cookies covers the edge cases in more depth.

What's storedExampleConsent needed?
Cart contentsItems added to a basket on a store without accountsUsually no: the visitor asked for a cart
Login stateAn authentication token for a members' areaNo, once the user logs in
The consent choice itselfWhich categories the visitor acceptedNo
A setting the visitor choseA language or dark-mode toggle they clickedOften no, if it's an expected part of the feature they used
Form draftAutosaving a long application form in sessionStorageOften no, when it serves the form the visitor is filling in
Analytics identifierAn anonymous user ID kept by a product analytics SDKYes
Session replay stateRecording IDs and queued eventsYes
A/B test assignmentWhich variant the visitor was put inGenerally yes: it serves you, not the visitor
Advertising dataFrequency caps, audience flags, click IDsYes
Personalization the visitor didn't ask for"Recently viewed" products tracked automaticallyUsually yes, as functional storage

The last row is a common grey area. Showing recently viewed items is useful, but the visitor didn't request it, so most sites put it in a functional category that the visitor can switch off. If you're unsure, the cookie categories guide explains how to decide between necessary, functional, analytics and marketing.

sessionStorage isn't exempt because it's short-lived

sessionStorage is cleared when the tab closes, so it can feel harmless. Duration doesn't decide the question, though. Art. 5(3) asks whether information is stored or read, and why. A session replay tool that keeps its recording ID in sessionStorage still needs consent, even though the value disappears within minutes.

Duration does matter in other ways. It belongs in your cookie policy, and shorter storage is easier to justify under the GDPR's storage limitation principle (Art. 5(1)(e)) once you have a legal basis. Just don't rely on it as a reason to skip consent.

Reading counts as well as writing. Code that checks whether an identifier already exists in localStorage is "gaining access" to stored information, so the check itself belongs behind consent if the identifier serves a non-essential purpose.

Where web storage comes from on a typical site

Most storage you didn't write yourself arrives with third-party tools. Common sources include:

  • Product analytics SDKs, which often keep an anonymous ID and a queue of unsent events in localStorage.
  • Session replay and heatmap tools, which track recording state across pages.
  • A/B testing and personalization tools, which remember variant assignments.
  • Chat widgets, which store conversation state so the chat survives a page change.
  • Embedded players, which keep player preferences and identifiers in their own origin's storage. The YouTube embeds guide covers a common example.
  • Your own front end, for example a state library that persists its store to localStorage.

To see what's there, open your site in a private window, then open DevTools and look under Application → Storage for Local storage, Session storage and IndexedDB. Each entry shows its key and value, and you can delete entries there to test what reappears. Check before and after you accept the banner. Anything non-essential that appears before you choose is a problem.

Manual checks miss pages you don't visit. PrivacyQuick's cookie scanner crawls your site in a real browser and records local and session storage as well as cookies, so web storage shows up in the same inventory.

For third-party tools, the reliable fix is to stop the script loading until the visitor allows its category. The guide to blocking cookies before consent covers the techniques. For storage your own code writes, wrap each write in a consent check.

Here is a small helper using the PrivacyQuick JavaScript API. The same pattern works with any consent tool that exposes the visitor's choices.

function canStore(category) {
  if (category === 'necessary') return true;
  return !!(window.PrivacyQuick && PrivacyQuick.has(category));
}

function rememberRecentlyViewed(productIds) {
  if (!canStore('functional')) return;
  try {
    localStorage.setItem('recently_viewed', JSON.stringify(productIds.slice(0, 10)));
  } catch (e) {
    // storage can be full or disabled; the feature simply stays off
  }
}

When a visitor withdraws consent, remove what you stored for that category. Your code can clear any key on your own origin, including keys written by third-party scripts that ran in your page:

document.addEventListener('pq:consent', function (e) {
  var allowed = e.detail.categories;
  if (allowed.indexOf('functional') < 0) {
    localStorage.removeItem('recently_viewed');
  }
  if (allowed.indexOf('analytics') < 0) {
    Object.keys(localStorage).forEach(function (key) {
      // replace with the key prefixes your analytics tool uses
      if (key.indexOf('analytics_') === 0) localStorage.removeItem(key);
    });
  }
});

Check your tools' documentation for the exact keys they use. Clearing a key while the script that wrote it is still running only helps briefly, because the script may write it again. That's why blocking the script matters more than cleaning up after it.

Browser limits on web storage

Browsers impose their own limits, separate from the law. Safari's Intelligent Tracking Prevention deletes all script-writable storage for a site, including localStorage and IndexedDB, after seven days of Safari use without the visitor interacting with that site. Chrome, Firefox and Safari partition storage inside third-party iframes by the top-level site, so an embedded widget can't use the same storage across different websites.

None of this changes whether you need consent. It does mean identifiers in web storage are less durable than many tools assume. The guide to cookie lifespans covers browser caps in more detail.

Related: consent in mobile apps, sharing consent across subdomains and A/B testing tools and consent.

Next steps

  • List every localStorage, sessionStorage and IndexedDB key on your site, before and after consent, in a private window.
  • Give each key a category, a purpose and an owner (you or a named vendor), and add them to your cookie policy.
  • Block third-party scripts that write non-essential storage until their category is allowed.
  • Wrap your own non-essential storage writes in a consent check, and remove keys when consent is withdrawn.
  • Re-scan after every new tool or front-end release, since storage often arrives quietly with an update.

Frequently asked questions

Is IndexedDB covered by cookie consent rules too?

Yes. IndexedDB is another way to store information in the browser, so the same ePrivacy Art. 5(3) logic applies. Whether you need consent depends on what the data is used for, not on the storage API.

Do I have to list localStorage items in my cookie policy?

You should. The consent rules require clear and comprehensive information about what you store and why, so list web storage keys alongside cookies with their purpose, provider and how long they persist.

Does the CCPA require consent before using localStorage?

No. The CCPA has no general opt-in rule for device storage. If localStorage is used for selling or sharing personal information, such as cross-context behavioral advertising, the opt-out rights apply whatever technology you use.

Can a consent tool store the visitor's choice in localStorage?

Yes. Remembering a visitor's consent choice is widely treated as strictly necessary, so it needs no consent of its own. The same applies whether the choice is kept in a cookie or in web storage.

Check your own website in 30 seconds

Our free scanner lists every cookie and tracker your site sets, sorted into categories, before you add a banner.

Run a free cookie scan Start free

This guide is general information, not legal advice. Laws and regulator guidance change; check the current rules for your situation or ask a lawyer.