2011-08-02 137 views
3

我試圖使用jQuery查找頁面上的所有鏈接,然後單擊它們時使用Google Analytics(分析)跟蹤事件。jQuery使用Google Analytics(分析)跟蹤事件

我有鏈接部分工作,我只是沒有看到跟蹤工作事件火災(當用httpfox監控時)。

任何人都可以看到我做錯了什麼嗎?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>jQuery and GA</title> 

<!-- This Jquery reference is already on the page --> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 

<!-- This script block should go anywhere on the page, ideally the head, or really ideally in the a master scripts file --> 
<script> 
$(document).ready(function() { 

// Each time a link is clicked, this function gets called 
$('a').click(function(){ 

// Get the url 
var url = this.href;   


// Set the category to External link, unless it matches the portal link/url pattern (containing /community/), 
// in which case category gets set to whatever word follows /community/ in the path. 
var category = "External link"; 
var matches = url.match(/\/community\/([^\/]+)/); 
if (matches) { 
category = matches[1]; 
} 
alert(category); 


// Get the link text 
var linkText = $(this).text().trim(); 
alert(linkText); 


// Alert the url, just for the order below's sake 
alert(url); 


// Track the click 
//$(this).attr("onclick","_gaq.push(['_trackEvent'," + category + ",'click','" + linkText + "','" + url + "'])"); 
_gaq.push(['_trackEvent'," + category + ",'click','" + linkText + "','" + url + "']) 


//return false; 

}); 
}); 
</script> 

</head> 

<body> 

<h1>Simulating links on Site</h1> 

<ul> 
<li><a href="http://inet.asdf.asdf.pvt/portal/server.pt/community/home/_articleviewer?ArticleId=s_011610">Read more</a></li> 

</ul> 

<script type="text/javascript"> 

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'UA-24902347-1']); 
_gaq.push(['_trackPageview']); 

(function() { 
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
})(); 

</body> 
</html> 

回答

5

您將太多參數傳遞給_gaq.push()函數。它只能拿3個字符串參數,但你傳遞4.

_gaq.push(['_trackEvent'," + category + ",'click','" + linkText + "','" + url + "']) 
    //     | category | action|  label  | value | 

結合的論點2是一個,它會工作(或擺脫「點擊」串動作,因爲它是一種浪費)。但是,你也沒有傳遞變量名稱;你只是傳遞等價於變量名稱的字符串。它看起來像你實際上是試圖做這樣的事情:

_gaq.push(['_trackEvent', category, linkText , url]); 
+0

似乎火災的事件。非常感謝! – ben

1

yahelc的回答可以幫助你得到正確的PARAMS到事件跟蹤,但它看起來像你缺少一個右</script></body>之前標記這可能意味着GA尚未運行。

+0

謝謝,我錯過了。 – ben

相關問題