Marcin Doliwa
Marcin Doliwa

Reputation: 3659

Counting clicks to external links with rails

I have Entry model with url field, which contains link to external site. In view I list these links, and now I'd like to start counting when someone clicks it, and keep this info in database. What's the best way of doing it?

Upvotes: 2

Views: 987

Answers (1)

jeremy.clark
jeremy.clark

Reputation: 852

You can easily use google analytics to track outbound links: http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920

If that is not an option you will need to add some javascript to your links make an ajax request to the server to increment the count before transferring the user to the new url. Something similar to this jquery code:

$('a').click(function(){
  var stored_ulr = $(this).attr('href');

  $.ajax({
   url: #your server url to increment count,
   data: #data you need to send,
   success: function() { window.location = stored_url; },
  });

 return false;
});

The above code is just a general outline. You will have to fill in the blanks and make it work for your needs.

Upvotes: 2

Related Questions