首页 文章

如何将传入的Google Analytics广告系列字符串传递给传出点击

提问于
浏览
0

我想设置一个设置,如果我使用Google Analytics广告系列字符串为我的网站带来流量:utm_campaign和utm_source,这些字符串将被存储,然后附加到我网站上的任何外发链接/点击 .

例如,如果我想从Linkedin中的新闻文章中获取流量

传入链接:

https://mywebsite.com/landing-page/?utm_campaign=news&utm_source=linkedin

我想要它,所以当访问者点击外发链接(或我网站上的任何链接)时,外发链接将附加如下的utm字符串:

https://outgoinglink.com/welcome-aboard/?utm_campaign=news&utm_source=linkedin

任何人都可以帮助如何拥有这样的东西 . 我的网站是在Wordpress中,似乎没有特定的插件 .

2 回答

  • 0

    你可以用Javascript做到这一点,肯定用jQuery会更容易,所以我会给你jQuery解决方案 .

    $(function(){
      var params = window.location.search;
      if (!!params) {
        $('a[href]').each(function(){
          $(this).attr('href', $(this).attr('href') + params);
        });
      }
    });
    

    将它放在 Headers 中,它会为您的链接添加参数 . 请注意,如果链接已经附加了查询字符串,则可能会破坏链接 . 在这种情况下,此代码应该还有1个边缘情况 .

    $(function(){
      var params = window.location.search;
      if (!!params) {
        $('a[href]').each(function(){
          if ($(this).attr('href').indexOf('?') === -1) {
            $(this).attr('href', $(this).attr('href') + params);
          } else {
            $(this).attr('href', $(this).attr('href') + '&' + params.substr(1));
          }
        });
      }
    });
    

    请注意,此代码供您学习,可以针对 生产环境 目的进行优化并使其更加安全 .

    如果您需要在第二页或更高页面上保留参数,则应首先将其添加到localStorage,然后从中读取它并再次附加到每个链接上 .

    希望有所帮助

    编辑:

    你可以查看这支笔,

    https://codepen.io/mnikolaus/pen/dZeVLv

    编辑2:

    jQuery(function(){
      var params = window.location.search;
      if (!!params) {
        jQuery('a[href]').each(function(){
          if (jQuery(this).attr('href').indexOf('?') === -1) {
            jQuery(this).attr('href', jQuery(this).attr('href') + params);
          } else {
            jQuery(this).attr('href', jQuery(this).attr('href') + '&' + params.substr(1));
          }
        });
      }
    });
    
  • 1

    试试这个 . 一个心灵的客户想要在几个月前做同样的事情,我给他这个她,现在它正在工作 .

    <script type="text/javascript">
        (function ($) {
            // Get the utm tag from current url
          var params = window.location.search;
          // if utm available...
          if (!!params) {
            $('a[href]').each(function(){
                // Apply the extracted utms to link
              $(this).attr('href', $(this).attr('href') + params);
            });
          }
        }(jQuery));
    </script>
    

    它基本上检查没有utm标签的任何网址 .

    希望能帮助到你 .

相关问题