Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

javascript - 覆盖UTM参数(Overwrite UTM Parameter)

We do have some Campaigns (Google, facebook,...) When the user arrives the landingpage (abo.mysite.com) he does have the utm parameter utm_source=theCampaignSource .(我们确实有一些Campaigns(Google,facebook,...)。当用户到达登录页面(abo.mysite.com)时,他确实具有utm参数utm_source=theCampaignSource 。)

When the user clicks an CTA the CTA gives an new UTM utm_source=abo and he goes to shop.mysite.com.(当用户单击CTA时,CTA会提供一个新的UTM utm_source=abo abo,然后转到shop.mysite.com。) We are not able to remove the UTM from abo.mysite.com.(我们无法从abo.mysite.com删除UTM。) Is there a way to check if a user have already an UTM, and when he does have one to kepp them until shop.mysite.com?(有没有一种方法可以检查用户是否已经拥有UTM,以及何时有用户可以将其保留直到shop.mysite.com?) So we know that the user is comming from Google (...)?(因此,我们知道用户来自Google(...)吗?) We know that how this Thing is set up is a very bad practice, and we are working on it.(我们知道如何设置此事物是一个非常不好的做法,我们正在努力。) Ive found a code snippet which is manipulating the links on a site:(我已经找到了一个操纵网站链接的代码片段:) links.forEach(function(link){ link.setAttribute("href","abo.mysite.com") }) but i couldn get it work - cause i do have a lack of experience.(但我无法使它正常工作-因为我确实缺乏经验。) Update(更新资料) To my specific needs a made it that way:(对于我的特定需求,可以这样制作:) 1) Remove existing UTM from Links on the Site(1)从网站上的链接中删除现有的UTM) <script> var link = document.getElementsByTagName("a"); for (var i = 0; i < link.length; i++) { link[i].href = link[i].href.replace(/(?)utm[^&]*(?:&utm[^&]*)*&(?=(?!utm[^s&=]*=)[^s&=]+=)|?utm[^&]*(?:&utm[^&]*)*$|&utm[^&]*/gi, '$1'); } </script> 2) Hash the UTM in the URL(2)将UTM哈希到URL中) <script> if(!window.jQuery) { document.write('<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">x3C/script>'); } </script> <script type="text/javascript"> $(document).ready(function() { function getUrlVars() { var vars = [], hash; var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); for (var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; } return vars; } var parameters = getUrlVars(); var utm_source = decodeURIComponent(parameters['utm_source']); var utm_campaign = decodeURIComponent(parameters['utm_campaign']); var utm_medium = decodeURIComponent(parameters['utm_medium']); </script> 3)rewrite every URL on the Site with the hashed UTMs(3)用散列的UTM重写网站上的每个URL) <script> $('a').each(function(){ $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign' + utm_campaign + '&utm_medium' + utm_medium); }); }); Edit Thanks to Michele Pisani(编辑感谢Michele Pisani) this works well - BUT, if a user does not have an UTM, and he clicks the button, the UTM will be set to undefined(这很好用-但是,如果用户没有UTM,并且他单击该按钮,则UTM将设置为undefined) Is there a way to set the UTM Parameter from the URL when the User already has one, or to use the existing UTM (which are hardcoded in the button) when he does not have an UTM in the URL.(当用户已经有一个UTM参数时,是否可以通过URL设置它;如果URL中没有UTM,则可以使用现有的UTM(在按钮中进行硬编码)。) Edit 2 & update Finally - with the help of you guys - i found a solution:(编辑2并最终更新 -在你们的帮助下-我找到了解决方案:) <script> var link = document.querySelectorAll('a:not([href*="#"])'); for (var i = 0; i < link.length; i++) { //link[i].href = link[i].href.replace(/(?)utm[^&]*(?:&utm[^&]*)*&(?=(?!utm[^s&=]*=)[^s&=]+=)|?utm[^&]*(?:&utm[^&]*)*$|&utm[^&]*/gi, '$1'); } </script> <script type="text/javascript"> $(document).ready(function() { function getUrlVars() { var vars = [], hash; var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); for (var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; } return vars; } //var parameters = getUrlVars(); //var utm_source = decodeURIComponent(parameters['utm_source']); //var utm_campaign = decodeURIComponent(parameters['utm_campaign']); //var utm_medium = decodeURIComponent(parameters['utm_medium']); var url_string = window.location.href; //window.location.href var url = new URL(url_string); //var c = url.searchParams.get("c"); var utm_source = url.searchParams.get("utm_source"); var utm_campaign = url.searchParams.get("utm_campaign"); var utm_medium = url.searchParams.get("utm_medium"); $('a:not([href^="#"])').each(function() { if(utm_source != "" && utm_source != null){ var href = $(this).attr("href"); href = href.replace(/(?)utm[^&]*(?:&utm[^&]*)*&(?=(?!utm[^s&=]*=)[^s&=]+=)|?utm[^&]*(?:&utm[^&]*)*$|&utm[^&]*/gi, '$1'); $(this).attr("href",href); $(this).attr('href', $(this).attr('href') + '?utm_source=' + utm_source + '&utm_campaign=' + utm_campaign + '&utm_medium=' + utm_medium); } }); }); </script>   ask by Tom tom translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With JavaScript, to remove UTM parameters from links in page you can try this function with regex:(使用JavaScript,要从页面链接中删除UTM参数,您可以使用regex尝试以下功能:)

var link = document.getElementsByTagName("a"); for (var i = 0; i < link.length; i++) { link[i].href = link[i].href.replace(/(?)utm[^&]*(?:&utm[^&]*)*&(?=(?!utm[^s&=]*=)[^s&=]+=)|?utm[^&]*(?:&utm[^&]*)*$|&utm[^&]*/gi, '$1'); } If you are using Google Tag Manager you can add it in a custom HTML tag and fires it on DOM Ready.(如果您使用的是Google跟踪代码管理器,则可以将其添加到自定义HTML标记中,并在DOM Ready上触发它。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...