2012-06-14 257 views
1

我一直在使用这个谷歌分析代码来跟踪mailto链接,它根本不工作。我觉得这是一件简单的事情(一个支架在某处或某处丢失),但我尝试了几件事情,但无法弄清楚。有什么建议么?谷歌分析mailto跟踪

<script type="text/javascript"> 

$(document).ready(function(){ 

    $('a').mouseup(function(){ 
     href = $(this).attr('href'); 
     if (href !== null) {    
      href_lower = href.toLowerCase(); 
      if(href_lower.substr(-3) == "pdf" || href_lower.substr(-3) == "xls" || href_lower.substr(-3) == "doc" || 
       href_lower.substr(-3) == "mp3" || href_lower.substr(-3) == "mp4" || href_lower.substr(-3) == "flv" || 
       href_lower.substr(-3) == "txt" || href_lower.substr(-3) == "csv" || href_lower.substr(-3) == "zip") { 
       _gaq.push(['_trackEvent', 'Downloads', href_lower.substr(-3), href]); 
      } 
     } 
     else if (href_lower.substr(0, 4) == "http") { 
      var domain = document.domain.replace("www.",''); 
       if(href_lower.indexOf(domain) == -1){ 
        href = href.replace("http://",''); 
        href = href.replace("https://",''); 
        _gaq.push(['_trackEvent', 'Outbound Traffic', href]); 
       } else if (href && href.match(/^mailto\:/i)) { 
        jQuery(this).click(function() { 
        var mailLink = href.replace(/^mailto\:/i, ''); 
        _gaq.push(['_trackEvent', 'Email', 'Click', mailLink]); 
        }); 
       } 
      } 
     } 
    ) 
}); 

+0

如果这个问题^ h作为回答,不要忘记标记为这样:) –

回答

2

你要找的 “电子邮件地址:” 内的其他人,如果链接以 “http” 开头。整个if-else-if应该用“mailto:”重写为“第一类”条件,而不是“http:”的子条件。

+0

这是有道理的。事情只是断章取义。感谢您的快速回复。 – jcbfshr

0

的这部分代码只

else if (href_lower.substr(0, 4) == "http") 

时,这是真正的将这个mailto条件甚至有机会运行,但是当这是真的的mailto条件将永远是假的,因为邮寄地址语法不下手“HTTP”

<a href="mailto:[email protected]">Send email</a> 

我想你也许可以这样做

if(href !== null) { // ....} 
else { 
    if(href_lower.indexOf('mailto:') !== -1) { // mailto code } 
    else { other than mailto code } 
}