Tracking users and search engines spiders in coldfusion
If you want to find out what CGI variables you have available, just create a <cfdump var = "#cgi#">
Example output from local host:
struct
AUTH_PASSWORD
[empty string]
AUTH_TYPE
[empty string]
AUTH_USER
[empty string]
CERT_COOKIE
[empty string]
CERT_FLAGS
[empty string]
CERT_ISSUER
[empty string]
CERT_KEYSIZE
[empty string]
CERT_SECRETKEYSIZE
[empty string]
CERT_SERIALNUMBER
[empty string]
CERT_SERVER_ISSUER
[empty string]
CERT_SERVER_SUBJECT
[empty string]
CERT_SUBJECT
[empty string]
CF_TEMPLATE_PATH
C:\Inetpub\wwwroot\ApplayIT\cgi_test.cfm
CONTENT_LENGTH
0
CONTENT_TYPE
[empty string]
GATEWAY_INTERFACE
CGI/1.1
HTTPS
off
HTTPS_KEYSIZE
[empty string]
HTTPS_SECRETKEYSIZE
[empty string]
HTTPS_SERVER_ISSUER
[empty string]
HTTPS_SERVER_SUBJECT
[empty string]
HTTP_ACCEPT
*/*
HTTP_ACCEPT_ENCODING
gzip, deflate
HTTP_ACCEPT_LANGUAGE
en-us
HTTP_CONNECTION
Keep-Alive
HTTP_COOKIE
CFID=11202; CFTOKEN=60629250
HTTP_HOST
localhost
HTTP_REFERER
[empty string]
HTTP_USER_AGENT
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Dreamweaver MX 2004 7.0.2052)
PATH_INFO
/applayit/cgi_test.cfm
PATH_TRANSLATED
c:\inetpub\wwwroot\applayit\cgi_test.cfm
QUERY_STRING
[empty string]
REMOTE_ADDR
127.0.0.1
REMOTE_HOST
127.0.0.1
REMOTE_USER
[empty string]
REQUEST_METHOD
GET
SCRIPT_NAME
/applayit/cgi_test.cfm
SERVER_NAME
localhost
SERVER_PORT
80
SERVER_PORT_SECURE
0
SERVER_PROTOCOL
HTTP/1.1
SERVER_SOFTWARE
Microsoft-IIS/5.1
WEB_SERVER_API
[empty string]
Example code:
<cfoutput>#CGI.SERVER_NAME#</cfoutput>
Output:
localhost
This tutorial will show who is visiting your pages, both users and search engine spiders. You can also see how a visitor goes from page to page on your site. It will also give you information about which page or URL the visitors came from. You will also get the name of the search engines spider and you can see which pages the spider has indexed.
What information we need to capture:
The date and time:
The Now() function returns the current date and time from the server.
If you are using a SQL database, use getDate() instead of Now().
The referring page information:
The CGI variable CGI.HTTP_REFERER will capture referring page information. Using the Cold Fusion LEFT string function strips the extra characters providing the number of characters you choose.
Example: LEFT(CGI.HTTP_REFERER, 128)
Notice:
Some versions of IE 5.5, Opera, and AOL don't even recognize the http_referer command. Netscape 7 doesn't like it at all. Microsoft and Netscape have both realized this problem and aren't offering any resolutions.
The IP address of the client computer:
The CGI variable CGI.REMOTE_ADDR will capture the IP address of the client computer. Example: 216.119.65.128.
The client hostname:
The CGI variable CGI.HTTP_HOST will capture the client hostname server.
The browser type of the visitor:
The CGI variable CGI.HTTP_USER_AGENT will capture the browser type of the visitor.
The page name on your site:
The name of the page is typed in on the pages you want to track.
Example:
For the home page:
<cfmodule template = "admin/act_tracker.cfm" page_track = "Home">
For the product page:
<cfmodule template = "admin/act_tracker.cfm" page_track = "Product">
If you?re using Fusebox, write this code in the fbx_layouts.cfm layout file on the root:
<cfmodule template = "admin/act_tracker.cfm" page_track = "#fusebox.circuit#">
In the examples above we have the tracker.cfm file in the directory admin. Change this if you?re using another path to tracker.cfm.
Creating the database:
Here's an example of an Access database for the user tracking:
Field Name
Data Type
Description
Tracking_ID AutoNumber Primary Key.
Tracking_Date Date/Time The date and time.
Tracking_Referrer Text The referring page information.
Tracking_Client_Address Text The IP address of the client computer.
Tracking_Client_Host Text The client hostname.
Tracking_Client_Browser Text The browser type of the visitor.
Tracking_Resource Text The page name on your site.
Follow this link to see an animated tutorial how to add and manage your data source connections and Data Source Names (DSNs).
Setting global variables in the application file.
The application file:
<!--- The name of your database. --->
<cfset request.main_access_DB = "database_name">
<!--- The database username. --->
<cfset request.dbusername = "your_username">
<!--- The database password. --->
<cfset request.dbpassword = "your_password">
The act_tracker.cfm file:
The purpose of this file is to get the name of the page(s) you want to track, and save information to the database.
If you have access to the CustomTag directory on your server you can save the file there, or you can save the file in another directory and use cfmodule tag to call the act_tracker.cfm file.
This file needs one parameter, the name of the page you want to track. Below are some examples of the code you need to implement on the pages you want to track.
Here's an example using the cfmodule tag.
<cfmodule template="admin/act_tracker.cfm"
page_track = "Home">
In the example the act_tracker.cfm file is located under the admin directory and the page we want to track is 'Home'.
If you want to track a page called 'Products' in the directory products:
<cfmodule template="admin/act_tracker.cfm"
page_track = "Products">
Or if you track your index.cfm file:
<cfmodule template="admin/act_tracker.cfm"
page_track = "The index file">
If you are using FuseBox, put the code in the fbx_layouts.cfm file:
<cfmodule template="admin/act_tracker.cfm"
page_track = "#fusebox.circuit#">
Notice: The #fusebox.circuit# contains the fuseaction which is being processed.
Here?s code for the act_tracker.cfm file:
<!--- The date and time. --->
<cfset Tracking_Date = Now()>
<!--- The referring page information. --->
<cfparam name="cgi.http_referer" type="string"default="">
<!--- The IP address of the client computer. --->
<cfparam name="cgi.remote_addr" type="string"default="">
<!--- The client hostname. --->
<cfparam name="cgi.http_host" type="string"default="">
<!--- The browser type of the visitor. --->
<cfparam name="cgi.http_user_agent" type="string"default="">
<!--- The page name on your site. --->
<cfparam name="attributes.page_track" type="string" default="Page Name">
<!--- Ok we have that data. Insert them into our database. --->
<cfquery name="tracker_insert" datasource="your_datasource" username="your_username" password="your_password">
INSERT INTO tracker_db
(Tracking_Date,
Tracking_Referrer,
Tracking_Client_Address,
Tracking_Client_Host,
Tracking_Client_Browser,
Tracking_Resource)
VALUES
(#Tracking_Date#,
'#left(cgi.http_referer,128)#',
'#left(cgi.remote_addr,128)#',
'#left(cgi.http_host,128)#',
'#left(cgi.http_user_agent,128)#',
'#attributes.page_track#')
</cfquery>
If you?re running SQL delete the
<!--- The date and time. --->
<cfset Tracking_Date = Now()>
and replace the insert code:
INSERT INTO tracker_db
(Tracking_Date,
Tracking_Referrer,
Tracking_Client_Address,
Tracking_Client_Host,
Tracking_Client_Browser,
Tracking_Resource)
VALUES (getdate(),
(getdate(),
'#left(cgi.http_referer,128)#',
'#left(cgi.remote_addr,128)#',
'#left(cgi.http_host,128)#',
'#left(cgi.http_user_agent,128)#',
'#attributes.page_track#')
The dsp_tracker.cfm file:
This file will display the contains in the database. You can specify in the query what you want to display.
Here's an example displaying all the fileds and the last 100 records:
<cfquery name="User_tracking" datasource="# request.main_access_DB#" username="#request.dbusername#" password="#request.dbpassword #">
SELECT * FROM tracker_db
order by Tracking_ID Desc
</cfquery>
<cfoutput query="User_tracking" maxrows="100">
#Tracking_Date# #Tracking_Referrer# #Tracking_Client_Address# #Tracking_Client_Host# #Tracking_Client_Browser# #Tracking_Resource# <br>
</cfoutput>
You can also add an automatic refrech on the dsp_tracker.cfm file, example refrech = 30 sec.
No comments:
Post a Comment