ruby - Creating a simple hit counter in Rails -
i've been trying implement gem impressionist, doc confusing , gem carries bit of overhead simple use case wonder how go implementing myself?
i want track impressions (requests) of action in controller.
the impressions don't have registered unique visitors.
i need counter unique each record in model (i.e. shop 1: 34 hits, shop 2: 77 hits etc).
the counter should not include requests bots on list: http://www.user-agents.org/allagents.xml
what minimal code required accomplish above?
update
to clarify, if possible don't want use gem impressionist. used prelude describe problem i'm trying solve.
you need add attribute model.
if using impressionsit gem, should use built-in migration generator add correct scheme database.
otherwise, if not planing use gem, need create migration following:
mymigration < activerecord::migration def change add_column :pluralized_model_eg_users, :integer, default: 0 end end
then, on actions want count, use code :
unless request.env["http_user_agent"].match(/\(.*https?:\/\/.*\)/) @model.update_attribute :impressions, @model + 1 end
it not uses list gave avoid overhead. however, most robots have url in user agent identify then, using should safe.
using list site need add caching list, might add uneeded complexity in code if aims simple feature.
Comments
Post a Comment