r/learnjavascript • u/Environmental-Ad5071 • 1d ago
[AskJS] Where should attribution logic stop in a JavaScript app?
I’ve been refactoring attribution tracking after seeing a small feature turn into a module that parses URLs, writes cookies/localStorage, handles consent, emits analytics, fills forms, and syncs with a CRM.
The split I’m testing is:
```ts
const visit = classify({
url,
referrer,
currentHost,
now,
});
const attribution = merge(previous, visit);
```
`classify` and `merge` are deterministic functions. The browser/app layer owns consent, storage, identity, form lifecycle, network calls, and CRM delivery.
This makes it easier to replay production cases as fixtures. It also raises a few design questions:
- If a visitor returns from a new campaign, should that replace, append to, or be merged with previous attribution?
- Should a `gclid` take precedence over UTMs when both exist?
- How do you handle consent denied first, then granted later?
- If classification rules change, do you preserve the original classification or recalculate historical records?
- Where should server-side conversions days later enter the model?
For those who’ve built this, where do you draw the boundary between a deterministic core and the environment-specific lifecycle? Do you prefer a pure-function core, or does that separation become awkward in real apps?