Tuesday, 29 November 2011

DOWNLOAD TRACKER

Another popular web statistic that is valuable to track is how many times an item has been downloaded. This may seem a little more indepth than our other web statistic scripts, but just take it step by step and you will make it. This little script will track the title of each item that is downloaded and it will track how many times it was downloaded each day.

The first thing you need is a file to be downloaded. For our purposes, we will say that we want to download this file: http://www.aspwebpro.com/downloads/document.txt.

Second, create a web page called downloads.asp within you root directory. Then, add a hyperlink on your web page like this:

< a href="includes/downloadtracker.asp?Title=MyDocument&File=document.txt">Download My Document Here

Third, create a new table called tblDownloads in your Access database and give it these fields:
ID - Autonumber
fTitle - Text, 255
fFile - Text, 255
fDownloads - Number
fDateDownloaded - Date/Time

The fourth step is creating the downloadtracker.asp page. First, create an /includes folder within your root directory. Next, create a blank page and copy the below code into it. Lastly, save the page as downloadtracker.asp and save it within your new /includes directory.



<%
DIM mySQL, strTitle, strFile, strDate, objRS
strTitle = Request("Title")
strFile = Request("File")
strDate = Date()
mySQL = "SELECT * FROM tblDownloads WHERE fTitle = ' " & strTitle & " ' AND fDateDownloaded = ' " & strDate & " ' ;"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn, adOpenKeyset, adLockPessimistic, adCmdText

IF objRS.EOF THEN
objRS.AddNew
objRS("fTitle") = Request("Title")
objRS("fFile") = Request("File")
objRS("fDownloads") = 1
objRS("fDateDownloaded") = Date()
objRS.Update
ELSE
objRS.MoveFirst
DIM intCount
intCount = objRS("fDownloads")
objRS("fDownloads") = intCount + 1
objRS.Update
END IF

objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing

Response.Redirect("/downloads/" & Request("File"))
%>

All "mySQL" statements should appear on one line in your source code to function properly.

Just create a /downloads folder within your root directory and place all of your downloads within it. Believe it or not, that's it! Now you can track which items are being downloaded from your website, how often, and how many downloads occur.

http://www.aspwebpro.com/aspscripts/statistics/downloadtracker.asp

No comments:

Post a Comment