Counting visits and page viewsAnother example where I use Application variables is to implement the fun count of active users seen in the top right corner of every page. This involved a file not discussed yet on this site - global.asa. This file contains four functions that are called by the server at specific times, as documented in the file shown below. global.asa<script language=JavaScript runat=server>
// ============================================
// this function is called when the first user visits the page after the server
// has been rebooted. also called when this file is changed.
// ============================================
function Application_OnStart ( )
{
// you dont need to lock the Application object
// in this function - but remember to do it elsewhere...
// Application.Lock ( );
// number of users currently on the site
Application ( 'ActiveUsers' ) = 0;
// number of users on the site today
Application ( 'UsersToday' ) = 0;
// number of pages viewed today
Application ( 'PagesToday' ) = 0;
// has the count been restarted today?
Application ( 'NewToday' ) = 1;
// number of headers viewed ever
Application ( 'HeadersViewed' ) = 0;
// number of footers viewed ever
Application ( 'FootersViewed' ) = 0;
// initialize new stuff in utils/Init.asp
Application ( 'BrandNewDay' ) = 1;
// a list of IP addresses that have clicked an ad
Application ( 'ClickFromIP' ) = '';
// counters for rotating banners
Application ( 'CurrentTopBanner' ) = 0;
Application ( 'CurrentSideBanner' ) = 0;
Application ( 'CurrentAffiliate' ) = 0;
// remember todays date
var d = new Date;
Application ( 'Today' ) = d.getDate ( );
// Application.Unlock ( );
}
// ============================================
// this function is called when the server shuts down
// ============================================
function Application_OnEnd ( )
{
}
// ============================================
// this function gets called whenever a new user visits the site. note that
// session variables require the client browser to accept cookies
// ============================================
function Session_OnStart ( )
{
// you must lock the global Application object
// when writing to it - ok to read without lock
Application.Lock ( );
// one more active user
Application ( 'ActiveUsers' )++;
// is it a new day?
var d = new Date;
var nDate = d.getDate ( );
if ( Application ( 'Today' ) == nDate )
{
// same day, so increment users today
Application ( 'UsersToday' )++;
}
else
{
// new day, so restart count
Application ( 'Today' ) = nDate;
Application ( 'UsersToday' ) = 1;
Application ( 'PagesToday' ) = 1;
Application ( 'BrandNewDay' ) = 1;
// and say I havent restarted today
Application ( 'NewToday' ) = 0;
}
Application.Unlock ( );
}
// ============================================
// this function gets called after a user session times out - by default
// after 20 minutes. or you can call Session.Abandon ( )
// ============================================
function Session_OnEnd ( )
{
// you must lock the global Application object
// when writing to it - ok to read without lock
Application.Lock ( );
// one less active user
Application ( 'ActiveUsers' )--;
Application.Unlock ( );
}
</script>
|
Part 4: Displaying visits and page views...
|