-Log in
-Register (free)


-RSS Feeds
-New MyDesk Module
-Update to Profile

ASP.NET AJAX advice
Visual illustrated complete normilization database
asp application




 Lesson 23: Referral tracking
Lesson 23: Referral tracking

Author: Alexander Haneng
Difficulty: Medium
Requires: ASP
Demo: Demo
Download: Lesson23.zip
Summary:
Learn how to track where your visitors come from.

Intro:
Traffic is the key to a successful website. It doesn't matter if you have the greatest site in the world if nobody visits it. One of the most important things you should do to increase your traffic is analyze where your current traffic is coming from. Is it search engines? Other sites on the same subject? The results are often amazing! Once you locate the sources of your traffic you can start building better ties to those sources to increase your traffic. E.g. if you have a cookware site where you sell pots and pans, maybe you got a small stream of visitors coming to your from a site offering cooking recipes. The recipes site has a little list of cookware sites hidden at the bottom of the page. Then you can contact the webmaster for that site and trade links with him. So if he has your link visibly at the top of the site, you will do the same for him. You now get more visitors and you know they are in your target group since the obviously like to cook. And the webmaster of the other sites gets visitors that might just want to test a new recipe in the pan they just bought from you, enriching your customers web experience.

So how can I find out where they come from?
ASP comes to the rescue once again. :-) Through the ASP Request object you can access a value called ServerVariables("HTTP_REFERER"). This value contains the URL of the page that reffered the visitor to this ASP page. Please note that this URL might be empty. This happens if the user got to your page through a bookmark, clicked on a link in an email or he/she just typed the URL directly into the his/hers address bar.

Big brother sees you!
Let's start with a little ASP script that retrieves the URL and displays it:
<HTML>
<BODY>
You came from: <%=Request.ServerVariables("HTTP_REFERER")%>
</BODY>
</HTML>

See it in action here: Simple.asp
(It will open in a new window, just close the window to return)


Counting referrals
We could store all the URLs we got in a big list, but that wouldn't be very easy to analyze. So what we will do is to make a counter for each URL that refers to our page. That way we can see which source gives us most traffic etc. The concept is the following:
  • Ref.asp is our counting script. We will include that in all the pages we want to monitor.
  • SomePage.asp is just that kind of page, so we have included Ref.asp in this page
  • ViewStats.asp is for our view only. It gives us a list over URLs and their respective count.

ViewStats.asp
Here is what ViewStats.asp might look like:

  Referring URL:   Count:  
  Bookmark or other   10  
  http://www.haneng.com/lessons.asp   8  
  http://www.haneng.com/lessons_9.asp   8  
  http://www.haneng.com/lessons_18.asp   3  
  http://www.haneng.com/default.asp   1  


The code
So now to the coding, let's start with Ref.asp:
(The included comments should explain what it does.)

<%
'Get the reffering URL
RefURL = Request.ServerVariables("HTTP_REFERER")

'We connect to the database using a DSN less connection
DataSource = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("ref.mdb") & ";"
Set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.Open DataSource

'Check if the person was referred by a page or not (e.g. bookmark)
IF Len(RefURL) = 0 THEN

  
'The person was not referred by a page
  InsertIntoID = 1
ELSE

  
'The person was referrd by a page
  'Check if the URL already is in the database
  'if it is get the URL's Ref_ID value
  'if not set InsertIntoID = 0

  SQL_query = "SELECT Ref_ID FROM RefTable WHERE URL = '" & RefURL & "'"
  Set RS = MyConn.Execute(SQL_query)
  IF NOT RS.EOF THEN
    InsertIntoID = TRIM(RS("Ref_ID"))
  ELSE
    InsertIntoID = 0
  END IF
END IF

'If InsertIntoID = 0 then the URL is a new URL
IF InsertIntoID = 0 THEN

  
'Insert new url with count = 1
  SQL_query = "INSERT INTO RefTable (URL, Counter) VALUES ('" & RefURL & "',1)"
  MyConn.Execute(SQL_query)
ELSE

  
'Update existing URL by adding 1 to the count
  SQL_query = "UPDATE RefTable SET Counter = Counter + 1 WHERE Ref_ID =" & InsertIntoID
  MyConn.Execute(SQL_query)
END IF
MyConn.Close
Set MyConn = nothing
%>


Here is the code for the SomePage.asp, very straight forward:
(Just think of it like all the code in Ref.asp is run when anybody access SomePage.asp)

<!--#INCLUDE FILE="Ref.asp"-->
<HTML>
<HEAD>
<TITLE>Some page</TITLE>
</HEAD>
<BODY>
Welcome to this page!
</BODY>
</HTML>


In ViewStats.asp we can analyze the results generated from Ref.asp:
(The included comments should explain what it does.)

<HTML>
<HEAD>
<TITLE>Display referal stats</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#000000" VLINK="#000000" ALINK="#000000">
<CENTER>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=3>
<TR BGCOLOR="#000000">
<TD>&nbsp;</TD>
<TD><FONT FACE="Arial, Helvetica" SIZE=2 COLOR="#FFFFFF"><B>Referring URL:</B></FONT></TD>
<TD>&nbsp;</TD>
<TD><FONT FACE="Arial, Helvetica" SIZE=2 COLOR="#FFFFFF"><B>Count:</B></FONT></TD>
<TD>&nbsp;</TD>
<%
'We connect to the database using a DSN less connection
DataSource = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("ref.mdb") & ";"
Set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.Open DataSource

'We select all the rows ordered by the count with the highest count first
'If you want the lowest count first, just replace DESC with ASC

SQL_query = "SELECT * FROM RefTable ORDER BY Counter DESC"
Set RS = MyConn.Execute(SQL_query)

'Then we loop through every row and writes it out
WHILE NOT RS.EOF

  'This little code lets us get different background color for every other row
  IF BGColor = "#F7F7E7" THEN
    BGColor = "#E7E7D6"
  ELSE
    BGColor = "#F7F7E7"
  END IF
  Db_URL = RS("URL")
  %>
  <TR BGCOLOR="<%=BGColor%>">
  <TD>&nbsp;</TD>
  <TD>
  <FONT FACE="Arial, Helvetica" SIZE=2>
  <A HREF="<%=Db_URL%>"><%=Db_URL%></A>
  </FONT>
  </TD>
  <TD>&nbsp;</TD>
  <TD ALIGN=RIGHT>
  <FONT FACE="Arial, Helvetica" SIZE=2>
  <%=RS("Counter")%>
  </FONT>
  </TD>
  <TD>&nbsp;</TD>
  <%
  RS.MoveNext
WEND
RS.Close
Set RS = nothing
MyConn.Close
Set MyConn = nothing
%>
</TABLE>
</CENTER>
</BODY>
</HTML>


Download the code:
Lesson23.zip

Modifications:
You can also add date to the equation, like a start date and a last date. This will help you see trends in your traffic. Another thing you can do is to add a charting applet to display your stats in an even better way, see our lesson Lesson 18: Turning numbers into graphs and the HanengCharts website for more information.

Where to go next:
Check out the other lessons.


22: Restarting PWS
1: Intro to ASP
| Info |
© Copyright 1997-2008 Alexander Haneng