Repository navigation
feat(traffic): follow an AI visitor to conversions in the tracking snippet, consent first (CE-017, part 2) - #1528
Conversation
… 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
left a comment
There was a problem hiding this comment.
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
nfcheck as before. - Nothing is read from or written to storage, and
window.ansvisorstays 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 whenokis already true: a Consent Mode update that granted, a default beforet.jsthat names no region, an id kept earlier, oralways. - A denied update, or a denied default with no update after it, wins over an id kept earlier and over
always. A default pushed aftert.jsonly 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.
- In
- 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.
- The snippet's limits sit inside
- Size and safety.
- About 3.4 KB more before gzip, plain ES5, all inside the IIFE.
cmdand the handlers declare their ownx,nandit, so the endpoint and the navigation entry aren't overwritten.document.currentScriptis checked beforedata-cis read.
- Deploy.
t.jscomes 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:
-
server/src/lib/tracking-script.js:136(reached from:141and: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
updateon 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 keepvin 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.
- Some CMPs push the visitor's current state as an
-
tracking-script.js:160-162: the queue runs in order, so a conversion queued before adeniedgoes out whenokis already true (an id kept earlier, oralways). Run the queuedconsentcalls 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. -
tracking-script.js:139-187: on a site withdata-c="1", every visitor gets the hooks:dataLayer.pushandhistory.pushStatewrapped;- a submit listener;
- a
window.dataLayerwhere 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-opwindow.ansvisor. Most visitors' pages are then left as they were. In the two wrappers, wrap our part intry/catch, so the site's own push and route change always go through.
…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]>
|
Thanks, all three follow-ups are in (feeda07).
Also: the popstate listener's part is in The comment block is updated too. Server: |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good.
What I checked:
- Earlier items.
- Done (66d1a87).
deny()now deletes the stored id and drops what waited, but keepsvin 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.
- Done.
- Queued
consentcalls 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.
- Queued
- Done.
- With no id once the queue has run, the snippet returns (
:171).window.ansvisorstays a no-op, nowindow.dataLayeris made,pushandpushStatearen'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,pushStateand popstate handlers is intry/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/catchis actually tested.
- With no id once the queue has run, the snippet returns (
- Done (66d1a87).
- No regressions.
- Nothing before
if(!cv)returnchanged, so withoutdata-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.
- Nothing before
- 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 inschema.sql, which comes from #1527's migration. CI passes at feeda07. - Deploy. Same as before:
t.jscomes from the API, so none of this runs until the API is deployed.
…ersion-events # Conflicts: # supabase/schema.sql
|
Merged the PR below it forward (fb97135), which brings in Checks after the merge:
|
…irst, so a long address reads fast and an encoded dot is caught (review) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ush review) Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good.
What I checked:
- Since round 31 (feeda07), three merges bring in #1527's merge of main and its
holdsEmailfix: fb97135, cf3d50c and f15fe50.- Re-merging each one's parents with
merge-treegives 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.
- Re-merging each one's parents with
- Merge state.
- Deploy. As before:
t.jscomes from the API, so none of this runs until the API is deployed.
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 carriesdata-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":localStorage(ansvisor_v), with the first AI visit: its source, page and time.data-consent="wait", the id is kept only once the site says the visitor agreed:ansvisor('consent', 'granted'), or Google Consent Mode grantinganalytics_storage.update, or through adefaulton the page beforet.jsthat names no region.t.js(a GTM Consent Initialization tag, an async CMP) only ever pauses.ansvisor('consent', 'denied'), or a Consent Mode update takinganalytics_storageback, deletes the id. Adefaultthat's denied only pauses, even beside regional defaults.deniedthe site queued beforet.jsloaded is taken before anything is kept or sent.dataLayeryet, 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.k: "pv"), including single-page-app routes (pushState,popstate), never the same page twice in a row.k: "cv", originform):data-ansvisor-goal="demo"(or another goal) says otherwise;data-ansvisor="off"opts the form out;autocomplete="new-password". A login doesn't count, unless the form says its goal.data-ansvisor-label, id or name is its label.ansvisor('conversion', {type, value, currency, units, order, label}).t.jsloads (from GTM, say) go through the queue stub,window.ansvisor.q."49.90"is read as a number);ansvisor('conversion', 'demo')works as a short form.?email=…never leaves the browser.Limits (written in the snippet's doc block; the docs come in part 6). Not seen:
form.submit()from script, and posts without a form;A CMP template inside GTM that sets consent only through GTM's own consent API, not the
dataLayer, needs a GTM tag that callsansvisor('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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
00286(feat(traffic): take a journey's pageviews and conversions from the AI Traffic snippet (CE-017, part 1: server) #1527) and setbrands.conversions_enabled = trueon a test brand.<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.?utm_source=chatgpt.com.localStorage.ansvisor_vholds the first visit.ai_traffic_eventsgets apagerow and aconversionrow (signup,form, label = the form's id), both credited tochatgpt.com. The login form adds nothing.ansvisor('conversion', {type: 'demo'}). That adds ademoconversion with origincall.data-consent="wait"(or nodata-consent), nothing is kept or sent untilansvisor('consent', 'granted').I ran steps 1–5 in a real browser on a local test page against the dev database. The
page,signup(form) anddemo(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
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdnpm run lintpasses (run fromserver/)npm testpasses (run fromserver/)The snippet's tests run it in a stand-in browser:
dataLayerthat isn't there yet;cryptoid path, and storage that throws;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:
deniedcomes first.🤖 Generated with Claude Code