Our analytics told us how many people signed up. It couldn't tell us which page, search or link brought any one of them. GA4 knew a signup came from "Organic Search" and landed on the homepage. Our database knew the account. Nothing joined the two.

When signups looked low last week, that gap meant we were guessing.

The setup is a common one: a marketing site on www (Next.js) and the app on a subdomain (React + Vite), with the signup form in the app. This is what we shipped to record the first touch on every new account, with no third-party tool and no personal data.

1. Record the first visit in a cookie both subdomains can read

The marketing site sets one first-party cookie on the parent domain the first time someone visits. It never overwrites it, so it keeps the first touch even if the visitor comes back three times before signing up.

const COOKIE = 'first_touch'; const MAX_AGE = 60 * 60 * 24 * 90; // 90 days const UTM_KEYS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']; function externalReferrer() { try { const ref = new URL(document.referrer); if (ref.hostname === 'example.com' || ref.hostname.endsWith('.example.com')) return ''; return `${ref.origin}${ref.pathname}`.slice(0, 300); // no query string } catch { return ''; } } if (!document.cookie.split('; ').some((c) => c.startsWith(`${COOKIE}=`))) { const params = new URLSearchParams(location.search); const touch = { lp: location.pathname, ref: externalReferrer(), ts: Date.now() }; for (const key of UTM_KEYS) { if (params.get(key)) touch[key] = params.get(key).slice(0, 200); } document.cookie = `${COOKIE}=${encodeURIComponent(JSON.stringify(touch))}; path=/; max-age=${MAX_AGE}; SameSite=Lax; Secure; domain=.example.com`; } 

Three choices matter here:

  • domain=.example.com, so the app subdomain can read what the marketing site wrote.
  • Origin and path only for the referrer. A referrer's query string can carry search terms or tokens, and we don't want them.
  • First touch, not last. Last touch tells you which page closed the deal. We wanted to know what found them.

2. The app captures it too, then sends it with the signup

Some people land on the app first, from a link in a doc or a direct signup URL. The app runs the same capture and prefixes the path with app: so we can tell them apart.

At signup it reads the cookie and adds two things: the page the form was on, and GA4's client id from the _ga cookie, so an account can be matched to its sessions later.

function gaClientId() { const raw = document.cookie.split('; ').find((c) => c.startsWith('_ga='))?.slice(4) || ''; const match = raw.match(/^GA\d\.\d\.(\d+\.\d+)$/); return match ? match[1] : ''; } await api.post('/auth/signup', { name, email, password, acquisition: getAcquisition() }); 

Google sign-in uses the same object, sent in the body of the call that creates the account.

3. The server trusts none of it

It's client data, so the backend only ever stores it. Every field is a trimmed, length-capped string, unknown keys are dropped, and anything malformed is ignored instead of failing the signup.

const FIELDS = { landingPage: 300, referrer: 300, utmSource: 200, utmMedium: 200, utmCampaign: 200, signupPage: 300 }; const clean = (v, max) => typeof v === 'string' ? v.replace(/[\u0000-\u001f]/g, '').trim().slice(0, max) : ''; export function sanitizeAcquisition(input) { if (!input || typeof input !== 'object' || Array.isArray(input)) return undefined; const out = {}; for (const [field, max] of Object.entries(FIELDS)) { const value = clean(input[field], max); if (value) out[field] = value; } const ga = clean(input.gaClientId, 40); if (/^\d{1,20}\.\d{1,12}$/.test(ga)) out.gaClientId = ga; return Object.keys(out).length ? out : undefined; } 

Two bugs I nearly shipped:

  • Our request validator strips unknown keys. Until I added acquisition to the schema, the field silently vanished before the controller ever saw it.
  • My first schema was Joi.object().max(12). That would have rejected the whole signup if a browser sent something odd. A bad source value should never cost you a signup, so the validator now accepts anything there and the sanitizer drops what it doesn't like. A { "$ne": null } sent as a field becomes nothing, not a database operator.

What it doesn't do

  • It only covers accounts created from now on. There's nothing to backfill.
  • If cookies are blocked, there's no source. Signup still works.
  • It's first touch only. Every touch is a different table.

Our admin screen now shows "Came from: referrer → landing page" on each new account. It's a small change, but it's the difference between guessing and knowing which pages bring people who sign up.

I build Cresca, an email platform, and this is running there now.

A question for you: do you record first touch, last touch, or both at signup? If both, has the difference ever changed what you built next?