SOLVED

Capture URL parameters and use on links

Go to solution
Rajesh_Khatri
Level 2

Re: Capture URL parameters and use on links

Thank you very much Sanford. 

This code worked, it removed UTM's from Tel link and applied to rest of HREF links. 

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Capture URL parameters and use on links

Good to hear, may add this code to the Products blog in future.
Rajesh_Khatri
Level 2

Re: Capture URL parameters and use on links

Hi Sanford,

 

Your this script worked very well for us. Thank you for sharing. 
Based on your below script, I made a small tweak to fit this on mailto links, but the problem is, it is not copying all utms but just the first utm and leaving all other utm parameters.

 

The change made by me  in your code is highlighted in bold commented inline. 

 

Can you please help to fix the code so it can carry all utm's to body of mailto: 

<span style="color: #00a4de;"><a href="mailto:${SectionSupportContact2}?subject= Please do not Modify the First line in Email Body&body=" style="font-weight: 600;color:rgb(0,164,222);">${SectionSupportContact2}</a></span>
<!-- Carrying UTM's Code in Mailto links -->
<script>
   (function() {
      const arrayify = getSelection.call.bind([].slice);
      const appendableSearch = document.location.search.substring(1);
      if (appendableSearch) {
         arrayify(document.links)
            .filter(function(link) {
               // changed “https?” to “mailto?”
               return /mailto?/.test(link.protocol) & !/^#/.test(link.getAttribute("href"));
            })
            .forEach(function(httpLink) {
               httpLink.search += (httpLink.search ? "&" : "") + appendableSearch;
            });
      }
   })();
</script>

<!-- End Carrying UTM's Code -->

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Capture URL parameters and use on links

Why would you want to add URL params to your mailto: links? Those have no function except to look weird to the end user.
Rajesh_Khatri
Level 2

Re: Capture URL parameters and use on links

We place email ids on our PPC landing pages. When user clicks on email, he lands up on email client. User posts the email to us, but we do not know from which PPC campaign the request has come. So we want to capture the campaign name from UTM parameters and post in body content.