%
'''TODO for you! Configuration:
Function GetStatDir()
GetStatDir = Server.MapPath("/ctrfiles/stats/")
If Right(GetStatDir,1) <> "/" Or Right(GetStatDir,1) <> "\" Then
GetStatDir = GetStatDir & "\"
End If
End Function
''Some constants for file handling
Const File_OpenForReading = 1, File_OpenForWriting = 2, File_OpenForAppending = 8
Function GetExistingDates()
'
'Walk through all files
Dim nCount
Dim fs, f, f1, fc, s
nCount = 0
Set oRet = Server.CreateObject("Scripting.Dictionary")
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(GetStatDir())
Set fc = f.Files
For Each f1 in fc
If Left( f1.name,7) = "PVPAGE_" Then
nCount = nCount + 1
oRet.Add Mid( f1.name, 8, 8 ), ""
End If
Next
Set GetExistingDates = oRet
Set fs = Nothing
End Function
Function DeleteFilesFromDate( sDate )
'
' Now we should delete all files from that date
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
fs.DeleteFile GetStatDir() & "PVPAGE_" & sDate & ".log"
fs.DeleteFile GetStatDir() & "PVSUM_" & sDate & ".log"
fs.DeleteFile GetStatDir() & "REF_" & sDate & ".log"
fs.DeleteFile GetStatDir() & "VI_" & sDate & ".log"
Set fs = Nothing
End Function
Function GetFormattedDate( dDate )
Dim sMonth, sDay
sMonth = DatePart( "m", dDate)
If Len(sMonth) = 1 Then
sMonth = "0" & sMonth
End If
sDay = DatePart( "d", dDate)
If Len(sDay) = 1 Then
sDay = "0" & sDay
End If
GetFormattedDate = DatePart( "yyyy", dDate ) & sMonth & sDay
End Function
Sub LogVisit()
'
'1. What should the file be called
Dim sFileName, oFile, nCount
sFileName = GetStatDir() & "VI_" & GetFormattedDate( Now() ) & ".log"
Set oFile = File_OpenExistingOrCreate( sFileName, File_OpenForReading )
If oFile.AtEndOfStream = True Then
nCount = 0
Else
nCount = oFile.ReadLine()
If NOT IsNumeric(nCount) Then
nCount = Cint(nCount)
End if
End If
oFile.Close
Set oFile = Nothing
nCount = nCount + 1
Set oFile = File_OpenExistingOrCreate( sFileName, File_OpenForWriting )
oFile.WriteLine nCount
oFile.Close
Set oFile = Nothing
' Response.Write sFileName
End Sub
Sub LogPageView()
'1. What should the file be called
Dim sFileName, oFile, nCount
sFileName = GetStatDir() & "PVSUM_" & GetFormattedDate( Now() ) & ".log"
Set oFile = File_OpenExistingOrCreate( sFileName, File_OpenForReading )
If oFile.AtEndOfStream = True Then
nCount = 0
Else
nCount = oFile.ReadLine()
End If
oFile.Close
Set oFile = Nothing
nCount = nCount + 1
Set oFile = File_OpenExistingOrCreate( sFileName, File_OpenForWriting )
oFile.WriteLine nCount
oFile.Close
Set oFile = Nothing
' Now one for each pageview...
sFileName = GetStatDir() & "PVPAGE_" & GetFormattedDate( Now() ) & ".log"
Set oFile = File_OpenExistingOrCreate( sFileName, File_OpenForAppending )
oFile.WriteLine Request.ServerVariables("SCRIPT_NAME")
oFile.Close
Set oFile = Nothing
End Sub
Sub LogExternalReferer()
Dim sFileName, oFile
sFileName = GetStatDir() & "REF_" & GetFormattedDate( Now() ) & ".log"
Set oFile = File_OpenExistingOrCreate( sFileName, File_OpenForAppending )
oFile.WriteLine Request.ServerVariables("HTTP_REFERER")
oFile.Close
Set oFile = Nothing
End Sub
Sub StatLog()
'1. Find out if
' Where are we coming from?
Dim sReferer, sHost
sHost = lcase("http://" & Request.ServerVariables("SERVER_NAME"))
sReferer = lcase(Request.ServerVariables("HTTP_REFERER"))
If sReferer = "" Then
LogVisit
Else
If Left( sReferer, Len(sHost) ) <> sHost Then
LogVisit
If sReferer <> "" Then
LogExternalReferer
End If
Else
End If
End If
LogPageView
End Sub
Function File_OpenExistingOrCreate( strPath, nAccess )
' strPath = the path to file
' nAccess should be one of the constants above
On Error Resume Next
Dim objFileObj
Dim objFile
Set objFileObj = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFileObj.OpenTextFile( strPath, nAccess, True, False )
If Err = 0 Then
Set File_OpenExistingOrCreate = objFile
Else
Set File_OpenExistingOrCreate = Nothing
End If
Set objFileObj = Nothing
End Function
Function GetVisitorsCount( dDate1, dDate2 )
If dDate1 = "" Then
dDate1 = "1900-01-01"
End If
If dDate2 = "" Then
dDate2 = "2020-01-01"
End If
Dim sName1, sName2
sName1 = "VI_" & GetFormattedDate( dDate1 ) & ".log"
sName2 = "VI_" & GetFormattedDate( dDate2 ) & ".log"
'Walk through all files
Dim fs, f, f1, fc, s, nCount
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(GetStatDir())
Set fc = f.Files
nCount = 0
GetVisitorsCount = 0
For Each f1 in fc
If Left( f1.name,3) = "VI_" Then
If f1.name >= sName1 AND f1.name <= sName2 Then
'Loop through it
nCount = CInt(GetCount( f1.name ))
GetVisitorsCount = GetVisitorsCount + nCount
End If
End If
Next
Set fs = Nothing
End Function
Sub AddPageViews( sFileName, ByRef oDictionary )
Dim oFile, sText
Set oFile = File_OpenExistingOrCreate( GetStatDir() & sFileName , File_OpenForReading )
While Not oFile.AtEndOfStream
sText = oFile.ReadLine()
If sText <> "" Then
If oDictionary.Exists( sText ) Then
oDictionary.Item( sText ) = oDictionary.Item( sText ) + 1
Else
oDictionary.Add sText, 1
End If
End If
Wend
Set oFile = Nothing
End Sub
Function GetPageViewCount2( dDate1, dDate2 )
Dim oRet
Set oRet = Server.CreateObject("Scripting.Dictionary")
If dDate1 = "" Then
dDate1 = "1900-01-01"
End If
If dDate2 = "" Then
dDate2 = "2020-01-01"
End If
Dim sName1, sName2
sName1 = "PVPAGE_" & GetFormattedDate( dDate1 ) & ".log"
sName2 = "PVPAGE_" & GetFormattedDate( dDate2 ) & ".log"
'Walk through all files
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(GetStatDir())
Set fc = f.Files
For Each f1 in fc
If Left( f1.name,7) = "PVPAGE_" Then
If f1.name >= sName1 AND f1.name <= sName2 Then
AddPageViews f1.name, oRet
End If
End If
Next
Set GetPageViewCount2 = oRet
Set oRet = Nothing
Set fs = Nothing
End Function
Function GetRefererCount( dDate1, dDate2 )
Dim oRet
Set oRet = Server.CreateObject("Scripting.Dictionary")
If dDate1 = "" Then
dDate1 = "1900-01-01"
End If
If dDate2 = "" Then
dDate2 = "2020-01-01"
End If
Dim sName1, sName2
sName1 = "REF_" & GetFormattedDate( dDate1 ) & ".log"
sName2 = "REF_" & GetFormattedDate( dDate2 ) & ".log"
'Walk through all files
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(GetStatDir())
Set fc = f.Files
For Each f1 in fc
If Left( f1.name,4) = "REF_" Then
If f1.name >= sName1 AND f1.name <= sName2 Then
AddPageViews f1.name, oRet
End If
End If
Next
Set GetRefererCount = oRet
Set oRet = Nothing
Set fs = Nothing
End Function
Function GetCount( sFile )
Dim sFileName, oFile, nCount, sText
sFileName = GetStatDir() & sFile
Set oFile = File_OpenExistingOrCreate( sFileName, File_OpenForReading )
If oFile.AtEndOfStream Then
nCount = 0
Else
nCount = oFile.ReadLine()
End If
oFile.Close
Set oFile = Nothing
GetCount = nCount
End Function
''''Date routines
Function Date_GetYear( dDate )
Date_GetYear = DatePart( "yyyy", dDate )
End Function
Function Date_GetMonthNo( dDate )
Date_GetMonthNo = DatePart( "m", dDate )
End Function
Function Date_GetDayNo( dDate )
Date_GetDayNo = DatePart( "d", dDate )
End Function
Function Date_GetMonthName( nMonthNo )
Select Case nMonthNo
Case 1
Date_GetMonthName = "Jan"
Case 2
Date_GetMonthName = "Feb"
Case 3
Date_GetMonthName = "Mar"
Case 4
Date_GetMonthName = "Apr"
Case 5
Date_GetMonthName = "May"
Case 6
Date_GetMonthName = "Jun"
Case 7
Date_GetMonthName = "Jul"
Case 8
Date_GetMonthName = "Aug"
Case 9
Date_GetMonthName = "Sep"
Case 10
Date_GetMonthName = "Oct"
Case 11
Date_GetMonthName = "Nov"
Case 12
Date_GetMonthName = "Dec"
End Select
End Function
Sub Date_FillBoxWithYears( nFirst, nLast, nDefault )
Dim nYear
If nDefault = "" Then
nDefault = Date_GetYear( Now() )
End If
For nYear = nFirst To nLast
Response.Write ""
Next
End Sub
Sub Date_FillBoxWithDays( nDefault )
Dim nDay
If nDefault = "" Then
nDefault = Date_GetDayNo( Now() )
End If
For nDay = 1 To 31
Response.Write ""
Next
End Sub
Sub Date_FillBoxWithMonths( nDefault, fUseNumbersAsValue )
Dim nMonth
If nDefault = "" Then
nDefault = Date_GetMonthNo( Now() )
End If
For nMonth = 1 To 12
Response.Write ""
Next
End Sub
Function DomainFromReq( sText )
'
Dim nIndex
If LCase(Left( sText, 7 )) = "http://" Then
sText = Mid( sText, 8 )
End If
If LCase(Left( sText, 8 )) = "https://" Then
sText = Mid( sText, 9 )
End If
nIndex = InStr( sText, "/")
If nIndex > 0 Then
sText = Left( sText, nIndex - 1 )
End If
DomainFromReq = sText
End Function
Function DomainRefFromPageRef( oDict )
Dim oRet, oKey, sPage, sDomain
Set oRet = Server.CreateObject("Scripting.Dictionary")
'Now get the domain name from all
For Each oKey In oDict.Keys
sPage = oKey
If LCase(Left( oKey, 7 )) <> "http://" And LCase(Left( oKey, 8 )) <> "http://s" Then
sDomain = "Unknown"
Else
sDomain = DomainFromReq( sPage )
End If
If oRet.Exists( sDomain ) Then
oRet.Item( sDomain ) = oRet.Item( sDomain ) + oDict.Item(oKey)
Else
oRet.Add sDomain, oDict.Item(oKey)
End If
Next
Set DomainRefFromPageRef = oRet
Set oRet = Nothing
End Function
%>