# Counting unique viewers?

**URL:** <https://www.ruby-forum.com/t/counting-unique-viewers/84682>\
**Category:** Rails\
**Created:** [February 6, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/counting-unique-viewers/84682 "2007-02-06T23:30:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 6, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/counting-unique-viewers/84682/1 "2007-02-06T23:30:30Z")

</div>

I’m building an app where I’d like to track the number of views of each  
item, but I’d like to keep it from being spammable, so it counts unique  
viewers rather than just the number of pageviews.

What’s a good way to go about this? Should I build a database to record  
the IP addresses and save the request.remote\_addr for each viewer in the  
database, associated with each item? And then check each viewer against  
that item’s database of IPs to see if they’re unique before incrementing  
the view counter?

Are there other ways of doing it?

Thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 6, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/counting-unique-viewers/84682/2 "2007-02-06T23:40:25Z")

</div>

> I’m building an app where I’d like to track the number of views of each  
> item, but I’d like to keep it from being spammable, so it counts unique  
> viewers rather than just the number of pageviews.
> 
> What’s a good way to go about this? Should I build a database to record  
> the IP addresses and save the request.remote\_addr for each viewer in the  
> database, associated with each item? And then check each viewer against  
> that item’s database of IPs to see if they’re unique before incrementing  
> the view counter?

IP address won’t work… all the machines at my company appear as a  
single  
public IP, but there’s 50 of us…

About the only thing you can do is set a cookie and hope your viewers  
don’t bother to delete it.

Or force them to login, but that might not be an option…

-philip

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 6, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/counting-unique-viewers/84682/3 "2007-02-06T23:48:26Z")

</div>

On 2/6/07, Jeff C.man [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Are there other ways of doing it?  
> You have to take into consideration the ISPs who force user requests  
> through their proxy servers. Those users will appear to have the same  
> IP address a lot of the time, AOL users for example. I usually go  
> with checking that the UP address isn’t new in the last 24 hours or  
> so, since dial up users stand a good chance of getting assigned a  
> different proxy their next session.

If you’re using MySQL I wouldn’t store raw IP addresses. That will  
cost you a varchar(15) field which may be up to 16 bytes. Instead  
convert the IP to a signed integer before storing it. That will only  
cost you 4 bytes, until ipv6 gets more love anyway.

def ip2long( ip )  
long = 0  
ip.split( /./ ).reverse.each\_with\_index do |x, i|  
long += x.to\_i \<\< ( i \* 8 )  
end  
long  
end

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
