Prototype mesure d'audience par UID localStorage (collecteur, snippet, agrégateur, tests)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Cette révision appartient à :
Théo GastineletClaude Sonnet 5.5 a révisé 2026-10-24 15:21:38 +02:00
révision 41c57f6cd2
7 fichiers modifiés
+414

Aucun fichier ne correspond à votre recherche

+19
Voir le fichier
@@ -0,0 +1,19 @@
<!doctype html>
<html lang="fr"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Démo mesure d'audience</title>
<script src="/m.js" defer></script>
<style>body{font:16px system-ui;max-width:40rem;margin:2rem auto;padding:0 1rem}button{margin:.2rem}</style>
</head><body>
<h1>Démo mesure d'audience (local)</h1>
<p>Chaque chargement envoie un événement à <code>/m</code>. <a href="/page2.html">Page 2</a></p>
<p>UID stocké : <code id="u">?</code></p>
<button id="off">Ne plus être compté</button> <button id="on">Être compté à nouveau</button>
<script>
document.addEventListener("DOMContentLoaded", function () {
var show = function () { document.getElementById("u").textContent = localStorage.getItem("gm_uid") || (localStorage.getItem("gm_optout") ? "(opposition enregistrée)" : "(aucun)"); };
document.getElementById("off").onclick = function () { gmOptOut(); show(); };
document.getElementById("on").onclick = function () { gmOptOut(false); show(); };
show();
});
</script>
</body></html>
+24
Voir le fichier
@@ -0,0 +1,24 @@
/* Mesure d'audience gastinel.me — UID aléatoire en localStorage, 13 mois max, sans prolongation. */
(function (w, n, d) {
var K = "gm_uid", O = "gm_optout", MAX = 395 * 864e5, ls;
try { ls = w.localStorage; } catch (e) { return; }
w.gmOptOut = function (on) {
try { if (on === false) ls.removeItem(O); else { ls.setItem(O, "1"); ls.removeItem(K); } } catch (e) {}
};
if (n.doNotTrack == "1" || w.doNotTrack == "1" || n.globalPrivacyControl === true) return;
try { if (ls.getItem(O) == "1") return; } catch (e) { return; }
var v, now = Date.now();
try { v = JSON.parse(ls.getItem(K) || "null"); } catch (e) { v = null; }
if (!v || !/^[0-9a-f]{32}$/.test(v.id) || !(now - v.c < MAX) || v.c > now + 864e5) {
var b = new Uint8Array(16), h = "";
w.crypto.getRandomValues(b);
for (var i = 0; i < 16; i++) h += (b[i] + 256).toString(16).slice(1);
v = { id: h, c: now };
try { ls.setItem(K, JSON.stringify(v)); } catch (e) { return; }
}
var r = "";
try { r = d.referrer ? new URL(d.referrer).hostname : ""; } catch (e) {}
var body = JSON.stringify({ u: v.id, p: location.pathname.slice(0, 200), r: r.slice(0, 100),
m: w.matchMedia && w.matchMedia("(pointer:coarse)").matches ? 1 : 0 });
if (n.sendBeacon) n.sendBeacon("/m", body);
})(window, navigator, document);
+3
Voir le fichier
@@ -0,0 +1,3 @@
<!doctype html>
<html lang="fr"><head><meta charset="utf-8"><title>Démo page 2</title><script src="/m.js" defer></script></head>
<body><h1>Page 2</h1><p><a href="/">Retour</a></p></body></html>