Skip to content

feat(traffic): follow an AI visitor to conversions in the tracking snippet, consent first (CE-017, part 2) - #1528

Merged
gkhngyk merged 20 commits into
ansvisor:mainfrom
cihan-gyk:feature/ce-017-snippet-journeys
Oct 26, 2026
Merged

gkhngyk merged 20 commits into
ansvisor:mainfrom
cihan-gyk:feature/ce-017-snippet-journeys

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

CE-017, part 2 of 6: the tracking snippet. Stacked on #1527 (part 1, server). The PR's own changes are its commits after #1527's branch.

The snippet (t.js) follows a visitor who came from an AI assistant to conversions. This happens only on a site whose install tag carries data-c="1" (the brand turned conversions on; the Settings section comes in part 4). Without it nothing changes: same beacons, nothing kept in the browser.

With data-c="1":

  • Visitor id, consent first.
    • A visitor who came from an AI assistant gets a random id in localStorage (ansvisor_v), with the first AI visit: its source, page and time.
    • In the default data-consent="wait", the id is kept only once the site says the visitor agreed: ansvisor('consent', 'granted'), or Google Consent Mode granting analytics_storage.
    • Consent Mode counts through an update, or through a default on the page before t.js that names no region.
    • A regional default ("denied in the EU, granted elsewhere", Google's own example) can't say which one this visitor got, so the snippet waits for their choice.
    • A default pushed after t.js (a GTM Consent Initialization tag, an async CMP) only ever pauses.
    • An unexpired id kept earlier counts as consent given then, on the pages after: it's only ever written after a grant.
    • Until consent, events wait on the page.
    • ansvisor('consent', 'denied'), or a Consent Mode update taking analytics_storage back, deletes the id. A default that's denied only pauses, even beside regional defaults.
    • A denied the site queued before t.js loaded is taken before anything is kept or sent.
    • Consent Mode is read before anything is kept. Without a dataLayer yet, the snippet makes one, as gtag does, so a later grant is heard.
    • data-consent="always" keeps the id at once, for a site whose own banner covers it, unless Consent Mode says otherwise.
  • Journey pages: the pages the visitor goes on to (k: "pv"), including single-page-app routes (pushState, popstate), never the same page twice in a row.
  • First touch: the first AI visit keeps the credit for 30 days. A later AI visit is a page of the same journey. Past 30 days, a new AI visit starts a new journey with a new id, and an expired record is deleted.
  • Forms (k: "cv", origin form):
    • a form with an email field (by type or by name) counts as a signup, once per page;
    • data-ansvisor-goal="demo" (or another goal) says otherwise;
    • data-ansvisor="off" opts the form out;
    • a form with a password field counts as a signup only with autocomplete="new-password". A login doesn't count, unless the form says its goal.
    • Nothing typed in a form is read; the form's data-ansvisor-label, id or name is its label.
  • The site's own calls: ansvisor('conversion', {type, value, currency, units, order, label}).
    • The goals are signup, demo, order, booking, newsletter; any other type is a custom goal named after it.
    • Calls made before t.js loads (from GTM, say) go through the queue stub, window.ansvisor.q.
    • Values are sent only as the server takes them:
      • an amount that isn't a finite number from 0 to 1e9 is left out ("49.90" is read as a number);
      • units must be a whole number;
      • long currency and order strings are cut.
    • ansvisor('conversion', 'demo') works as a short form.
  • No query strings: pages go as origin + path, so a thank-you page's ?email=… never leaves the browser.

Limits (written in the snippet's doc block; the docs come in part 6). Not seen:

  • forms in an iframe (a Typeform, a Calendly) or in a shadow root;
  • form.submit() from script, and posts without a form;
  • journeys across subdomains (storage is per origin).

A CMP template inside GTM that sets consent only through GTM's own consent API, not the dataLayer, needs a GTM tag that calls ansvisor('consent', 'granted'). The docs will say so.

The script stays plain ES5 apart from URL / URLSearchParams. It grows from about 2.5 KB to 6 KB (about +1 KB gzipped) and is still cached for an hour.

Related issue

Content Editor spec v2, CE-017 (§7b point 31).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Apply 00286 (feat(traffic): take a journey's pageviews and conversions from the AI Traffic snippet (CE-017, part 1: server) #1527) and set brands.conversions_enabled = true on a test brand.
  2. On a test page, install <script async src="<api>/t.js" data-t="<tracking_code>" data-c="1" data-consent="always"></script> with a form that has an email field, and a login form.
  3. Open the page with ?utm_source=chatgpt.com. localStorage.ansvisor_v holds the first visit.
  4. Go to another page of the site and send the email form. ai_traffic_events gets a page row and a conversion row (signup, form, label = the form's id), both credited to chatgpt.com. The login form adds nothing.
  5. In the console, run ansvisor('conversion', {type: 'demo'}). That adds a demo conversion with origin call.
  6. With data-consent="wait" (or no data-consent), nothing is kept or sent until ansvisor('consent', 'granted').

I ran steps 1–5 in a real browser on a local test page against the dev database. The page, signup (form) and demo (call) rows were stored, and the login form added nothing; the rows were removed after.

The landing visit itself wasn't stored on the dev database. It lacks 00250 / 00251 (traffic_salt, source_surface). That predates this PR, and production has both.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • npm run lint passes (run from server/)
  • npm test passes (run from server/)
  • Changes are focused — one concern per PR

The snippet's tests run it in a stand-in browser:

  • the old tests are unchanged and pass;
  • new ones cover:
    • consent, including regional Consent Mode defaults, an id kept earlier, an "always" site paused by a denied default, and a dataLayer that isn't there yet;
    • journey pages and single-page-app routes, first touch and the 30-day window;
    • forms (once a page, new-password signups, email fields by name);
    • the call queue, and value and unit handling;
    • the crypto id path, and storage that throws;
    • events the server's schema takes as they are.

A pre-push review ran; its fixes are the second commit. The consent logic had a second review of its own, walking through ten consent scenarios; its three fixes are the last commit, with tests:

  • a late regional default never grants;
  • a denying default pauses beside regional ones;
  • a queued denied comes first.

🤖 Generated with Claude Code

cihan-gyk and others added 5 commits October 26, 2026 02:16
… Traffic snippet, for brands with conversions on (CE-017, part 1: server)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rder id sent first by anyone can't shut the real one out; keep no label or order ref that looks personal; check an event before reading the brand (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ippet, with consent first: journey pages, forms, ansvisor('conversion'), Consent Mode (CE-017, part 2)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ent Mode default, read Consent Mode before keeping anything, take an id kept earlier as consent given, send only values the server takes, count a form once a page (pre-push review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e after t.js, pause on a denying default even beside regional ones, and take a queued "denied" before keeping or sending anything (consent review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, with follow-ups.

What I checked:

  • Without data-c="1".
    • A visit that isn't from an AI assistant still returns before anything else runs.
    • An AI visit sends the same beacon and the same nf check as before.
    • Nothing is read from or written to storage, and window.ansvisor stays undefined (tested).
    • The old tests are unchanged.
  • Nothing kept without consent.
    • In wait, the id is made in memory only.
    • It's written only from grant(), or at load when ok is already true: a Consent Mode update that granted, a default before t.js that names no region, an id kept earlier, or always.
    • A denied update, or a denied default with no update after it, wins over an id kept earlier and over always. A default pushed after t.js only ever pauses.
    • Events wait in memory until a grant. A denial deletes the stored id.
    • Every storage read and write is in a try, and an expired record is deleted.
  • Nothing personal sent.
    • Journey pages go as origin and path.
    • A form sends its goal and its own label, id or name, never a field's value. Login forms don't count.
    • The server checks labels and order refs again (eventLabel, orderRef).
  • What the server takes.
    • The snippet's limits sit inside traffic-events.js's schema: label 80, currency 10, ref 200, value 0 to 1e9, whole units up to 100000.
    • The goals come from CONVERSION_GOALS.
    • A bad value is left out on its own, so the event still goes, and a test runs the beacons through eventRow.
  • Size and safety.
    • About 3.4 KB more before gzip, plain ES5, all inside the IIFE.
    • cmd and the handlers declare their own x, n and it, so the endpoint and the navigation entry aren't overwritten.
    • document.currentScript is checked before data-c is read.
  • Deploy. t.js comes from the API, so none of this runs until the API is deployed. It's then cached for an hour, as before.
  • Merge. Linear on #1527, no merge commits.

Follow-ups:

  1. server/src/lib/tracking-script.js:136 (reached from :141 and :168): deny() drops the in-memory id too (v=null), so a grant later on the same page has nothing to keep.

    • Some CMPs push the visitor's current state as an update on each page load, denied until they choose. A visitor can also click "Reject" and then change their mind.
    • Either way the AI landing is lost for good. The next page has no AI referrer, so no id is ever made, and the site's conversions from AI visits read as none.
    • In deny(), delete the stored id and drop the queue, but keep v in memory. It's still never kept or sent without a grant.
    • Add a test: on the AI landing, an update that denies and then one that grants keep the id and send what waited.
  2. tracking-script.js:160-162: the queue runs in order, so a conversion queued before a denied goes out when ok is already true (an id kept earlier, or always). Run the queued consent calls first, then the rest. Then "a queued denied is taken before anything is sent" holds whatever the order. Add the case to the existing test.

  3. tracking-script.js:139-187: on a site with data-c="1", every visitor gets the hooks:

    • dataLayer.push and history.pushState wrapped;
    • a submit listener;
    • a window.dataLayer where the site has none.

    Only a visitor with an id can ever send anything, and the id is set only at load. When there's no v, return right after the queue is drained, keeping a no-op window.ansvisor. Most visitors' pages are then left as they were. In the two wrappers, wrap our part in try/catch, so the site's own push and route change always go through.

cihan-gyk and others added 8 commits October 26, 2026 09:36
…id and one tracking code can store, keep an order's id only as a hash, and drop an index nothing reads (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…itor takes back, run queued consent calls first, add no hooks for a visitor it doesn't follow, and never stop the site's own push or route change (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… journey's reads compare keys instead of working each address out again (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…se that ran 00286 before them, and keep a handle such as /@acme in a path: only an email goes (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…'s part fails, and test the push wrapper with a send that fails (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Thanks, all three follow-ups are in (feeda07).

  1. deny() deletes the stored id and drops what waited, but keeps the id in memory. A grant later on the same page keeps the AI landing and sends what waited after the refusal. This covers a CMP that pushes "denied" until the visitor chooses, and a visitor who changes their mind. Test: on the AI landing, a denying update, then a conversion, then a granting update → the id is kept and the conversion is sent.
  2. The queue runs consent calls first, then the rest, so a queued "denied" comes before anything is sent, whatever the order. Test: a conversion queued before a denied, with an id kept earlier → nothing sent, id deleted.
  3. A visitor it doesn't follow gets no hooks. With no id once the queue is drained, it returns before anything else: window.ansvisor stays a no-op, there's no window.dataLayer made, push and pushState aren't wrapped, and there's no submit or popstate listener. Our part of the push and pushState wrappers is in try/catch, so the site's own push and route change always go through. Tests for both.

Also: the popstate listener's part is in try/catch too. The "never stops the site's push" test now uses a grant that sends what waited while the beacon fails, so the wrapper's try/catch really runs.

The comment block is updated too. Server: npm test 2,788 passed (1 skipped). Also merged #1527's fixes in.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

What I checked:

  • Earlier items.
    1. Done (66d1a87).
      • deny() now deletes the stored id and drops what waited, but keeps v in memory (server/src/lib/tracking-script.js:143).
      • A grant later on the same page keeps the AI landing and sends what waited. Nothing is kept or sent before that grant.
      • Tested: on the AI landing, a denying update, then a conversion, then a granting update.
    2. Done.
      • Queued consent calls run before the rest (:169-170), and in their own order among themselves.
      • Tested: a conversion queued before a denied, with an id kept earlier. Nothing is sent and the id is deleted.
    3. Done.
      • With no id once the queue has run, the snippet returns (:171). window.ansvisor stays a no-op, no window.dataLayer is made, push and pushState aren't wrapped, and there's no submit or popstate listener.
      • The id is only ever set at load, so such a visitor can't need the hooks later.
      • Our part of the push, pushState and popstate handlers is in try/catch (:177-178, :196-197; popstate in feeda07), so the site's own push and route change go through even when a send throws.
      • The push test now makes the send throw inside the wrapper, so the try/catch is actually tested.
  • No regressions.
    • Nothing before if(!cv)return changed, so without data-c="1" the snippet works as before.
    • A denying update at load still wins over an id kept earlier and over always (:148).
    • keep() and an earlier visitor's first page view still run after the queued consent calls (:173-174), so a queued denial comes before anything is kept or sent.
    • The comment block matches the code.
  • Merges. 655c35b, 6c90ccf and a7ba6b5 only merge their parents (re-merged with merge-tree: the trees match). They bring in #1527's fixes up to fb41530. The PR merges on main with a conflict only in schema.sql, which comes from #1527's migration. CI passes at feeda07.
  • Deploy. Same as before: t.js comes from the API, so none of this runs until the API is deployed.

@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Merged the PR below it forward (fb97135), which brings in main (00285 and the brief and editor work). No conflicts; schema.sql matches build-schema.sh.

Checks after the merge:

@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Merged #1527's holdsEmail fix forward (f15fe50). No change of its own.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

What I checked:

  • Since round 31 (feeda07), three merges bring in #1527's merge of main and its holdsEmail fix: fb97135, cf3d50c and f15fe50.
    • Re-merging each one's parents with merge-tree gives the commit's tree, with no conflict.
    • The PR's own change on top of #1527 is the same as at round 31, line for line.
  • Merge state.
    • schema.sql at f15fe50 is what build-schema.sh builds.
    • The PR merges cleanly on 4efa150. On today's main (with 00289), only schema.sql conflicts, because of #1527's migration.
    • CI passes.
  • Deploy. As before: t.js comes from the API, so none of this runs until the API is deployed.

@gkhngyk
gkhngyk merged commit 1ed971d into ansvisor:main Oct 26, 2026
4 of 5 checks passed
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants