Statistics | Site Although the commonly used click Registration technology can calculate the number of clicks on your Web site, but it would be better if you knew how long visitors stayed on the site. If thousands of people clicked and opened your homepage, they would have gone to another site before the beautiful "welcome" graphic was fully downloaded, so that the investment you spent on building and maintaining the site would not have been well rewarded.
There are two good ways to record how much time a user spends on your site. The first is to use sessions based on ASP server, and the second is to keep the client-side cookies. Keep in mind that using sessions will add load to the processing work of the server, but they do provide the most concise approach. It is also important to note that if the client's browser does not support the cookie feature, neither of these methods will work.
ASP Session Technology
Using ASP session requires you to save the current time of this session to the user's session level variable, which will use the Session_OnStart event handle in the Global.asa file under your site or virtual path. Then, in the Session_OnEnd event handle, you can calculate the duration of the session and write the result to the log file or database. The log file is used in the example here:
< script language= "VBScript" runat= "Server" >
Sub Session_OnStart ()
' Save the time ', the session started
Session ("starttime") = Now ()
End Sub
Sub Session_OnEnd ()
' Get the ' time ' the user's last loaded a page
' assumes the default session timeout of minutes
On Error Resume Next
' Set path and name of log file to is created
' Edit to suit your own machine directory layout
' Remember to give ' directory, Write or full
' CONTROL permission to the IUSR_MACHINE account
strFileName = "C:tempvisit_lengths.txt"
Datstarttime = Session ("StartTime")
Datendtime = DateAdd ("n", -20, Now ())
Intminutes = DateDiff ("n", Datstarttime, Datendtime)
If intminutes > 0 Then
' Got a valid time so add it to the log file
Strinfo = "Visit ending at" & Datendtime _
& "lasted for" & Intminutes & "Minute (s)."
' Add user name to the ' log entry string here if required
' Strinfo = strinfo & User Name: & strUserName
Set objfileobject = Server.CreateObject ("Scripting.FileSystemObject")
' Open text file to append data (the ForAppending constant = 8)
Set objfile = Objfileobject.opentextfile (strFileName, 8, True)
objFile.WriteLine Strinfo
Objfile.close
End If
End Sub
</script >
As you can see, when the session ends, we subtract the timeout value from the current time, and if you take into account the amount of time the user takes to load the last page, the value can be slightly smaller. This number is up to you, because the technique does not measure the actual value.
Note that if you use the ASP's Session.Abandon method on any page, you will not get the correct results. Because this method interrupts the session immediately, subtracting the session length from the actual time gives an incorrect access time (sometimes even a negative number). Worse still, in the ASP 2.0 version, this approach is often completely unable to start the Session_OnEnd event.
Use a "Abort server operation" link on some sites to start the Session.Abandon method, but with experience few users will click on it. They just go to another site and let the session interrupt itself.
Here are some of the records we get from the log file:
Visit ending at 6/5/00 1:05:26 AM lasted for 2 minute (s).
Visit ending at 6/5/00 1:06:14 AM lasted for minute (s).
Visit ending at 6/5/00 1:12:18 AM lasted for minute (s).
Visit ending at 6/5/00 1:29:54 AM lasted for 9 minute (s).
If the user has less than 1 minutes to visit (for example, after 1 minutes of their session has not yet been able to load another page), our code will not appear in the list. Subtracting this session's timeout from the entire length of the sessions, you get 0, where our code discards it:
If intminutes > 0 Then?
Of course you can modify the code to suit your needs.
Note: Remember to start the entry of the log file before the session ends. You can't see them right away. If you want to try to see the results more quickly, you can modify the Session.Timeout properties on the page.
Record results in a database
To record the results of a calculation in a database instead of a log file, you can create an appropriate SQL insert declaration to perform to update a database table that you have provided:
...
strSQL = "INSERT into yourtable (UserName, Sessionend," _
& "Sessionlength" VALUES (' & strUserName & "', #" _
& Datendtime & "#," & Intminutes & ")"
Set oconn = Server.CreateObject ("ADODB. Connection ")
oConn.Open "DSN=YOURDSN; Uid=username; Pwd=password; "
Oconn.execute strSQL
Set oconn = Nothing
...
Then you can use the data in any way. You can create an ASP page to read the data and present it to the administrator, or copy it to an electronic worksheet from the database, and then analyze it when you have time.
But keep in mind that using ASP sessions can create problems. In ASP 2.0, sometimes sessions are lost when there is a Global.asa replica in the nested directory under the main application directory. Also, if you use letters in different sizes in URLs, page filenames, and hyperlinks between pages, browsers like navigator treat URLs as case sensitive, so you don't send a special ASP session cookie back, The use of this method is also unreliable.
"Client Side Cookie" technology
It is also easy to use the client side cookies. The code that completes this work can be placed in an ASP #include file, and then inserted into the Web page where the user will definitely visit. Of course, if you want, you can insert it into all the pages. As long as it works properly during user access, the correct results can be given.
After you set the path and log file name, the code defines a subroutine that attaches a value to the log file as in the previous example of "ASP Sessions." If you want, you can replace the code we use to update a database table instead of a log file.
<%
' Measure visit length with cookie
' Set path and name of log file to is created
' Edit to Sui