Files
linkspin/public/assets/conversions.js
T
martbost 98e3ed36d8 Conversion tracking fixes (Marty approved 2026-10-26): own clicks are tests on every click type, Banner Spin straight-to-page clicks carry lsclk
1. Own clicks: a goal owner's own click on ANY click type (LinkSpin/InstantAdPay ads, spin links, Banner Spins) is kept as a test, never counted, never fires a goal postback. Own = ad click flagged self for that owner, a spin-link / Banner Spin click marked at click time (signed in as the owner, or from an owner soft key: markOwnClick in server.js), or the clicking or converting browser's soft key is one the owner signs in from. auth.onSeen notes LinkSpin session soft keys (throttled, kept 31 days in convtrack.json S.own); /api/partner/clicks takes owners [{owner, sk, ts}] from InstantAdPay before the clicks, so IAP members' own browsers are known too. Linked positions share the owner's email. X-LS-Result adds ;self. partnerSummary returns selfTests for the IAP card. Conversions screen copy: test with Send a test conversion and the &test=1 page check, own clicks never count (conversions.js ?v=20261026a).
3. Banner Spin: a click whose target is a direct page (not a /r/ spin link) gets ?lsclk=<k click id> when the owner tracks goals (string insert like lsconv.withClickId: params kept, before #hash, an existing lsclk never replaced). convtrack.lookup resolves k ids through bannerspin.clickById; conversions record k:'bspin' with the Banner Spin, banner, page and source; report By Banner Spin, CSV site 'Banner Spin', postback ctx with its sub ids.
Guides: /conversion-tracking (Banner Spin straight to a page now counted, own clicks on every type are tests, By Banner Spin), /refuel tile. Chatbot CANNED + FACTS, KNOWLEDGE_DATE 2026-10-26.
QA: convpixel 77/77 (new: owner soft keys, own ad/spin/Banner Spin/IAP clicks as tests, stranger still counts, own-browser conversion, withClickId, Banner Spin lsclk + By Banner Spin + spin-link banner untouched, selfTests in the IAP summary, no postback for own clicks); lsid 31/31; fuel all pass (engine.js / engine-room.js / kit.js untouched, byte-identical with InstantAdPay); bannerspin 75/75; member walk 0 bugs; public 0 bugs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 06:19:54 -05:00

72 lines
10 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 (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 => '<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 Banner Spin</h2><div class="tbl lst"><div class="hd"><span>Banner Spin</span><span class="r">Goals</span></div>' + lst(G.banners, x => esc(x.name || x.code) + ' <span class="pill">/c/' + esc(x.code) + '</span>') + '</div><div class="muted" style="margin-top:8px">Banners that point straight at a page. A banner that points at a spin link is counted under that spin link.</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);
})();