Sooner or later, a tracking task needs to remember something between pages. A campaign name has to survive until the form on page three. A test group has to stay the same on the next visit. The browser offers three obvious places to keep it, and picking the wrong one tends to produce data that looks fine until you check it closely.
If you need the basics first, start with what website cookies are. This article is about choosing between the options.
How the three differ
- Cookies. The browser sually sends them back to the server in a request header. A cookie can be shared across subdomains using
Domain, it has an expiry you set, and it usually holds only about 4 KB. - Local storage. It is scoped to the origin and survives closing the browser. All tabs on the same origin share it. It stays on the device unless your code sends it somewhere.
- Session storage. It is scoped to both the origin and the tab, and closing the tab removes it.
The word "origin" causes most of the surprises. www.example.com and shop.example.com are different origins, so they cannot read each other's local or session storage. A cookie set on example.com can reach both.
Four questions that decide it
Ask these in order, and stop when one gives you the answer:
