| Creating a Quick and
Dirty Hit Counter By Michael Smith |
|||||||||||||||||||||||||
| This article reprinted
with permission from TeraTech
and Miachael Smith. Copyright
1999-2000 TeraTech. For more sign up for FusionAuthority's free weekly CF news alert. |
|||||||||||||||||||||||||
|
OverviewNowadays, having a hit counter on your website is essential -- to find out how many visitors come to your site, who they are, and if possible where they came from. Sometimes you want a hit counter on your page that you can control the format of (unlike the prebuilt free graphics hit counters that are out there). With ColdFusion, it is easy to create a simple hit counter, using a database to store the current count. Our first example only tracks how many times a page has been visited or refreshed. While this counter won't give you detailed statistics, you can easily modify your table to include columns for tracking items such as the visitor's IP address, browser type and referring page. We do this at the end of the article.
Creating Tables in ColdFusionTo get started, you will need a table in your database to store the number of hits. Just for fun, I will show you how to create a table using pure ColdFusion and SQL code. (Note: This table can be created even when you don't have direct access to your database. If you have direct access to your ColdFusion data source, then of course you can just create the table in your database by hand.)The table in question, "hit_counter" (see Listing 1 below), stores the number of hits a web page has received in an integer field called hit_count, thus tracking the number of visits to your web page. FTP the create table example below to your web server and run it by typing the name of the file into your browser. In this case, the file is called "createcounter.cfm". When the CFQUERY successfully creates your table, you will see the message "Table Created!" in your browser window. Listing 1:
Note that you can also delete tables using the DROP TABLE command in SQL. Listing 1.5
Listing 2 below shows how to update and display the results of your counter in your specified web page. In line A, we are checking to see how many "hits" are in our database. This is followed by a few CFIF queries which check the number and update the hit count in your database each time your web page is visited (or refreshed). In line B, we are checking to see if a number exists in your database (i.e., at least "1"). When we created the table (in Listing 1) above, we did not insert a number into the database. The code below is required to allow for the case of there being no records in the counter. If we didn't do this, the hit_count variable would be null rather than 0, which would cause an error when we tried to use it in lines C and D. Using the query "CreateHit" below, your hit counter will start at "1" if it is brand new. Line C displays the actual number of hits on the web page. And Line D will increment the current number of hits in our database to +1 for each new visitor to the web page. Listing 2
Using this simple hit counter will give you a good starting point, but I encourage you to modify the example code and add more information about your visitors to your database. Doing so will allow you to reap the rewards of this simple tracking device. Complex Counting and User TrackingHere is the code for a more complex counter. In this case, we want to save both every page hit and track individual customers. This code is typically placed in your application.cfm file that is run at the front of every page or in a header.cfm include file.Listing 3
To track repeat visitors, we use a cookie called user_id. The code in section A checks to see if the cookie exists and copies the ID into a page variable called user_id. Otherwise, we default user_id to "", so that we won't get undefined variable errors later in the code. Next, in code B, we set
page variables for the user's IP address, machine name, etc., using the
standard ColdFusion CGI variables. If you want to see a complete list of
these variables, turn on debug on your CF server and look at the bottom of
the page.
In addition to the user id, we give many of our users a variable called "custno" that may be passed on URL from a ColdFusion-generated email or via a session variable. In the latter case, we default the page variable "custno" to the session variable in part C. Listing 3 (continued)
Then, in D, we start creating the tracking records. For new users we create a ClientInfo record and a cookie based on the ID using the <CFCOOKIE> tag. For existing users we add a new record to the ClientTrack table for their userid (Section E). This way we can track every page a particular user visits. Listing 3 (continued)
Finally, in sections F, G and H, we set the "custno" session variable and update the ClientInfo database with the "custno", if it was passed in. Listing 3 (continued) Michael
is President of TeraTech,
Inc., a maker of over 30 different tools and libraries for DOS and
Windows programmers. In addition, TeraTech is a Premier Allaire
Value-Added-Reseller and sponsors of the Maryland
ColdFusion User Group. Their TeraTech
ColdCuts site contains a wealth of tips & techniques and is an
invaluable resource to the CF community. Michael can be reached via email
at michael@teratech.com. |