3

I'm trying to use jQuery to find all links on a page, then when they are clicked track the event with Google Analytics.

I have the link part working, I'm just not seeing the tracking work event fire (when monitoring with httpfox).

Can anybody see what I'm doing wrong?

<!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>
asked Aug 2, 2011 at 17:37

3 Answers 3

4

You're passing too many parameters to the _gaq.push() function. It can only take 3 string arguments, but you're passing 4.

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

Combine 2 of the arguments to be one, and it'll work (or get rid of the "click" string for action, since its a waste). But, you're also not passing the variable names; you're just passing strings that are equivalent to the variable names. It looks like you're actually trying to do something like this:

_gaq.push(['_trackEvent', category, linkText , url]);
answered Aug 2, 2011 at 18:13
Sign up to request clarification or add additional context in comments.

Comments

1

yahelc's answer should help you get the right params into the event tracking, but it looks like you're missing a closing </script> tag before the </body> which likely means that GA isn't working at all yet.

answered Aug 2, 2011 at 18:19

Comments

0

Alternatively, you can streamline the whole thing with data attributes and a jquery plugin: https://github.com/pascalvgemert/jquery-analytics-event-tracking.

answered Nov 14, 2014 at 16:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.