Conteggiare il numero di click sui link è relativamente semplice con jQuery.
La soluzione è la seguente:
$( "a" ).click(function( e ) {
e.preventDefault();
var $a = $( this ),
url = $a.attr( "href" );
// Ottengo il numero di click
$.post( "/api/urls", { url: url }, function( response ) {
var count = parseInt( response.count, 10 );
// Lo incremento e lo aggiorno
var updatedCount = count++;
$.post( "/api/urls/update", { url: url, count: updatedCount }, function( resp ) {
if( resp.saved ) {
window.location = url;
}
});
});
});