// LinkSpin Conversions (conversion pixel, 2026-10-23): goals + pixel ids, the two snippets, a test button, and per goal the
// conversions and cost per goal by ad campaign (LinkSpin and InstantAdPay), spin link, destination and traffic source.
(function () {
const $ = id => document.getElementById(id); const esc = CR.esc, fmt = CR.fmt;
let RANGE = 30, D = null, PICK = '';
CR.mount({ active: 'conversions', title: 'Conversions', sub: 'loading', actions: '7d 30d 90d
CSV ' });
$('range').querySelectorAll('button').forEach(b => b.addEventListener('click', () => { RANGE = Number(b.dataset.d); $('range').querySelectorAll('button').forEach(x => x.classList.toggle('on', x === b)); $('csvBtn').href = '/api/my/conversions.csv?days=' + RANGE; load(); }));
const ago = ts => { if (!ts) return 'never'; const m = Math.round((Date.now() - ts) / 60000); return m < 1 ? 'just now' : m < 60 ? m + ' min ago' : m < 1440 ? Math.round(m / 60) + ' h ago' : Math.round(m / 1440) + ' d ago'; };
const money = v => v ? '$' + Number(v).toFixed(2) : '';
const METH = [['url', 'Address'], ['stored', 'Kept'], ['cookie', 'Cookie'], ['soft', '24 h match']];
function snippets() {
const g = (D.goals || []).find(x => x.px === PICK) || (D.goals || [])[0];
const base = D.base || ('https://' + (D.host || 'linkspin.app'));
if (!g) { ['snTag', 'snImg', 'snJs'].forEach(k => { $(k).textContent = 'Add a goal first. Its snippets show here.'; }); $('testBtn').disabled = true; $('testNote').textContent = ''; return; }
$('testBtn').disabled = false;
$('snTag').textContent = '';
$('snImg').textContent = ' ';
$('snJs').textContent = '';
$('testNote').textContent = g.lastTest ? 'Last test received ' + ago(g.lastTest) : 'No test received yet';
}
function paint() {
const goals = D.goals || [];
CR.sub(RANGE + ' days · ' + goals.length + ' goal' + (goals.length === 1 ? '' : 's') + ' · ' + fmt(D.total || 0) + ' counted');
$('goalRows').innerHTML = goals.length ? goals.map(g => '
' + esc(g.name) + ' last ' + esc(ago(g.lastAt)) + (g.lastTest ? ' · test ' + esc(ago(g.lastTest)) : '') + ' ' + esc(g.px) + ' ' + (g.value ? esc(money(g.value)) : 'none ') + ' ' + fmt(g.conv) + ' Snippets Edit Remove
').join('')
: 'No goals yet. Add one, for example subscriber or sale .
';
if (!goals.some(g => g.px === PICK)) PICK = goals.length ? goals[0].px : '';
$('goalPick').innerHTML = goals.map(g => '' + esc(g.name) + ' ').join('');
snippets();
const m = D.methods || {}; const tot = METH.reduce((n, [k]) => n + (m[k] || 0), 0);
$('methRows').innerHTML = METH.map(([k, label]) => '' + label + ' ' + fmt(m[k] || 0) + ' ' + (tot ? Math.round((m[k] || 0) / tot * 100) + '%' : '·') + '
').join('');
$('totalNote').textContent = D.selfTests ? D.selfTests + ' from your own clicks (ads, spin links, Banner Spins), kept as tests and left out' : '';
$('goalReports').innerHTML = (D.byGoal || []).filter(G => G.n || !G.removed).map(G => {
const camp = G.campaigns.length ? G.campaigns.map(c => '' + esc(c.name || ('Campaign #' + c.id)) + '' + esc(c.site) + ' #' + c.id + (c.spentAsOf ? ' · spend as of ' + esc(ago(c.spentAsOf)) : '') + ' ' + (c.spent != null ? fmt(c.spent) + ' cr' : '·') + ' ' + fmt(c.clicks) + ' ' + fmt(c.n) + ' ' + (c.rate != null ? c.rate + '%' : '·') + ' ' + (c.costPer != null ? fmt(c.costPer) + ' cr' : '·') + '
').join('') : 'No ad campaign conversions for this goal yet.
';
const lst = (rows, f) => rows && rows.length ? rows.map(x => '' + f(x) + ' ' + fmt(x.n) + '
').join('') : 'nothing yet
';
const gm = G.methods || {};
return 'Goal: ' + esc(G.goal) + (G.removed ? ' (removed)' : '') + ' · ' + fmt(G.n) + ' counted' + (G.value ? ' · ' + esc(money(G.value)) : '') + ' · ' + METH.map(([k, l]) => l + ' ' + fmt(gm[k] || 0)).join(', ') + '
'
+ 'By ad campaign Campaign Spent Clicks Goals Rate Cost / goal
' + camp + '
Cost per goal = the credits the campaign has spent so far / its conversions of this goal, in that site\'s ad credits. Clicks and goals are for the range above; spend is the campaign\'s total.
'
+ 'By spin link Link Goals
' + lst(G.links, x => esc(x.name || x.code) + '
/r/' + esc(x.code) + ' ') + '
'
+ '
By destination page Page Goals
' + lst(G.dests, x => '
' + esc(x.k) + ' ') + '
'
+ 'By Banner Spin Banner Spin Goals
' + lst(G.banners, x => esc(x.name || x.code) + '
/c/' + esc(x.code) + ' ') + '
Banners that point straight at a page. A banner that points at a spin link is counted under that spin link.
'
+ 'By traffic source Source Goals
' + lst(G.sources, x => esc(x.name)) + '
A named source is one you set up under
Sources ; otherwise it is the referring site or the ad placement.
';
}).join('');
$('goalRows').querySelectorAll('[data-pick]').forEach(b => b.addEventListener('click', () => { PICK = b.dataset.pick; $('goalPick').value = PICK; snippets(); $('snTag').scrollIntoView({ behavior: 'smooth', block: 'center' }); }));
$('goalRows').querySelectorAll('[data-edit]').forEach(b => b.addEventListener('click', () => goalForm(goals.find(g => g.px === b.dataset.edit))));
$('goalRows').querySelectorAll('[data-del]').forEach(b => b.addEventListener('click', async () => {
if (!await CR.dialog({ title: 'Remove this goal?', text: 'Its pixel stops counting at once. Conversions already counted stay in your reports.', ok: 'Remove', danger: true })) return;
try { await CR.api('/api/my/conversions/goals/' + b.dataset.del, { remove: true }); CR.toast('Goal removed'); load(); } catch (e) { CR.toast(e.message, true); }
}));
}
async function goalForm(g) {
const v = await CR.dialog({ title: g ? 'Edit goal' : 'Add a goal', text: 'One word for what you count, like subscriber or sale. The default value is optional, in US dollars, used when the pixel carries none.',
fields: [{ k: 'name', label: 'Goal name', value: g ? g.name : '', placeholder: 'subscriber', max: 32 }, { k: 'value', label: 'Default value (optional)', value: g && g.value ? String(g.value) : '', placeholder: '0.00', max: 10 }], ok: g ? 'Save' : 'Add goal' });
if (!v) return;
try { const r = g ? await CR.api('/api/my/conversions/goals/' + g.px, v) : await CR.api('/api/my/conversions/goals', v); if (r.goal) PICK = r.goal.px; CR.toast(g ? 'Saved' : 'Goal added. Its snippets are ready below.'); load(); } catch (e) { CR.toast(e.message, true); }
}
$('addGoal').addEventListener('click', () => goalForm(null));
$('goalPick').addEventListener('change', () => { PICK = $('goalPick').value; snippets(); });
document.querySelectorAll('[data-copy]').forEach(b => b.addEventListener('click', () => CR.copy($(b.dataset.copy).textContent)));
// the test loads the real image pixel, the same request a thank-you page makes; it is recorded apart and never counted
$('testBtn').addEventListener('click', async () => {
const px = PICK; if (!px) return; $('testBtn').disabled = true; $('testNote').textContent = 'Sending...';
let ok = false; try { const r = await fetch('/cv.gif?px=' + px + '&test=1&r=' + Date.now(), { cache: 'no-store' }); ok = r.ok && /^test/.test(r.headers.get('X-LS-Result') || ''); } catch (e) {}
if (!ok) { try { await CR.api('/api/my/conversions/goals/' + px, { test: true }); ok = true; } catch (e) {} }
CR.toast(ok ? 'Test received. Your pixel id works.' : 'The test did not arrive. Try again in a minute.', !ok); $('testBtn').disabled = false; load();
});
async function load() {
try { D = await CR.get('/api/my/conversions?days=' + RANGE); } catch (e) { CR.sub('could not load'); return; }
paint();
}
CR.requireSession(load);
})();