Saturday, 22 December 2012

How to Count Website Visitors in Asp.net


Hi Friends,
In this article i will tell you how to count total number of visitors in website by using simple 3 steps
This is a very simple website user counter to see how many people have visited a web page if you want to show it on a web page.

Step 1:  
 We can do this by using (or creating) a global.asax file.
Every time a session is started, you increment an integer, which holds the number of current visitors. Every time a session is ended, you subtract one from that integer. Next, you will have to use public properties for all pages to be able to retrieve the information.


public class Global : System.Web.HttpApplication
{
    private static int totalNumberOfUsers = 0;
    private static int currentNumberOfUsers = 0;

        protected void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
       totalNumberOfUsers = 0;
    }


    protected void Session_Start(Object sender, EventArgs e)
    {
        // Code that runs when a new session is started
      totalNumberOfUsers += 1;
      currentNumberOfUsers += 1;
    }
    protected void Session_End(Object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
       // is set to InProc in the Web.config file. 
        //If session mode is set to   StateServer
        // or SQLServer, the event is not raised.

      currentNumberOfUsers -= 1;
    }
    protected void Application_End(Object sender, EventArgs e)
    { }

    public static int TotalNumberOfUsers{ get { return totalNumberOfUsers; } }
    public static int CurrentNumberOfUsers{ get { return currentNumberOfUsers; } }
}

Step 2:  

Next, in your control, set the values of the global.asax file in the right labels.
private void Page_Load(object sender, System.EventArgs e)
{
  int currentNumberOfUsers = HitCounters.Global.CurrentNumberOfUsers;
  int totalNumberOfUsers = HitCounters.Global.TotalNumberOfUsers;
  lblCurrentNumberOfUsers.Text = currentNumberOfUsers.ToString();
  lblTotalNumberOfUsers.Text = totalNumberOfUsers.ToString();
}


Step 3:  

   In order for this to work correctly we need to enable sessionstate and configure its mode to InProc value (in our web.config file):

        <system.web>
        <sessionState mode="InProc" cookieless="false" timeout="20" />
        </system.web>

In-process mode stores session state values and variables in memory on the local Web server. 
It is the only mode that supports the Session_OnEnd event that we used previously.

Timeout value (in minutes, not in seconds) configures how long our sessions are kept 'alive' - in other words
here we set how long our users can be inactive before considered Offline.

In this example timeout is set to 20 minutes, so while our visitors click on some links in our website at least once
in a 20 minutes, they are considered online.



Happy Programming!!

Don’t forget to leave your feedback and comments below!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal

12 comments :

Faisal Alsaeed said...

It is nice tutorial sujeet

thanks
regards
Faisal

Juhi said...

when i put hitcounter.global in my master page then it shows me error of any assembly reference missing.
which it is?

Anonymous said...

global.aspx not global.asax.....sir g

Anonymous said...

abe asax hi hota h wo beta

Anonymous said...

Bhai sujeet total visitor count nahi kar raha ye article session
end hone par fir se restart ho jata h ye online visitor ke liye hi sahi h only

Pawan Rahate said...

For total visitor count, just maintain the count in text file or in database.

great article Sujit sir. It helps lot.

Sujit Bhujbal said...

Thanks Pawan

Janani Muthaiyan said...

whats HitCounters? Its not accepting in my sample

Rajesh Balbale said...

It is nice tutorial sir

thanks
regards
Rajesh Balbale

dadi said...

babaji ka thulu sr bdiya h

midhun mohandas k said...

'HitCounters' doesnot exsist in the current context---- error ocurring alwayzz....

Manish Sharma said...

Http counter does not exist

Post a Comment