Track number of clicks on a link

Hello,

So I am trying to simply track the number of hits a link has received
and display that number on the site so other users can see which links
are more popular. So once the link is clicked on it adds a +1 value to
the hits column in the database and then redirects the person to the
url that the link is supposed to go to. So far this is what I have.

I have a tutorials table in my database and a column for hits and a
column for url.

** tutorials_controller.rb **

def update_hits_count
     @tutorial.update_attribute :hits, params[:count].size + 1
   if @tutorial.save
      redirect_to url_for(@tutorial.url)
   end
end

** view/tutorials/show.html.erb **

<% for tutorial in @tutorials %>
    <%=link_to tutorial.title, {:action => 'update_hits_count', :count
=> tutorial.hits} %>
<% end %>

That is what I have so far and when you click on the link the hits
column in the tutorials table just gets set to null and it does not
redirect them to the proper url. Any help would be greatly
appreciated. Thanks in advance.


Hello,
So I am trying to simply track the number of hits a link has received
and display that number on the site so other users can see which links
are more popular. So once the link is clicked on it adds a +1 value to
the hits column in the database and then redirects the person to the
url that the link is supposed to go to. So far this is what I have.
I have a tutorials table in my database and a column for hits and a
column for url.
** tutorials_controller.rb **
def update_hits_count
@tutorial.update_attribute :hits, params[:count].size + 1
if @tutorial.save
redirect_to url_for(@tutorial.url)
end
end

modify the method so it suits any controller,

like in self.update_attribute

add the method to application_controller

whenever you want to add a hit you call it,

like in

def index

update_hits_count

end

Thanks for the response, sorry I'm totally new to this any way you can
elaborate or show example? Any help would be greatly appreciated,
thanks in advance

Note that writing to the db every time something on your site is viewed can really limit your capacity and scale and hasten the day when you’ll need to think about multi-master configuration. So unless you’re using something like amazon SimpleDB, which scales for you, it might be better to figure out a better way to track view counting (e.g. central log server, cron job to aggregate and update db?).

m


Thanks for the response, sorry I'm totally new to this any way you can
elaborate or show example? Any help would be greatly appreciated,
thanks in advance
> > Hello,
>
> > So I am trying to simply track the number of hits a link has received
> > and display that number on the site so other users can see which links
> > are more popular. So once the link is clicked on it adds a +1 value to
> > the hits column in the database and then redirects the person to the
> > url that the link is supposed to go to. So far this is what I have.
>
> > I have a tutorials table in my database and a column for hits and a
> > column for url.
>
> > ** tutorials_controller.rb **
>
> > def update_hits_count
> >      @tutorial.update_attribute :hits, params[:count].size + 1
> >    if @tutorial.save
> >       redirect_to url_for(@tutorial.url)
> >    end
> > end
>
> modify the method so it suits any controller,
> like in self.update_attribute
> add the method to application_controller
> whenever you want to add a hit you call it,
> like in
> def index
>     update_hits_count
> end
>

it really depends on what you want to track

hits on a single controller/action (probably writing on a file) or in many

more likely you’ll need a tracking model “hits”

that has something like

controller :string

action :string

hits :integer

define a addhit method on the model

self.hits = self.hits +1

create a method in application controller

def update_hit_counts

@hit.find(:first, :controller => controller, :method => method)

@hit.addhit

(you can use the current method and controller form rails environmental variables)

then when you want to register a hit

inside the method you call

update_hit_counts

(ads one query and one write per request)


>
>
>
>
> > ** view/tutorials/show.html.erb **
>
> > <% for tutorial in @tutorials %>
> >     <%=link_to tutorial.title, {:action => 'update_hits_count', :count
> > => tutorial.hits} %>
> > <% end %>
>
> > That is what I have so far and when you click on the link the hits
> > column in the tutorials table just gets set to null and it does not
> > redirect them to the proper url.  Any help would be greatly
> > appreciated.  Thanks in advance.
>
> > >
> --
>
>  smime.p7s
> 7KViewDownload
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en)
-~----------~----~----~----~------~----~------~--~---
-- Andres Paglayan
CTO, StoneSoup LLC Ph: 505 629-4344
Mb: 505 690-2871
FWD: 65-5587
Testi. Codi. Vinci.

Hey Dan,

Why don't you pass an unique ID parameter in each link (either GET or
POST) and use this in a before_filter in the application controller?
The filter would just increment the count for that ID in a separate
table. This is a general design that would work for anything. Later
you can use this information with some sort of mapping that says what
each ID means (or just put a title for each ID in the table).
Something like:

== Links

<a href="http://mysite.com?ID=1">Link 1</a>

== application.rb

  before_filter :inc_link_count

  def inc_link_count
    li = Link.find(params[:ID])
    li.count++
    li.save
  end

Cheers, Sazima

  def inc_link_count
    li = Link.find(params[:ID])
    li.count++
    li.save
  end

To avoid multiple connections stepping on one another, you should
consider using update_counters (or a custom method that does the same
thing) instead of doing a find, modifying, and saving.

Peace.

Ar Chron is right, I should just put some sort of voting system in
place, guess I was trying to take the easy way out. Back to the
drawing board, thanks everyone for your help