Unfortunately you won't get a referer if the search comes from the experimental google AJAX search or from the new https search.
This is a problem that I will have to deal with in my
WP TID Generator product [wptidgenerator.com], too.
binarymonkey suggested the following code on
the google forum [google.com] to deal with that problem:
/**
* Returns an array with details held in Google Analytics cookie,
* or false if cookie not set.
*
* @returns array|bool
*/
function google_analytics_cookie_data() {
if(isset($_COOKIE['__utmz'])) {
preg_match_all('$[a-z]*=[^\|]*$', $string, $matches);
$ga_data = array();
foreach($matches[0] as $kvpair) {
$bits = explode('=',$kvpair,2);
$key = $bits[0];
$val = $bits[1];
switch($key) {
case 'utmcsr':
$key = "Source";
break;
case 'utmccn':
$key = "Campaign";
break;
case 'utmcmd':
$key = "Medium";
break;
case 'utmcct':
$key = "Referring Page";
break;
case 'utmctr':
$key = "Keywords";
break;
}
$ga_data[$key] = $val;
}
return $ga_data;
} else {
return false;
}
}
It extracts the keywords from the google analytics cookie, which of course means that you have to run google analytics in order to be able to use it.
Mike