简体   繁体   English

UTM 参数出现不止一次

[英]UTM Parameters showing up more than once

I have a web page and in it, I've embedded a donation form.我有一个 web 页面,我在其中嵌入了捐赠表格。 When a donor visits, they do so with a link that has a source code at the end.当捐助者访问时,他们会使用末尾带有源代码的链接进行访问。 I've figured out how to add the source code to the embedded URL. Here is my code:我已经弄清楚如何将源代码添加到嵌入式 URL。这是我的代码:

$(document).ready(function(){  
//Step 1: this outputs the window URL source code
var results = null
$.urlParam = function(name){
    results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
};
console.log(decodeURIComponent($.urlParam('source')));  
  
//Step 2: this outputs the full qGiv div and script copied from qGiv
var qGivURL = $("#donation-form").html();
console.log(qGivURL);
  
//Step 3: this replaces the source codes
var twoVar = $(".qgiv-embed-container").attr("data-embed");
var finalURL = twoVar + "?source=" + results;
console.log(finalURL);
});

And here is the embedded code:这是嵌入式代码:

<div id="donation-form">
<div class="qgiv-embed-container" data-qgiv-embed="true" data-embed-id="65951" data-embed="https://secure.qgiv.com/for/sote/embed/65951/" data-width="630"></div>
<script>(function(w, d, s, id){var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return;js = d.createElement(s); js.id = id;js.src = "https://secure.qgiv.com/resources/core/js/embed.js";fjs.parentNode.insertBefore(js, fjs);})(window, document, 'script', 'qgiv-embedjs');</script>
</div>

My problem is event though this line of code works var finalURL = twoVar + "?source=" + results;我的问题是 event 尽管这行代码有效var finalURL = twoVar + "?source=" + results; it adds?source=code twice.它添加了?source=code 两次。

Can someone help me understand why this is happening and how to fix?有人可以帮助我理解为什么会发生这种情况以及如何解决吗?

TIA! TIA!

You have multiple problems in your code.您的代码中有多个问题。

Your urlParam function has errors in it.您的 urlParam function 有错误。 Plus you never actually set results to the returned value of urlParam you are just logging it.另外,您实际上从未将结果设置为 urlParam 的返回值,您只是在记录它。

My solution uses the vanilla js function URLSearchParams我的解决方案使用 vanilla js function URLSearchParams

 $(document).ready(function() { const urlParams = new URLSearchParams(window.location.href); const results = urlParams.get('source') || ""; var twoVar = $(".qgiv-embed-container").attr("data-embed"); var finalURL = twoVar + "?source=" + results; console.log(finalURL); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="donation-form"> <div class="qgiv-embed-container" data-qgiv-embed="true" data-embed-id="65951" data-embed="https://secure.qgiv.com/for/sote/embed/65951/" data-width="630"></div> <script> (function(w, d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "https://secure.qgiv.com/resources/core/js/embed.js"; fjs.parentNode.insertBefore(js, fjs); })(window, document, 'script', 'qgiv-embedjs'); </script> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM