Visual Basic
Python
Web Scripting
Java
Online Tools


Home

What's New

Articles

Code Downloads

Code Snippets

Message boards
Links
Tool Box

Books

Mailing List
Receive free code snippets and notices when this site is updated.

Contact

Tracking Click Throughs

There are times when it is useful to know which sites visitors are clicking though to from your links page or elsewhere on your site.  This article shows you how using ASP and a very simple Access database this can be done.

The schema for this database is shown below.  It simply records a URL and the number of times it has been clicked.

DB Schema

In this table the URL field is the primary key so a URL will only ever be entered once in the table.

In order to record click throughs you create a URL like this:

redirect.asp?url=http://www.developersdomain.com

This runs the ASP code below.  This code opens a connection to the database and tries to insert a new record with the URL given.  If this URL is already in the database an error will be thrown because the URL field is the primary key and can only be entered in the table once.  If there is an error an UPDATE is performed instead to increment the ClickCount.

After the changes to the database are made the user is redirect to the URL.

redirect.asp

<%
  SiteURL = Request.QueryString("URL")
  If Len(SiteURL) > 0 Then
    Set objConn = CreateObject("ADODB.Connection")
    objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
 Server.MapPath("clicks.mdb"))
	
    ' If the URL is already in the database an error will be thrown and
    '  we perform an UPDATE instead of an INSERT.
    on error resume next
    objConn.Execute "INSERT INTO SiteClicks(URL,ClickCount) VALUES('" & _
                     SiteURL & "',1)"
    If Err.Number <> 0 Then
      objConn.Execute "UPDATE SiteClicks SET ClickCount = ClickCount + 1 " & _
                      "WHERE URL='" & SiteURL & "'"
    End If
    objConn.Close
    Set objConn= Nothing
    Response.redirect SiteURL
  End If
%>

And that's all it takes to track the number of people clicking on a link.

Viewing the results

But tracking click throughs is of little use if you have no way to view the results.  You could just download the database when you want to view it or create a ASP file to display the results.  The simple ASP file below will display the number of clicks to a URL.  The username and password are stored in the ASP file for simplicity.

clickview.asp

<HTML>
<HEAD><TITLE>Clicks</TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF">
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=0 Align="Center">
<%
  If Request.Form("Username") = "MyUsername" And _
      Request.Form("Password") = "MyPassword" Then
    Set objConn = CreateObject("ADODB.Connection")
    objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
 Server.MapPath("clicks.mdb"))
    Set rsURLs = objConn.Execute("SiteClicks")
    Response.write "<TR><TD>URL</TD><TD>Click Count</TD></TR>"
    Do While Not rsUrls.EOF
	Response.write "<TR><TD>" & rsURLs("URL") & "</TD>"
	Response.write "<TD align=right>" & rsURLs("ClickCount") & "</TD></TR>"
        rsURLS.MoveNext
    Loop
    rsURLs.Close
    Set rsURLs = Nothing
    objConn.Close
    Set objConn= Nothing
  Else %>
<TR><TD>
<H1>Click Counts</H1>
  <FORM action="clickview.asp" method="Post">
    Username: <INPUT type=text name="Username"><BR>
    Password: <INPUT type=password name="Password"><BR>
  <INPUT type=Submit value="Log In">
  </FORM>
</TD></TR>
<%  End If %>
</TABLE>
</BODY>
</HTML>

To track click throughs download the code and database and place redirect.asp?URL= before each URL you want to track e.g.

redirect.asp?URL=http://www.someplaceonthenet.com

Download Click Tracker Code

Tell a friend about this article