将 URL 片段排除在自定义脚本检测之外,以使用 URL 参数装饰内部链接

Exclude URL Fragments from being detected by the custom script to decorate internal links with URL parameters

我正在使用 Analytics Mania 的脚本将 UTM 传递给网站上的 links,但我希望它跳过散列 linked/fragments,因为它破坏了网站上片段的功能.

原文:<a href="#fragment"> </a>

将其更改为:<a href="https://appstore.com/?utm_source=google&utm_medium=cpc#fragment"> </a>

我可以在下面的脚本中更改什么,以便跳过所有零散的 link 标签?

脚本的

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

参见下面的脚本:

<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={{URL - utm_medium}}", // IN GTM, CREATE A URL VARIABLE utm_medium
 "utm_source={{URL - utm_source}}", // IN GTM, CREATE A URL VARIABLE utm_source
 "utm_campaign={{URL - 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>

调整代码以忽略片段链接:

for (var index = 0; index < links.length; index += 1) {
 var tempLink = links[index].href,
 if (tempLink.match(/\#/)) continue; // skip decorating if link containg hash sign '#'
 tempParts;

...