2014-06-11 61 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) { 
    } 
} 
相关问题