07379803d7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
71 lines
9.8 KiB
JavaScript
71 lines
9.8 KiB
JavaScript
// 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: '<span style="display:inline-flex;gap:8px;align-items:center"><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> <a class="btn" id="csvBtn" href="/api/my/conversions.csv?days=30">CSV</a></span>' });
|
|
$('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 = '<script async src="' + base + '/t.js?px=' + g.px + '"></script>';
|
|
$('snImg').textContent = '<img src="' + base + '/cv.gif?px=' + g.px + '&goal=' + g.name + '" width="1" height="1" alt="" style="display:none">';
|
|
$('snJs').textContent = '<script async src="' + base + '/cv.js?px=' + g.px + '&goal=' + g.name + '"></script>';
|
|
$('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 => '<div class="rw"><span><b>' + esc(g.name) + '</b><span class="muted" style="display:block;font-size:11px">last ' + esc(ago(g.lastAt)) + (g.lastTest ? ' · test ' + esc(ago(g.lastTest)) : '') + '</span></span><span class="mono">' + esc(g.px) + '</span><span class="r num">' + (g.value ? esc(money(g.value)) : '<span class="muted">none</span>') + '</span><span class="r num">' + fmt(g.conv) + '</span><span class="r"><button class="btn sm" type="button" data-pick="' + g.px + '">Snippets</button> <button class="btn sm" type="button" data-edit="' + g.px + '">Edit</button> <button class="btn sm" type="button" data-del="' + g.px + '">Remove</button></span></div>').join('')
|
|
: '<div class="rw"><span class="muted">No goals yet. Add one, for example <b>subscriber</b> or <b>sale</b>.</span><span></span><span></span><span></span><span></span></div>';
|
|
if (!goals.some(g => g.px === PICK)) PICK = goals.length ? goals[0].px : '';
|
|
$('goalPick').innerHTML = goals.map(g => '<option value="' + g.px + '"' + (g.px === PICK ? ' selected' : '') + '>' + esc(g.name) + '</option>').join('');
|
|
snippets();
|
|
const m = D.methods || {}; const tot = METH.reduce((n, [k]) => n + (m[k] || 0), 0);
|
|
$('methRows').innerHTML = METH.map(([k, label]) => '<div class="m"><span class="k">' + label + '</span><span class="v">' + fmt(m[k] || 0) + '</span><span class="muted mono" style="font-size:11px">' + (tot ? Math.round((m[k] || 0) / tot * 100) + '%' : '·') + '</span></div>').join('');
|
|
$('totalNote').textContent = D.selfTests ? D.selfTests + ' from your own clicks, 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 => '<div class="rw"><span>' + esc(c.name || ('Campaign #' + c.id)) + '<span class="muted" style="display:block;font-size:11px">' + esc(c.site) + ' #' + c.id + (c.spentAsOf ? ' · spend as of ' + esc(ago(c.spentAsOf)) : '') + '</span></span><span class="r num">' + (c.spent != null ? fmt(c.spent) + ' cr' : '·') + '</span><span class="r num">' + fmt(c.clicks) + '</span><span class="r num">' + fmt(c.n) + '</span><span class="r num">' + (c.rate != null ? c.rate + '%' : '·') + '</span><span class="r num">' + (c.costPer != null ? fmt(c.costPer) + ' cr' : '·') + '</span></div>').join('') : '<div class="rw"><span class="muted">No ad campaign conversions for this goal yet.</span><span></span><span></span><span></span><span></span><span></span></div>';
|
|
const lst = (rows, f) => rows && rows.length ? rows.map(x => '<div class="rw"><span style="overflow-wrap:anywhere">' + f(x) + '</span><span class="r num">' + fmt(x.n) + '</span></div>').join('') : '<div class="rw"><span class="muted">nothing yet</span><span></span></div>';
|
|
const gm = G.methods || {};
|
|
return '<div class="sec">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(', ') + '</div>'
|
|
+ '<div class="panel"><h2>By ad campaign</h2><div class="tbl camps"><div class="hd"><span>Campaign</span><span class="r">Spent</span><span class="r">Clicks</span><span class="r">Goals</span><span class="r">Rate</span><span class="r">Cost / goal</span></div>' + camp + '</div><div class="muted" style="margin-top:8px">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.</div></div>'
|
|
+ '<div class="two"><div class="panel"><h2>By spin link</h2><div class="tbl lst"><div class="hd"><span>Link</span><span class="r">Goals</span></div>' + lst(G.links, x => esc(x.name || x.code) + ' <span class="pill">/r/' + esc(x.code) + '</span>') + '</div></div>'
|
|
+ '<div class="panel"><h2>By destination page</h2><div class="tbl lst"><div class="hd"><span>Page</span><span class="r">Goals</span></div>' + lst(G.dests, x => '<span class="mono">' + esc(x.k) + '</span>') + '</div></div></div>'
|
|
+ '<div class="panel"><h2>By traffic source</h2><div class="tbl lst"><div class="hd"><span>Source</span><span class="r">Goals</span></div>' + lst(G.sources, x => esc(x.name)) + '</div><div class="muted" style="margin-top:8px">A named source is one you set up under <a href="/sources">Sources</a>; otherwise it is the referring site or the ad placement.</div></div>';
|
|
}).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);
|
|
})();
|