Skip to content

Instantly share code, notes, and snippets.

@piroor
Created December 21, 2022 10:15
Show Gist options
  • Save piroor/d559859729a1f5d811744f2da6b9c668 to your computer and use it in GitHub Desktop.
Save piroor/d559859729a1f5d811744f2da6b9c668 to your computer and use it in GitHub Desktop.
demonstration of third-party tracker cookie
/*
Demonstration of third-party tracker cookie
Original: https://iq.opengenus.org/third-party-cookies/
How to run:
1. Download this as a file `demo.js`.
2. Run `npm install express` and `npm install cookie-parser`.
3. Run `node demo.js`.
4. Open `http://127.0.0.1:3000`, `http://127.0.0.2:3000`, and `http://127.0.0.3:3000` on your web browser.
5. Reload those tabs again and again.
Expected result if third-party tracker cookies are blocked:
You'll see different "[tracker] visitor is *" message for each tab, or
you'll see "[tracker] new visitor: *" for every loading.
Expected result if third-party tracker cookies are not blocked:
You'll see same "[tracker] visitor is *" message for all tabs.
*/
const express = require('express');
const cookieParser = require('cookie-parser');
// tracker
(async () => {
const app = express();
const port = 8000;
app.use(cookieParser());
app.get('/', (request, response) => res.send('tracker'))
let count = 0;
app.get('/tracker', (request, response) => {
count++;
if (!request.cookies || !request.cookies["third-party"]){
response.cookie('third-party', count.toString(), { maxAge: 9999999 });
console.log('[tracker] new visitor: ', count.toString());
}
else{
console.log('[tracker] visitor is ', request.cookies);
}
response.send('');
});
app.listen(port, () => console.log(`tracker listening on the port ${port}`))
})();
(async () => {
const app = express();
const port = 3000;
const cookieParser = require('cookie-parser');
app.use(cookieParser());
app.get('/', (request, response) => {
response.cookie('first-party', 'first');
response.type('html');
response.send(`
<!DOCTYPE html>
<head>
</head>
<body onload="getCookie()">
<script type="application/x-javascript" src="http://127.0.0.254:8000/tracker"></script>
<p>Cookie:
<script type="application/x-javascript">
document.write(document.cookie);
</script></p>
</body>
</html>
`.trim()
);
});
app.listen(port, () => console.log(`Example website app listening on the port ${port}`))
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment