Understanding cookies

Cookies are simple text file information which are stored within your browser.

To have a better idea of what cookies are, let's simply create one with a script like this:

<script>document.cookie = "cars";</script>

This script means that when the page is loaded, it stores a cookie which has for value "cars".

Cookies are then read with scripts like this:

var x = document.cookie;

So if we write a script like this on a blank page:

document.cookie = "cars";

we will get a blank page with the word cars displayed on it.:

A cookie read

If we look at the cookies within our browser (in Firefox right click --> Inspect Element --> Storage) we will see:

A cookie shown in a browser

that there is a cookie in our browser which has a value equals to cars. However this cookie has no name as we did not define one. So let's define one as it will be easier to manage the day we will set many different cookies. In order to give a name to our cookie we nee to structure it like this:

document.cookie = "interest=cars";

As a result if we modify our script into:

document.cookie = "interest=cars";

we will get in our browser:

A cookies with a name

When you create a simple cookie his expiry date is when the browser is closed. This is what we call a session cookie. If you set an expiry date, your cookie is then what we call a persistent cookie.

As you can read a cookie as different attributes:

  • Name: the name you are giving to your cookie or the one of the solution who sets it.
  • Domain: the domain name on which you were when the cookie has been set.
  • Path: the associated path.
  • Expiry date: the date at which the cookie will disappear.
  • Last time the cookie was updated.
  • Value: the value you want to store.
  • ...

How cookies are concerned when it is about analytics software?

In analytics, cookies are used to:

  • identify a visitor, so when the visitor comes for the first time on your website, a cookie is stored on its browser in order to identify it the next time.
  • identify a visit. Without a session cookie each pageview would count as a new visit.
  • identify the referrer url, it will then help identify the different ways a visitor used in order to visit your website.
  • testing purposes.
Last modified: Wednesday, 29 July 2020, 4:45 PM