Traffic sources: network macros, campaign URLs, per-source postbacks, Reports dimension
A Source is a first-class record of a network a member buys traffic on (PropellerAds,
Zeropark, Adsterra, ExoClick and 25 more from the shipped catalog, or defined by hand).
It remembers the network's macro tokens and conversion postback, attaches to spin links,
builds the exact campaign URL to paste into the network, fires that network's own
postback only for clicks that came through it, and shows up in Reports by source and
by the zone / placement / site the macro carried.
- data/traffic-sources.json: catalog of 29 networks (BeMob template harvest), un-ignored
via data/* + !data/traffic-sources.json; loader tolerates missing fields, null costModel,
{{slot}} labels with spaces and parentheses, optional slots, unknown single-brace macros
- sources.js: DATA_DIR/sources.json model, 30 per account, 20 params, key allow-list,
postbacks.validateUrl on the filled postback; list/create/update/get/urlFor/test
- rotator.js: update() takes sourceId (owner-checked), pub() exposes it, hits and conversions
are stamped with the source at click time (sr), wider query allow-list (cost, creative,
site, banner, keyword, geo, os, browser, device, carrier, format, external_id, visitor_id),
summary() adds sources[] and placements[], sourceCounts(), detachSource()
- postbacks.js: fire() delivers the matching source's postback after the manual ones through
the same deliver() (guards, retry, rate cap, auto-disable); log rows carry source; convertFor,
inbound and the page lead path pass sourceId; fill/deliver/logForSource exported
- routes (GET and POST): /api/sources/catalog, /api/my/sources, /api/my/sources/<id>,
/api/my/sources/<id>/url; /sources screen
- /sources Control Room screen (list + Campaign URL / Postback / Settings tabs, two-step
New source dialog); Links share kit gets a source select and a "Campaign URL for <network>"
first row; Reports gets Traffic sources and Placements; rail entry; chatbot canned answer,
FACTS line and KNOWLEDGE_DATE 2026-10-12
- cr.css: phone rail icons only at 34px so ten entries fit; cache tags bumped to 20261012a
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
+21
-9
@@ -11,11 +11,11 @@ const crypto = require('crypto');
|
||||
const dns = require('dns').promises;
|
||||
const net = require('net');
|
||||
|
||||
let DATA_DIR = null, ownHosts = () => [], rotator = null;
|
||||
let DATA_DIR = null, ownHosts = () => [], rotator = null, SOURCES = null; // SOURCES = sources.js: a traffic source's own postback fires for its own clicks (2026-10-12)
|
||||
const J = { db: null, FILE: () => path.join(DATA_DIR, 'postbacks.json'),
|
||||
load() { try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); } catch (e) { this.db = { v: 1, items: [], log: [], keys: {}, clicks: {} }; } for (const k of ['items', 'log']) if (!Array.isArray(this.db[k])) this.db[k] = []; for (const k of ['keys', 'clicks']) if (!this.db[k]) this.db[k] = {}; },
|
||||
save() { try { fs.writeFileSync(this.FILE(), JSON.stringify(this.db)); } catch (e) {} } };
|
||||
function init(opts) { DATA_DIR = opts.dataDir; ownHosts = opts.ownHosts || (() => []); rotator = opts.rotator; J.load(); }
|
||||
function init(opts) { DATA_DIR = opts.dataDir; ownHosts = opts.ownHosts || (() => []); rotator = opts.rotator; SOURCES = opts.sources || null; J.load(); }
|
||||
const norm = e => String(e || '').trim().toLowerCase();
|
||||
const EVENTS = ['join', 'lead', 'wallet', 'purchase', 'external'];
|
||||
const MACRO = /\{([a-z0-9_]{1,24})\}/gi;
|
||||
@@ -67,6 +67,7 @@ async function update(owner, id, body) {
|
||||
J.save(); return { ok: true, postback: pubItem(p) };
|
||||
}
|
||||
async function logFor(owner, n) { return J.db.log.filter(l => l.owner === norm(owner)).slice(-(n || 50)).reverse(); }
|
||||
async function logForSource(owner, sid, n) { return J.db.log.filter(l => l.owner === norm(owner) && l.source === String(sid)).slice(-(n || 30)).reverse(); }
|
||||
|
||||
// -- partner key (inbound)
|
||||
function partnerKey(owner) { const o = norm(owner); if (!J.db.keys[o]) { J.db.keys[o] = crypto.randomBytes(12).toString('hex'); J.save(); } return J.db.keys[o]; }
|
||||
@@ -91,23 +92,34 @@ async function deliver(p, url, ctx) {
|
||||
}
|
||||
} catch (e) { note = String(e && e.message || e).slice(0, 120); }
|
||||
p.lastStatus = status || note.slice(0, 40); p.lastAt = Date.now(); if (ok) { p.sent = (p.sent || 0) + 1; p.fails = 0; } else { p.fails = (p.fails || 0) + 1; if (p.fails >= 10) p.enabled = false; }
|
||||
J.db.log.push({ ts: t0, owner: p.owner, pb: p.id, name: p.name, event: ctx.event, lsid: ctx.lsid || '', url: url.slice(0, 300), status, ok, ms: Date.now() - t0, note, test: !!ctx.test });
|
||||
J.db.log.push({ ts: t0, owner: p.owner, pb: p.id, name: p.name, event: ctx.event, lsid: ctx.lsid || '', url: url.slice(0, 300), status, ok, ms: Date.now() - t0, note, test: !!ctx.test, source: ctx.viaSource || undefined });
|
||||
if (J.db.log.length > 2000) J.db.log = J.db.log.slice(-1500);
|
||||
J.save(); return { ok, status, note };
|
||||
}
|
||||
// fire every enabled postback of `owner` that listens to `event`; ctx carries the click's params and the event facts
|
||||
async function fire(owner, event, ctx) {
|
||||
const o = norm(owner); const out = [];
|
||||
const q = (ctx.q || {});
|
||||
const vals = { clickid: q.clickid || q.cid || q.click_id || q.external_id || q.visitor_id || '', subid: q.subid || q.sub_id || q.s1 || '', s1: q.s1 || q.sub1 || '', s2: q.s2 || q.sub2 || '', s3: q.s3 || q.sub3 || '', s4: q.s4 || q.sub4 || '', s5: q.s5 || q.sub5 || '', source: ctx.source || '', event, payout: ctx.payout != null ? ctx.payout : '', currency: ctx.currency || 'USD', txid: ctx.txid || '', lsid: ctx.lsid || '', code: ctx.code || '', country: ctx.country || '', device: ctx.device || '' };
|
||||
for (const [k, v] of Object.entries(q)) if (/^(utm_[a-z]+|sub\d|s\d|aff_sub\d?|t\d|cost|zone|placement|site|campaign|creative|banner|keyword|geo|os|browser|carrier|format)$/.test(k) && vals[k] == null) vals[k] = v;
|
||||
for (const p of J.db.items.filter(x => x.owner === o && x.enabled && x.events.includes(event))) {
|
||||
if (!rateOk(o)) break;
|
||||
const q = (ctx.q || {});
|
||||
const vals = { clickid: q.clickid || q.cid || q.click_id || '', subid: q.subid || q.sub_id || q.s1 || '', s1: q.s1 || q.sub1 || '', s2: q.s2 || q.sub2 || '', s3: q.s3 || q.sub3 || '', s4: q.s4 || q.sub4 || '', s5: q.s5 || q.sub5 || '', source: ctx.source || '', event, payout: ctx.payout != null ? ctx.payout : '', currency: ctx.currency || 'USD', txid: ctx.txid || '', lsid: ctx.lsid || '', code: ctx.code || '', country: ctx.country || '', device: ctx.device || '' };
|
||||
for (const [k, v] of Object.entries(q)) if (/^(utm_[a-z]+|sub\d|s\d|aff_sub\d?|t\d)$/.test(k) && vals[k] == null) vals[k] = v;
|
||||
const url = fill(p.url, vals);
|
||||
let r = await deliver(p, url, Object.assign({}, ctx, { event }));
|
||||
if (!r.ok && !ctx.test) { await new Promise(res => setTimeout(res, 4000)); r = await deliver(p, url, Object.assign({}, ctx, { event })); }
|
||||
out.push({ id: p.id, name: p.name, ok: r.ok, status: r.status, note: r.note });
|
||||
}
|
||||
// the traffic source that sent this click (and only that one) hears about it through its own postback
|
||||
if (ctx.sourceId && SOURCES) {
|
||||
const s = SOURCES.forFire(o, ctx.sourceId);
|
||||
if (s && (s.events || []).includes(event) && rateOk(o)) {
|
||||
const url = fill(s.postbackUrl, vals);
|
||||
let r = await deliver(s, url, Object.assign({}, ctx, { event, viaSource: s.id }));
|
||||
if (!r.ok && !ctx.test) { await new Promise(res => setTimeout(res, 4000)); r = await deliver(s, url, Object.assign({}, ctx, { event, viaSource: s.id })); }
|
||||
SOURCES.save();
|
||||
out.push({ id: s.id, name: s.name, source: true, ok: r.ok, status: r.status, note: r.note });
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
async function test(owner, id) {
|
||||
@@ -122,7 +134,7 @@ async function convertFor(email, event, facts) {
|
||||
const c = clickOf(email); if (!c) return null;
|
||||
const hit = rotator && rotator.convert ? await rotator.convert(c.lsid, event, facts || {}) : null;
|
||||
if (!hit) return null;
|
||||
const ctx = Object.assign({ q: hit.q || {}, source: hit.s, country: hit.c, device: hit.dv, lsid: c.lsid, code: hit.code }, facts || {});
|
||||
const ctx = Object.assign({ q: hit.q || {}, source: hit.s, country: hit.c, device: hit.dv, lsid: c.lsid, code: hit.code, sourceId: hit.sourceId || '' }, facts || {});
|
||||
try { await fire(hit.owner, event, ctx); } catch (e) {}
|
||||
return hit;
|
||||
}
|
||||
@@ -134,8 +146,8 @@ async function inbound(key, body) {
|
||||
const hit = rotator && rotator.convert ? await rotator.convert(lsid, event, { payout: value, label }) : null;
|
||||
if (!hit) return { error: 'No click with that lsid.' };
|
||||
if (hit.owner !== owner) return { error: 'That click is not yours.' };
|
||||
const ctx = { q: hit.q || {}, source: hit.s, country: hit.c, device: hit.dv, lsid, code: hit.code, payout: value, currency: String(body.currency || 'USD').slice(0, 6), txid: String(body.txid || '').slice(0, 80), label };
|
||||
const ctx = { q: hit.q || {}, source: hit.s, country: hit.c, device: hit.dv, lsid, code: hit.code, sourceId: hit.sourceId || '', payout: value, currency: String(body.currency || 'USD').slice(0, 6), txid: String(body.txid || '').slice(0, 80), label };
|
||||
try { await fire(owner, event, ctx); } catch (e) {}
|
||||
return { ok: true, recorded: event, lsid };
|
||||
}
|
||||
module.exports = { init, EVENTS, validateUrl, list, create, update, logFor, partnerKey, rotateKey, bindClick, clickOf, fire, test, convertFor, inbound };
|
||||
module.exports = { init, EVENTS, validateUrl, fill, deliver, list, create, update, logFor, logForSource, partnerKey, rotateKey, bindClick, clickOf, fire, test, convertFor, inbound };
|
||||
|
||||
Reference in New Issue
Block a user