2011-08-02 18 views
3

jQueryを使用してページ上のすべてのリンクを見つけようとしていますが、クリックしてGoogleアナリティクスでイベントをクリックすると、Google Analyticsでイベントを追跡するためのjQuery

私はリンク部分が働いています、私は追跡作業イベントfire(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つの文字列引数を取ることができますが、一つであることが、引数の2を組み合わせる4.

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

を渡している、それがうまくいく(または以来、行動のための「クリック」の文字列を取り除きますその廃棄物)。しかし、変数名も渡していません。変数名に相当する文字列を渡すだけです。あなたが実際にこのような何かをしようとしているように見えます:

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

イベントを発生させるようです。どうもありがとうございます! – ben

1

yahelcの答えはあなたのイベントの追跡に右のparamsを得るのを助ける必要がありますが、あなたは決算</script>が欠けているように見えますタグが</body>の前に表示されている可能性があります。これは、GAがまだ動作していない可能性が高いことを意味します。

+0

ありがとう、私はそれを逃した。 – ben

関連する問題