diff --git a/src/detector.impressions.test.ts b/src/detector.impressions.test.ts index df8ec57..8a116b8 100644 --- a/src/detector.impressions.test.ts +++ b/src/detector.impressions.test.ts @@ -34,3 +34,19 @@ test("check impresssions", async () => { }, ]); }); + +test("don't consider empty attributes", async () => { + window.TS = { + token: "token", + }; + const events: any[] = []; + window.addEventListener("topsort", (e) => { + events.push((e as any).detail); + }); + document.body.innerHTML = ` + <div data-ts-resolved-bid=""></div> + `; + await import("./detector"); + + expect(events).toMatchObject([]); +}); diff --git a/src/detector.ts b/src/detector.ts index d525d4b..e6edca4 100644 --- a/src/detector.ts +++ b/src/detector.ts @@ -249,27 +249,27 @@ function interactionHandler(event: Event): void { const intersectionObserver = !!window.IntersectionObserver ? new IntersectionObserver( - (entries) => { - for (const entry of entries) { - if (entry.isIntersecting) { - const node = entry.target; - if (node instanceof HTMLElement) { - logEvent(getEvent("Impression", node), node); - if (intersectionObserver) { - intersectionObserver.unobserve(node); - } + (entries) => { + for (const entry of entries) { + if (entry.isIntersecting) { + const node = entry.target; + if (node instanceof HTMLElement) { + logEvent(getEvent("Impression", node), node); + if (intersectionObserver) { + intersectionObserver.unobserve(node); } } } - }, - { - threshold: INTERSECTION_THRESHOLD, - }, - ) + } + }, + { + threshold: INTERSECTION_THRESHOLD, + }, + ) : undefined; const PRODUCT_SELECTOR = - "[data-ts-product],[data-ts-action],[data-ts-items],[data-ts-resolved-bid]"; + '[data-ts-product]:not([data-ts-product=""]),[data-ts-action]:not([data-ts-action=""]),[data-ts-items]:not([data-ts-items=""]),[data-ts-resolved-bid]:not([data-ts-resolved-bid=""])'; function addClickHandler(node: HTMLElement) { const clickables = node.querySelectorAll("[data-ts-clickable]");