Files
linkspin/public/assets/reports.js
T
martbost f93bca81a7 Banner Spin phase 3: reports, health checks, postbacks verified, admin switch button
- bannerspin.js: every image load also counted raw per placement (bots/prefetch and servers/mail image proxies kept apart from views); one aggregation for the Stats tab and a new summary() for Reports: per banner (views, uniques, clicks, CTR, match rate, conversions), per placement with a "caches banners" flag, by Central day, country and device, bots shown separately; WINNER badge once two live banners have 1,000 views each and one leads on CTR. Click params now keep the banner macro (same list as spin links). Owner pause/resume clears an auto-pause; a new image or target resets health.
- health.js: the 30-minute pass also checks each live banner: image must load as an image (magic bytes) at the Banner Spin size, target must pass the spin-link checks (a spin-link target only has to exist). 3 failures pause, 2 clean passes resume, the last live banner is never auto-paused; owner gets an inbox notice. checkBannerSpins() for admin.
- server.js (own blocks): health.init gets bannerspin; /api/my/reports adds bannerSpins; POST /api/admin/bannerspins/health runs the checks now.
- Builder: health line + AUTO-PAUSED tag on every banner row; Stats tab adds uniques, WINNER, CACHED placements, loads, country/device, bots line.
- Reports screen: Banner Spin section + Banner views/clicks tiles beside spin links.
- Admin > Member content: real Banner Spin on/off button (with confirm) and "Run banner checks".
- Traffic-source postbacks on Banner Spin clicks verified end to end (network click id, event, payout, lsid reach the source postback).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 05:39:50 -05:00

78 lines
13 KiB
JavaScript

// LinkSpin Reports: one view across every link the member owns (rotator.summary via /api/my/reports).
(function () {
const $ = id => document.getElementById(id); const esc = CR.esc, fmt = CR.fmt;
let RANGE = 30;
CR.mount({ active: 'reports', title: 'Reports', sub: 'loading', actions: '<div class="range" id="range"><button type="button" data-d="7">7d</button><button type="button" data-d="30" class="on">30d</button><button type="button" data-d="90">90d</button></div>' });
$('range').querySelectorAll('button').forEach(b => b.addEventListener('click', () => { RANGE = Number(b.dataset.d); $('range').querySelectorAll('button').forEach(x => x.classList.toggle('on', x === b)); load(); }));
const kv = (rows, total) => rows && rows.length ? rows.map(x => '<span>' + esc(x.k) + '</span><span class="bar"><i style="width:' + Math.round(x.n / (rows[0].n || 1) * 100) + '%"></i></span><span class="r num">' + fmt(x.n) + (total ? ' <span class="muted">' + Math.round(x.n / total * 100) + '%</span>' : '') + '</span>').join('') : '<span class="muted">nothing yet</span><span></span><span></span>';
async function load() {
let s; try { s = await CR.get('/api/my/reports?days=' + RANGE); } catch (e) { CR.sub('could not load'); return; }
const t = s.totals || {};
CR.sub(RANGE + ' days · ' + fmt(s.links || 0) + ' link' + (s.links === 1 ? '' : 's') + ' · Central time days');
// sparklines: one point per Central day in the range (days without hits are zero); delta = second half of the range vs the first
const dl = s.days || []; const byDay = {}; dl.forEach(d => { byDay[d.day] = d; });
const keys = []; for (let i = RANGE - 1; i >= 0; i--) keys.push(new Date(Date.now() - i * 86400000).toLocaleDateString('en-CA', { timeZone: 'America/Chicago' }));
const ser = f => keys.map(k => byDay[k] ? f(byDay[k]) : 0); const half = a => { const m = Math.floor(a.length / 2); return [a.slice(0, m).reduce((x, y) => x + y, 0), a.slice(m).reduce((x, y) => x + y, 0)]; };
const su = ser(d => d.u), sb = ser(d => d.b); const [u0, u1] = half(su), [b0, b1] = half(sb);
$('tiles').innerHTML = [
CR.tile({ k: 'Served', v: fmt(t.served) }),
CR.tile({ k: 'Unique', v: fmt(t.unique), series: su, delta: CR.delta(u1, u0, 'vs prior') }),
CR.tile({ k: 'Bots dropped', v: fmt(t.bots), series: sb, delta: CR.delta(b1, b0, 'vs prior'), note: t.served ? Math.round(t.bots / (t.served + t.bots) * 100) + '% of hits' : '' }),
CR.tile({ k: 'Joins', v: fmt(t.joins), note: t.unique ? (t.joins / t.unique * 100).toFixed(1) + '% of unique' : '' }),
CR.tile({ k: 'Buyers', v: fmt(t.purchases), hi: true, note: t.joins ? Math.round(t.purchases / t.joins * 100) + '% of joins' : '' }),
CR.tile({ k: 'Partner conversions', v: fmt(t.external) }),
// Banner Spin totals beside spin links (2026-10-20)
CR.tile({ k: 'Banner views', v: fmt(((s.bannerSpins || {}).totals || {}).imps), note: ((s.bannerSpins || {}).count || 0) + ' Banner Spin' + (((s.bannerSpins || {}).count || 0) === 1 ? '' : 's') }),
CR.tile({ k: 'Banner clicks', v: fmt(((s.bannerSpins || {}).totals || {}).clicks), note: ((s.bannerSpins || {}).totals || {}).ctr != null ? s.bannerSpins.totals.ctr + '% CTR' : '' })].join('');
const days = s.days || []; const dmax = Math.max(1, ...days.map(d => Math.max(d.u, d.b)));
$('dayBars').style.gridTemplateColumns = 'repeat(' + Math.max(1, days.length) + ',minmax(0,1fr))';
$('dayBars').innerHTML = days.map(d => '<i class="' + (days.length >= 3 && d.u === dmax && d.u ? 'hi' : d.u ? '' : 'dim') + '" style="height:' + Math.round(d.u / dmax * 100) + '%" title="' + d.day + ' · ' + d.u + ' unique, ' + d.b + ' bots"></i>').join('');
$('dayAxis').innerHTML = days.length ? '<span>' + days[0].day + '</span><span>' + days[Math.floor(days.length / 2)].day + '</span><span>' + days[days.length - 1].day + '</span>' : '<span class="muted">no clicks yet</span>';
$('srcRows').innerHTML = kv(s.source, t.unique); $('geoRows').innerHTML = kv(s.country, t.unique);
const dev = s.device || []; const dt = dev.reduce((n, x) => n + x.n, 0) || 1; $('devRow').textContent = dev.map(x => x.k + ' ' + Math.round(x.n / dt * 100) + '%').join(' · ');
$('topRows').innerHTML = (s.top || []).length ? s.top.map(x => '<div class="rw"><a href="/links#' + x.id + '" style="text-decoration:none;color:var(--ink)"><b>' + esc(x.name) + '</b><br><span class="mono muted" style="font-size:11px">' + esc(x.code) + '</span></a><span class="r num">' + fmt(x.unique) + '</span><span class="r num">' + fmt(x.hits) + '</span><span class="r num">' + fmt(x.conv) + '</span></div>').join('') : '<div class="rw"><span class="muted">No links yet.</span></div>';
const hmax = Math.max(1, ...(s.hours || [])); $('hourBars').innerHTML = (s.hours || []).map((n, i) => '<i class="' + (n >= hmax * 0.6 ? '' : 'dim') + '" style="height:' + Math.round(n / hmax * 100) + '%" title="' + i + ':00 UTC · ' + n + '"></i>').join('');
const best = (s.hours || []).map((n, i) => [n, i]).sort((a, b) => b[0] - a[0]).slice(0, 3).filter(x => x[0]).map(x => x[1] + ':00');
$('hourNote').textContent = best.length ? 'Busiest hours (UTC): ' + best.join(', ') + '. Hour rules per link come next.' : 'No clicks yet.';
$('convRows').innerHTML = kv(s.conversions || [], 0);
// -- Pages and Zones (2026-10-10): one bar per Central day in the range (empty days are zero), totals, per-item table
const dayBars = (barsId, axisId, rows, f, g, empty) => {
const map = {}; (rows || []).forEach(r => { map[r.day] = r; });
const ser = keys.map(k => ({ day: k, a: map[k] ? f(map[k]) : 0, b: map[k] ? g(map[k]) : 0 }));
const max = Math.max(1, ...ser.map(x => Math.max(x.a, x.b))); const any = ser.some(x => x.a || x.b);
const el = $(barsId); el.style.gridTemplateColumns = 'repeat(' + keys.length + ',minmax(0,1fr))'; el.classList.add('stack');
el.innerHTML = ser.map(x => '<i class="' + (x.a ? '' : 'dim') + '" style="height:' + Math.round(x.a / max * 100) + '%" title="' + x.day + ' · ' + x.a + ' / ' + x.b + '">' + (x.b ? '<b style="height:' + Math.round(x.b / x.a * 100 || 0) + '%"></b>' : '') + '</i>').join('');
$(axisId).innerHTML = any ? '<span>' + keys[0] + '</span><span>' + keys[Math.floor(keys.length / 2)] + '</span><span>' + keys[keys.length - 1] + '</span>' : '<span class="muted">' + empty + '</span>';
};
const mini = rows => rows.map(([k, v]) => '<div class="m"><span class="k">' + esc(k) + '</span><span class="v">' + v + '</span></div>').join('');
const pg = s.pages || { byDay: [], byPage: [] };
// bar = views, inner bar = leads (leads as a share of that day's views; a day with leads but no views still shows)
dayBars('pgBars', 'pgAxis', pg.byDay, d => Math.max(d.views, d.leads), d => d.leads, 'no page views yet');
$('pgBars').querySelectorAll('i b').forEach(b => { b.style.background = 'var(--sig)'; });
$('pgTiles').innerHTML = mini([['Views', fmt(pg.views)], ['Unique', fmt(pg.uniques)], ['Clicks through', fmt(pg.clicks)], ['Leads', fmt(pg.leads)]]);
$('pgRows').innerHTML = (pg.byPage || []).length ? pg.byPage.map(x => '<div class="rw"><a href="/pages" style="text-decoration:none;color:var(--ink)"><b>' + esc(x.title) + '</b><br><span class="mono muted" style="font-size:11px">' + esc(x.type) + ' · ' + (x.status === 'published' ? '<span class="ok">live</span>' : 'draft') + '</span></a><span class="r num">' + fmt(x.views) + '</span><span class="r num">' + fmt(x.uniques) + '</span><span class="r num">' + fmt(x.clicks) + '</span><span class="r num">' + fmt(x.leads) + '</span></div>').join('') : '<div class="rw"><span class="muted">No pages yet. <a href="/pages">Make one</a>.</span></div>';
// -- Traffic sources (2026-10-12): per source, and per placement value the network's macro carried
const ts = s.sources || [], pl = s.placements || [];
$('tsRows').innerHTML = ts.length ? ts.map(x => '<div class="rw"><a href="/sources#' + esc(x.id) + '" style="text-decoration:none;color:var(--ink)"><b>' + esc(x.name) + '</b>' + (x.site ? '<br><span class="mono muted" style="font-size:11px">' + esc(x.site) + '</span>' : '') + '</a><span class="r num">' + fmt(x.hits) + '</span><span class="r num">' + fmt(x.unique) + '</span><span class="r num">' + fmt(x.conv) + (x.unique ? ' <span class="muted" style="font-size:10px">' + (x.conv / x.unique * 100).toFixed(1) + '%</span>' : '') + '</span></div>').join('') : '<div class="rw"><span class="muted">No clicks from a traffic source yet. Point a link at one under <a href="/sources">Sources</a>.</span></div>';
$('plRows').innerHTML = pl.length ? pl.map(x => '<div class="rw"><span><b>' + esc(x.source) + '</b><br><span class="mono muted" style="font-size:11px">' + esc(x.key) + ' = ' + esc(x.k) + '</span></span><span class="r num">' + fmt(x.n) + '</span><span class="r num">' + fmt(x.conv) + '</span></div>').join('') : '<div class="rw"><span class="muted">Nothing yet. Placements appear once a source\'s zone or placement macro rides in on clicks.</span></div>';
// -- Banner Spins (2026-10-20): views by Central day (bots and servers as the lighter bar), per banner with the winner, per placement with the cache flag
const bs = s.bannerSpins || { totals: {}, days: [], banners: [], placements: [], country: [], device: [] }; const bt = bs.totals || {};
// outer bar = views + bot / server loads, inner lighter share = the bot / server part
dayBars('bsBars', 'bsAxis', bs.days, d => d.imps + (d.bots || 0) + (d.servers || 0), d => (d.bots || 0) + (d.servers || 0), 'no banner views yet');
$('bsTiles').innerHTML = mini([['Views', fmt(bt.imps)], ['Clicks', fmt(bt.clicks)], ['CTR', bt.ctr == null ? '-' : bt.ctr + '%'], ['Match rate', bt.matchRate == null ? '-' : bt.matchRate + '%'], ['Unique (daily)', fmt(bt.uniques)], ['Conversions', fmt(bt.conversions)]]);
$('bsCountry').innerHTML = kv(bs.country, 0); $('bsDevice').innerHTML = kv(bs.device, 0);
$('bsBotNote').textContent = 'Never counted as views or clicks, shown here so you can see them: ' + fmt(bt.bots) + ' bot and prefetch image loads, ' + fmt(bt.serverLoads) + ' loads by servers or mail image proxies, ' + fmt(bt.botClicks) + ' bot clicks.';
$('bsRowsB').innerHTML = (bs.banners || []).length ? bs.banners.map(x => '<div class="rw"><a href="/links#b' + x.spinId + '" style="text-decoration:none;color:var(--ink)"><b>' + esc(x.label) + '</b>' + (x.winner ? '<span class="bswin" data-winner="' + x.spinId + ':' + x.id + '">WINNER</span>' : '') + '<br><span class="mono muted" style="font-size:11px">' + esc(x.spin) + ' · ' + esc(x.size) + (x.autoPaused ? ' · <span class="warn">auto-paused</span>' : x.active ? '' : ' · paused') + '</span></a><span class="r num">' + fmt(x.imps) + '</span><span class="r num">' + fmt(x.uniques) + '</span><span class="r num">' + fmt(x.clicks) + '</span><span class="r num">' + (x.ctr == null ? '-' : x.ctr + '%') + '</span><span class="r num">' + fmt(x.conversions) + '</span></div>').join('')
: '<div class="rw"><span class="muted">' + (bs.count ? 'No banners yet.' : 'No Banner Spins yet. <a href="/links#b">Make one</a> (opens with your own $25 package).') + '</span></div>';
$('bsWinNote').textContent = 'WINNER = the best click rate in its Banner Spin once two or more live banners have ' + fmt(bs.winMin || 1000) + ' views each. Unique viewers are counted per day from a sample.';
$('bsRowsP').innerHTML = (bs.placements || []).length ? bs.placements.slice(0, 15).map(x => '<div class="rw"><span style="overflow-wrap:anywhere"><b>' + esc(x.host === '(none)' ? 'No referrer (email, apps)' : x.host) + '</b>' + (x.caches ? ' <span class="tag warn">CACHED</span><span class="bscache" data-caches="' + esc(x.host) + '">' + esc(x.caches) + '</span>' : '') + '</span><span class="r num">' + fmt(x.imps) + '</span><span class="r num">' + fmt(x.clicks) + '</span><span class="r num">' + (x.ctr == null ? '-' : x.ctr + '%') + '</span><span class="r num">' + (x.matchRate == null ? '-' : x.matchRate + '%') + '</span></div>').join('')
: '<div class="rw"><span class="muted">No placements yet. Put a Banner Spin\'s code on a page and its host shows here.</span></div>';
const zn = s.zones || { byDay: [], byZone: [] };
// bar = all views, lighter inner share = the ones that were NOT viewable
dayBars('znBars', 'znAxis', zn.byDay, d => d.views, d => d.views - d.viewable, 'no zone views yet');
$('znTiles').innerHTML = mini([['Views', fmt(zn.views)], ['Viewable', fmt(zn.viewable) + (zn.views ? ' <small class="muted" style="font-size:11px">' + Math.round(zn.viewable / zn.views * 100) + '%</small>' : '')], ['Clicks', fmt(zn.clicks)], ['Credits earned', fmt(zn.credits)]]);
$('znRows').innerHTML = (zn.byZone || []).length ? zn.byZone.map(x => '<div class="rw"><a href="/zones" style="text-decoration:none;color:var(--ink)"><b>' + esc(x.name) + '</b><br><span class="mono muted" style="font-size:11px">' + esc(x.site) + ' · ' + esc(x.size) + (x.paused ? ' · <span class="warn">paused</span>' : x.active ? '' : ' · off') + '</span></a><span class="r num">' + fmt(x.views) + '</span><span class="r num">' + fmt(x.viewable) + (x.viewablePct != null ? ' <span class="muted" style="font-size:10px">' + x.viewablePct + '%</span>' : '') + '</span><span class="r num">' + fmt(x.clicks) + '</span><span class="r num">' + fmt(x.credits) + '</span></div>').join('') : '<div class="rw"><span class="muted">No zones yet. <a href="/zones">Add one</a>.</span></div>';
}
CR.requireSession(load);
})();