Using ASP to count users ' stay in the site (1)

Source: Internet
Author: User
Tags count current time insert log modify sessions
Although the commonly used click Registration technology can calculate how many clicks your Web site gets, it's even better to know how long visitors stay 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 completely downloaded.
Go, so that the investment you spend on building and maintaining your site is not 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

...



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.