1b77825256
Engine Room fix: the streak line said "Refuel the other site this cycle" after BOTH sites were refueled; engine.js streakNote() now says "Both engines fueled this cycle: +X% on each", names the other site when only one is done, and quotes next rates when the window is open (test added, 11/11). Chatbot: CANNED refuel answer + refuelFact point to /refuel, say either site alone keeps the streak, and describe the Pit Stop exactly as refuel.js decides it (another package on a site already refueled this cycle before the window; the other site's first package is the second engine). my.js tags: fuel.js + engine-room.js ?v=20261024rg. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
15 KiB
JavaScript
149 lines
15 KiB
JavaScript
// The Engine Room (Fuel Your Offers stage 4, Marty approved the build 2026-10-24). The SAME file ships in InstantAdPay and
|
|
// LinkSpin. Each site gathers its OWN facts (facts shape below) and answers the other's signed /api/partner/engine call
|
|
// with them; build() turns both sites' facts + the refuel streak (InstantAdPay is its source of truth) + the member's goal
|
|
// into one plain dashboard: three dials, the streak panel, warning lights, six part lights and one "next move".
|
|
//
|
|
// facts (per site): { site: 'iap'|'linkspin', ok, credits: { available, inCampaigns }, campaigns: [{ id, name, type, status,
|
|
// budget, delivered, clicks, created }], clicksWeek, loginFree, buyUrl, adsUrl,
|
|
// LinkSpin only: goalsMonth, goalsCount, conv30, parts: { bannerSpin, spinLink, page, testing } each { state, note } }
|
|
// Everything is credits and counts. Nothing here is money or a promise.
|
|
'use strict';
|
|
const DAY = 86400000;
|
|
const n0 = v => (typeof v === 'number' && isFinite(v) ? v : Number(v) || 0);
|
|
const LINKS = {
|
|
iapBuy: 'https://instantadpay.com/my#buy', iapCampaigns: 'https://instantadpay.com/my#campaigns', iapTracking: 'https://instantadpay.com/my#campaigns',
|
|
lsBuy: 'https://linkspin.app/credits', lsAds: 'https://linkspin.app/ads', lsLinks: 'https://linkspin.app/links', lsPages: 'https://linkspin.app/pages', lsConv: 'https://linkspin.app/conversions',
|
|
};
|
|
const ACTIVE = new Set(['active', 'paused']);
|
|
|
|
// credits a day across recent campaigns: what each delivered over the days it has run (30 at most), summed
|
|
function burnPerDay(camps, now) {
|
|
let b = 0;
|
|
for (const c of camps) {
|
|
const age = Math.max(1, Math.min(30, (now - n0(c.created)) / DAY));
|
|
if (c.status !== 'active' && now - n0(c.created) > 30 * DAY) continue;
|
|
b += n0(c.delivered) / age;
|
|
}
|
|
return b;
|
|
}
|
|
function part(state, label, note, link) { return { state, label, note, link }; }
|
|
|
|
function build(o) {
|
|
const now = o.now || Date.now();
|
|
const rf = o.refuel || { on: false };
|
|
if (!rf.on) return { on: false };
|
|
const sites = [o.iap, o.ls].filter(f => f && f.ok);
|
|
const camps = sites.flatMap(f => (f.campaigns || []).map(c => Object.assign({ site: f.site }, c)));
|
|
const recent = camps.filter(c => ACTIVE.has(c.status) || now - n0(c.created) <= 30 * DAY || c.status === 'out');
|
|
const locked = !rf.started;
|
|
// ---- dial 1: fuel left vs this cycle's refuel ----
|
|
const left = sites.reduce((s, f) => s + n0(f.credits && f.credits.available) + n0(f.credits && f.credits.inCampaigns), 0);
|
|
const cy = rf.cycle || null;
|
|
const refuelCredits = cy ? ['iap', 'linkspin'].reduce((s, k) => s + (cy[k] && cy[k].refueled ? n0(cy[k].credits) + n0(cy[k].bonus) : 0), 0) : 0;
|
|
const burn = burnPerDay(recent, now);
|
|
const daysLeft = burn > 0 ? Math.floor(left / burn) : null;
|
|
const fuel = { left: Math.round(left), refuelCredits: Math.round(refuelCredits), pct: refuelCredits > 0 ? Math.max(0, Math.min(100, Math.round(left / refuelCredits * 100))) : null,
|
|
burnPerDay: Math.round(burn), daysLeft, label: 'Fuel left' };
|
|
// ---- dial 2: clicks this week, and per 1,000 credits on recent campaigns ----
|
|
const clicksWeek = sites.reduce((s, f) => s + n0(f.clicksWeek), 0), clicksPrev = sites.reduce((s, f) => s + n0(f.clicksPrevWeek), 0);
|
|
const del = recent.reduce((s, c) => s + n0(c.delivered), 0), clk = recent.reduce((s, c) => s + n0(c.clicks), 0);
|
|
// the dial reads this week against last week: half way = the same as last week
|
|
const clicks = { week: clicksWeek, prevWeek: clicksPrev, pct: clicksPrev > 0 ? Math.min(100, Math.round(clicksWeek / (2 * clicksPrev) * 100)) : null, per1000: del >= 100 ? Math.round(clk / del * 1000 * 10) / 10 : null, label: 'Clicks this week' };
|
|
// ---- dial 3: goals this month (conversion pixel, LinkSpin keeps it for both sites) vs the member's own goal ----
|
|
const lsF = o.ls && o.ls.ok ? o.ls : null;
|
|
const goalTarget = Math.max(0, Math.round(n0(o.goal))) || null;
|
|
const goals = { month: lsF ? n0(lsF.goalsMonth) : null, target: goalTarget, pct: goalTarget && lsF ? Math.min(100, Math.round(n0(lsF.goalsMonth) / goalTarget * 100)) : null, tracking: lsF ? n0(lsF.goalsCount) > 0 : null, label: 'Goals this month' };
|
|
// ---- warning lights ----
|
|
const low = camps.filter(c => c.status === 'active' && n0(c.budget) > 0 && n0(c.delivered) >= n0(c.budget) * 0.8);
|
|
const out = camps.filter(c => c.status === 'out' && now - n0(c.created) <= 60 * DAY);
|
|
const running = camps.filter(c => c.status === 'active');
|
|
const refuelSoon = rf.active && rf.daysToRefuel != null && rf.daysToRefuel <= 5;
|
|
const loginFree = sites.filter(f => n0(f.loginFree) > 0).map(f => f.site);
|
|
const warnings = [
|
|
{ key: 'low', on: low.length > 0, label: 'Ads at 80%', note: low.length ? low.length + ' ad' + (low.length === 1 ? ' is' : 's are') + ' past 80% of budget' : 'none running low' },
|
|
{ key: 'out', on: out.length > 0, label: 'Ads out', note: out.length ? out.length + ' ad' + (out.length === 1 ? '' : 's') + ' finished delivering' + (running.length ? '' : ', nothing running now') : 'none out' },
|
|
{ key: 'refuel', on: !!refuelSoon, label: 'Refuel day coming', note: rf.active ? (rf.windowOpen ? 'window open: refuel day ' + rf.refuelDayLabel : 'refuel day ' + rf.refuelDayLabel) : 'no refuel day yet' },
|
|
{ key: 'login', on: loginFree.length > 0, label: 'Login slot open', note: loginFree.length ? 'a login-ad slot is free on ' + loginFree.map(s => s === 'iap' ? 'InstantAdPay' : 'LinkSpin').join(' and ') : 'all login slots taken' },
|
|
];
|
|
// ---- the six parts ----
|
|
const P = (lsF && lsF.parts) || {};
|
|
const pFuel = !camps.length ? part('grey', 'Fuel', 'no ads yet', LINKS.iapCampaigns)
|
|
: out.length && !running.length ? part('amber', 'Fuel', 'nothing running', LINKS.iapCampaigns)
|
|
: low.length || (daysLeft != null && daysLeft < 5) ? part('amber', 'Fuel', daysLeft != null && daysLeft < 5 ? 'about ' + daysLeft + ' days left at your burn' : 'an ad is running low', LINKS.iapBuy)
|
|
: running.length ? part('green', 'Fuel', running.length + ' ad' + (running.length === 1 ? '' : 's') + ' running', LINKS.iapCampaigns) : part('amber', 'Fuel', 'nothing running', LINKS.iapCampaigns);
|
|
const pick = (k, label, link) => { const x = P[k]; return x ? part(x.state, label, x.note, link) : part('grey', label, lsF ? 'not set up' : 'LinkSpin did not answer', link); };
|
|
const parts = [pFuel, pick('bannerSpin', 'Banner spin', LINKS.lsLinks), pick('spinLink', 'Spin link', LINKS.lsLinks), pick('page', 'Page', LINKS.lsPages), pick('testing', 'Testing', LINKS.lsLinks),
|
|
lsF ? (n0(lsF.goalsCount) === 0 ? part('grey', 'Conversion tracking', 'no goal set', LINKS.lsConv) : n0(lsF.conv30) === 0 ? part('amber', 'Conversion tracking', 'no conversions tracked yet', LINKS.lsConv) : part('green', 'Conversion tracking', n0(lsF.conv30) + ' in 30 days', LINKS.lsConv))
|
|
: part('grey', 'Conversion tracking', 'LinkSpin did not answer', LINKS.iapTracking)];
|
|
// ---- one next move ----
|
|
const per1000 = c => n0(c.delivered) >= 200 ? n0(c.clicks) / n0(c.delivered) * 1000 : null;
|
|
const ranked = running.filter(c => per1000(c) != null).sort((a, b) => per1000(b) - per1000(a));
|
|
const linkFor = c => c.site === 'linkspin' ? LINKS.lsAds : LINKS.iapCampaigns;
|
|
const availAll = sites.reduce((s, f) => s + n0(f.credits && f.credits.available), 0);
|
|
let next;
|
|
if (locked) next = { key: 'start', label: 'Refuel to start your engine', why: 'Your first counted refuel ($' + (rf.minUsd ? rf.minUsd.iap : 20) + '+ here or $' + (rf.minUsd ? rf.minUsd.linkspin : 25) + '+ on LinkSpin) starts the streak and lights this room.', link: LINKS.iapBuy };
|
|
else if (rf.windowOpen) next = { key: 'refuel', label: 'Refuel now', why: 'Your refuel window is open: +' + rf.next.single + '% on one site, +' + rf.next.both + '% on each with both.', link: LINKS.iapBuy };
|
|
else if (out.length && !running.length && availAll >= 100) next = { key: 'relaunch', label: 'Relaunch an ad', why: 'Nothing is running and you have ' + Math.round(availAll).toLocaleString('en-US') + ' credits free.', link: linkFor(out[0]) };
|
|
else if (daysLeft != null && daysLeft < 5) next = { key: 'pitstop', label: 'Top up' + (rf.pitStop && (rf.pitStop.iap || rf.pitStop.linkspin) ? ' (Pit Stop +' + rf.pitStop.pct + '%)' : ''), why: 'About ' + daysLeft + ' days of fuel left at your current burn.', link: LINKS.iapBuy };
|
|
else if (low.length) { const best = low.slice().sort((a, b) => (per1000(b) || 0) - (per1000(a) || 0))[0]; next = { key: 'topup', label: 'Top up the winner', why: '"' + (best.name || 'your ad') + '" is past 80% of its budget.', link: linkFor(best) }; }
|
|
else if (parts[5].state === 'grey') next = { key: 'tracking', label: 'Set up conversion tracking', why: 'Count signups and sales back to the ad that sent them.', link: parts[5].link };
|
|
else if (parts[1].state === 'grey') next = { key: 'bannerspin', label: 'Make a Banner Spin', why: 'Test up to 5 banners in one ad slot and let the winner show.', link: LINKS.lsLinks };
|
|
else if (parts[4].state !== 'green') next = { key: 'testing', label: 'Test A against B', why: 'Add a second page to your spin link so the rotation can pick a winner.', link: LINKS.lsLinks };
|
|
else if (ranked.length >= 2 && per1000(ranked[0]) > per1000(ranked[ranked.length - 1]) * 1.5) { const worst = ranked[ranked.length - 1]; next = { key: 'cut', label: 'Cut the loser', why: '"' + (worst.name || 'an ad') + '" brings the fewest clicks per 1,000 credits. Pause it and move the credits to "' + (ranked[0].name || 'your best ad') + '".', link: linkFor(worst) }; }
|
|
else next = { key: 'cruise', label: 'Keep it running', why: 'Every part is lit. Check again on your refuel day.', link: LINKS.iapCampaigns };
|
|
return {
|
|
on: true, locked, fuel, clicks, goals,
|
|
streak: { active: !!rf.active, months: rf.streak || 0, best: rf.best || 0, refuelDay: rf.refuelDayLabel || null, graceEnd: rf.graceLabel || null, windowOpen: !!rf.windowOpen, next: rf.next || null, pitStop: rf.pitStop || null, freeMiss: rf.freeMiss || null, both: !!(cy && cy.both), note: streakNote(rf, cy) },
|
|
warnings, parts, next, sites: { iap: !!(o.iap && o.iap.ok), linkspin: !!(o.ls && o.ls.ok) }, at: now,
|
|
};
|
|
}
|
|
|
|
// this site's own facts for one member. d: { site, adminMember, ads, coach, now?, bannerspin?, rotator?, pages?, convtrack? }
|
|
const monthStartCT = now => { const day = new Date(now).toLocaleDateString('en-CA', { timeZone: 'America/Chicago' }); const [y, m] = day.split('-').map(Number);
|
|
for (const off of [5, 6]) { const ts = Date.UTC(y, m - 1, 1, off); const t = new Date(ts); if (t.toLocaleDateString('en-CA', { timeZone: 'America/Chicago' }) === day.slice(0, 8) + '01' && Number(t.toLocaleString('en-US', { timeZone: 'America/Chicago', hour: 'numeric', hour12: false })) % 24 === 0) return ts; } return Date.UTC(y, m - 1, 1, 6); };
|
|
async function facts(d, email) {
|
|
const now = d.now || Date.now(); const e = String(email || '').toLowerCase();
|
|
const out = { site: d.site, ok: true, credits: { available: 0, inCampaigns: 0 }, campaigns: [], clicksWeek: 0, loginFree: 0 };
|
|
try { const v = await d.adminMember.view(e); if (v && v.credits) out.credits = { available: n0(v.credits.available), inCampaigns: n0(v.credits.inCampaigns) }; } catch (er) {}
|
|
let list = []; try { list = (await d.ads.listCampaigns(e)).filter(c => !c.house && c.type !== 'game'); } catch (er) {}
|
|
out.campaigns = list.map(c => ({ id: c.id, name: c.name || null, type: c.type, status: c.status, budget: n0(c.budget), delivered: n0(c.spent), clicks: n0(c.clicks), created: n0(c.created) }));
|
|
try { out.clicksWeek = await d.coach.clicksSince(list.map(c => c.id), now - 7 * DAY); out.clicksPrevWeek = Math.max(0, (await d.coach.clicksSince(list.map(c => c.id), now - 14 * DAY)) - out.clicksWeek); } catch (er) {}
|
|
try { out.loginFree = n0((await d.ads.loginSlots()).free); } catch (er) {}
|
|
if (d.site === 'linkspin') {
|
|
const since30 = now - 30 * DAY;
|
|
try { out.goalsCount = d.convtrack.goalsOf(e).length; out.goalsMonth = d.convtrack.countSince(e, monthStartCT(now)); out.conv30 = d.convtrack.countSince(e, since30); } catch (er) { out.goalsCount = 0; out.goalsMonth = 0; out.conv30 = 0; }
|
|
const parts = {};
|
|
let spins = [], rots = [], pgs = [];
|
|
try { spins = await d.bannerspin.list(e); } catch (er) {}
|
|
try { rots = await d.rotator.list(e); } catch (er) {}
|
|
try { pgs = await d.pages.list(e); } catch (er) {}
|
|
const liveB = s => s.banners.filter(b => b.active && !b.adminOff);
|
|
if (!spins.length) parts.bannerSpin = { state: 'grey', note: 'not set up' };
|
|
else { const seen = spins.filter(s => !s.paused && s.imps30 > 0); parts.bannerSpin = seen.length ? { state: 'green', note: seen.reduce((n, s) => n + s.imps30, 0).toLocaleString('en-US') + ' views in 30 days' } : { state: 'amber', note: 'no views yet' }; }
|
|
if (!rots.length) parts.spinLink = { state: 'grey', note: 'not set up' };
|
|
else { const w = rots.reduce((n, r) => n + n0(r.hits7), 0); parts.spinLink = w > 0 ? { state: 'green', note: w.toLocaleString('en-US') + ' clicks this week' } : { state: 'amber', note: 'no clicks this week' }; }
|
|
const pub = pgs.filter(p => p.status === 'published');
|
|
parts.page = !pgs.length ? { state: 'grey', note: 'not set up' } : !pub.length ? { state: 'amber', note: 'draft only, not published' } : pub.some(p => n0(p.views) > 0) ? { state: 'green', note: pub.length + ' published' } : { state: 'amber', note: 'published, no views yet' };
|
|
const rotTest = rots.some(r => !r.paused && r.destinations.filter(x => x.active).length >= 2);
|
|
const rotTraffic = rots.some(r => !r.paused && r.destinations.filter(x => x.active && x.hits7 > 0).length >= 2);
|
|
const bsTest = spins.some(s => !s.paused && liveB(s).length >= 2);
|
|
const bsTraffic = spins.some(s => !s.paused && liveB(s).filter(b => b.imps30 > 0).length >= 2);
|
|
parts.testing = rotTraffic || bsTraffic ? { state: 'green', note: 'two or more versions getting traffic' } : rotTest || bsTest ? { state: 'amber', note: 'two versions set up, waiting for traffic' } : (rots.length || spins.length) ? { state: 'amber', note: 'only one version: add a second to test' } : { state: 'grey', note: 'not set up' };
|
|
out.parts = parts;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// the streak panel's one line about the next refuel (2026-10-24): once BOTH sites are refueled this cycle it says so,
|
|
// and only a cycle with one site done asks for the other one, by name
|
|
function streakNote(rf, cy) {
|
|
const nx = rf && rf.next; if (!rf || !rf.active || !nx) return '';
|
|
if (rf.windowOpen) return 'Window open: next refuel +' + nx.single + '% (+' + nx.both + '% on each with both).';
|
|
const iapDone = !!(cy && cy.iap && cy.iap.refueled), lsDone = !!(cy && cy.linkspin && cy.linkspin.refueled);
|
|
if ((cy && cy.both) || (iapDone && lsDone)) return 'Both engines fueled this cycle: +' + (nx.secondEngine || (cy.iap && cy.iap.pct) || 0) + '% on each.';
|
|
if (nx.secondEngine) return 'Refuel ' + (iapDone ? 'LinkSpin' : 'InstantAdPay') + ' this cycle for +' + nx.secondEngine + '% on both.';
|
|
return '';
|
|
}
|
|
|
|
module.exports = { streakNote, build, facts, burnPerDay, monthStartCT, LINKS };
|