Logging Queries To Your Site



After adding a search box to your website, you'll probably want to know how the box is being used by visitors. For instance, what queries are people making? At what times of day (or night)? From what domains are they arriving? Answers to questions like these will help you do a better job designing your site (or at the very least help satisfy your curiosity!).

To do this, you need a log file that captures the query that is sent to the Ultraseek search engine. We will show two alternatives for logging queries typed into your search text box.

  1. PHP script opens a log file, writes your query text, then sends the query to search engine.
  2. CGI PERL script creates writes to a log file and sends query to search engine

PHP scripts for Logging Queries

Log using PHP example
questionlog.txt
default.html
log-search-query.php
PERL scripts for Logging Queries


Good luck!
#path to your PERL interpreter
#!/usr/bin/perl

##############################################################
# Make sure that these PERL modules are installed on your web
# server
##############################################################
use LWP::Simple;
use Time::localtime;
use CGI;

##############################################################
# HTML page header
##############################################################
$req = new CGI;
print $req->header();

##############################################################
# pass the query to Ultraseek and assign result to scratch
##############################################################
$query = $ENV{QUERY_STRING};
# for Chattanooga
$_ = get("http://search.tennessee.edu/utc/query.html?"."$query");
# for Institute of Agriculture
$_ = get("http://search.tennessee.edu/utia/query.html?"."$query");
# for Institute of Public Service
$_ = get("http://search.tennessee.edu/utips/query.html?"."$query");
# for Knoxville
$_ = get("http://search.tennessee.edu/utk/query.html?"."$query");
# for Martin
$_ = get("http://search.tennessee.edu/utm/query.html?"."$query");
# for Memphis
$_ = get("http://search.tennessee.edu/utmem/query.html?"."$query");
# for UTSI
$_ = get("http://search.tennessee.edu/utsi/query.html?"."$query");
# for UT system-wide
$_ = get("http://search.tennessee.edu/query.html?"."$query");

# VERY IMPORTANT!! YOU MUST ALTER THE PATH BELOW TO POINT TO WHERE
#THIS SCRIPT RESIDES ON YOUR SERVER!!
s/query.html/http:\/\/www.your.machine.edu\/path\/to\/this\/script/g;

# use full urls for images and stylesheet
s/\/images/http:\/\/search.tennessee.edu\/images/g;
s/images\/UT_plain.gif/http:\/\/search.tennessee.edu\/images\/UT_plain.gif/g;
s/default.css/http:\/\/search.tennessee.edu\/default.css/g;
s/<div class=search>/<\/center>\n<div class=search>/g;

# replace the default search box with a customized one
# user can pack a customized search box into the $query_box
$query_box = "<p>\n<br>\n<form name=seek method=GET \

# VERY IMPORTANT!! YOU MUST ALTER THE PATH BELOW TO POINT TO WHERE
THIS SCRIPT RESIDES ON YOUR SERVER!!
    action=http://www.your.machine.edu/path/to/this/script \
    <table cellspacing=0 cellpadding=6 border=1 width=100% class=query>\
    <tr>\
    <td>\
    <input type=hidden name=col value=\"utk\"> \
    <input type=text name=qt size=40> \
    <input type=hidden name=qp value=\"+site:www.cs.utk.edu\"> \
    <br><input type=submit value=\"Search\">\n \
    <a href=\"http://www.ultraseek.com/\"><img alt=\"\" \
    src=\"http://search.tennessee.edu/images/littlelogo.gif\"
    width=126 height=30 border=0 align=right><\/a>\
    <\/td>\
    <\/tr>\
    <\/table>\n<\/form>";
s/\<form(.|\n)+\/form>/$query_box/;

##############################################################
#print out the edited result which is an HTML document
##############################################################
print;

##############################################################
# logging capability
##############################################################
@variables = split(/&/, $query);
$flag = 1;
while($flag == 1) {
    $temp = shift(@variables);

    # retrieve the qt variable
    if ($temp =~ /^qt=/) {
        $qt = $temp;
        @qta = split(/=/, $qt);
        $qt = @qta[1];
        $flag = 0;
    }
}
open (QTHANDLE, ">> logs/qt_log.txt") or die;

# output the qt variable together with remote host name and
# the current time to the log file
print QTHANDLE "$qt\t$ENV{REMOTE_HOST}\t", ctime(), "\n";
close(QTHANDLE);

 


Example Logging for queries

Here we search the OIT site and log the query



<html>
<form name="seek" method="GET" action="http://bmw.ws.utk.edu/cgi-bin/querylog.pl">
<input type="hidden" name="col" value="utk">
<input type="text" size="30" maxlength="35" name="qt"><br><br>
<input type="hidden" name="qp" value="+site:oit.utk.edu">
<input type="submit" value="Search">
</form>
</body>
</html>