// Banner Spin click attribution (Michael Camire 2026-11-14: his "Login Ad Explosion" showed Clicks 3 overall while every // banner's own Clicks said 0. All three clicks came in with no view on record for that visitor, so they were counted on // the fallback and no banner got them). Checks, on bannerspin.js directly (throwaway DATA_DIR, fake rotator, no server): // impression -> click from the same browser credits that banner (note), the script embed's ?x= credits exactly, // a click from the same connection in ANOTHER browser (mail app / exchange app view, click opens in the phone browser) // credits the banner it was shown (ip), a click with no view anywhere stays "not tied to a banner", // and banner rows + fallback shown + not tied add up to the Banner Spin totals. // node qa/bannerspin-attrib.test.cjs 'use strict'; const assert = require('assert'); const fs = require('fs'); const os = require('os'); const path = require('path'); const SITE = path.join(__dirname, '..'); const DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'bs-attrib-')); const bs = require(path.join(SITE, 'bannerspin.js')); const rotator = { cellAt: () => 0, zoneFor: () => 'UTC', isOn: () => true, rawRotations: () => [], cleanHours: () => ({}), cleanTz: () => ({ tz: 'visitor' }) }; const OWNER = 'owner@bs.test'; let passed = 0, failed = 0; const results = []; async function t(name, fn) { try { await fn(); passed++; results.push('ok ' + name); } catch (e) { failed++; results.push('FAIL ' + name + '\n ' + String(e && e.stack || e).split('\n').slice(0, 4).join('\n ')); } } const req = (url, ip, ua, referer) => ({ url, headers: Object.assign({ 'x-forwarded-for': ip, 'user-agent': ua }, referer ? { referer } : {}), socket: {} }); const TG = n => 'https://offer.example/b' + n; (async () => { bs.init({ dataDir: DATA, rotator, adminEmail: OWNER, schedule: false }); let S, code; await t('a Banner Spin with 3 banners (one target each) and a fallback', async () => { const c = await bs.create(OWNER, { name: 'Attrib', size: '468x60', fallback: 'https://fallback.example/' }); assert.ok(!c.error, c.error); S = c.spin; code = S.code; for (const n of [1, 2, 3]) { const r = await bs.addBanner(OWNER, S.id, { image: 'https://img.example/' + n + '.png', target: TG(n), weight: 10 }); assert.ok(!r.error, r.error); } }); const shown = async (ip, ua, ref) => { const im = await bs.image(code, req('/i/' + code + '.gif', ip, ua, ref), null); const n = Number(/\/(\d)\.png$/.exec(im.url)[1]); return n; }; const clickAs = (ip, ua, ref, q) => bs.click(code, req('/c/' + code + (q || ''), ip, ua, ref), 'linkspin.app'); const want = { 1: 0, 2: 0, 3: 0 }; await t('impression -> click, same browser, with a referrer: that banner, matched by note', async () => { for (let i = 0; i < 6; i++) { const ip = '10.9.0.' + i, ua = 'Mozilla/5.0 Desktop' + i; const n = await shown(ip, ua, 'https://exchange-a.example/surf'); const c = await clickAs(ip, ua, 'https://exchange-a.example/surf'); assert.strictEqual(c.url, TG(n)); assert.strictEqual(c.matched, 'note'); assert.strictEqual(c.banner.id, n); want[n]++; } }); await t('impression -> click, same browser, no referrer on either (mailer, privacy settings): that banner', async () => { const n = await shown('10.9.1.1', 'Mozilla/5.0 Quiet', ''); const c = await clickAs('10.9.1.1', 'Mozilla/5.0 Quiet', ''); assert.strictEqual(c.banner && c.banner.id, n); assert.strictEqual(c.matched, 'note'); want[n]++; }); await t('viewed in a mail or exchange app, clicked in the phone browser (same IP, other UA): that banner, matched by ip', async () => { const n = await shown('10.9.2.2', 'MailApp/3.1 (Android) WebView', ''); const c = await clickAs('10.9.2.2', 'Mozilla/5.0 (Linux; Android 14) Chrome/130 Mobile', ''); assert.strictEqual(c.banner && c.banner.id, n); assert.strictEqual(c.matched, 'ip'); assert.strictEqual(c.url, TG(n)); want[n]++; }); await t('script embed click (?x=2) credits banner 2 exactly', async () => { const c = await clickAs('10.9.3.3', 'Mozilla/5.0 Script', 'https://blog.example/', '?x=2'); assert.strictEqual(c.banner.id, 2); assert.strictEqual(c.matched, 'exact'); want[2]++; }); await t('a click with no view anywhere: fallback link, not tied to a banner', async () => { const c = await clickAs('10.9.4.4', 'Mozilla/5.0 NeverSaw', ''); assert.strictEqual(c.banner, null); assert.strictEqual(c.matched, 'none'); assert.strictEqual(c.url, 'https://fallback.example/'); }); await t('stats: per-banner clicks match what was shown, and banners + fallback shown + not tied = the total', async () => { bs.flush(); const st = await bs.stats(OWNER, S.id); for (const b of st.banners) assert.strictEqual(b.clicks, want[b.id], 'banner ' + b.id + ' clicks ' + b.clicks + ' want ' + want[b.id]); assert.strictEqual(st.unmatched.clicks, 1); assert.strictEqual(st.fallback.clicks, 0); const sum = st.banners.reduce((n, b) => n + b.clicks, 0) + st.fallback.clicks + st.unmatched.clicks; assert.strictEqual(sum, st.totals.clicks); assert.strictEqual(st.totals.clicks, 10); assert.strictEqual(st.totals.matched, 9); assert.strictEqual(st.totals.unmatched, 1); const views = st.banners.reduce((n, b) => n + b.imps, 0) + st.fallback.imps; assert.strictEqual(views, st.totals.imps); assert.strictEqual(st.totals.imps, 8); }); console.log(results.join('\n')); console.log('\nbannerspin-attrib: ' + passed + ' passed, ' + failed + ' failed'); try { fs.rmSync(DATA, { recursive: true, force: true }); } catch (e) {} process.exit(failed ? 1 : 0); })();