Conversion tracking, click side: every /api/ads/click gets a click id (i + 11 hex) appended as lsclk=<id> (existing params and hash kept, an existing lsclk never replaced, unparsable targets unchanged), the owner's own click flagged self. Rows in ad_clicks (MySQL) or a capped JSON file, pushed signed to LinkSpin /api/partner/clicks every 3 s (500 per request, retry with backoff, marked pushed only on ok). Member proxies /api/my/conversions (summary, 60 s cache), /goal (create/update/remove), /test. Admin lssync view shows push stats.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-23 18:01:02 -05:00
parent ebdab6c545
commit 46bb7dd9dd
2 changed files with 191 additions and 4 deletions
+37 -4
View File
@@ -48,6 +48,7 @@ const videosweep = require('./videosweep');
const sbcheck = require('./sbcheck');
const reach = require('./reach');
const lssync = require('./lssync');
const lsconv = require('./lsconv'); // conversion tracking: ad click ids pushed to LinkSpin + the member's goals (2026-10-23)
const lsinbound = require('./lsinbound'); // reverse registry sync: LinkSpin-first members arrive as waiting accounts (2026-10-15)
const SF = require('./sponsorfollow'); // deliberate sponsor changes follow to LinkSpin until chain-bound + one network tank (2026-10-22)
let follow = null, followStore = null;
@@ -517,6 +518,7 @@ async function boot() {
await db.init({ dataDir: DATA_DIR }); // no-op without DATABASE_URL (JSON mode)
marks.init({ db });
marks.ensureTable().catch(e => console.error('marks table', e.message));
lsconv.init({ db, dataDir: DATA_DIR });
setInterval(() => marks.prune().catch(() => {}), 6 * 3600 * 1000).unref();
chain.init({ onEvent: ev => {
// the in-memory feed is per instance and always runs
@@ -3176,7 +3178,7 @@ const server = http.createServer(async (req, res) => {
if (p === '/api/admin/walletnudge' && req.method === 'GET') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, walletnudge.adminView()); }
if (p === '/api/admin/reach' && req.method === 'GET') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, reach.adminView()); }
if (p === '/api/admin/reach/run' && req.method === 'POST') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); const b = await readBody(req); return json(res, 200, await reach.run({ dry: !!(b && b.dry) })); }
if (p === '/api/admin/lssync' && req.method === 'GET') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, Object.assign(lssync.adminView(), { inbound: lsinbound.adminView() })); }
if (p === '/api/admin/lssync' && req.method === 'GET') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, Object.assign(lssync.adminView(), { inbound: lsinbound.adminView(), conv: lsconv.adminView() })); }
if (p === '/api/admin/lsinbound' && req.method === 'GET') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); return json(res, 200, lsinbound.adminView()); }
// -- reverse registry sync from LinkSpin (2026-10-15): the same signed rows LinkSpin accepts from us (HMAC LS_SYNC_SECRET
// over ts + newline + raw body, 10-minute window). New people become waiting accounts with their LinkSpin line; existing
@@ -4680,14 +4682,45 @@ const server = http.createServer(async (req, res) => {
m = /^\/api\/ads\/click\/(\d+)$/.exec(p);
if (m && req.method === 'GET') {
// an advertiser opening their own ad's link (from Campaigns, a preview, a test) is not a click: relay it uncounted (Marty, 2026-09-30)
let own = false;
try { const s = await auth.fromRequest(req); if (s && s.email) { const cm = await ads.campaignById(m[1]); own = !!(cm && cm.owner && String(cm.owner).toLowerCase() === String(s.email).toLowerCase()); } } catch (e) {}
let own = false, cm = null;
try { cm = await ads.campaignById(m[1]); } catch (e) {}
try { const s = cm && await auth.fromRequest(req); if (s && s.email) own = !!(cm.owner && String(cm.owner).toLowerCase() === String(s.email).toLowerCase()); } catch (e) {}
const target = own ? await ads.targetOf(m[1]) : await ads.click(m[1]);
if (!target) { res.writeHead(404, baseHeaders()); return res.end(); }
if (!own) coach.recordClick(m[1], req.headers.referer); // where the click happened
res.writeHead(302, baseHeaders({ Location: target }));
// conversion tracking (2026-10-23): every click, the owner's own too (flagged self, so they can test), gets a click id
// on the target URL as lsclk=<id>; the row goes to LinkSpin, which matches the advertiser's goal pixel to it
let loc = target;
if (cm) { try { const ip = clientIp(req); const row = await lsconv.record(cm, req, { self: own, target, ip, country: geo.countryOf(ip) || null }); loc = lsconv.withClickId(target, row.id); } catch (e) { console.error('lsconv record', e.message); } }
res.writeHead(302, baseHeaders({ Location: loc }));
return res.end();
}
// Campaigns > Conversion tracking: the member's goals and counts live on LinkSpin; these proxy the signed calls
if (p === '/api/my/conversions' && req.method === 'GET') {
const s = await auth.fromRequest(req);
if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
if (!lsconv.on()) return json(res, 200, { ok: false, reason: 'off', error: 'Conversion tracking is not switched on yet. Your ads and clicks work as usual.' });
try { return json(res, 200, Object.assign({}, await lsconv.summary(s.email, u.searchParams.get('fresh') === '1'), { ok: true })); }
catch (e) { return json(res, 200, { ok: false, reason: 'down', error: 'LinkSpin, the service that counts your conversions, did not answer just now. Your ads and clicks are fine. Try again in a minute.' }); }
}
if ((p === '/api/my/conversions/goal' || p === '/api/my/conversions/test') && req.method === 'POST') {
const s = await auth.fromRequest(req);
if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });
if (!lsconv.on()) return json(res, 503, { error: 'Conversion tracking is not switched on yet.' });
const b = await readBody(req) || {};
const op = p.endsWith('/test') ? 'test' : String(b.op || '');
if (!['create', 'update', 'remove', 'test'].includes(op)) return json(res, 400, { error: 'Unknown action.' });
const f = {};
if (op !== 'create') { const px = String(b.px || '').trim().toLowerCase(); if (!/^px[0-9a-f]{10}$/.test(px)) return json(res, 400, { error: 'That pixel id does not look right.' }); f.px = px; }
if (op === 'create' || op === 'update') {
if (b.name != null) { const nm = String(b.name).trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9_-]/g, '').slice(0, 32); if (!nm) return json(res, 400, { error: 'Give the goal a short name, like subscriber or sale (letters, numbers, dashes).' }); f.name = nm; }
else if (op === 'create') return json(res, 400, { error: 'Give the goal a short name, like subscriber or sale.' });
if (b.value != null && String(b.value).trim() !== '') { const v = Number(b.value); if (!isFinite(v) || v < 0 || v > 1e6) return json(res, 400, { error: 'The value should be a number, like 19.95, or left empty.' }); f.value = Math.round(v * 100) / 100; }
else if (op === 'update' && b.value != null) f.value = null;
}
try { return json(res, 200, Object.assign({ ok: true }, await lsconv.goalOp(s.email, op, f))); }
catch (e) { return json(res, e.plain && e.status && e.status < 500 ? 400 : 502, { error: e.plain ? e.message : 'LinkSpin did not answer just now. Nothing was changed. Try again in a minute.' }); }
}
if (p === '/api/my/trace' && req.method === 'GET') { // payment trace for yourself or anyone within 3 levels of you, either direction
const s = await auth.fromRequest(req);
if (!s || !s.email) return json(res, 401, { error: 'Sign in first.' });