// 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: '
' }); $('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 => '' + esc(x.k) + '' + fmt(x.n) + (total ? ' ' + Math.round(x.n / total * 100) + '%' : '') + '').join('') : 'nothing yet'; 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) })].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 => '').join(''); $('dayAxis').innerHTML = days.length ? '' + days[0].day + '' + days[Math.floor(days.length / 2)].day + '' + days[days.length - 1].day + '' : 'no clicks yet'; $('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 => '
' + esc(x.name) + '
' + esc(x.code) + '
' + fmt(x.unique) + '' + fmt(x.hits) + '' + fmt(x.conv) + '
').join('') : '
No links yet.
'; const hmax = Math.max(1, ...(s.hours || [])); $('hourBars').innerHTML = (s.hours || []).map((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 => '' + (x.b ? '' : '') + '').join(''); $(axisId).innerHTML = any ? '' + keys[0] + '' + keys[Math.floor(keys.length / 2)] + '' + keys[keys.length - 1] + '' : '' + empty + ''; }; const mini = rows => rows.map(([k, v]) => '
' + esc(k) + '' + v + '
').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 => '
' + esc(x.title) + '
' + esc(x.type) + ' · ' + (x.status === 'published' ? 'live' : 'draft') + '
' + fmt(x.views) + '' + fmt(x.uniques) + '' + fmt(x.clicks) + '' + fmt(x.leads) + '
').join('') : '
No pages yet. Make one.
'; // -- 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 => '
' + esc(x.name) + '' + (x.site ? '
' + esc(x.site) + '' : '') + '
' + fmt(x.hits) + '' + fmt(x.unique) + '' + fmt(x.conv) + (x.unique ? ' ' + (x.conv / x.unique * 100).toFixed(1) + '%' : '') + '
').join('') : '
No clicks from a traffic source yet. Point a link at one under Sources.
'; $('plRows').innerHTML = pl.length ? pl.map(x => '
' + esc(x.source) + '
' + esc(x.key) + ' = ' + esc(x.k) + '
' + fmt(x.n) + '' + fmt(x.conv) + '
').join('') : '
Nothing yet. Placements appear once a source\'s zone or placement macro rides in on clicks.
'; 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 ? ' ' + Math.round(zn.viewable / zn.views * 100) + '%' : '')], ['Clicks', fmt(zn.clicks)], ['Credits earned', fmt(zn.credits)]]); $('znRows').innerHTML = (zn.byZone || []).length ? zn.byZone.map(x => '
' + esc(x.name) + '
' + esc(x.site) + ' · ' + esc(x.size) + (x.paused ? ' · paused' : x.active ? '' : ' · off') + '
' + fmt(x.views) + '' + fmt(x.viewable) + (x.viewablePct != null ? ' ' + x.viewablePct + '%' : '') + '' + fmt(x.clicks) + '' + fmt(x.credits) + '
').join('') : '
No zones yet. Add one.
'; } CR.requireSession(load); })();