Return to Stevenator's ASP Page

ASP Application: Site Statistics

Click here to see this site's statistics.

Using a database log, you can track a wide variety of important statistics about your users. Many web hosting services offer these kinds of statistics, but they are often not configurable. Using ASP, and your own database tables, you can customize your logs to track anything and everything you want, and display it accordingly. The sample listed above is for this site, and displays information based on user hits. For a private web, username could be tracked, but since this site requires no login, you'll notice that username is 'Unknown' for all entries. The source code involved for this page is listed at the bottom of this page. Aside from the 'usage.asp' page, you will also need to implement the following two code snippets to make it work. The first one is to log connections. The second one logs page hits.

In your global.asa, Session_OnStart, add:
	'Update user database
	set conntemp=server.createobject("adodb.connection")
	cnpath="DBQ=" & server.mappath("/stevesmith/data/timesheet.mdb")
	conntemp.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & cnpath
	sqlString = "INSERT INTO user_log " & _
		" (user_id,log_date,action_code,browser,ip_address) " & _
		" VALUES ('Unknown',now,'L','" & _
		Request.ServerVariables("HTTP_USER_AGENT") & "','" & _
		Request.ServerVariables("REMOTE_HOST") & "')"
	Set logRS=conntemp.execute(sqlString)
	Set logRS=nothing
Include on any page whose hits you wish to track:
	'Update pagelog
	Dim hdrconntemp
	Dim hdrcnpath
	Dim hdrsqlString
	Dim hdrlogRS
	set hdrconntemp=server.createobject("adodb.connection")
	hdrcnpath="DBQ=" & server.mappath("/stevesmith/data/timesheet.mdb")
	hdrconntemp.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & hdrcnpath
	hdrsqlString = "INSERT INTO page_log " & _
		" (page,log_date,action_code,browser,ip_address) " & _
		" VALUES ('" & Request.ServerVariables("SCRIPT_NAME") & "',now,'L','" & _
		Request.ServerVariables("HTTP_USER_AGENT") & "','" & _
		Request.ServerVariables("REMOTE_HOST") & "')"
	Set hdrlogRS= hdrconntemp.execute(hdrsqlString)
	Set hdrlogRS=nothing

Naturally, you'll need to create the tables and the DSN yourself for your own site. The table setup should be fairly obvious from the INSERT statements. I called the tables user_log and page_log. Lastly, you will need to display your statistics somewhere. Here is the source code for usage.asp:

1    <% OPTION EXPLICIT %>
2    <!--#include virtual="stevesmith/top.asp"-->
3    <%
4    ' Initialize Variables
5     Dim MyServer 'Address Server Portion
6     Dim MyPath 'Address Path Portion
7     Dim MySelf 'Full HTTP Address
8     Dim starttime 'Time page began
9     Dim objConnect 'Default Connection Object
10    Dim cnpath 'Connection path
11    Dim objCmd 'Default Command Object
12    Dim objRst 'Default Recordset Object
13    Dim total 'Total logins
14    Dim totalhits 'Total page hits
15    Dim successfullogins 'Number of Successful logins
16    Dim failedlogins 'Number of failed logins
17    Dim IE 'Number of Internet Explorer Hits
18    Dim Net 'Number of Netscape Navigator Hits
19    Dim I 'Loop control variable
20    Dim hourhits 'Hits in last hour
21    Dim hourusers 'Users in last hour
22    Dim dayhits 'Hits in last day
23    Dim dayusers 'Users in last day
24    Dim weekhits 'Hits in last week
25    Dim weekusers 'Users in last week
26    Dim monthhits 'Hits in last month
27    Dim monthusers 'Users in last month
28    Dim tmpDate,tmpDate2 'Date
29   
30   MyServer=Request.ServerVariables("SERVER_NAME")
31   MyPath=Request.ServerVariables("SCRIPT_NAME")
32   MySelf="HTTP://" & MyServer & MyPath
33   %>

34   <html>
35   <head>
36   <title>Usage</title>
37   <meta HTTP-EQUIV="REFRESH" CONTENT="300;<%=Myself%>">
38   </head>
39   <body background="http://<%=Request.ServerVariables("SERVER_NAME")%>/stevesmith/images/whtmarb.jpg">
40   <a HREF="http://<%=Request.ServerVariables("SERVER_NAME")%>/stevesmith/index.asp">
41   <img SRC="http://<%=Request.ServerVariables("SERVER_NAME")%>/stevesmith/images/return.gif"
42   ALIGN="LEFT" ALT="Return to Stevenator's ASP Page" WIDTH="40" HEIGHT="40"></a>
43   
44   <center><h1>Usage (May 1998 to February 1999)</h1></center>
45   <%
46   starttime = now
47   
48    'Establish connection
49    Set objConnect = Server.CreateObject("ADODB.Connection")
50    cnpath="DBQ=" & server.mappath("/stevesmith/data/timesheet.mdb")
51    objConnect.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & cnpath
52   
53    'Create Command object
54    Set objCmd = Server.CreateObject("ADODB.Command")
55    Set objCmd.ActiveConnection=objConnect
56    objCmd.CommandType = adCmdText
57   
58    'Set up objRst
59    Set objRst = Server.CreateObject("ADODB.Recordset")
60    Set objRst.ActiveConnection=objConnect
61    Set objRst.Source = objCmd
62   
63    'Get users/hits in last hour
64    tmpDate = DateAdd("h",-1,now())
65    objCmd.CommandText = "SELECT count(user_id) as cnt FROM user_log " & _
66    " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
67    objRst.Open
68    hourusers = objRst("cnt")
69    objRst.Close
70    objCmd.CommandText = "SELECT count(ip_address) as cnt FROM page_log " & _
71    " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
72    objRst.Open
73    hourhits = objRst("cnt")
74    objRst.Close
75   
76    'Get users/hits in last day
77    tmpDate = DateAdd("d",-1,now())
78    objCmd.CommandText = "SELECT count(user_id) as cnt FROM user_log " & _
79    " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
80    objRst.Open
81    dayusers = objRst("cnt")
82    objRst.Close
83    objCmd.CommandText = "SELECT count(ip_address) as cnt FROM page_log " & _
84    " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
85    objRst.Open
86    dayhits = objRst("cnt")
87    objRst.Close
88   
89    'Get users/hits in last week
90    tmpDate = DateAdd("d",-7,now())
91    objCmd.CommandText = "SELECT count(user_id) as cnt FROM user_log " & _
92    " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
93    objRst.Open
94    weekusers = objRst("cnt")
95    objRst.Close
96    objCmd.CommandText = "SELECT count(ip_address) as cnt FROM page_log " & _
97    " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
98    objRst.Open
99    weekhits = objRst("cnt")
100    objRst.Close
101   
102    'Get users/hits in last month
103    tmpDate = DateAdd("m",-1,now())
104    objCmd.CommandText = "SELECT count(user_id) as cnt FROM user_log " & _
105    " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
106    objRst.Open
107    monthusers = objRst("cnt")
108    objRst.Close
109    objCmd.CommandText = "SELECT count(ip_address) as cnt FROM page_log " & _
110    " WHERE log_date between #" & now & "# AND #"& tmpDate & "#"
111    objRst.Open
112    monthhits = objRst("cnt")
113    objRst.Close
114   
115    objCmd.CommandText = "SELECT count(user_id) as cnt FROM user_log " & _
116    " WHERE action_code = 'L'"
117    objRst.Open
118    total = objRst("cnt")
119    objRst.Close
120    objCmd.CommandText = "SELECT count(*) as cnt" & _
121    " FROM page_log "
122    objRst.Open
123    totalhits = objRst("cnt")
124   %>

125   <center>
126   <b>Your IP: <font COLOR="RED"><%=Session("IP")%></font></b>
127   <table BORDER bgColor="#f3efd0">
128   <tr bgColor="#dfcba0">
129    <th>Item</th>
130    <th>Last Hour</th>
131    <th>Last Day</th>
132    <th>Last Week</th>
133    <th>Last Month</th>
134    <th>Total</th>
135   </tr>
136   <tr>
137   <td align="left"><b>Logins:</b></td>
138   <td><%=hourusers%></td>
139   <td><%=dayusers%></td>
140   <td><%=weekusers%></td>
141   <td><%=monthusers%></td>
142   <td align="CENTER"><font color="red"><%=total%></font></td>
143   </tr>
144   <tr>
145   <td align="left"><b>Page Hits:</b></td>
146   <td><%=hourhits%></td>
147   <td><%=dayhits%></td>
148   <td><%=weekhits%></td>
149   <td><%=monthhits%></td>
150   <td align="CENTER"><font color="red"><%=totalhits%></font></td>
151   </tr>
152   
153   </table>
154   <hr>
155   <!-- Display Total Hits by UserID, with IP Address -->
156   <%
157    objRst.Close
158    objCmd.CommandText = "SELECT count(user_id) as cnt, user_id, ip_address, max(log_date) as ld" & _
159    " FROM user_log WHERE " & _
160    " action_code = 'L' " & _
161    " GROUP BY ip_address,user_id " & _
162    " ORDER BY ip_address "
163    objRst.Open
164   %>

165   <table border>
166   <caption><b>Hits by User</b></caption>
167   <tr bgColor="#dfcba0">
168   <td><b>Name</b></td>
169   <td><b>IP Address</b></td>
170   <td><b># of Logins</b></td>
171   <td><b>Last Login</b></td>
172   </tr>
173   <tr>
174   <td COLSPAN="4"><font COLOR="RED">Chopped for Brevity. Source code available
175   <a HREF="http://www.aspalliance.com/stevesmith/samples/sitestats.asp">here.</a></font></td>
176   </tr>
177   <!--<%' Do While Not objRst.EOF%><tr bgColor="#f3efd0"><td align="CENTER"> <%=objRst("user_id")%> </td><td align="CENTER"> <%=objRst("ip_address")%> </td><td align="CENTER"> <%=objRst("cnt")%> ( <%=fix(CInt(objRst("cnt"))*100/total)%> %) </td><td> <%=objRst("ld")%></td></tr> <%' objRst.MoveNext' Loop%>-->
178   </table><hr>
179   <!-- Display Total Hits by Page, with IP Address -->
180   <%
181    objRst.Close
182    objCmd.CommandText = "SELECT count(page) as cnt, page, max(log_date) as ld" & _
183    " FROM page_log WHERE " & _
184    " action_code = 'L' " & _
185    " GROUP BY page " & _
186    " ORDER BY count(page) DESC "
187    objRst.Open
188   %>

189   <table border>
190   <caption><b>Hits by Page</b></caption>
191   <tr bgColor="#dfcba0">
192   <td><b>Page</b></td>
193   <td><b># of Hits</b></td>
194   <td><b>Last Hit</b></td>
195   </tr>
196    <%
197    Do While Not objRst.EOF
198   %>

199   <tr bgColor="#f3efd0">
200   <td align="left">
201    <%=objRst("page")%>
202    </td>
203   <td align="CENTER">
204    <%=objRst("cnt")%>
205    (
206    <%=fix(CInt(objRst("cnt"))*100/totalhits)%>
207    %) </td>
208   <td>
209    <%=objRst("ld")%>
210   </td>
211   </tr>
212    <%
213    objRst.MoveNext
214    Loop
215   %>

216   
217   </table>
218   
219   <hr>
220   <!-- Display total hits by Browser type -->
221   <%
222    objRst.Close
223    objCmd.CommandText = "SELECT browser, count(*) as cnt " & _
224    " FROM user_log " & _
225    " WHERE browser is not null " & _
226    " GROUP BY browser " & _
227    " ORDER BY count(browser) desc"
228    objRst.Open
229   %>

230   <table border>
231   <caption><b>Hits by
232    Browser</b></caption>
233   <tr bgColor="#dfcba0">
234   <td><b>Browser</b></td>
235   <td><b>Hits</b></td>
236   </tr>
237    <%
238    IE = 0
239    Net = 0
240    Do While Not objRst.EOF
241    If InStr(objRst("browser"),"MSIE") Then
242    IE = IE + CInt(objRst("cnt"))
243    ElseIf InStr(objRst("browser"),"Mozilla") Then
244    Net = Net + CInt(objRst("cnt"))
245    End If
246   %>

247   <tr bgColor="#f3efd0">
248   <td>
249    <%=objRst("browser")%>
250    </td>
251   <td>
252    <%=objRst("cnt")%>
253    (
254    <%=fix(CInt(objRst("cnt"))*100/total)%>
255    %)
256   </td>
257   </tr>
258    <%
259    objRst.MoveNext
260    Loop
261   %>

262   
263   </table>
264   <hr>
265   <!-- Display percent Explorer vs Netscape users -->
266   <table BORDER BGCOLOR="#f3efd0">
267   <caption><b>Hits by
268    Browser</b></caption>
269   <tr bgColor="#dfcba0">
270   <td><b>Netscape</b></td>
271   <td><b>Explorer</b></td>
272   <td><b>Other</b></td>
273   </tr>
274   
275   <tr>
276   <td>
277    <%=Net%>
278    (
279    <%=fix(Net*100/total)%>
280    %)</td>
281   <td>
282    <%=IE%>
283    (
284    <%=fix(IE*100/total)%>
285    %)</td>
286   <td>
287    <%=total-Net-IE%>
288    (
289    <%=fix((total-Net-IE)*100/total)%>
290    %)</td>
291   </tr>
292   
293   </table>
294   <hr>
295   <!-- Display total hits by IP and browser - what browser does each user prefer -->
296   <%
297    objRst.Close
298    objCmd.CommandText = "SELECT ip_address, browser, count(*) as cnt " & _
299    " FROM user_log " & _
300    " WHERE action_code = 'L' " & _
301    " GROUP BY browser, ip_address " & _
302    " ORDER BY ip_address"
303    objRst.Open
304   %>

305   <table border>
306   <caption><b>Browser Hits by
307    User</b></caption>
308   <tr bgColor="#dfcba0">
309   <td><b>User</b></td>
310   <td><b>Browser</b></td>
311   <td><b>Hits</b></td>
312   </tr>
313   <tr>
314   <td COLSPAN="3"><font COLOR="RED">Chopped for Brevity(10 lines shown). Source code available
315   <a HREF="http://www.aspalliance.com/stevesmith/samples/sitestats.asp">here.</a></font></td>
316   </tr>
317    <%
318    I = 0
319    Do While I < 10
320    I = I + 1
321   %>

322   <tr bgColor="#f3efd0">
323   <td><%=objRst("ip_address")%>
324   </td>
325   <td>
326    <%=objRst("browser")%>
327    </td>
328   <td>
329    <%=objRst("cnt")%>
330    (
331    <%=fix(CInt(objRst("cnt"))*100/total)%>
332    %)
333   </td>
334   </tr>
335    <%
336    objRst.MoveNext
337    Loop
338   %>

339   
340   </table>
341   <hr>
342   <!-- Display last ten users and time of use -->
343   <%
344    objRst.Close
345    objCmd.CommandText = "SELECT ip_address, " & _
346    " log_date FROM user_log WHERE " & _
347    " action_code = 'L' " & _
348    " ORDER BY log_date DESC"
349    objRst.Open
350   %>

351   <table border>
352   <caption><b>Last 10
353    Users</b></caption>
354   <tr bgColor="#dfcba0">
355   <td>&nbsp;</td>
356   <td><b>Date</b></td>
357   <td><b>IP Address</b></td>
358   </tr>
359    <%
360    I = 0
361    Do While (Not objRst.EOF) AND (I < 10)
362    I = I + 1
363   %>

364   <tr bgColor="#f3efd0">
365   <td>
366    <%=I%>
367   <td>
368    <%=objRst("log_date")%>
369    </td>
370   <td>
371    <%=objRst("ip_address")%>
372    </td>
373   </tr>
374   <% objRst.MoveNext
375    Loop %>

376   <!-- Display Total Server time to generate this data -->
377   </table>
378   <b>Total load time:
379   <font color="red">
380   <%=datediff("s",starttime,now)%>
381   </font>seconds</b>
382   </center>
383   <!--#INCLUDE VIRTUAL="/stevesmith/bottom.asp"-->
384   </body>
385   </html>

Main | FAQ | Tutorials | Samples | Other | Privacy
Steven Smith, MCSE + Internet, MCSD
Last Modified: 3/23/2000 7:27:33 AM
History: 3/23/2000 7:27:33 AM
Valid HTML 4.0!