Course contentsShow
← Web Development
Lesson 2970 of 2,98253. Privacy and ComplianceFree lesson

Storing and Respecting Consent Choices

Persisting user consent preferences and ensuring they're respected across sessions and page loads.

Storing and Respecting Consent Choices

What you'll learn: How to persist user consent preferences so their privacy choices remain honored across visits and page loads.

Why Persistence Matters

When a user makes privacy choices in your cookie banner, those decisions must be remembered—otherwise you'd annoy them with repeated consent requests and potentially violate their expressed preferences on every page load. Think of it like a hotel remembering your "do not disturb" sign: once you've hung it, housekeeping shouldn't knock every hour asking if you're sure.

Where to Store Consent

You need a client-side storage mechanism that survives page refreshes:

  • Cookies: Ironically, you typically store consent preferences in a cookie (often named something like cookie_consent). This is usually considered an "essential" cookie that doesn't require consent itself.
  • localStorage: Alternative persistent storage, though cookies are more common for consent since they're automatically sent to servers.

The stored value should include:

  • Which categories the user accepted (analytics, marketing, etc.)
  • Timestamp of the decision
  • Version of your consent policy they agreed to

Respecting the Choice

On every page load, before executing any tracking scripts or setting non-essential cookies, your code must:

  1. Check for existing consent preferences
  2. Only load scripts/set cookies the user approved
  3. Block or remove previously-set cookies if consent was withdrawn

This means conditionally loading analytics libraries, advertising pixels, and other third-party integrations based on the stored preferences.

Key Takeaway: Store user consent choices in persistent storage (typically a first-party cookie or localStorage), check them on every page load before executing any tracking code, and ensure previously-denied services remain blocked until explicit consent is granted.