javascript - 出站链接跟踪和 'undefined' 链接

标签 javascript jquery google-analytics flexslider

我已在网站上安装了以下出站链接跟踪代码,该代码运行良好。

问题是它导致网站上的图像 slider 中的点出现问题(使用 flexslider)。单击这些点时,它们通常会将图像 slider 移动到该幻灯片,但链接跟踪脚本导致页面重新加载并转到“/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 是否未定义,这似乎是 slider 点的情况,如下所示:

$(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);
            }
        }
    });
});

关于javascript - 出站链接跟踪和 'undefined' 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31945802/

相关文章:

google-analytics - Google Analytics - 在同一次推送中发出多个命令,正确的命名空间

javascript - 如何使用 AngularJS 上传 FormData 对象

javascript - 模块版本不匹配

javascript - remove() 不会从同一类的所有父元素中删除元素

google-analytics - Opencart 上 'add to cart' 的 GA 跟踪

php - 来自 GA API 的 GA Google/自然流量

javascript - 为什么此 Javascript 不适用于 Opera 或 Chrome?

javascript - 为什么这两个空数组的填充方式不同?

javascript - 浏览器添加 HTML5 queryselector 不支持

javascript - 搜索并获取备份删除的元素