Skip to content

Commit 07e08c6

Browse files
author
Paulo Junior
committed
fix: gtm datalayer props v5 - find nearest link to use in event
1 parent 2c7bb69 commit 07e08c6

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/utils/addCustomEventListener.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@ export const addCustomClickEventListener = (eventName: string) => {
88
// [span, div, div, section, body]
99
const path = event.composedPath() as (HTMLElement & HTMLAnchorElement & HTMLFormElement)[]
1010

11-
// Fetch reference to the element that was actually clicked
12-
const targetElement = path[0]
13-
1411
// Check if the element is WITHIN the shadow DOM (ignoring the root)
15-
const shadowFound = path.length
16-
? path.filter(function (i) {
17-
return !targetElement.shadowRoot && !!i.shadowRoot
18-
}).length > 0
19-
: false
12+
const shadowFound = path.length ? path.filter((i) => !!i.shadowRoot).length > 0 : false
2013

2114
// If only shadow DOM events should be tracked and the element is not within one, return
2215
if (trackOnlyShadowDom && !shadowFound) return // Push to dataLayer
2316

17+
// Try to get the first element with href or action, falls back to the first layer of the clicked element
18+
const targetElement = path.find((el) => !!el.href || !!el.action) || path[0]
19+
2420
if (!window.dataLayer) {
2521
window.dataLayer = []
2622
}

0 commit comments

Comments
 (0)