views:

237

answers:

7

I want to count the number of visits (or visitors, not page requests or hits) made to my whole site.

I want to store each visit in a database to show some graphics with this data in the future.

Also, I want it to be reliable, fast and not to be polling to database for each page request. So, I think I should put some code in some entry point, but I don't know where, and not in the Global.asax event AppEndRequest. I'm guessing to code the SessionStart or SessionEnd events.

How can I make this visit counter?

A: 

you just need to update the counter value of your database. you can use session start and update counter valude in database by 1..

Syed Tayyab Ali
A: 

I think you can put a global variable on the Global.asax, increment it on Application_OnEndRequest and in other page, provide the code for save the value to the database, as an administrative task.

void Application_OnEndRequest(object sender, EventArgs e) {
    int visits;
    if (int.TryParse(Application["visits"], out visits)) {
     visits++;
    } else {
     visits = 1;
    }
    Application["visits"] = visits; 
}
Jhonny D. Cano -Leftware-
This is definitely not thread-safe, and the visit counter will not persist between application resets.
Juliet
I think he is asking for simplicity, anybody can save each request or visit or hit in a database, but he does not want to poll database on every request, ... for getting thread-safe you can call Monitor.Lock, or something like that
Jhonny D. Cano -Leftware-
+1  A: 

You could just parse your existing server logs, either using your own software or by using existing log-parsing software. I know that Microsoft's Log Parser is able to convert logs into sql if need be. The parser can easily be extended to use simple com objects to perform the parsing if the log is in a format it does not understand..

Brian
That would save a lot of work, good idea. Do the logs contain session information though (visitor tracking) or just page request information (hit tracking)?
Barry Fandango
Probably just hit tracking, though it almost definitely includes IP addresses.
Brian
A: 

Note that Syed's suggestion will track the number of visitors, while Jhonny's will track the page hits. You might want to decide which one you're after.

Assuming you want visitors, I agree with Syed, catch Session_Start in global and increment a number in the database.

You could get more useful information if you store extra info in the database - for example a table like this:

tblVisitor
- SessionID nvarchar(32)
- SessionStart datetime

This would probably be better than just incrementing a counter, because you can check the number of visitors last month or this year, etc. and also get information about time-of-day usage.

However you did ask for the simplest possible way, and a simple counter would accomplish that for sure.

Barry Fandango
+5  A: 

Use google analytics or something similar. It's the simplest solution.

Rashack
+1 Google Analytics rocks.
Dead account
A: 

In general, I agree with Rashack in the general principle that the best thing to do is take advantage of free services from third parties who are good at visitor analysis. Beyond Google Analytics, you may want to consider Quantcast.

chaos