Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions src/auto-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
49 changes: 39 additions & 10 deletions test/unit/auto-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("auto-events", function () {
const listeners = [];
const { hostname, pathname } = new URL(href);
const link = {
tagName: "A",
href,
hostname,
pathname,
Expand All @@ -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);
},
Expand All @@ -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 };
}

Expand Down Expand Up @@ -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);
Expand All @@ -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 },
});
Expand All @@ -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());

Expand All @@ -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 = "<span>Example</span>";
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();
});
});
Loading