Here is some code I use as part of a script to highlight search terms on pages
PHP Code:
$from_serp=false;
$engines = array (
array ('google\.', 'p|q', 'Google'),
array ('yahoo\.', 'p|q', 'Yahoo'),
array ('search\.aol\.co','query','AOL'),
array ('search\.dogpile\.com','q','Dogpile')
);
while ((list(, $engine) = each ($engines)) && $from_serp==false) {
$regex="/http:\/\/[^\/]*".$engine[0].".*[?&](".$engine[1].")=([^?&]*)[?&]?/";
if (preg_match($regex,$_SERVER['HTTP_REFERER'],$serp_matches)) {
$from_serp=true;
$serp_source=$engine[2];
$serp_search=$serp_matches[2];
}
}
If you put that at the top of your redirect script it will extract the search engine used ($serp_source) and the query used ($serp_search). You can use those to create your tracking ID. $from_serp will be false if it doesn't recognise the seach engine.
The array at the top contains an array for each search engine. The first value is something unique about the domain name to recognise that search engine - this is used in a regular expression, so full-stops need to be escaped to \.
The second value is a list of url parameter names that may contain the search query - | is the regular expression 'or' character; e.g. google normally uses q, but sometimes uses p.
The third value is just the name of this engine, which the $serp_source variable will be set to.
Using this format makes it a bit simpler to add or modify search engine details.