雖然通常使用的點擊注冊技術可以計算出你的Web站點得到多少點擊,但是,如果能夠知道訪問者在站點上停留了多長時間就更好了。如果有上千人點擊并打開了你的主頁,但他們卻在漂亮的“歡迎”圖形完全下載之前就已經跑到別的站點 去了,這樣,你所花在建設和維護站點上的投資就沒有得到很好的回報。
有兩種很好的方法用來記錄用戶在你的站點上花費了多少時間。第一個是使用基于ASP服務器的sessions,第二是通過保持客戶機端cookies。要記住,使用sessions將給服務器的處理工作增加負荷,但是它們確實提供了最簡潔的方法。還有一點要注意,那就是如果用戶端的瀏覽器不能支持cookie功能,那么這兩種方法都不能工作。
ASP Session 技術 使用ASP Session 是要求你把這個session 開始的當前時間保存成那個用戶的session 級別變量,這將要用到你的站點或虛擬路徑下的global.asa 文件中的Session_onStart 事件句柄。然后,在Session_onEnd 事件句柄中,你就可以計算出session 持續的時間,并將這個結果寫到日志文件或數據庫中。在這里的例子中使用了日志文件:
< script language="VBScript" runat="server" >
Sub Session_onStart()
'save the time that the session started
Session("StartTime") = Now()
End Sub
Sub Session_onEnd()
'get the time that the user last loaded a page
'assumes the default session timeout of 20 minutes
On Error Resume Next
'set path and name of log file to be created
'edit to suit your own machine directory layout
'remember to give the directory Write or Full
'Control permission for 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 >
你可以看到,當session 結束時,我們從當前時間中減去了session 的timeout的數值,如果考慮到用戶裝載最后一頁時所花費的時間,減去的值可以稍微小一點。這個數量由你去猜,因為用這個技術并不能測出實際值。
注意,如果你在任何頁面中使用了ASP的 Session.Abandon 方法,就不能得到正確的結果。因為這種方法立即中斷session,這樣,從實際時間中減去session長度就會給出一個不正確的訪問時間(有時候甚至是負數)。更糟糕的是,在ASP 2.0版本中,這種方法還經常徹底不能啟動Session_OnEnd事件。
在某些站點上使用一種“中止服務器操作”的鏈接來啟動Session.Abandon方法,但是根據經驗,很少有用戶會去點擊它。他們只是轉到另一個站點,讓session自行中斷。
這是我們從日志文件中得到的一些記錄:
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 47 minute(s).
Visit ending at 6/5/00 1:12:18 AM lasted for 22 minute(s).
Visit ending at 6/5/00 1:29:54 AM lasted for 9 minute(s).
如果用戶訪問的時間少于1分鐘(比如說,他們的session開始后過了1分鐘還沒能裝載另一頁),用我們的代碼就不顯示在列表中。從整個session長度中減去這個session的timeout ,就會得到0,在這一點我們的代碼就將其舍棄:
If intMinutes > 0 Then ?
當然你可以修改代碼以適應自己的需要。
注意:要記住session結束后才開始寫日志文件的條目。你不能立刻看到它們。如果想試著更快地看到結果,可以在頁面上修改Session.Timeout 的屬性。
在數據庫中記錄結果 要將計算的結果記錄數據庫中而不是日志文件中,可以創建一個適當的SQL INSERT聲明,執行它來更新一個你已經提供的數據庫表:
...
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
...
|