# Page view counter

**URL:** <https://www.ruby-forum.com/t/page-view-counter/183034>\
**Category:** Rails\
**Created:** [February 6, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/page-view-counter/183034 "2010-02-06T15:57:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![seSSSSor\_tower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@seSSSSor\_tower](https://www.ruby-forum.com/u/seSSSSor_tower)\
**Post date:** [February 6, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/page-view-counter/183034/1 "2010-02-06T15:57:42Z")

</div>

Hi, Anyone can point me in the right direction…  
I have a very simple rails app and I want to do a page view counter  
for it so that I can display  
how many people have viewed a given page on it since day one  
and also in the last 24 hours…

I have seen this in some php sites before…  
I searched a bit but it did not lead me to anything remotely close for  
a rails app.  
if anyone has a link to some site where this had been done please  
share it.  
I am a newbie btw ☹

thanks  
Señor.

---

<div class="post-metadata">

**Author:** ![seSSSSor\_tower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@seSSSSor\_tower](https://www.ruby-forum.com/u/seSSSSor_tower)\
**Post date:** [February 6, 2010, 5:34pm UTC](https://www.ruby-forum.com/t/page-view-counter/183034/2 "2010-02-06T17:34:18Z")

</div>

umm, I wasn’t -still not sure - that google analytics can be used to  
display public data…  
But I will see what i find!

---

<div class="post-metadata">

**Author:** ![seSSSSor\_tower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@seSSSSor\_tower](https://www.ruby-forum.com/u/seSSSSor_tower)\
**Post date:** [February 6, 2010, 4:35pm UTC](https://www.ruby-forum.com/t/page-view-counter/183034/3 "2010-02-06T16:35:44Z")

</div>

seÃ±or tower wrote:

> Hi, Anyone can point me in the right direction…  
> I have a very simple rails app and I want to do a page view counter  
> for it so that I can display  
> how many people have viewed a given page on it since day one  
> and also in the last 24 hours…
> 
> I have seen this in some php sites before…  
> I searched a bit but it did not lead me to anything remotely close for  
> a rails app.  
> if anyone has a link to some site where this had been done please  
> share it.  
> I am a newbie btw ☹
> 
> thanks  
> Seï¿½or.

Why not just use Google Analytics?

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![seSSSSor\_tower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@seSSSSor\_tower](https://www.ruby-forum.com/u/seSSSSor_tower)\
**Post date:** [February 6, 2010, 6:42pm UTC](https://www.ruby-forum.com/t/page-view-counter/183034/4 "2010-02-06T18:42:46Z")

</div>

Thanks for our unfounded point of view Marnen.

I still would like to know if anyone else has come across anything  
similar.  
a simple hit counter… to display some basic metrics.  
thanks so much.

señor.

---

<div class="post-metadata">

**Author:** ![seSSSSor\_tower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@seSSSSor\_tower](https://www.ruby-forum.com/u/seSSSSor_tower)\
**Post date:** [February 6, 2010, 10:39pm UTC](https://www.ruby-forum.com/t/page-view-counter/183034/5 "2010-02-06T22:39:17Z")

</div>

I’m relatively new to ruby on rails, but launching the server with –  
debug and throwing debugger into an application\_controller  
before\_filter, I was able to determine where the URI was stored:

request.env[‘REQUEST\_URI’]

If you could grab this value and throw it into a table and/or  
increment its counter, you could easily retrieve the count later.

There’s probably ActiveRecord magic where you can add it if it is new,  
otherwise increment the record’s count column.

The simplest table to match your listed requirements would be to have  
URI and date columns as the primary key, then you will have a history  
of counts on a per day basis including the current day (though no  
greater granularity for the current day).

Please post if you find a solution.

Mike

---

<div class="post-metadata">

**Author:** ![seSSSSor\_tower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@seSSSSor\_tower](https://www.ruby-forum.com/u/seSSSSor_tower)\
**Post date:** [February 6, 2010, 6:31pm UTC](https://www.ruby-forum.com/t/page-view-counter/183034/6 "2010-02-06T18:31:38Z")

</div>

seÃ±or tower wrote:

> umm, I wasn’t -still not sure - that google analytics can be used to  
> display public data…  
> But I will see what i find!

You probably shouldn’t be displaying your counter anyway…

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![seSSSSor\_tower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@seSSSSor\_tower](https://www.ruby-forum.com/u/seSSSSor_tower)\
**Post date:** [February 6, 2010, 10:40pm UTC](https://www.ruby-forum.com/t/page-view-counter/183034/7 "2010-02-06T22:40:27Z")

</div>

Humm… thought I posted something to this thread, but nothing has  
shown up.

I’m sure you are looking for something more out-of-the-box, but I  
thought I’d share with you what I just discovered myself should it be  
useful to you. Disclaimer: I’m a relative newbie to Rails.

After starting the script/server with --debug and calling debugger in  
an application\_controller before\_filter, I was able to find the URL  
within request.env[‘REQUEST\_URI’]. If you create a model with URI and  
date as a unique key, you could create a new URI+date it it doesn’t  
exist and increment a counter each time a page is viewed. It  
shouldn’t take more than an hour to implement (maybe three for me as a  
newbie 😉

– Mike

---

<div class="post-metadata">

**Author:** ![seSSSSor\_tower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@seSSSSor\_tower](https://www.ruby-forum.com/u/seSSSSor_tower)\
**Post date:** [February 8, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/page-view-counter/183034/8 "2010-02-08T16:31:48Z")

</div>

Hi,

On Sat, 2010-02-06 at 09:40 -0800, señor tower wrote:

> Thanks for our unfounded point of view Marnen.
> 
> I still would like to know if anyone else has come across anything  
> similar.  
> a simple hit counter… to display some basic metrics.  
> thanks so much.

We did something on a project I worked on a while back that might be  
relevant. It was audit functionality but the ‘bones’ of it were pretty  
much what you’re talking about I think. The basics were to install a  
before\_filter to record the method being invoked. That’s close to a  
page hit counter. You’d probably want to tweak it if you’re app  
includes Ajax renders.

HTH,  
Bill
