How to get the accurate total visitors count in ASP.NET

15,569

Solution 1

You can never get an entirely accurate number: there is no way to (reliably) detect that a user "has navigated to another site" (and left yours) or that that user "closed the browser".

The Session_Start/Session_End way has the problem that Session_End is only called for "InProc" sessions, not if the sessions are stored in StateServer or SqlServer.

What you might be able to do:

  • Hold a Dictionary<string, DateTime> in Application scope. This stored session-id's (the string key) against the time of latest access (the DateTime value)
  • For each request with a valid session, find the session entry in the dictionary and update it's latest-access time (add a new entry if not found)
  • When you want to get the number of online users, first loop through all entries in the dictionary and remove items where the session timeout has passed. The remaining count is the number of online users.

One problem (at least): if one user uses two browsers simultaneously, he has two sessions open and is counted double. If users always log in, maybe you could count on login-id instead of session-id.

Solution 2

I want to propose something completely another: try to use Google Analytic for this. These guys add beta functionality that allows tracking online statistics now.

EDIT: You don't get me. I mentioned not standard Google Analytic functionality. I mentioned new feature - Real-Time. Check next article for details - What’s happening on your site right now? or watch this video - Google Analytics real-time beta

Share:
15,569
Jonas T
Author by

Jonas T

.NET Web Application Developer [email protected] Linkedin: http://au.linkedin.com/pub/thein-than-oo/29/285/468

Updated on July 10, 2022

Comments

  • Jonas T
    Jonas T almost 2 years

    I want to know the number of visitors online on my site. I did my research and found two solutions.

    Source: Code Project
    Online active users counter in ASP.NET

    It is easy to setup and easy to use but it increases the user count for every Ajax request/response too. My home page alone has 12 Ajax requests(8 requests to one page and 4 requests to another page). This dramatically increases the user count.

    Source: Stack Overflow Q/A
    Count the no of Visitors This one works exactly the same as the previous one.

    Source: ASP.Net Forum How to see "who is online" using C#

    This one looks better than the previous two. Here is the detail code of this solution.

    void Application_Start(object sender, EventArgs e) 
        {
            // Code that runs on application startup
            HttpContext.Current.Application["visitors_online"] = 0;
        }
    
    void Session_Start(object sender, EventArgs e) 
        {
            Session.Timeout = 20; //'20 minute timeout
            HttpContext.Current.Application.Lock();
            Application["visitors_online"] = Convert.ToInt64(HttpContext.Current.Application["visitors_online"]) + 1;
            HttpContext.Current.Application.UnLock();
        }
    
    void Session_End(object sender, EventArgs e) 
        {
            HttpContext.Current.Application.Lock();
            Application["visitors_online"] = Convert.ToInt64(HttpContext.Current.Application["visitors_online"]) - 1;
            HttpContext.Current.Application.UnLock();
        }
    

    It seems to be able to ignore the increasing the count for every Ajax response but it still adds up for each page refresh or page request.

    Is there any approach to count the accurate number of online visitors in ASP.Net?

  • Hans Kesting
    Hans Kesting almost 12 years
    You don't HAVE to switch to StateServer or SqlServer, it's when you USE them that Session_End doesn't work.
  • Chibueze Opata
    Chibueze Opata almost 12 years
    I actually thought you were ill-advising him against InProc, if he's developing a website or webapp to accommodate a lot of traffic or use multiple servers, InProc will definitely be a bad idea, the article above clearly explains the pros and cons of using InProc.
  • Jonas T
    Jonas T almost 12 years
    Thanks a lot Hans. It is all working now but the site is now working really slow for some reasons. I will update my code below if someone needs this code or has any comment.
  • Jonas T
    Jonas T almost 12 years
    We are already using it. The problem is we know how many visitors visited for each day but we don't know how many users are currently on our site. That's why I need to implement this on my own way.
  • PreguntonCojoneroCabrón
    PreguntonCojoneroCabrón over 7 years
    performance Session_Start and more 4000 users in appweb ? HttpHandler ?
  • PreguntonCojoneroCabrón
    PreguntonCojoneroCabrón over 7 years
    Any solution for Session_End sessions are stored in SqlServer?
  • PreguntonCojoneroCabrón
    PreguntonCojoneroCabrón over 7 years
    full sample using GA in asp.net ?
  • RredCat
    RredCat over 7 years
    @PreguntonCojoneroCabrón you don't need server side code to start using GA. Just put JavaScript code that Google provides on your account page into Master page of your asp.net app.