Conversion linkage, InstantAdPay side (Marty 2026-11-07, "Yes ship these"): a LinkSpin click stays with the account, and its sign-up, wallet link and each package purchase are reported to LinkSpin. Reporting only: no sponsor, payout, credit or chain path changes; accounts.js only gains a notification after a wallet is linked.

lsreport.js. A click id (lsclk or lsid) arriving on any page is kept in a first-party cookie iap.lsclk, 30 days, last click wins, like the sponsor cookie. The join page also sends its own copy with the code request and verify (the cookie is refused in traffic-exchange frames and never crosses browsers, the 2026-09-29 lesson) and it is remembered per email. It is stored in its own table ls_account_clicks when the account is created or a waiting LinkSpin shell is claimed, on /api/signup, email sign-in and in-game sign-in (a game may pass lsclk; none does yet).

Reports queue in ls_events, whose primary key is the idempotency key (join:<hash>, wallet:<hash>, purchase:<tx>:<log index>), so two app copies in a deploy cannot double a row, and LinkSpin dedupes on the same key. A background loop sends them signed, retrying up to every 5 minutes; a sign-up or a purchase never waits on LinkSpin. The purchase hook runs after the existing ev: claim and its value is the USD price on the chain event. A first purchase finds its buyer through the contract's wallet for that member id, since member_id may not be stored yet. Linked positions count as the account's purchases. Past 30 days from the sign-up nothing is queued (LinkSpin's window is from the click).

Campaigns > Conversion tracking lists everything the member's clicks led to, and joins, wallets and purchases on each campaign row. Guide, privacy page and chatbot (CANNED + FACTS) updated; common.js, join.js and my.js tags bumped.

QA: bash qa/run.sh netconv (54 checks: local InstantAdPay + local LinkSpin + a mock Polygon RPC, a real Purchase log through the real event fan-out, LinkSpin down, two copies racing).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-07 07:48:36 -06:00
parent bfbea142b6
commit 03284aa281
36 changed files with 487 additions and 39 deletions
+29 -2
View File
@@ -51,6 +51,7 @@ 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 lsreport = require('./lsreport'); // conversion linkage: the LinkSpin click on the account, sign-up / wallet / purchase reported, reporting only (2026-11-07)
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)
const bixpay = require('./bixpay'); // BixPay Visa card: partner-link field, sponsor-follows resolver, tracked clicks (2026-10-29, gated by site config bixpayLive)
@@ -535,6 +536,8 @@ async function boot() {
marks.init({ db });
marks.ensureTable().catch(e => console.error('marks table', e.message));
lsconv.init({ db, dataDir: DATA_DIR });
lsreport.init({ db, dataDir: DATA_DIR });
accounts.onWalletLinked(e => { lsreport.wallet(e).catch(err => console.error('lsreport wallet', err.message)); });
auth.onSeen((email, req) => lsconv.noteSession(email, clientIp(req), req.headers['user-agent'])); // own-click tests on LinkSpin (2026-10-26)
setInterval(() => marks.prune().catch(() => {}), 6 * 3600 * 1000).unref();
chain.init({ onEvent: ev => {
@@ -555,6 +558,7 @@ async function boot() {
wednesday.onEvent(ev).catch(e => console.error('wednesday bonus', e.message));
refuel.onIapPurchase(ev).catch(e => console.error('refuel streak', e.message)); // its own bonus, stacked on Friday/Wednesday; no-op while refuelStreak is off
sponsorSyncOnEvent(ev).catch(e => console.error('sponsor sync', e.message));
lsReportPurchase(ev).catch(e => console.error('lsreport purchase', e.message)); // reporting only: one local row, delivered in the background
}).catch(e => console.error('marks claim', e.message));
} });
auth.init({ dataDir: DATA_DIR, chain, isProd: IS_PROD, site: 'instantadpay.com' });
@@ -1056,6 +1060,15 @@ async function postWalkUp(buyerId, oldRef, toId, pur) {
// The referral follows the money (Marty, 2026-09-16): when a position is created on-chain under a
// different sponsor than the account's stored referrer (walk-up past an unactivated sponsor, or the
// catch-position fallback), move the account to the sponsor that actually got paid, #1 included.
// a package Purchase, for the click on the buyer's account (lsreport.js, reporting only). The buyer's account: main member id, a linked
// position, or on a first purchase, when member_id is not stored yet, through the wallet the contract holds for that id.
async function lsReportPurchase(ev) {
if (!ev || ev.type !== 'Purchase' || !ev.buyerId) return;
let a = await accounts.byMemberId(ev.buyerId);
if (!a) { const pos = await accounts.positionByMember(ev.buyerId); if (pos && pos.email) a = { email: pos.email }; }
if (!a) { const m = await chain.member(ev.buyerId); if (m && m.account) { a = await accounts.byAddress(m.account); if (!a) { const pos = await accounts.positionOwner(m.account); if (pos && pos.email) a = { email: pos.email }; } } }
if (a && a.email) await lsreport.purchase(ev, a.email);
}
async function sponsorSyncOnEvent(ev) {
if (!ev || ev.type !== 'MemberActivated' || !ev.account) return;
const acct = await accounts.byAddress(ev.account); if (!acct) return;
@@ -2264,6 +2277,15 @@ const server = http.createServer(async (req, res) => {
res.writeHead(302, { Location: to, 'Cache-Control': 'no-store', 'X-Robots-Tag': 'noindex' }); return res.end();
} }
// a LinkSpin click arriving on any page (?lsclk= or ?lsid=): the first-party cookie, 30 days, last click wins (reporting only, lsreport.js).
// Added to whatever the page sends, like the viral-link cookies below.
if (req.method === 'GET' && !p.startsWith('/api/') && !p.startsWith('/admin') && !/\.[a-z0-9]{2,5}$/i.test(p)) {
const lsc = lsreport.fromUrl(u);
if (lsc) {
const ck = lsreport.cookieHeader(lsc, IS_PROD); const wh = res.writeHead.bind(res);
res.writeHead = function (code, h) { if (typeof h === 'string') return wh.apply(res, arguments); h = Object.assign({}, h || {}); h['Set-Cookie'] = [].concat(res.getHeader('Set-Cookie') || [], h['Set-Cookie'] || [], ck); return wh(code, h); };
}
}
// -- traffic log: public page views by referring domain (admin > Traffic)
if (req.method === 'GET' && ((TRAFFIC_PAGES.has(p) && !(p === '/refuel' && u.searchParams.has('t'))) || /^\/(join|from|wall|blog)\/[^/]+$/.test(p))) traffic.hit(p.startsWith('/blog/') ? '/blog/*' : p, req.headers.referer, req.headers['user-agent']);
// -- viral links (Marty, 2026-09-21): ANY public page + ?ref=<username|code|id> is that member's
@@ -2628,6 +2650,7 @@ const server = http.createServer(async (req, res) => {
const r = await accounts.signup(b.email, b.password, ref);
if (r.error) return json(res, 400, r);
pendingSponsor.del(String(b.email || '').trim().toLowerCase());
await lsreport.signup(r.account.email, lsreport.pick(b.lsclk, parseCookies(req)[lsreport.COOKIE]), lsconv.softKey(clientIp(req), req.headers['user-agent'])).catch(e => console.error('lsreport signup', e.message));
// sponsor is notified once the new member picks a username (onboarding),
// so the email can name them — see /api/my/profile
if (b.newsletter) sendy.subscribe(r.account.email, r.account.username || '').catch(() => {}); // pre-checked opt-in, silent
@@ -2659,7 +2682,8 @@ const server = http.createServer(async (req, res) => {
const code = String(Math.floor(100000 + Math.random() * 900000));
emailCodes.set(e, { code, exp: Date.now() + 15 * 60 * 1000, tries: 0, nextAt: Date.now() + 60 * 1000 });
{ const ck = parseCookies(req); const bound = pageTok(b.ref) || ck['iap.sponsor'] || '';
if (bound && !(await accounts.byEmail(e))) pendingSponsor.set(e, { ref: bound, via: pageTok(b.ref) ? (String(b.angle || '').toLowerCase().slice(0, 20) || ck['iap.angle'] || 'page') : (ck['iap.angle'] || ''), joinedRef: decodeURIComponent(ck['iap.ref'] || '') || null }); }
if (bound && !(await accounts.byEmail(e))) pendingSponsor.set(e, { ref: bound, via: pageTok(b.ref) ? (String(b.angle || '').toLowerCase().slice(0, 20) || ck['iap.angle'] || 'page') : (ck['iap.angle'] || ''), joinedRef: decodeURIComponent(ck['iap.ref'] || '') || null });
const lsc = lsreport.pick(b.lsclk, ck[lsreport.COOKIE]); if (lsc) { const ex = await accounts.byEmail(e); if (!ex || accounts.isUnclaimedShell(ex)) lsreport.remember(e, lsc).catch(err => console.error('lsreport remember', err.message)); } } // the click travels with the email, like the sponsor
if (mailer.hasKey()) {
try { await mailer.sendCode(e, code); } catch (err) {
console.error('sendCode failed', err.message);
@@ -2701,6 +2725,7 @@ const server = http.createServer(async (req, res) => {
if (r.error) return json(res, 400, r);
if (r.created) pendingSponsor.del(e);
if (claim) await claimShell(e, existing, ref);
if (r.created || claim) await lsreport.signup(e, lsreport.pick(b.lsclk, parseCookies(req)[lsreport.COOKIE]), lsconv.softKey(clientIp(req), req.headers['user-agent'])).catch(err => console.error('lsreport signup', err.message));
if (r.created || claim) { fraud.recordSignup(e, req, fraudFlags).catch(() => {}); if (fraudFlags.length) fraudAlert(e, { flags: fraudFlags, ip: fraud.ipOf(req) }, null, false); }
else fraud.recordSeen(e, req).catch(() => {});
// the lead is in the door: queue the getting-started sequence (opt-in box is pre-checked on both forms)
@@ -2767,6 +2792,7 @@ const server = http.createServer(async (req, res) => {
const ref = pageTok(b.ref);
const srcTag = String(b.src || '').toLowerCase().replace(/[^a-z0-9_.-]/g, '').slice(0, 30); // paid-traffic source the game saw (2026-10-03)
if (!(await accounts.byEmail(e))) pendingSponsor.set(e, { ref: ref || '', via: String(b.game), joinedRef: g.host() + (srcTag ? '/' + srcTag : '') });
{ const lsc = lsreport.pick(b.lsclk, parseCookies(req)[lsreport.COOKIE]); if (lsc) { const ex = await accounts.byEmail(e); if (!ex || accounts.isUnclaimedShell(ex)) lsreport.remember(e, lsc).catch(err => console.error('lsreport remember', err.message)); } } // a game sends its own lsclk when it has one; no cookie crosses here
console.log('game-signin code', b.game, e.replace(/^(.).*(@.*)$/, '$1***$2'), ref ? 'ref ' + ref : '');
if (mailer.hasKey()) {
try { await mailer.sendCode(e, code); } catch (err) { console.error('sendCode failed', err.message); return json(res, 502, { error: 'Could not send the email. Try again in a minute.' }, cors); }
@@ -2803,6 +2829,7 @@ const server = http.createServer(async (req, res) => {
}
const r = await accounts.ensure(e, ref, via, joinedRef); // first touch wins; existing accounts unchanged
if (r.error) return json(res, 400, r, cors);
if (r.created || claim) await lsreport.signup(e, lsreport.pick(b.lsclk, parseCookies(req)[lsreport.COOKIE]), lsconv.softKey(clientIp(req), req.headers['user-agent'])).catch(err => console.error('lsreport signup', err.message));
if (claim) { await claimShell(e, existing, ref); fraud.recordSignup(e, req, fraudFlags).catch(() => {}); if (fraudFlags.length) fraudAlert(e, { flags: fraudFlags, ip: fraud.ipOf(req) }, null, false); }
if (r.created) {
pendingSponsor.del(e);
@@ -3508,7 +3535,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(), conv: lsconv.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(), report: lsreport.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