Top Search Queries for Drupal Sites
Posted: Mon 11/13/2006 by ramiroAny webmaster who cares about SEO monitors the server logs to get information about the searches that drive traffic to their sites. There are many useful statistics packages that help performing these tasks.
To learn even more about your visitor's interests it is also good to know what they type into the search boxes on your web site.
For SEO-Expert-Blog.com I am using the fantastic Drupal CMS. The built-in watchdog module is used to log and track system events including user searches.
Putting the PHP code snippet below into a user defined Drupal block you will get a list of the searches performed by your visitors. Search strings are ordered by frequency and link to the search query, so you can easily check which results are presented to your visitors.
The code snippet is tested with Drupal 4.7.x.
<?php
/**
* Creates a list of the latest searches visitors performed
* on your site ordered by top search queries.
*
*/
$query = "SELECT message from {watchdog} WHERE type = 'search'";
$result = db_query($query);
while ($row = db_fetch_object($result)) {
preg_match('/<em>(.*)<\/em>/', $row->message, $match);
$string = strtolower($match[1]);
$search_strings[$string] += 1;
}
if(count($search_strings)) {
arsort($search_strings);
foreach ($search_strings as $key => $val) {
$query = preg_replace('/\s+/', '+', $key);
$search_link = l($key,'search/node/'.$query,$key);
$search_link .= " ($val)";
$items[] = $search_link;
}
return theme('item_list',$items,'','ol');
}
?>

You can see the top search terms in:
/admin/logs/searchin Drupal 5.Post new comment