Home > Tools > CMS > Drupal > Snippets > Top Search Queries for Drupal Sites

Top Search Queries for Drupal Sites

Any 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('/&lt;em>(.*)&lt;\/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');
}
?>
This sort of functionality (and better) will be available out of the box in Drupal 5.0. It will rank search terms according to popularity/frequency. Just a small preview of what is the come. ;-)
I have installed the Beta and think there are great improvements in usability and lots of exciting new features for developers.
Does this not work with 4.7? Had some trouble on a 4.7 site? Thanks.
I use this on a Drupal 4.7 site and it works. What kind of problems do you have?
Have you tried using it on Drupal 5.1? I just upgraded my Drupal to 5.1 and it has some changes to modules. Elvis Internet Marketing Watch
so, where is this functionality in drupal 5.0? it's not in there, is it? maybe 6.0? 7.0? guess I'll write a module to add it.

You can see the top search terms in: /admin/logs/search in Drupal 5.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <br> <img> <h2> <h3> <h4> <h5>

More information about formatting options

Loading