0

I want to count the number of visits on my blog? Can someone please suggest the overall method to implement this feature?

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78

3 Answers3

3

It is just an idea. You can add a count_view column in the database into blogs table with default value 0.

And in the show action of BlogsController add the following code

def show
 @blog = Blog.where('id = ?', params[:id]).first
 @blog.update_column('count_view', @blog.count_view + 1) if @blog.present?
end

You can modify this logic as per your requirement.

Bachan Smruty
  • 5,686
  • 1
  • 18
  • 23
1

You can check the hit counter gem or the impressionist gem.

mrudult
  • 2,480
  • 3
  • 35
  • 54
0

You could also use an existing (free) analytics solutions if you want to get much more data than the number of times the action was called (please note that if the same user refreshes the browser 5 times, you get 5 hits):

http://www.google.com/analytics/

Using these you can get data like unique visitors, referral URL, locations data, browser, OS, and a lot of different stuff to make informed decisions. There are several other options (paid, free, real time) available as well:

https://mixpanel.com

https://www.kissmetrics.com/

amit_saxena
  • 7,450
  • 5
  • 49
  • 64