Refill: 7-day idle follow-up (unanswered out notice, >=100 credits, nothing running; once per record, tracked as state idle); ad click relay ignores an advertiser opening their own ad
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,8 @@ let X = {}, FILE = null, S = null;
|
|||||||
const DAY = 86400000;
|
const DAY = 86400000;
|
||||||
const LOOKBACK = 45 * DAY; // a send older than this stops being watched for a conversion
|
const LOOKBACK = 45 * DAY; // a send older than this stops being watched for a conversion
|
||||||
const MAX_PER_TICK = 25; // owners emailed in one pass, so a backfill goes out in batches
|
const MAX_PER_TICK = 25; // owners emailed in one pass, so a backfill goes out in batches
|
||||||
|
const IDLE_AFTER = 7 * DAY; // follow-up: an "out" notice this old, still unanswered, with credits idle and nothing running (Marty, 2026-09-30)
|
||||||
|
const IDLE_MIN_CREDITS = 100; // enough to relaunch without buying
|
||||||
|
|
||||||
function init(opts) { X = opts; FILE = path.join(opts.dataDir, 'refill.json'); S = load(); }
|
function init(opts) { X = opts; FILE = path.join(opts.dataDir, 'refill.json'); S = load(); }
|
||||||
function load() { try { return JSON.parse(fs.readFileSync(FILE, 'utf8')); } catch (e) { return { sent: {}, seq: 0 }; } }
|
function load() { try { return JSON.parse(fs.readFileSync(FILE, 'utf8')); } catch (e) { return { sent: {}, seq: 0 }; } }
|
||||||
@@ -89,18 +91,23 @@ async function compose(owner, list, state, credits) {
|
|||||||
const totalImps = list.reduce((n, c) => n + n0(c.imps), 0);
|
const totalImps = list.reduce((n, c) => n + n0(c.imps), 0);
|
||||||
const totalClicks = list.reduce((n, c) => n + n0(c.clicks), 0);
|
const totalClicks = list.reduce((n, c) => n + n0(c.clicks), 0);
|
||||||
const canRelaunch = credits >= 100;
|
const canRelaunch = credits >= 100;
|
||||||
const subject = state === 'out'
|
const subject = state === 'idle' ? money(Math.round(credits)) + ' credits are sitting idle on your InstantAdPay account'
|
||||||
|
: state === 'out'
|
||||||
? (many ? 'Your InstantAdPay ads have finished delivering' : 'Your ad "' + (list[0].name || 'campaign') + '" has finished delivering')
|
? (many ? 'Your InstantAdPay ads have finished delivering' : 'Your ad "' + (list[0].name || 'campaign') + '" has finished delivering')
|
||||||
: (many ? 'Your InstantAdPay ads are nearly out of credits' : 'Your ad "' + (list[0].name || 'campaign') + '" is nearly out of credits');
|
: (many ? 'Your InstantAdPay ads are nearly out of credits' : 'Your ad "' + (list[0].name || 'campaign') + '" is nearly out of credits');
|
||||||
const opener = state === 'out'
|
const opener = state === 'idle'
|
||||||
|
? 'A week ago ' + (many ? 'your ads' : 'your ad "' + (list[0].name || 'campaign') + '"') + ' finished delivering, and nothing has run since. Your credits are still there, and nobody is seeing your link while they sit.'
|
||||||
|
: state === 'out'
|
||||||
? (many ? 'Your ' + list.length + ' campaigns have spent their full budget and stopped. They delivered everything you paid for.'
|
? (many ? 'Your ' + list.length + ' campaigns have spent their full budget and stopped. They delivered everything you paid for.'
|
||||||
: 'That campaign spent its full budget and stopped. It delivered everything you paid for.')
|
: 'That campaign spent its full budget and stopped. It delivered everything you paid for.')
|
||||||
: (many ? 'Your ' + list.length + ' campaigns are close to spending their full budget. When they do, they stop.'
|
: (many ? 'Your ' + list.length + ' campaigns are close to spending their full budget. When they do, they stop.'
|
||||||
: 'That campaign is close to spending its full budget. When it does, it stops.');
|
: 'That campaign is close to spending its full budget. When it does, it stops.');
|
||||||
const totals = (totalImps || totalClicks)
|
const totals = (totalImps || totalClicks)
|
||||||
? 'Between them so far: ' + money(totalImps) + ' impressions and ' + money(totalClicks) + ' clicks.'
|
? 'Between them so far: ' + money(totalImps) + (list.every(c => c.type === 'video') ? ' watches' : ' impressions') + ' and ' + money(totalClicks) + ' clicks.'
|
||||||
: '';
|
: '';
|
||||||
const next = canRelaunch
|
const next = state === 'idle'
|
||||||
|
? money(Math.round(credits)) + ' credits are ready to run. A banner or text ad takes a minute to set up and starts showing the moment you launch it, no purchase needed: ' + SITE + '/my#campaigns'
|
||||||
|
: canRelaunch
|
||||||
? 'You still have ' + money(Math.round(credits)) + ' credits in your balance, which is enough to put an ad back up right now without buying anything. Open Campaigns and start one: ' + SITE + '/my#campaigns'
|
? 'You still have ' + money(Math.round(credits)) + ' credits in your balance, which is enough to put an ad back up right now without buying anything. Open Campaigns and start one: ' + SITE + '/my#campaigns'
|
||||||
: 'Your balance is down to ' + money(Math.round(credits)) + ' credits, so the next campaign needs a top-up first: ' + SITE + '/my#buy';
|
: 'Your balance is down to ' + money(Math.round(credits)) + ' credits, so the next campaign needs a top-up first: ' + SITE + '/my#buy';
|
||||||
const lines = [opener, totals, next, ladderText(),
|
const lines = [opener, totals, next, ladderText(),
|
||||||
@@ -149,12 +156,34 @@ async function tick() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
await checkConversions();
|
await checkConversions();
|
||||||
|
sent += await followUps(all, sent);
|
||||||
if (sent) { save(); console.log('refill notices sent:', sent); }
|
if (sent) { save(); console.log('refill notices sent:', sent); }
|
||||||
return { sent };
|
return { sent };
|
||||||
} finally { running = false; }
|
} finally { running = false; }
|
||||||
}
|
}
|
||||||
|
|
||||||
async function notify(owner, list, state) {
|
// Seven days after an "out" notice that went unanswered: if the owner still holds enough credits to relaunch
|
||||||
|
// and has nothing running, one more nudge. Once per out-record; tracked like any other send.
|
||||||
|
async function followUps(all, alreadySent) {
|
||||||
|
S.records = S.records || {};
|
||||||
|
const now = Date.now(); let sent = 0;
|
||||||
|
const due = Object.values(S.records).filter(r => r.state === 'out' && !r.converted && !r.followUp && now - r.ts >= IDLE_AFTER && now - r.ts < LOOKBACK);
|
||||||
|
for (const r of due) {
|
||||||
|
if (alreadySent + sent >= MAX_PER_TICK) break;
|
||||||
|
const running = all.some(c => c.owner === r.owner && !c.house && c.status === 'active');
|
||||||
|
if (running) { r.followUp = 'running'; continue; } // something is live: no nudge, and never revisit this record
|
||||||
|
let credits = 0; try { credits = await X.creditsFor(r.owner); } catch (e) {}
|
||||||
|
if (credits < IDLE_MIN_CREDITS) { r.followUp = 'nocredits'; continue; }
|
||||||
|
const list = r.campaigns.map(x => all.find(c => c.id === x.id) || x);
|
||||||
|
const ok = await notify(r.owner, list, 'idle', r.id).catch(e => { console.error('refill follow-up', r.owner, e.message); return false; });
|
||||||
|
r.followUp = ok ? S.seq : 'failed';
|
||||||
|
if (ok) sent++;
|
||||||
|
}
|
||||||
|
if (due.length) save();
|
||||||
|
return sent;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function notify(owner, list, state, parentId) {
|
||||||
const acct = await X.accounts.byEmail(owner); if (!acct) return false;
|
const acct = await X.accounts.byEmail(owner); if (!acct) return false;
|
||||||
const stopped = X.drip && X.drip.isUnsubscribed ? await X.drip.isUnsubscribed(owner).catch(() => false) : false;
|
const stopped = X.drip && X.drip.isUnsubscribed ? await X.drip.isUnsubscribed(owner).catch(() => false) : false;
|
||||||
let credits = 0; try { credits = await X.creditsFor(owner); } catch (e) {}
|
let credits = 0; try { credits = await X.creditsFor(owner); } catch (e) {}
|
||||||
@@ -165,9 +194,9 @@ async function notify(owner, list, state) {
|
|||||||
const rec = {
|
const rec = {
|
||||||
id: ++S.seq, owner, state, ts: Date.now(), emailed: !stopped && X.mailer.hasKey(),
|
id: ++S.seq, owner, state, ts: Date.now(), emailed: !stopped && X.mailer.hasKey(),
|
||||||
campaigns: list.map(c => ({ id: c.id, name: c.name || null, type: c.type || null, budget: Math.round(n0(c.budget)), delivered: Math.round(delivered(c)), imps: n0(c.imps), clicks: n0(c.clicks) })),
|
campaigns: list.map(c => ({ id: c.id, name: c.name || null, type: c.type || null, budget: Math.round(n0(c.budget)), delivered: Math.round(delivered(c)), imps: n0(c.imps), clicks: n0(c.clicks) })),
|
||||||
creditsAtSend: Math.round(credits), memberId: acct.memberId || 0, converted: null,
|
creditsAtSend: Math.round(credits), memberId: acct.memberId || 0, converted: null, parent: parentId || null,
|
||||||
};
|
};
|
||||||
for (const cm of list) S.sent[key(cm.id, state)] = rec.id;
|
if (state !== 'idle') for (const cm of list) S.sent[key(cm.id, state)] = rec.id;
|
||||||
S.records = S.records || {}; S.records[rec.id] = rec;
|
S.records = S.records || {}; S.records[rec.id] = rec;
|
||||||
save();
|
save();
|
||||||
return true;
|
return true;
|
||||||
@@ -196,7 +225,7 @@ async function checkConversions() {
|
|||||||
if (fresh) { mark(r, 'relaunch', { campaignId: fresh.id, name: fresh.name || null }); continue; }
|
if (fresh) { mark(r, 'relaunch', { campaignId: fresh.id, name: fresh.name || null }); continue; }
|
||||||
// only meaningful for an ad that had actually stopped: a "running low" ad is still active by
|
// only meaningful for an ad that had actually stopped: a "running low" ad is still active by
|
||||||
// definition, so counting that as a response would mark every low notice converted on the spot
|
// definition, so counting that as a response would mark every low notice converted on the spot
|
||||||
if (r.state === 'out') {
|
if (r.state === 'out' || r.state === 'idle') {
|
||||||
const revived = all.find(c => c.status === 'active' && r.campaigns.some(x => x.id === c.id) && delivered(c) < n0(c.budget));
|
const revived = all.find(c => c.status === 'active' && r.campaigns.some(x => x.id === c.id) && delivered(c) < n0(c.budget));
|
||||||
if (revived) { mark(r, 'resumed', { campaignId: revived.id, name: revived.name || null }); continue; }
|
if (revived) { mark(r, 'resumed', { campaignId: revived.id, name: revived.name || null }); continue; }
|
||||||
}
|
}
|
||||||
@@ -215,7 +244,7 @@ function mark(r, kind, detail) {
|
|||||||
function stats() {
|
function stats() {
|
||||||
S.records = S.records || {};
|
S.records = S.records || {};
|
||||||
const recs = Object.values(S.records).sort((a, b) => b.ts - a.ts);
|
const recs = Object.values(S.records).sort((a, b) => b.ts - a.ts);
|
||||||
const by = { out: { sent: 0, converted: 0 }, low: { sent: 0, converted: 0 } };
|
const by = { out: { sent: 0, converted: 0 }, low: { sent: 0, converted: 0 }, idle: { sent: 0, converted: 0 } };
|
||||||
const kinds = {};
|
const kinds = {};
|
||||||
let revenueCents = 0, hours = [];
|
let revenueCents = 0, hours = [];
|
||||||
for (const r of recs) {
|
for (const r of recs) {
|
||||||
|
|||||||
@@ -2970,9 +2970,12 @@ const server = http.createServer(async (req, res) => {
|
|||||||
}
|
}
|
||||||
m = /^\/api\/ads\/click\/(\d+)$/.exec(p);
|
m = /^\/api\/ads\/click\/(\d+)$/.exec(p);
|
||||||
if (m && req.method === 'GET') {
|
if (m && req.method === 'GET') {
|
||||||
const target = await ads.click(m[1]);
|
// 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) {}
|
||||||
|
const target = own ? await ads.targetOf(m[1]) : await ads.click(m[1]);
|
||||||
if (!target) { res.writeHead(404, baseHeaders()); return res.end(); }
|
if (!target) { res.writeHead(404, baseHeaders()); return res.end(); }
|
||||||
coach.recordClick(m[1], req.headers.referer); // where the click happened
|
if (!own) coach.recordClick(m[1], req.headers.referer); // where the click happened
|
||||||
res.writeHead(302, baseHeaders({ Location: target }));
|
res.writeHead(302, baseHeaders({ Location: target }));
|
||||||
return res.end();
|
return res.end();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user