How to track click links

I am trying to track the last "link_to" the user clicked on. I found some general ideas on how to do this, but I will not be able to get it to work.
As a rule, I have an index list, and I try to direct link_to on each line to the action of the controller, which will save: id to the cookie and user record in the database, and then redirect to the actual external link.

I am wondering if there is a better way than using link_to.

This is probably a pretty simple problem, but I'm stuck in my brain! Appreciate any help!

+4
source share
1 answer

You can track through AJAX and then redirect to the link, all gracefully diverging so as not to track.

Assuming jQuery:

# view <%= link_to 'Foo', 'whatever', :'data-trackable' => 'id-to-log' %> # js $('a[data-trackable]').click(function(){ $.post('/clicks', { id: $(this).data('trackable') }, function(){ window.location = $(this).attr('href'); }); return false; }); 

I think that maybe I work to work hard, but some time has passed since I did this, and I can’t remember how to block until the AJAX request is sent and then returned. Feel free to edit if you know.

+4
source

All Articles