Search code examples
javascriptjqueryhtmlgoogle-tag-manager

Google tag manager script in header won't allow to make changes in href


As the title says I have code which changes the part of href if I don't have google tag manager, but if I enter it the code won't change the href

 $("a").attr("href", function(_, href) { return href.replace("tel", "skype"); });

Solution

  • Are you asking how to make changes to hrefs on the page via Google Tag Manager? If so, you can use the below code to trigger at the gtm.dom custom event. You will want to use the gtm.dom event to make sure the links have been created on the page before your tag in GTM is executed.

    var docLinks = window.document.links;
    for (var i = 0; i < docLinks.length; i++) {
      docLinks[i].href = docLinks[i].href.replace('tel', 'skype');
    }