Table of Contents

How does a tracking cookie work? A step by step example

About

This page shows you a basic example of a trackingcookie so that you can understand and implement the underlying mechanisms.

These cookies are third-party cookie.

A tracking cookie is also known as:

Usage

They are a tracking techniques that allows cross site tracking

These are mainly used for advertising and tracking across the web.

They are sent back at every future HTTP request (fetch) of the third-party resources (image, script,…) to their origin (the third-party server - advertiser).

A Step by Step Example

How do you create a tracking server script?

The tracking resources are implemented with any HTTP server script.

These scripts produce a HTTP response that can send any type of media:

In this example, we use a php script to produce Javascript.

The below code is the content of the tracking.php file located on the server that is fetched in the next step.

The script reads and creates if necessary two third-party cookies:

To be able to receive a third-party cookie, you need to set on it the following cookie properties:

Example in php:

$crossSiteCookiesOptions = array (
  // cross-site cookie
  'samesite' => 'none'  // mandatory for cross-site tracking
  'secure' => true, // mandatory for samesite none
  // scope
  'domain' => 'combostrap.com', // Third party domain
  'path' => '/tracking/tracking.php',  // Path to the tracking script
  // other
  'httponly' => false, // false or true does not matter as it does not work on cross-request as specified by the standard
  'expires' => time() + 60*60*24*30, /* expire in 30 day */
);

Identifier and counter cookies

In this part, we read the cookie and create them if they don'exist.

$consumerCookieName = "identifier"; // the name of the cookie
$consumerId = $_COOKIE[$consumerCookieName]??uniqid(); // the value if exists or an uniqid
setcookie($consumerCookieName, $consumerId, $crossSiteCookiesOptions); 
$counteCookieName = "counter";
$counter = $_COOKIE[$counteCookieName]??0;
$counter++;
setcookie($counteCookieName, $counter, $crossSiteCookiesOptions); 
header("$counteCookieName: $counter"); // to be able to read the value in the browser

Browser Fetch Authorization or CORS

These authorizations are only mandatory if you want to use Javascript to fetch the script.

In our case, we do not, as we are embedding the script with the HTML script tag but if you want to dynamically use them on the browser, this configuration are mandatory.

Example: The CORS headers may be set to allow the browser javascript :

Access-Control-Allow-Origin: https://datacadamia.com
Access-Control-Expose-Headers: counter
Access-Control-Allow-Credentials: true

Example in php:

header('Access-Control-Allow-Origin: https://datacadamia.com');
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Expose-Headers: counter");

The produced javascript

We return the following javascript that prints a message generated with:

Example in php:

<?php
/** Return Javascript **/
header('content-type: text/javascript');
?>
console.log('Server: Hello User (<?php echo $consumerId ?>), you have fetched this script <?php echo $counter ?> times');

How can you show a javascript file in the URL in place of a Php file

This is just a trick but thanks to URL rewrite, you can redirect/rewrite any URL.

For example with the Apache htaccess configuration file, you can redirect an URL with the path tracking.js to tracking.php

RewriteBase /
RewriteRule ^tracking.js$ tracking.php

We don't do it in our example but most of the server does it.

You think that you fetch a file (image, javascript, stylesheet) but what you fetch, is just a HTTP response that sends back bytes in the body that have a type.

The tracking

The tracking works by just embedding this script in your pages.

Third-party tracking on this site

To insert the third-party cookie, we just need to call the tracking endpoint.

Example

<script type="text/javascript" src="https://combostrap.com/tracking/tracking.php"></script>

Result: (If you refresh this page, the counter will increment)

Third-party tracking on another site

Now:

It will bring you to a third editor where the same code will run.

You will see that:

You should see:

Server: Hello User (your Id), you have fetched this script x+1 times

First Party Tracking

This script works also as first party tracking cookie.

You should see:

Server: Hello User (your Id), you have fetched this script x+1 times

The tracking works with any HTML fetch tag

HTML tags that have an URL as an attribute execute a fetch. It means that you can rewrite the above php script to return:

<img href="https://combostrap.com/tracking/tracking.png"/>
<link rel="stylesheet" href="https://combostrap.com/tracking/tracking.php"></script>

This is easier than with a Javascript fetch because the script execution is controlled by the CORS header while HTML tags are not.

Format

A tracking cookie may take the form of a cookie that has always:

Browser

Authorization and Blockage

Because they are third-party cookies, they can be blocked via Browser Configuration

DevTool

In the devtool, you can check the cookie value that is sent and received. See Third-party cookies devtool

Devtool Tracking Cookie Identifier Counter