Files
linkspin/public/assets/reports.js
T

28 lines
4.1 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');
$('tiles').innerHTML = [['Served', fmt(t.served), ''], ['Unique', fmt(t.unique), ''], ['Bots dropped', fmt(t.bots), t.served ? Math.round(t.bots / (t.served + t.bots) * 100) + '% of hits' : ''], ['Joins', fmt(t.joins), t.unique ? (t.joins / t.unique * 100).toFixed(1) + '% of unique' : ''], ['Buyers', fmt(t.purchases), t.joins ? Math.round(t.purchases / t.joins * 100) + '% of joins' : ''], ['Partner conversions', fmt(t.external), '']]
.map(([k, v, n], i) => '<div class="tile' + (i === 4 ? ' hi' : '') + '"><span class="k">' + k + '</span><span class="v">' + v + '</span>' + (n ? '<span class="muted mono" style="font-size:11px">' + n + '</span>' : '') + '</div>').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 style="height:' + Math.round(d.u / dmax * 100) + '%;background:' + (d.u ? 'var(--accent)' : 'var(--line)') + '" 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);
}
CR.requireSession(load);
})();