Another popular web statistic that is valuable to track is how many times an item such as a file or image has been downloaded. This may seem a little more in-depth than our other web statistic scripts, but just take it step by step and you will be fine. 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.
To get started, you need a file to be downloaded and we will place all of our downloadable files in our /Downloads/ directory. For this example, we will say that we want to download this file: http://aspwebpro.com/downloads/document.txt
Second, create a web page called /Downloads.asp within your root directory to list all of your downloadable files. Then, add a link on your web page like this:
<a href="includes/downloadtracker.asp?Title=MyDocument&File=document.txt"> Download My Document Here</a>
Third, create a new table called tblDownloads in your database and with these fields:
ID – Autonumber
fTitle – Text, 255
fFile – Text, 255
fDownloads – Number
fDateDownloaded – Date/Time
Fourth, create a web page called /DownloadTracker.asp, place it in your /includes/ directory, and add the below code to it.
<!--#INCLUDE VIRTUAL="/includes/dbconnection.asp" --> <% 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")) %>
Just be sure to place all of your downloadable files in your /Downloads/ directory. That’s it! Now you can track which items are being downloaded from your website, how often, and how many downloads occur.