Tracking Users, Page Views by the Day Example

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
March 20, 2000
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement
You'll need a database that has the following columns in the table. (TrackID, TrackDate, Sessions)

Then use the following to refer to your stats throughout your site:

 <% = Application("ActiveUsers") %>
<% = Application("TotalSessions") %>
<% = Application("TodaySessions") %>

This is what the global.asa should look like:


<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

On Error Resume Next

Sub Application_OnStart
Application("ActiveUsers") = 0
End Sub

Sub Session_OnStart
Dim SQL
Dim RS
Dim oConn
Dim TotalSessions
Dim TodaySessions
Dim DBDriver
Dim DBPath
Dim DBUser
Dim DBPass

DBDriver = "Microsoft Access Driver (*.mdb)"
DBPath = Server.Mappath("/
ADIRECTORY/TOTHEDATABASE
.mdb")
DBUser = ""
DBPass = ""

Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open "DRIVER={" & DBDriver & "}; DBQ=" & DBPath, DBUser, DBPass

' Get total number of sessions for today
SQL = "Select Sessions From Sessions Where TrackDate = #" & Date() & "#"
Set RS = oConn.Execute(SQL)

' If no recorde for today was found, we need to add a new record
' and if a record was found, we increment the value and store
' it back to the database
If RS.EOF Then
  NewTotal = 1
  SQL = "Insert Into Sessions (TrackDate, Sessions) "
  SQL = SQL & "Values ('" & Date() & "', " & NewTotal & ")"
Else
  NewTotal = CInt(RS("Sessions")) + 1
  SQL = "Update Sessions Set Sessions = " & NewTotal & " "
  SQL = SQL & "Where TrackDate = #" & Date() & "#"
End If

Set RS = Nothing

oConn.Execute(SQL)

' Get total number of session since database created
SQL = "Select Sum(Sessions) As TotalSessions From Sessions"
Set RS = oConn.Execute(SQL)
TotalSessions = RS("TotalSessions")
Set RS = Nothing

' Get total number of sessions for today
SQL = "Select Sessions From Sessions "
SQL = SQL & "Where TrackDate = #" & Date() & "#"
Set RS = oConn.Execute(SQL)
TodaySessions = RS("Sessions")
Set RS = Nothing

oConn.Close
Set oConn = Nothing

Session.Timeout = 5
Session("Start") = Now

' Assign all of my application variables
Application.Lock
  Application("TotalSessions") = TotalSessions
  Application("TodaySessions") = TodaySessions
  Application("ActiveUsers") = Application("ActiveUsers") + 1
Application.Unlock
End Sub

Sub Session_OnEnd
' Remove user who is no longer on site
Application.Lock
  Application("ActiveUsers") = Application("ActiveUsers") - 1
Application.UnLock
End Sub

</SCRIPT>

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials