wordpress 带有重定向的出站链接跟踪

b91juud3  于 2023-02-21  发布在  WordPress
关注(0)|答案(2)|浏览(154)

我已经设置了Google Analytics来跟踪outbound links。但是,我也安装了一个名为Amazon Link Localizer的WordPress插件,它可以修改传出的URL,使其最终形式为prourls.com?url=www.amazon.com而不是www.amazon.com
是否可以修改分析脚本,以便仍然可以跟踪这些出站链接点击?

2izufjch

2izufjch1#

    • 步骤1:将trackOutboundLink脚本添加到您的网站。**

将以下代码添加到页面HTML的<head></head>部分:* (完整脚本位于此页底部,但使用此脚本有助于最小化页面大小)*

<script>
var trackOutboundLink=function(t,e){var n,o="object"==typeof t?t.href:t,a=document.createElement("a");function c(){document.location=o}e=e||o,a.href=e,/\bprourls\.com?$/i.test(a.host)?e=(n=/[\?&]url=([^&#]+)/.test(a.search)&&RegExp.$1||"")?decodeURIComponent(n):e:/\blinksynergy\.com$/i.test(a.host)&&(e=(n=/[\?&]murl=([^&#]+)/.test(a.search)&&RegExp.$1||"")?decodeURIComponent(n):e),"function"==typeof gtag?gtag("event","click",{event_category:"outbound",event_label:e,transport_type:"beacon",event_callback:c}):ga("send","event","outbound","click",e,{transport:"beacon",hitCallback:c})};window.addEventListener("load",function(){var t,e,n=document.getElementsByTagName("a")||[];function o(t){t.preventDefault();var e=this.getAttribute("data-outbound-link");trackOutboundLink(this.href,"1"===e?"":e)}for(t=0;t<n.length;t++)(e=n[t]).href&&!/^#/.test(e.href)&&location.host!==e.host&&""!==e.getAttribute("data-outbound-link")&&e.addEventListener("click",o,!1)},!1);
</script>

请注意,该脚本是Google Analytics提供的脚本的修改/自定义版本:

对于要跟踪的每个出站链接,将以下内容添加到链接的HTML:

data-outbound-link="1"

(And如果您之前在链接的HTML中添加了onclick="trackOutboundLink('http://www.example.com'); return false;",请将其替换为上面的代码。
因此,代替:<a href="http://www.example.com" onclick="trackOutboundLink('http://www.example.com'); return false;">Check out example.com</a>
..使用这个:<a href="http://www.example.com" data-outbound-link="1">Check out example.com</a>

    • 完整脚本**

以下是 * custom * trackOutboundLink脚本的完整、未缩小版本:(仅供参考)

<script>
var trackOutboundLink = function(link, url2track) {
  var url2visit = ('object' === typeof link) ? link.href : link,
    a = document.createElement('a'),
    b;

  url2track = url2track || url2visit;
  a.href = url2track;

  // Dynamic redirect URLs via Prourls.com; this was for Amazon URLs.
  if (/\bprourls\.com?$/i.test(a.host)) {
    b = (/[\?&]url=([^&#]+)/.test(a.search) && RegExp.$1) || '';
    url2track = b ? decodeURIComponent(b) : url2track;
  // Dynamic redirect URLs via Link Synergy; this was for Footlocker URLs.
  } else if (/\blinksynergy\.com$/i.test(a.host)) {
    b = (/[\?&]murl=([^&#]+)/.test(a.search) && RegExp.$1) || '';
    url2track = b ? decodeURIComponent(b) : url2track;
  }
  // All other URLs are not parsed and expected to be valid outbound URLs.

  function onTracked() {
    //alert('Tracked: ' + url2track);
    document.location = url2visit;
  }

  if ('function' === typeof gtag) {
    gtag('event', 'click', {
      'event_category': 'outbound',
      'event_label': url2track,
      'transport_type': 'beacon',
      'event_callback': onTracked
    });
  } else {
    ga('send', 'event', 'outbound', 'click', url2track, {
      'transport': 'beacon',
      'hitCallback': onTracked
    });
  }
};

window.addEventListener('load', function() {
  var links = document.getElementsByTagName('a') || [],
    i, a;

  function _go(e) {
    e.preventDefault();

    // Track a custom URL address, if specified.
    var b = this.getAttribute('data-outbound-link');

    trackOutboundLink(this.href, '1' === b ? '' : b);
  }

  for (i = 0; i < links.length; i++) {
    a = links[i];

    if (!a.href || /^#/.test(a.href) || location.host === a.host) {
      continue;
    }

    if ('' !== a.getAttribute('data-outbound-link')) {
      a.addEventListener('click', _go, false);
    }
  }
}, false);
</script>
f1tvaqid

f1tvaqid2#

试试这个:

    • 首先**,删除所有链接上的onclick处理程序。
    • 第二个**在<head>块中包含以下代码
var trackOutboundLink = function(url) {
    var newUrl = url;
    var re = /prourls\.co.*\?.*url=(.*)/
    var matches = url.match(re);
    if (matches && matches.length > 0) {
        newUrl = matches[1]
    }

    // console.log(newUrl);

    ga('send', 'event', 'outbound', 'click', newUrl, {
      'transport': 'beacon',
      'hitCallback': function(){document.location = url;}
    });
};

var btns = document.querySelectorAll('a.btn');
for (var i = 0, l = btns.length; i < l; i++) {
    btns[i].addEventListener('click', function(e) {
        // console.log(e.target.href);
        trackOutboundLink(e.target.href);
    });
}

现在修改的trackOutboundLink()

  • 检查其格式是否为prourls.com?url=XXXX
  • 提取url参数的值
  • 将该值传递给GA事件跟踪调用

相关问题