2012-06-14 365 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 } 
}