Determining what keywords people use in search engines, and what ultimately gets them from the results page to your page, is very useful information. Google offers an impressive array of tools to analyze traffic (Analytics, Webmaster Tools), but sometimes you may want to perform finer-grained analyses of keyword searches. In these cases, it may be useful to write your own keyword decoder.
The best way to do this is to take a look at the Referer header. Reading keywords from a Referer HTTP header is fairly straightforward, and by using associative arrays and a couple regular expressions, it's easy to build an expandable, customizeable keyword decoder:
<?php
// given a referer, check to see if it is a search engine,
// and if so, what keywords were used
function decodeKeywords ($referer) {
$search_phrase = false;
// associative array of search engine urls and the query-string
// parameter that is used for search phrases
$engines = array('google.com'=>'q',
'yahoo.com'=>'p',
'bing.com'=>'q');
// loop through engines and see if referer matches
foreach ($engines as $engine=>$parameter) {
if (preg_match('|' .$engine. '/|i', $referer)) {
// see if a search query is part of the referer's URL
if (preg_match('|[\&\?]' .$parameter. '=([^/^&]+)|', $referer, $matches)) {
// matching search phrase found
$search_phrase = urldecode($matches[1]);
break;
}
}
}
return $search_phrase;
}
// find referer (uses Referer header which CAN be spoofed)
// third_party_only -> when set to true, only returns referer if from another server
function traceReferer ($third_party_only = true) {
if (!empty($_SERVER['HTTP_REFERER'])) {
if ($third_party_only && !strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])) {
// referer is coming from another server
return $_SERVER['HTTP_REFERER'];
} elseif (!$third_party_only) {
return $_SERVER['HTTP_REFERER'];
}
}
return false;
}
?>
Commenting has been closed, but please feel free to contact me