【问题标题】:How to pass query string data to all pages of my website?如何将查询字符串数据传递到我网站的所有页面?
【发布时间】:2020-06-15 05:18:12
【问题描述】:

我正在尝试将我的 facebook 广告中的 UTM 参数传递到我网站上的所有页面。

示例查询字符串:www.mysite.com/?utm__source=facebook&utm_medium=psocial&utm_campaign=summer&utm_content=pool&fbclid=1234Jfshio

通常,utm 参数将存在于用户访问的第一页上,但一旦他们导航到新页面,查询字符串就会丢失。

我希望将查询字符串附加到用户在会话期间导航到的所有页面的 URL 末尾。

我必须这样做,以便当他们打开与我们的 LiveChat 服务的聊天时,我们的服务捕获的起始 URL 包含他们点击的广告的 utm 参数。

这是我当前使用的代码 - 但它没有按预期工作:

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

【问题讨论】:

    标签: javascript google-tag-manager


    【解决方案1】:

    看起来您需要正确检索查询字符串值。我使用this answer 更新您的代码(如下)。注意urlParams.get()的使用:

    <script type="text/javascript">
     (function() {
     const urlParams = new URLSearchParams(window.location.search);
     var utmInheritingDomain = "brilliance.com", // REPLACE THIS DOMAIN
     utmRegExp = /(\&|\?)utm_[A-Za-z]+=[A-Za-z0-9]+/gi,
     links = document.getElementsByTagName("a"),
     utms = [
     "utm_source=" + urlParams.get('utm_source'), // IN GTM, CREATE A URL VARIABLE utm_source
     "utm_medium=" + urlParams.get('utm_medium'), // IN GTM, CREATE A URL VARIABLE utm_medium
     "fbclid=" + urlParams.get( 'fbclid' ) // IN GTM, CREATE A URL VARIABLE fbclid
     ];
    
     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>
    

    【讨论】:

    • 我在 Google 跟踪代码管理器的第 3 行字符 2 上收到错误:const urlParams = new URLSearchParams(window.location.search);显然,“const”短语在 Google 跟踪代码管理器中不起作用。
    • Google 跟踪代码管理器只允许您生成起始代码。一旦你创建了代码,它就会出现在你的网站上。当您将我的代码放在您的网站上时会发生什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 2012-04-14
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多