我的 Persistent UTM 代码中有什么错误?

huangapple go评论64阅读模式
英文:

What is the bug in my Persistent UTM Code?

问题

我正在使用以下代码来跟踪我的网站上的UTM参数。然而,我注意到即使在URL中没有UTM参数,它也会向链接添加一个问号。有人可以帮助我找出这段代码中需要更改的部分吗?它应该只在URL中存在UTM参数时才尝试添加UTM参数到链接中。

<script>
(function() {
  var domainsToDecorate = [
          'example.com'
      ],
      queryParams = [
          'utm_medium', // 添加或删除要传递的查询参数
          'utm_source',
          'utm_campaign',
          'utm_content',
          'utm_term'
      ]
  // 请勿编辑此行以下的任何内容
  var links = document.querySelectorAll('a'); 

  // 检查链接是否包含在domainsToDecorate数组中的域名,然后进行装饰
  for (var linkIndex = 0; linkIndex < links.length; linkIndex++) {
      for (var domainIndex = 0; domainIndex < domainsToDecorate.length; domainIndex++) { 
          if (links[linkIndex].href.indexOf(domainsToDecorate[domainIndex]) > -1 && links[linkIndex].href.indexOf("#") === -1) {
              links[linkIndex].href = decorateUrl(links[linkIndex].href);
          }
      }
  }

  // 使用查询参数装饰URL
  function decorateUrl(urlToDecorate) {
      urlToDecorate = (urlToDecorate.indexOf('?') === -1) ? urlToDecorate + '?' : urlToDecorate + '&';
      var collectedQueryParams = [];
      for (var queryIndex = 0; queryIndex < queryParams.length; queryIndex++) {
          if (getQueryParam(queryParams[queryIndex])) {
              collectedQueryParams.push(queryParams[queryIndex] + '=' + getQueryParam(queryParams[queryIndex]))
          }
      }
      return urlToDecorate + collectedQueryParams.join('&');
  }

  // 获取查询参数的值的函数
  function getQueryParam(name) {
      if (name = (new RegExp('[?&]' + encodeURIComponent(name) + '=([^&]*)')).exec(window.location.search))
          return decodeURIComponent(name[1]);
  }

})();
</script>

这是您提供的代码的翻译。

英文:

I am using the following code to persist utms across my website. However i notice that its adding a question mark to links even without the UTM parameters. Can someone help me figure out what in this code needs to change. It should only be trying to add UTM parameters to the links if there is one present in the URL.

 &lt;script&gt;
(function() {
var domainsToDecorate = [
&#39;example.com&#39;
],
queryParams = [
&#39;utm_medium&#39;, //add or remove query parameters you want to transfer
&#39;utm_source&#39;,
&#39;utm_campaign&#39;,
&#39;utm_content&#39;,
&#39;utm_term&#39;
]
// do not edit anything below this line
var links = document.querySelectorAll(&#39;a&#39;); 
// check if links contain domain from the domainsToDecorate array and then decorates
for (var linkIndex = 0; linkIndex &lt; links.length; linkIndex++) {
for (var domainIndex = 0; domainIndex &lt; domainsToDecorate.length; domainIndex++) { 
if (links[linkIndex].href.indexOf(domainsToDecorate[domainIndex]) &gt; -1 &amp;&amp; links[linkIndex].href.indexOf(&quot;#&quot;) === -1) {
links[linkIndex].href = decorateUrl(links[linkIndex].href);
}
}
}
// decorates the URL with query params
function decorateUrl(urlToDecorate) {
urlToDecorate = (urlToDecorate.indexOf(&#39;?&#39;) === -1) ? urlToDecorate + &#39;?&#39; : urlToDecorate + &#39;&amp;&#39;;
var collectedQueryParams = [];
for (var queryIndex = 0; queryIndex &lt; queryParams.length; queryIndex++) {
if (getQueryParam(queryParams[queryIndex])) {
collectedQueryParams.push(queryParams[queryIndex] + &#39;=&#39; + getQueryParam(queryParams[queryIndex]))
}
}
return urlToDecorate + collectedQueryParams.join(&#39;&amp;&#39;);
}
// a function that retrieves the value of a query parameter
function getQueryParam(name) {
if (name = (new RegExp(&#39;[?&amp;]&#39; + encodeURIComponent(name) + &#39;=([^&amp;]*)&#39;)).exec(window.location.search))
return decodeURIComponent(name[1]);
}
})();
&lt;/script&gt;

答案1

得分: 1

decorateUrl 函数中,如果不存在 ?,则会添加 ?

urlToDecorate = (urlToDecorate.indexOf('&amp;') === -1) ? urlToDecorate + '&amp;' : urlToDecorate + '&amp;';

我建议您只在 collectedQueryParams 包含任何元素时才执行此操作。

function decorateUrl(urlToDecorate) {
      var collectedQueryParams = [];
      for (var queryIndex = 0; queryIndex < queryParams.length; queryIndex++) {
          if (getQueryParam(queryParams[queryIndex])) {
              collectedQueryParams.push(queryParams[queryIndex] + '=&amp;' + getQueryParam(queryParams[queryIndex]))
          }
      }

      if(collectedQueryParams.length == 0){
        return urlToDecorate;
      }

      //only add the ? if we have params AND if there isn't already one
      urlToDecorate = (urlToDecorate.indexOf('&amp;') === -1) ? urlToDecorate + '&amp;' : urlToDecorate + '&amp;';
      return urlToDecorate + collectedQueryParams.join('&amp;');
  }
英文:

In decorateUrl you are adding the ? if there is not one

urlToDecorate = (urlToDecorate.indexOf(&#39;?&#39;) === -1) ? urlToDecorate + &#39;?&#39; : urlToDecorate + &#39;&amp;&#39;;

I would suggest you may only want to do this if collectedQueryParams contains any elements

 function decorateUrl(urlToDecorate) {
var collectedQueryParams = [];
for (var queryIndex = 0; queryIndex &lt; queryParams.length; queryIndex++) {
if (getQueryParam(queryParams[queryIndex])) {
collectedQueryParams.push(queryParams[queryIndex] + &#39;=&#39; + getQueryParam(queryParams[queryIndex]))
}
}
if(collectedQueryParams.length == 0){
return urlToDecorate;
}
//only add the ? if we have params AND if there isn&#39;t already one
urlToDecorate = (urlToDecorate.indexOf(&#39;?&#39;) === -1) ? urlToDecorate + &#39;?&#39; : urlToDecorate + &#39;&amp;&#39;;
return urlToDecorate + collectedQueryParams.join(&#39;&amp;&#39;);
}

huangapple
  • 本文由 发表于 2023年6月1日 22:57:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定