You can use CSS selectors in JavaScript, such as: // Returns an Array populated with a HTML Elements with respective matched href attributes.
// *= is contains
var elems = document.querySelectorAll('a[href*="some/url/here"]');
// OR explicitly
var elems = document.querySelectorAll('a[href="some/url/here"]');
// Other attribute examples:
// Where onclick, data-attribute and href are HTML attributes of the elements you are wanting to add a Listener to.
var elems = document.querySelectorAll('a[onclick*="someFunction()"]');
var elems = document.querySelectorAll('a[data-attribute="some-data"]');
var elems = document.querySelectorAll('a[href="some/url/here"]');
// A complex use-case example, the use of "not" in a CSS selector
// Select all "a" Elements that:
// contain "/some/url" within the HREF attribute
// but do not have the "do-not-track" attribute set to "true"
var elems = document.querySelectorAll('a[href*="/some/url/"]:not([do-not-track="true"])');
// I hope this helps :-) // Then loop through the returned elements and append your event listener. You could also add data to the data layer by taking values from the attributes of the element in focus
... View more