2014-06-11 96 views
0

我設置了Google Analytics事件來跟蹤出站鏈接。我跟着the tutorial on Google's siteGoogle Analytics出站鏈接跟蹤中沒有「標籤」

我遇到的問題是'url'參數(事件標籤)沒有在Google Analytics中被跟蹤。 'outbound'和'click'都可以在ga()調用中正常工作。如果我在ga()調用之前提醒()線上的'url'變量,它會成功地將它提醒到屏幕。

爲什麼類別(出站)和動作(點擊)工作得很好。但是,標籤(var url)不會顯示在Google Analytics中?

jQuery(document).ready(function() { 
    initExternalLinkLogging(); 
}); 

function initExternalLinkLogging() { 
    // ':external' is from here and works great: http://stackoverflow.com/questions/1227631/using-jquery-to-check-if-a-link-is-internal-or-external 
    externalLinks = jQuery("a:external"); 
    jQuery(externalLinks).click(function() { 
     trackOutboundLink(this); 
    }); 
} 

function trackOutboundLink(url) { 
    // This is the triggered on each outbound link click successfully 
    // 'url' has the correct value, but is not showing up in Google Analytics 
    try { 
     ga('send', 'event', 'outbound', 'click', url, { 
      'hitCallback': function() { 
       document.location = url; 
      } 
     }); 
    } catch (err) { 
     // Do nothing for now 
    } 
} 

回答

0

我解決了這個問題。在這種情況下,'url'變量是一個對象,即使屏幕提示顯示它是URL的一個字符串。只需更改此函數調用即可解決問題。

//This doesn't work 
trackOutboundLink(this); 

//This fixes it. See ".href" 
trackOutboundLink(this.href); 

因此,對於處於工作狀態的參考的完整代碼是:

jQuery(document).ready(function() { 
    initExternalLinkLogging(); 
}); 

function initExternalLinkLogging() { 
    // ':external' is from here and works great: http://stackoverflow.com/questions/1227631/using-jquery-to-check-if-a-link-is-internal-or-external 
    externalLinks = jQuery("a:external"); 
    jQuery(externalLinks).click(function() { 
     trackOutboundLink(this); 
    }); 
} 

function trackOutboundLink(url) { 
    // This is the triggered on each outbound link click successfully 
    // 'url' has the correct value, but is not showing up in Google Analytics 
    try { 
     ga('send', 'event', 'outbound', 'click', url, {'hitCallback': 
       function() { 
        document.location = url; 
       } 
     }); 
    } catch (err) { 
    } 
} 
相關問題