出站链接跟踪'undefined'链接

Outbound link tracking and 'undefined' links

本文关键字:链接 undefined 跟踪      更新时间:2023-09-26

我在一个运行良好的网站上安装了以下出站链接跟踪代码。

问题是它会导致网站上的图像滑块中的点出现问题(使用flexslider)。当这些点被点击时,它们通常会将图像滑动条移动到该幻灯片上,但是链接跟踪脚本会导致页面重新加载并转到'/undefined'(即www.domain.com/undefined)。

$(function() {
    $("a").on('click',function(e) {
        var url = $(this).attr("href");
        if (e.currentTarget.host != window.location.host) {
            _gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host.replace(':80',''), url, 0);
            if (e.metaKey || e.ctrlKey || this.target == "_blank") {
                var newtab = true;
            }
            if (!newtab) {
                e.preventDefault();
                setTimeout('document.location = "' + url + '"', 100);
            }
        }
    });
});

任何关于如何解决这个问题的建议都将是非常感谢的。

提前感谢,

汤姆

您可以过滤您的选择器结果或检查何时href未定义,这似乎是滑动条的点的情况,如下所示:

$(function() {
    $("a").on('click', function(e) {
        var url = $(this).attr("href");
        if (url && e.currentTarget.host != window.location.host) {
            _gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host.replace(':80', ''), url, 0);
            if (e.metaKey || e.ctrlKey || this.target == "_blank") {
                var newtab = true;
            }
            if (!newtab) {
                e.preventDefault();
                setTimeout('document.location = "' + url + '"', 100);
            }
        }
    });
});