Files

55 lines
3.7 KiB
JavaScript

// Paid-traffic source tracking (Marty, 2026-10-03: AdsTerra/BeMob test into Drip Runner). A landing URL carries
// ?src=<source>&cid=<tracker click id>; we keep them in a first-party cookie (dr.src, 30 days, last paid touch wins)
// and count, per Central day and source: landing visits, guests started, runs started (and distinct players), sign-ins
// and NEW InstantAdPay accounts. A new account fires the tracker postback once (DR_POSTBACK_URL, {cid} {txid} tokens).
const crypto = require('crypto');
const store = require('./store');
const COOKIE = 'dr.src';
const clean = (v, n) => String(v || '').toLowerCase().replace(/[^a-z0-9_.-]/g, '').slice(0, n);
const ctDay = () => new Date().toLocaleDateString('en-CA', { timeZone: 'America/Chicago' });
function fromQuery(u) { // -> { src, cid } or null
const src = clean(u.searchParams.get('src') || u.searchParams.get('utm_source'), 30);
if (!src) return null;
const cid = String(u.searchParams.get('cid') || u.searchParams.get('clickid') || '').replace(/[^A-Za-z0-9_.-]/g, '').slice(0, 80);
return { src, cid };
}
function cookieHeader(t, isProd) { return COOKIE + '=' + encodeURIComponent(t.src + '|' + (t.cid || '') + '|' + Date.now()) + '; Path=/; Max-Age=' + 30 * 86400 + '; SameSite=Lax' + (isProd ? '; Secure' : ''); }
function fromRequest(req) {
const m = /(?:^|;\s*)dr\.src=([^;]+)/.exec(req.headers.cookie || ''); if (!m) return null;
const [src, cid, ts] = decodeURIComponent(m[1]).split('|');
return clean(src, 30) ? { src: clean(src, 30), cid: String(cid || '').replace(/[^A-Za-z0-9_.-]/g, '').slice(0, 80), ts: Number(ts) || 0 } : null;
}
function bump(src, field, id) {
const day = ctDay();
store.update('srcstats', {}, all => {
const d = all[day] = all[day] || {}; const r = d[src] = d[src] || { visits: 0, guests: 0, runs: 0, players: [], signins: 0, signups: 0 };
r[field] = (r[field] || 0) + 1;
if (id != null && !r.players.includes(String(id)) && r.players.length < 5000) r.players.push(String(id));
const days = Object.keys(all).sort(); while (days.length > 90) delete all[days.shift()];
return all;
});
}
function summary(days) {
const all = store.read('srcstats', {}); const keys = Object.keys(all).sort().slice(-(days || 30));
const rows = []; const tot = {};
for (const day of keys) for (const [src, r] of Object.entries(all[day])) {
rows.push({ day, src, visits: r.visits || 0, guests: r.guests || 0, runs: r.runs || 0, players: (r.players || []).length, signins: r.signins || 0, signups: r.signups || 0 });
const t = tot[src] = tot[src] || { src, visits: 0, guests: 0, runs: 0, signins: 0, signups: 0 };
for (const k of ['visits', 'guests', 'runs', 'signins', 'signups']) t[k] += r[k] || 0;
}
return { rows: rows.reverse(), totals: Object.values(tot) };
}
// the tracker conversion: once per click id, only for a NEW InstantAdPay account
async function postback(t, memberId) {
const tpl = String(process.env.DR_POSTBACK_URL || '').trim();
if (!tpl || !t || !t.cid) return false;
const done = store.read('postbacks', {}); if (done[t.cid]) return false;
const txid = crypto.createHash('sha256').update(String(memberId)).digest('hex').slice(0, 16);
const url = tpl.replace(/\{cid\}/g, encodeURIComponent(t.cid)).replace(/\{txid\}/g, txid).replace(/\{src\}/g, encodeURIComponent(t.src));
store.update('postbacks', {}, d => { d[t.cid] = { at: Date.now(), src: t.src, member: String(memberId) }; return d; });
try { const r = await fetch(url, { signal: AbortSignal.timeout(8000) }); console.log('postback', t.src, r.status); return r.ok; }
catch (e) { console.warn('postback failed', t.src, e.message); return false; }
}
module.exports = { fromQuery, fromRequest, cookieHeader, bump, summary, postback, COOKIE };