dimanche 1 mars 2020

Skip URL Fragments when using Analytics Mania's Pass UTM custom script

Hi I am using Analytics Mania's script for passing UTMs to links on the site. But I wish for this to skip hashed linked/fragments as it is breaking the functionality of the fragments on the site.

Original: <a href="#fragment"> </a>

Changes this to: <a href="https://appstore.com/?utm_source=google&utm_medium=cpc#fragment"> </a>

What can I change in the script below so it will skip all fragmented link tags?

Link of the script: https://www.analyticsmania.com/post/transfer-utm-parameters-google-tag-manager/

See script below

  <script type="text/javascript">
 (function() {
 var utmInheritingDomain = "appstore.com", // REPLACE THIS DOMAIN 
 utmRegExp = /(\&|\?)utm_[A-Za-z]+=[A-Za-z0-9]+/gi,
 links = document.getElementsByTagName("a"),
 utms = [
 "utm_medium=", // IN GTM, CREATE A URL VARIABLE utm_medium
 "utm_source=", // IN GTM, CREATE A URL VARIABLE utm_source
 "utm_campaign=" // IN GTM, CREATE A URL VARIABLE utm_campaign
 ];

 for (var index = 0; index < links.length; index += 1) {
 var tempLink = links[index].href,
 tempParts;

 if (tempLink.indexOf(utmInheritingDomain) > 0) { // The script is looking for all links with the utmInheritingDomain
 tempLink = tempLink.replace(utmRegExp, "");

 tempParts = tempLink.split("#");

 if (tempParts[0].indexOf("?") < 0 ) {
 tempParts[0] += "?" + utms.join("&"); // The script adds UTM parameters to all links with the domain you've defined
 } else {
 tempParts[0] += "&" + utms.join("&");
 }

 tempLink = tempParts.join("#");
 }

 links[index].href = tempLink;
 }
 }());
</script>

Thank you so much for your help!

Aucun commentaire:

Enregistrer un commentaire