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

时间:2017-11-22 22:15:26

标签: javascript jquery wordpress google-analytics

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

示例,如果我想从Linkedin

中的新闻文章中获取流量

传入链接:

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

我希望如此,当访问者点击外发链接(或我网站上的任何链接)时,外发链接将附加如下字符串:

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

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

2 个答案:

答案 0 :(得分:1)

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

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

将它放在标题中,它会为您的链接添加参数。请注意,如果链接已经附加了查询字符串,则可能会破坏链接。在这种情况下,此代码应该还有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 :(得分:0)

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

<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标签的情况下检查任何网址。

希望它有所帮助。