Page leads count as conversions again; pixel keeps the event name; cookieless pixel hits stop inflating audiences (Marty approved, 2026-10-22)
B2: the built-in page form posted an empty lsid (pt.js only filled .lsform, the pasted-form class), so page leads never
reached the rotator 'lead' conversion or the postback. The server now fills the hidden lsid at render from ?lsid or the
ls.click cookie, the lead POST falls back to the ls.click cookie when the form sends none, and pt.js fills both .ls-form
and .lsform as a fallback (cache tag pt.js?v=2). Pasted provider forms get no added field: some providers reject unknown
fields, so the lsid only rides along when the member's own form code has an lsid field.
B4: /p.gif now stores the pixel's e= event name (lowercase [a-z0-9_-]{1,32}) in a parallel ev map in audiences.json;
hits stay plain timestamps, so old rows count, match and prune as before. The audiences list shows counts by event
(events for cookie visitors, eventsSoft for 24 h soft keys).
B3: a pixel hit with no ls.vid cookie still offers the cookie but is counted under the 24 h IP + user agent key only,
not a fresh 30-day visitor id, so a cookie-blocking browser counts once.
Test: qa/lsid-pixel.test.mjs (bash qa/run.sh lsid), unit + local end to end on a throwaway data dir.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+17
-11
@@ -5,7 +5,8 @@
|
||||
// run. Extra audiences come from the pixel tag (<script src=/p.js data-aud=la-xxxxxx>) pasted on any site.
|
||||
// - Membership lasts 30 days from the last hit. Nothing personal is stored: hashes, timestamps, an optional event name.
|
||||
// - Serving: ads.js asks opts.audienceHas(id) for each targeted campaign; the server builds that closure per request.
|
||||
// Storage: DATA_DIR/audiences.json { v, audiences: { id: {id, owner, name, auto, created} }, hits: { id: { key: lastTs } } }
|
||||
// Storage: DATA_DIR/audiences.json { v, audiences: { id: {id, owner, name, auto, created} }, hits: { id: { key: lastTs } },
|
||||
// ev: { id: { key: lastEventName } } } (2026-10-22: the pixel's e= name, a parallel map so old hits rows stay as they are)
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
@@ -13,7 +14,7 @@ const crypto = require('crypto');
|
||||
const DAY = 86400000, KEEP_MS = 30 * DAY, SOFT_MS = 24 * 3600000, MAX_PER_OWNER = 20;
|
||||
let FILE = null, S = null, dirty = false, hitsSincePrune = 0;
|
||||
|
||||
function load() { if (S) return S; try { S = JSON.parse(fs.readFileSync(FILE, 'utf8')); } catch (e) { S = { v: 1, audiences: {}, hits: {} }; } S.audiences = S.audiences || {}; S.hits = S.hits || {}; return S; }
|
||||
function load() { if (S) return S; try { S = JSON.parse(fs.readFileSync(FILE, 'utf8')); } catch (e) { S = { v: 1, audiences: {}, hits: {}, ev: {} }; } S.audiences = S.audiences || {}; S.hits = S.hits || {}; S.ev = S.ev || {}; return S; }
|
||||
function save() { dirty = true; }
|
||||
function flush() { if (!dirty || !S) return; dirty = false; try { fs.writeFileSync(FILE + '.tmp', JSON.stringify(S)); fs.renameSync(FILE + '.tmp', FILE); } catch (e) { console.error('audiences save', e.message); } }
|
||||
function init(opts) { FILE = path.join(opts.dataDir, 'audiences.json'); load(); setInterval(flush, 5000).unref(); setInterval(prune, 3600000).unref(); }
|
||||
@@ -29,9 +30,10 @@ function list(owner) {
|
||||
.map(a => Object.assign({}, a, sizes(a.id)));
|
||||
}
|
||||
function sizes(id) {
|
||||
const h = load().hits[id] || {}; const now = Date.now(); let s30 = 0, s7 = 0, soft = 0;
|
||||
for (const k in h) { const t = h[k]; if (k[0] === 's' && k[1] === ':') { if (now - t <= SOFT_MS) soft++; continue; } if (now - t <= KEEP_MS) s30++; if (now - t <= 7 * DAY) s7++; }
|
||||
return { size30: s30, size7: s7, soft };
|
||||
const h = load().hits[id] || {}; const ev = S.ev[id] || {}; const now = Date.now(); let s30 = 0, s7 = 0, soft = 0; const events = {}, eventsSoft = {};
|
||||
// events: last pixel event name per counted visitor, split like the sizes (cookie visitors 30 days, soft keys 24 h)
|
||||
for (const k in h) { const t = h[k]; if (k[0] === 's' && k[1] === ':') { if (now - t <= SOFT_MS) { soft++; if (ev[k]) eventsSoft[ev[k]] = (eventsSoft[ev[k]] || 0) + 1; } continue; } if (now - t <= KEEP_MS) { s30++; if (ev[k]) events[ev[k]] = (events[ev[k]] || 0) + 1; } if (now - t <= 7 * DAY) s7++; }
|
||||
return { size30: s30, size7: s7, soft, events, eventsSoft };
|
||||
}
|
||||
function owns(owner, id) { const a = load().audiences[String(id || '')]; return !!(a && a.owner === norm(owner)); }
|
||||
function get(id) { return load().audiences[String(id || '')] || null; }
|
||||
@@ -59,7 +61,7 @@ function rename(owner, id, name) {
|
||||
function remove(owner, id) {
|
||||
const a = get(id); if (!a || a.owner !== norm(owner)) return { error: 'No such audience.' };
|
||||
if (a.auto) return { error: 'The automatic audience cannot be removed. Campaigns that use it will keep working.' };
|
||||
delete load().audiences[id]; delete load().hits[id]; save(); return { ok: true };
|
||||
delete load().audiences[id]; delete load().hits[id]; delete load().ev[id]; save(); return { ok: true };
|
||||
}
|
||||
|
||||
// ---- visitor keys
|
||||
@@ -72,10 +74,13 @@ function newVid() { return crypto.randomBytes(10).toString('hex'); }
|
||||
function cookieHeader(vid, prod) { return 'ls.vid=' + vid + '; Path=/; Max-Age=31536000; ' + (prod ? 'SameSite=None; Secure' : 'SameSite=Lax'); }
|
||||
|
||||
// ---- hits + membership
|
||||
function hit(id, vid, soft) {
|
||||
// the pixel's optional event name: lowercase, [a-z0-9_-]{1,32}, anything else dropped
|
||||
function cleanEvent(e) { const v = String(e == null ? '' : e).trim().toLowerCase(); return /^[a-z0-9_-]{1,32}$/.test(v) ? v : ''; }
|
||||
function hit(id, vid, soft, event) {
|
||||
const st = load(); if (!st.audiences[id]) return false;
|
||||
const h = st.hits[id] = st.hits[id] || {}; const now = Date.now();
|
||||
if (vid) h[vid] = now; if (soft) h[soft] = now;
|
||||
const e = cleanEvent(event); if (e) { const ev = st.ev[id] = st.ev[id] || {}; if (vid) ev[vid] = e; if (soft) ev[soft] = e; }
|
||||
save(); if (++hitsSincePrune > 2000) { hitsSincePrune = 0; prune(); }
|
||||
return true;
|
||||
}
|
||||
@@ -88,10 +93,11 @@ function has(id, vid, soft) {
|
||||
function prune() {
|
||||
const st = load(); const now = Date.now(); let n = 0;
|
||||
for (const id in st.hits) {
|
||||
if (!st.audiences[id]) { delete st.hits[id]; continue; }
|
||||
const h = st.hits[id];
|
||||
for (const k in h) { const lim = (k[0] === 's' && k[1] === ':') ? SOFT_MS : KEEP_MS; if (now - h[k] > lim) { delete h[k]; n++; } }
|
||||
if (!st.audiences[id]) { delete st.hits[id]; delete st.ev[id]; continue; }
|
||||
const h = st.hits[id]; const ev = st.ev[id];
|
||||
for (const k in h) { const lim = (k[0] === 's' && k[1] === ':') ? SOFT_MS : KEEP_MS; if (now - h[k] > lim) { delete h[k]; if (ev) delete ev[k]; n++; } }
|
||||
}
|
||||
for (const id in st.ev) { const h = st.hits[id], ev = st.ev[id]; if (!h) { delete st.ev[id]; continue; } for (const k in ev) if (!(k in h)) delete ev[k]; }
|
||||
if (n) save(); flush(); return n;
|
||||
}
|
||||
// per-request closure for ads.js: opts.audienceHas(id)
|
||||
@@ -99,4 +105,4 @@ function matcher(req) { const vid = cookieVid(req), soft = softKey(req); return
|
||||
|
||||
// admin Member content (2026-10-18): the audiences members made by hand (every member has an automatic one, not listed)
|
||||
function adminAll() { return Object.values(load().audiences).filter(a => !a.auto).map(a => Object.assign({}, a, sizes(a.id))); }
|
||||
module.exports = { init, adminAll, list, create, rename, remove, owns, get, nameOf, defaultFor, hit, has, prune, flush, isId, cookieVid, newVid, cookieHeader, softKey, matcher };
|
||||
module.exports = { init, adminAll, list, create, rename, remove, owns, get, nameOf, defaultFor, hit, has, prune, flush, isId, cleanEvent, cookieVid, newVid, cookieHeader, softKey, matcher };
|
||||
|
||||
Reference in New Issue
Block a user