views:

72

answers:

6

I am working on a component in C# to record how many unique viewers have viewed my website / page, making certain the same user revisiting, is not recorded twice. What is an efficient method to write such a component? Do you track cookies or session objects? Would I record their ip address (which is not static) or computer name? This information would be stored in a database (as far as I know)

Thanks.

A: 

Recording the IP address is not a good idea as many people actually use NAT to get from their local network to the Internet. So many people would appear to you as a single person because they all have the same public IP address. If you are on an intranet, this might work but definitely not for a public web site.

A better solution is to write a GUID to a cookie on the user's computer and stored in your database. Then look for that cookie again in the future and compare with the values in the database.

sarme
A: 

Unique visits by IP address is always a problem, as you could have an organisation with a unique IP but hundreds of people behind that IP.

I think cookies are your best bet.

I assume you have a reason for not using a stats or analytics package?

Damien Dennehy
stats or analytics package? Any free ones out there? Other than cost, its the configuration involved (versus developing a component that can be reused)
user279521
Google Analytics? http://www.google.com/analytics/All it requires is a few lines of Javascript on your pages.EDIT: You can disregard this I guess, as you updated the original question. Since you need to store your stats in a database an off-the-shelf package may not suit.
Damien Dennehy
+2  A: 

Yes you can record IP address, if you use a traditional Login scheme (if using membership user) this Link can guide you. Check this question on IP which tells you how IP can be gathered. It's not a bad idea if you get the MAC of the host machine so you can easily determine the unique visitor tracking.

Chaitanya
MAC isn't sent beyond the first router is it?
Martin Smith
Yes it is true.
Chaitanya
+1  A: 

People are saying that recording IP is a bad idea because you could have situations with hundreds of people behind the same IP, this is true, however, using IP address will still give you a statistically meaningul number.

If it is core for your application to guarantee unique, then unfortunatly that is not possible to do in a guaranteed way.

Tom Gullen
A: 

Both IP addresses and cookies have their tradeoffs. A single user could be hopping from wifi hotspot to wifi hotspot, throwing off your IP metrics. Many users could be behind the same IP address (employer, ISP, proxy, etc.). So IP address would not be entirely accurate.

On the other hand, a single user at a single IP could access your website through multiple browsers or devices, so cookies would not be entirely accurate.

And what of the user that accesses your website from home and from work? One visitor, two (at minimum) unique cookies and IP addresses.

Pick the invalid data that works best for you.

Anthony Pegram
+2  A: 

Why reinvent the wheel? Use Google Analytics - it's free, extremely easy to implement, provides more statistics than you could ever want, and can provide (as best as it is possible using cookies) unique visitor stats. If you really need to store the data in a database (why?) then you can use the Google Analytics API to get at it and do what you want with it.

Dan Diplo
+1 for the API link, didn't know Google had one for Analytics.
Damien Dennehy
@Dan, where would you store the data, if not the database? I am open to retool. Many thanks for the Google suggestion.
user279521
Well, I don't know what you need to do with the data - but if it's already stored in Google then do you need to replicate that storage locally? Or could you view / manipulate it directly from Google, via the API?
Dan Diplo