Dmitri Nesteruk
Dmitri Nesteruk

Reputation: 23789

What is a good way of tracking link clicks in Asp.Net Mvc app?

I have an MVC app with several <a> tags, and I want to have a way of tracking link clicks, preferably without doing redirects (i.e., without changing link addresses). Is there any way this can be done? And, if not, is there any way one can get this functionality automatically (e.g., with javascript)?

Thanks.

Upvotes: 0

Views: 2756

Answers (4)

Conceptdev
Conceptdev

Reputation: 3261

Since you are already using Google Analytics, they have a javascript function to put in the onclick event.

The method name is either urchinTracker or pageTracker._trackPageview depending on whether you are using urchin.js or ga.js as your Analytics script include.

An example (from here) in both formats:

<a href="/files/map.pdf" onClick="javascript:urchinTracker ('/downloads/map');">
<a href="/files/map.pdf" onClick="javascript:pageTracker._trackPageview ('/downloads/map');">

Basically what that does is call the javascript method (which does the 'analytics call' to Google's server) then does the <a> action as normal (ie. whatever the link was supposed to do - in this case open PDF files).

Is that what you are looking for - or is the jQuery answer what you wanted?

EDIT: in response to your question, this post contains a non-jQuery javascript include (here) that looks for 'downloadable files' and adds tracking. If you want to use jQuery, check out the other answer by ropstah since he/she has updated their answer to be Google Analytics-specific.

Upvotes: 1

Ropstah
Ropstah

Reputation: 17794

UPDATE

Taking your other wishes in consideration, this should be your way to go:

<script type="text/javascript">
$(document).ready(function() {  //On document ready
    $('a').click(function(e) {  //For each a (anchor), set the following function
        urchinTracker (this.href); 
        pageTracker._trackPageview (this.href);
        //Custom tracker here?
        return true; //To keep following href definitions
    });
});
</script>

Upvotes: 2

Matt Kocaj
Matt Kocaj

Reputation: 11535

Consider Woopra

Upvotes: 1

Anton Gogolev
Anton Gogolev

Reputation: 115731

Consider using Google Analytics: you can't do any better.

Upvotes: 0

Related Questions