javascript – 跟蹤Google Analytics(分析)中的所有出站連結
我一直在使用指令碼跟蹤出站連結幾個月.指令碼WORKS,但在Google Analytics(分析)生成的報告中,許多網址的末尾都有一個尾隨的“:80”(預設埠號).閱讀更多詳情.
可能重要的是,跟蹤這些出站連結的網站有大量的出站流量(將您的幻想乘以∞).
劇本的目的
它會跟蹤所有出站連結,並將其標記為Google Analytics(分析)中的“出站連結”.
指令碼被大量評論,並且有幾個console.log()的例項來幫助除錯(這些被保留註釋掉).
“出站連結”顯示在GA上,下:
內容>活動>熱門活動> “出站連結”[點選]> [顯示所有點選的URL]
問題
在“出站連結”報告中,我得到所有被點選的連結,在所報告的連結的至少2/3(可能更多)中,我得到“:80”. GA將ofollow,noindex" target="_blank">http://example.com 和http://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'); } */ }); });
原因: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);
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/12302300/track-all-outbound-links-in-google-analytics