From 67e490bb58f6f4fee28c0edf787c0c33a5d01131 Mon Sep 17 00:00:00 2001 From: Conductor Adriaan Date: Sun, 8 Nov 2026 07:51:55 +0000 Subject: [PATCH] Collect automated events for links added after the page loaded auto-events.js bound a listener to each link once the page was complete, so links rendered later by single-page apps or lazy content were never collected. One capturing click listener on the document now finds the clicked link and decides at click time, with the same event names and metadata. Fixes #154 Co-Authored-By: Claude Opus 5.5 --- src/auto-events.js | 45 ++++++++++++-------------------- test/unit/auto-events.test.js | 49 ++++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 38 deletions(-) diff --git a/src/auto-events.js b/src/auto-events.js index e6b4503..7f0a25b 100644 --- a/src/auto-events.js +++ b/src/auto-events.js @@ -174,37 +174,26 @@ collect = "phone"; } - if (!collect) return; - - link.addEventListener("click", function () { - saAutomatedLink(this, collect); - }); + return collect; } - function onDOMContentLoaded() { - try { - var a = document.getElementsByTagName("a"); - - // Loop over all links on the page - for (var i = 0; i < a.length; i++) { - var link = a[i]; - var href = link.getAttribute("href"); + // One listener on the document also sees links added after the page loaded. + // Capture runs before the website's own handlers, so a handler that stops + // the click doesn't stop collecting it. + doc.addEventListener( + "click", + function (event) { + try { + var link = event.target; + while (link && link.tagName !== "A") link = link.parentNode; // Skip links that don't have an href - if (!href) continue; - - collectLink(link); + var collect = link && link.getAttribute("href") && collectLink(link); + if (collect) saAutomatedLink(link, collect); + } catch (error) { + log(error.message, "warn"); } - } catch (error) { - log(error.message, "warn"); - } - } - - if (doc.readyState === "ready" || doc.readyState === "complete") { - onDOMContentLoaded(); - } else { - document.addEventListener("readystatechange", function (event) { - if (event.target.readyState === "complete") onDOMContentLoaded(); - }); - } + }, + true + ); })(typeof window < "u" && window); diff --git a/test/unit/auto-events.test.js b/test/unit/auto-events.test.js index 3b3f782..9e7d58c 100644 --- a/test/unit/auto-events.test.js +++ b/test/unit/auto-events.test.js @@ -12,6 +12,7 @@ describe("auto-events", function () { const listeners = []; const { hostname, pathname } = new URL(href); const link = { + tagName: "A", href, hostname, pathname, @@ -24,14 +25,12 @@ describe("auto-events", function () { setAttribute(name) { attributes.push(name); }, - addEventListener(type, listener) { - listeners.push({ type, listener }); - }, }; const document = { - readyState: "complete", currentScript: { dataset: {} }, - getElementsByTagName: () => [link], + addEventListener(type, listener, capture) { + listeners.push({ type, listener, capture }); + }, set location(value) { navigations.push(value); }, @@ -49,7 +48,8 @@ describe("auto-events", function () { document, console, }); - for (const { listener } of listeners) listener.call(link, event); + for (const { listener } of listeners) + listener.call(document, { target: link, ...event }); return { calls, navigations, attributes, listeners }; } @@ -119,7 +119,9 @@ describe("auto-events", function () { } ); expect(attributes).to.deep.equal([]); - expect(listeners.map(({ type }) => type)).to.deep.equal(["click"]); + expect(listeners.map(({ type, capture }) => [type, capture])).to.deep.equal( + [["click", true]] + ); expect(calls).to.have.length(1); expect(calls[0][0]).to.equal("outbound_example_org"); expect(calls[0][3]).to.equal(true); @@ -131,7 +133,7 @@ describe("auto-events", function () { [{ namespace: "ba" }, "ba_event"], [{ saGlobal: "my_event" }, "my_event"], ]) { - it(`sends events through the ${saGlobal} tracker function`, async function () { + it(`sends events through the ${saGlobal} tracker function`, function () { const dom = createTrackerDOM({ settings: { autoCollect: false, ...settings }, }); @@ -147,8 +149,6 @@ describe("auto-events", function () { readFileSync("dist/latest/auto-events.js", "utf8"), dom.getInternalVMContext() ); - // auto-events.js binds links once the document is complete - await new Promise((resolve) => setTimeout(resolve)); // jsdom doesn't implement navigation document.addEventListener("click", (event) => event.preventDefault()); @@ -164,4 +164,33 @@ describe("auto-events", function () { dom.window.close(); }); } + + it("collects links added after it loaded, also when a child is clicked", function () { + const dom = createTrackerDOM({ settings: { autoCollect: false } }); + const { document } = dom.window; + runInContext( + readFileSync("dist/latest/auto-events.js", "utf8"), + dom.getInternalVMContext() + ); + // jsdom doesn't implement navigation + document.addEventListener("click", (event) => event.preventDefault()); + + const link = document.createElement("a"); + link.href = "https://example.org/path"; + link.innerHTML = "Example"; + document.body.appendChild(link); + link.firstChild.click(); + + // A link with an inline event only sends that event + const inline = document.createElement("a"); + inline.href = "https://example.org/other"; + inline.setAttribute("data-simple-event", "partner_click"); + document.body.appendChild(inline); + inline.click(); + + expect( + dom.sent.map((request) => new URL(request.url).searchParams.get("event")) + ).to.deep.equal(["outbound_example_org", "partner_click"]); + dom.window.close(); + }); });