Tracking Users in the Global.asa File

So you've launched your new site and you're sitting back waiting for the thousands of concurrent connections that forced you to shell out the big bucks for SQL Server. Your sitting there looking at your home page wondering who else is looking at it right this minute. Will it look different while experiencing the adoring stares of users across the globe? How can you tell how many people have visited?

Some people like those speedometer looking graphic numbers that pronounce the number of times your home page has been seen since some date in the past. To me, prominent hit counters too often just confirm the fact that, in the scheme of things, nobody has visited your site much at all in the months since it was last updated (which is another thing...if you're not going to update it, don't put a date on there that proves it) and other than the occasional snicker, nobody really much cares anyway. Except for you. That's why you need an accurate, discreet way to track how many people have visited your site and how many are there right now.

The global.asa file is perfect for this. It is incredible to think that many if not most Dreamweaver users don't even know what a global.asa file is, but that's how sheltered you can become using a program like Dreamweaver MX if you don't take the initiative to learn how ASP works. The global.asa file is a site wide file that lives in the root directory of your domain. It contains just four functions by default:

Application_OnStart
Application_OnEnd
Session_OnStart
Session_OnEnd

The Application object is accessible and writable from your entire site, meaning that all of the users of you site can read and write to the same variables. The session object is unique to each user and is only accessible by them.

As is implied by their names, these functions are triggered when your application begins or ends and when an individual session begins or ends. The global.asa file sees these events automatically, so you don't need to do anything extra to cause the events that you place in these functions to occur.

The Application_OnStart function will really only trigger once unless you shut down your site or have some special functionality that restarts the application. We will use it to initialize and store variables that are available to everyone at the site. This allows the beginning of each session to increment our visits counter and our active counter and allows a counter page to display these figures for you. Let's look at the code:

Sub Application_OnStart
Application("visits") = 0
Application("Active") = 0
End Sub

That's all you need. Since the site just starts once, we initialize the two variables we are interested in; how many total visits have we had and how many active users are there.

The Application_OnEnd function has no code in this context. We are not explicitly calling the end of the application and an accidental crash of the site would not allow OnEnd code to run anyway so there is no need for it.

The Session_OnStart function trigger each time a new user visits our site. It this function, we will increment both counters to add a visit and a new active user to our application variables. Here is the code:


Sub Session_OnStart
Application.lock
Application("visits") = Application("visits") + 1
Application.unlock

Application.lock
Application("Active") = Application("Active") + 1
Application.unlock
End Sub

Notice that we call an Application.Lock and Application.Unlock each time we write to a variable. This ensures that two users don't try to overwrite each other and screw things up. Each variable that we initialized in the Application_OnStart function is incremented to update the counters with this current user.

All that is left is the Session_OnEnd function:

Sub Session_OnEnd
Application.lock
Application("Active") = Application("active") - 1
Application.unlock
End Sub

When a session ends (either explicitly, by calling the session.abandon event, or by timing out) this code runs and decrements the active users variable. Notice that the visits variable is not decremented and contains a complete history of the number of session ever started in the application.

Keep in mind that few sites even try, and fewer are successful, to coax users into logging out of a site. Therefore most sessions will end with a timeout. Remember that your count of active users will be off slightly because some users who have left your site still have active sessions as far as the server is concerned. But over time, you will receive an accurate average of the momentary usage of your site.

Now all you need is an ASP page to read the application variables and present your counters to you. You can do this by creating a simple page that just has this code on it:

< %
Response.write Application("Active") & " active users<BR>"
Response.Write Application("visits") & " total visits"

% >

Now copy your global.asa file into the root of your site and copy the users.asp file to wherever you would like it. By calling the users.asp you can keep tabs on the users of your site and know that people are enjoying it as you sit and watch.

Ray West

www.dwteam.com


back to top ^



Copyright © 2002, DWTeam.com. All Rights Reserved.