跟踪Google Analytics中的所有出站链接

Wal*_*rée 17 javascript jquery google-analytics outbound

我已经使用脚本来跟踪几个月的出站链接了.该脚本为WORKS,但在Google Analytics生成的报告中,许多网址末尾都有一个尾随":80"(默认端口号).请阅读以获得更多详情.

可能很重要的是,跟踪这些出站链接的网站有大量的出站流量(将您的幻想乘以∞).

脚本的目的

它会跟踪所有出站链接,并将其标记为Google Analytics中的"出站链接".

该脚本有很多注释,并有一些console.log()实例来帮助调试(这些实例被注释掉).

GA上的"出站链接"显示在下面:

内容>活动>热门活动>"出站链接"[点击它]> [报告显示所有点击的网址]

问题

在"出站链接"报告中,我获得了所有点击的链接,在报告的所有链接中至少有2/3结束时得到":80"(可能更多).GA将http://example.comhttp://example.com:80视为不同的链接,在报告中将它们分开.这当然不是所希望的.

值得一提:

以":80"结尾的链接总是比不具有":80"的等价物具有更多的点击量,任何点击量都会增加40%到60%.

想要的解决方案

  • 将以":80"结尾的链接与没有它的链接合并,或者
  • 如果可能,请避免将":80"附加到链接.
  • 奖励:了解为什么我们得到以":80"结尾的链接.

剧本

// Outbound Link Tracking with Google Analytics
// Requires jQuery 1.7 or higher (use .live if using a lower version)
$(function() {
    $("a").on('click',function(e){
        var url = $(this).attr("href");
        // Console logs shows the domain name of the link being clicked and the current window
        // console.log('e.currentTarget.host: ' + e.currentTarget.host);
        // console.log('window.location.host: ' + window.location.host);
        // If the domains names are different, it assumes it is an external link
        // Be careful with this if you use subdomains
        if (e.currentTarget.host != window.location.host) {
            // console.log('external link click');
            // Outbound link! Fires the Google tracker code.
            _gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host, url, 0);
            // Checks to see if the ctrl or command key is held down
            // which could indicate the link is being opened in a new tab
            if (e.metaKey || e.ctrlKey) {
                // console.log('ctrl or meta key pressed');
                var newtab = true;
            }
            // If it is not a new tab, we need to delay the loading
            // of the new link for a just a second in order to give the
            // Google track event time to fully fire
            if (!newtab) {
                // console.log('default prevented');
                e.preventDefault();
                // console.log('loading link after brief timeout');
                setTimeout('document.location = "' + url + '"', 100);
            }
        }
        /*
        else {
            console.log('internal link click');
        }
        */
    });
});
Run Code Online (Sandbox Code Playgroud)

Wal*_*rée 12

Fresheyeball答案是正确的,但由于很多人一直在寻求一个完整的答案,我将用Fresheyeball的贡献发布最终剧本.

简短的版本

// Outbound Link Tracking with Google Analytics
// Wallace Sidhrée - http://dreamyguy.com/
// Requires jQuery 1.7 or higher (use .live if using a lower version)
$(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);
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

完整版(评论和'可调试')

// Outbound Link Tracking with Google Analytics
// Wallace Sidhrée - http://dreamyguy.com/
// Requires jQuery 1.7 or higher (use .live if using a lower version)
$(function() {
    $("a").on('click',function(e){
        var url = $(this).attr("href");
        // Console logs shows the domain name of the link being clicked and the current window
        // console.log('e.currentTarget.host: ' + e.currentTarget.host);
        // console.log('window.location.host: ' + window.location.host);
        // If the domains names are different, it assumes it is an external link
        // Be careful with this if you use subdomains
        if (e.currentTarget.host != window.location.host) {
            // console.log('external link click');
            // Outbound link! Fires the Google tracker code.
            _gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host.replace(':80',''), url, 0);
            // Checks to see if the ctrl or command key is held down
            // which could indicate the link is being opened in a new tab
            // Also checks if target="_blank" on the link tag which indicates it should always be opened in a new tab
            if (e.metaKey || e.ctrlKey || this.target == "_blank")) {
                // console.log('ctrl or meta key pressed');
                var newtab = true;
            }
            // If it is not a new tab, we need to delay the loading
            // of the new link for a just a second in order to give the
            // Google track event time to fully fire
            if (!newtab) {
                // console.log('default prevented');
                e.preventDefault();
                // console.log('loading link after brief timeout');
                setTimeout('document.location = "' + url + '"', 100);
            }
        }
        /*
        else {
            console.log('internal link click');
        }
        */
    });
});
Run Code Online (Sandbox Code Playgroud)

  • Ctrl键检测是否还会检测链接上是否单击鼠标中键? (2认同)

Fre*_*all 5

:80你输出的原因是因为e.currentTarget.host

http://www.w3schools.com/jsref/prop_area_host.asp

我不确定你为什么要追踪你已经有功能的url变量,但你可以随时确保:80没有简单的字符串替换

_gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host.replace(':80',''), url, 0);