: Tracking click conversions with Google Analytics Is there anyway I can use Google Analytics to track click conversions on a link? For example, if I have a link to www.a.com , is it possible
Is there anyway I can use Google Analytics to track click conversions on a link?
For example, if I have a link to a.com , is it possible for google to track the number of times that particular link was shown on my page and then track how many times it was really clicked? The problem is that I do not show the link to a.com every time the page loads. I am using a random function (server side) to generate a different link everytime. I would like Google Analytics to provide me with the click conversion for each of the links I choose to show the user.
Thanks,
Joel
More posts by @Steve110
1 Comments
Sorted by latest first Latest Oldest Best
Yes, you can track just about anything using Google Analytics' Event Tracking - here's how you might do it for the two cases of (a) showing a link and (b) a user clicking the link:
(a) Track showing the link
<script type="text/javascript">
_gaq.push(['_trackEvent', 'Links', 'Shown', 'http://www.a.com/']);
</script>
(b) Track user clicking the link
<a href="http://www.a.com/" onclick="javascript:_gaq.push(['_trackEvent', 'Links', 'Clicked', 'http://www.a.com/']); return true;">...</a>
Of course, given that you probably don't want to hard-code for both of these Javascript events, you can use a handy Javascript function to do all the work for you when links have a specific class - I like to use jQuery, so here's a quick example:
<script type="text/javascript">
/*
... assuming jQuery has been loaded and you're using the
standard Google Analytics Asynchronous tracking snippet
URL: code.google.com/apis/analytics/docs/tracking/asyncTracking.html */
$(document).ready(function() {
if ( $('a.tracked').length ) {
$('a.tracked').each(function() {
_gaq.push(['_trackEvent', 'Links', 'Shown', $(this).attr('href')]);
$(this).bind( 'click', function() {
_gaq.push(['_trackEvent', 'Links', 'Clicked', $(this).attr('href')]);
return true;
});
});
}
});
</script>
... and then every link with class="tracked" should be tracked accordingly.
Update: New code, but sure - you could even specify which group a link belongs to and track the group instead:
<script type="text/javascript">
$(document).ready(function() {
if ( $('a.tracked').length ) {
$('a.tracked').each(function() {
var groupRegExp =/group([a-zA-Z])/i;
var group = groupRegExp.exec( $(this).attr('class') );
if (
( !group ) ||
( !group.length )
) {
var group = "groupUnknown";
} else {
var group = group[0];
}
_gaq.push(['_trackEvent', 'Links', 'Shown', group]);
$(this).bind( 'click', function() {
_gaq.push(['_trackEvent', 'Links', 'Clicked', group]);
return true;
});
});
}
});
</script>
... which should work for class="tracked groupA", class="groupA tracked", etc.
Update x2:
... and to account for the behavior which yc01 mentioned, a slight modification to the function bound to tracked anchor tags:
$(this).bind( 'click', function() {
_gaq.push(['_trackEvent', 'Links', 'Clicked', group]);
setTimeout('document.location = "' + $(this).attr('href') + '"', 100)
return false;
});
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.