Where Do They Come From?
Where Do They Come From?

Where Do They Come From?

by Jason Withrow

Ever wonder where your visitors are coming from? Here is a simple example of how you can find out, by logging all of your visitors stats.

First we need a database. I am using Access for my example but you may use any standard ODBC compliant RDBMS you like.

Create a New Database and name it siteLog, then create a new table inside of it named tblSession. Use the screenshot below as a reference.

tblSession Setup

* Note: If using MS SQL Server have the LID field an identity column and uncheck allow nulls.

Create a virtual directory off of the web servers root and name it 'data., place your newly created siteLog.mdb file in it.

Next, we want to create our global.asa file and set it up to log every visitor's information. We will be pulling their data by accessing the Server Variables collection and then updating it to the database.

Let's start by referencing the Browser Component in the Global.asa.

Place this at the very top of your global.asa.




<OBJECT RUNAT=Server SCOPE=Session ID=BC PROGID="MSWC.BrowserType">

</OBJECT>

This set's up a BC object available to use in the session scope.

Now, let's build the framework of the global.asa file.




<script language="vbscript" runat=server>

Sub Session_OnStart



	' ADO Constants

	Const adOpenForwardOnly = 0

	Const adOpenKeyset = 1

	Const adOpenDynamic = 2

	Const adOpenStatic = 3



	' LockTypeEnum Const Values

	Const adLockReadOnly = 1

	Const adLockPessimistic = 2

	Const adLockOptimistic = 3

	Const adLockBatchOptimistic = 4		 



'Define Data to Insert into Log



	'Sniff Browser Info From Header.

	Browser = Trim(Request.ServerVariables("HTTP_USER_AGENT"))



	'Get Users Browser Language Setting

	Language = _

		Trim(Request.ServerVariables("HTTP_ACCEPT_LANGUAGE"))



	' Where did they come from? Oh! That's Where!

	Referer = Trim(Request.ServerVariables("HTTP_REFERER"))

	SessionID = Trim(Session.SessionID)



	'Get User IP

	UserIP = Trim(Request.ServerVariables("REMOTE_HOST"))

	Session("IP") = UserIP



'What Part of the Site did they Hit First?

	FirstPage = Trim(Request.ServerVariables("Script_Name"))



'Resolve OS and Browser details if possible

	OS = bc.platform

	UA = bc.browser

	UAversion = bc.version

	Today = Now()



	'Log week and year number stats for later retrieval

	TWeekNumber = DatePart("ww", Now())

	TYearNumber = DatePart("yyyy", Now())



' Handle exceptions

	If Language = "" Then

		Language = "Unknown"

	End If 



	If Referer = "" Then

		Referer = "None"

	End If



	'Could be useful to persist REFERER info.

	Session("Referer") = Referer



	BrowserCode = "0"

	Set oSRS = Server.CreateObject("ADODB.RecordSet")

	

	'DB Connection Settings

	strSConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _

		& Server.MapPath("data\siteLog.mdb")

	strSQL = "SELECT * FROM tblSession"

	oSRS.CursorType = adOpenKeyset

	oSRS.LockType = adLockOptimistic



On Error Resume Next

	oSRS.Open strSQL, strSConn



	' Update the DB with the Info.

	oSRS.AddNew

		oSRS("Browser") = Browser

		oSRS("Language") = Language

		oSRS("Referer") =  Referer

		oSRS("SessionID") = Session.SessionID

		oSRS("UserIP") = UserIP

		oSRS("Date") = Date()

		oSRS("Time") = Time()

		oSRS("HitPage") = FirstPage

		oSRS("TWeekNumber") = TWeekNumber

		oSRS("TYearNumber") = TYearNumber

		oSRS("BrowserCode") = BrowserCode

		oSRS("OS") = OS

		oSRS("UA") = UA

		oSRS("UAversion") = UAversion

	oSRS.Update



	oSRS.Close

	Set oSRS = Nothing





	IF Err > 0 Then

		Response.Write Err.Number & " " & Err.Description

	End IF

 

End Sub



</script>

Whew! That wasn't so bad was it? Now we have a database that keeps track of all the traffic data from our visitors. So now what?

Let's make some use of that data, and build ourselves a page that can list the stats out. This will be useful for seeing what search engines our visitors are coming from and what the search phrases they are using.

Your finished page will look like the screenshot below.

Sitelog GUI

We will be using Recordset Paging, so if you aren't too familiar with it, brush up with this article. Otherwise, let's get started. Create a new ASP page and name it siteLog.asp.

I am not going to get into the code too much since it is mainly just pulling the data from the database, which is not in the scope of this article. The decode function at the bottom of the page may be useful for other purposes as well, since ASP comes with a urlEncode function, but no corresponding decode function.

You may visit www.originalsweep.com/sitelog.asp to see a working example.




<% Response.Buffer = False 

	

	'Max Records to display per page

	iPageSize = 25

	'Double Quote

	Q = Chr(34)

	'Line Feed

	CR = vbCrLf

	

	'Check for first page

	If Request.QueryString("page") = "" Then

		iPageCurrent = 1

	Else

		'Not 1st page, grab page # From querystring

		iPageCurrent = CInt(Request.QueryString("page"))

	End If

	

	'Do we want to show Browser details? If so Click the checkbox.

	IF lCase(Request("ShowBrowserDetails")) = "on" Then

		strChecked = "Checked"

		ShowBrowserDetails = True

	Else

		strChecked = ""

		ShowBrowserDetails = False

	End IF

%>



<html>

<head>

<title>sitelog</title>

<META Name="ROBOTS" Content="NOINDEX, NoFollow"

	

</head>



<body>

<%  

	Set RS = Server.CreateObject("ADODB.RecordSet")

	Conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _

		& Server.MapPath("data/siteLog.mdb")

	strSQL = "SELECT * FROM tblSession"



	on Error Resume Next

	RS.PageSize = iPageSize

	RS.CacheSize = iPageSize

	RS.CursorLocation = adUSeClient

	RS.Open strSQL, Conn, 3, 3	

	

	'Total # of pages

	iPageCount = RS.PageCount

	

	

IF iPageCount = 0 Then

	Response.Write "No records found!"

Else

	' Move to the selected page

	RS.AbsolutePage = iPageCurrent



	Response.Write "Page <B>" & iPageCurrent & "</B> of " _

		& "&nbsp;<B>" & iPageCount & "</B>&nbsp;&nbsp;&nbsp;" _

		& RS.RecordCount & " Total Records." & vbCrLf



	

%>

<Form Method="POST" Name="frmShowMoreDetails">

<font face="arial" size=1>Show Browser Details?</font>

	<input type="checkBox" name="ShowBrowserDetails"

	<%= strChecked %>

	onClick="document.frmShowMoreDetails.submit()">

</form>



<table border=0 width="100%" cellspacing="4" cellpadding="4">

<tr bgColor="skyBlue">



<td valign="top">Referer</td>

<td valign="top" width="20%"><b>IP Address</b></td>

<td valign="top"><b>Date</b></td>

<td valign="top"><b>Time</b></td>

<td valign="top"><b>First Page</b></td>

<% IF ShowBrowserDetails = True Then %>

	<td valign="top"><b>Browser</b></td>

	<td valign="top"><b>Version</b></td>

<% End IF %>



</tr>



<%  

	'counter to keep track of record #'s displayed

	iRecordsShown = 0

	'Loop thru records until Page Size is reached

	Do While iRecordsShown < iPageSize AND Not RS.EOF

	IF RS("Referer") = "None" Then

		strRef = "<b>Bookmark</b>"

	Else

		strRef = "<a href=" & Q & "#" & Q & " onClick=" & Q _

			& "var j = window.open('" & RS("Referer") _

			& "', 'newWin'); j.focus();" & Q & ">" _

			& urlDecode(RS("Referer")) & "</a>"

	End IF

%>



<tr bgColor="silver">

	<td valign="top" width="20%" WRAP=HARD><%= strRef %></td>

	<td valign="top"><b> <%= RS("UserIP") %></b></td>

	<td valign="top"><b> <%= RS("Date") %></b></td>

	<td valign="top"><b> <%= RS("Time") %></b></td>

	<td valign="top"><b> <%= RS("HitPage") %></b></td>

<% IF ShowBrowserDetails = True Then %>

	<td valign="top"><b> <%= RS("Browser") %></b></td>

	<td valign="top"><b> <%= RS("UAversion") %></b></td>

<% End IF %>

</tr>



<%  

	RS.MoveNext

		'increment counter

		iRecordsShown = iRecordsShown + 1

	Loop

	

End IF

	

	RS.Close

	Set RS = Nothing

	

	Response.Write "</table>"

	

	IF iPageCurrent > 1 Then

	   Response.Write "<input type=button " _

	      & "name=" & Q & "btnPrev" & Q & " " _

	      & "value=" & Q & "<< Previous "  & Q & " " _

	      & "onClick=" & Q & "window.location='SiteLog.asp?page=" _

	      & iPageCurrent - 1 & "'" & Q & ">" & vbCrLf

	End IF

	

	If iPageCurrent < iPageCount  Then

	   Response.Write "<input type=button " _

	      & "name=" & Q & "btnNext" & Q & " " _

	      & "value=" & Q & "Next >>" & Q & " " _

	      & "onClick=" & Q & "window.location='SiteLog.asp?page=" _

	      & iPageCurrent + 1 & "'" & Q & ">" & vbCrLf

	End IF



' Function to help make the referer data a little more readable.

' Replaces urlEncoded data with it's original character

Function urlDecode(str)



	str = Replace(str, "%3F", "?")

	str = Replace(str, "%2F", "/")

	str = Replace(str, "%7C", "|")

	str = Replace(str, "%5C", "\")

	str = Replace(str, "%21", "!")

	str = Replace(str, "%40", "@")

	str = Replace(str, "%23", "#")

	str = Replace(str, "%24", "$")

	str = Replace(str, "%25", "%")

	str = Replace(str, "%5E", "^")

	str = Replace(str, "%26", "&")

	str = Replace(str, "%2A", "*")

	str = Replace(str, "%28", "(")

	str = Replace(str, "%29", ")")

	str = Replace(str, "%7B", "{")

	str = Replace(str, "%7D", "}")

	str = Replace(str, "%3A", ":")

	str = Replace(str, "%2E", ".")

	str = Replace(str, "%2D", "-")

	str = Replace(str, "%5B", "[")

	str = Replace(str, "%5D", "]")

	str = Replace(str, "%2C", ",")

	str = Replace(str, "%3D", "=")

	str = Replace(str, "%2B", "+")

	str = Replace(str, "%2D", "-")

	str = Replace(str, "%5F", "_")

	str = Replace(str, "%7E", "~")

	str = Replace(str, "%60", "`") 

	str = Replace(str, "%27", "'")

	str = Replace(str, "%22", Chr(34))



	urlDecode = str

End Function

	

%>

That's about it, you can now check your siteLog.asp page and click on the links to visit the exact page that your visitors came from. Most of the search engines will even display the search phrase they used in the search box.

Enjoy.

- Jason Withrow

You can download a zip file of the code from here.

Close    To Top
  • Prev Article-Web Design:
  • Next Article-Web Design:
  • Now: Tutorial for Web and Software Design > Web Design > ASP > Web Design Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction