Page 1 of 1
Recording incoming keywords from google searches.
Posted: Fri Jan 26, 2007 10:32 pm
by thud
I need to record incoming keywords from google searches.
Ive seen websites that do this but I am unsure about how they are doing it.
Are any of you familiar with this?
Posted: Fri Jan 26, 2007 10:41 pm
by William
$_SERVER['HTTP_REFERER'] ?
Posted: Fri Jan 26, 2007 11:11 pm
by thud
I am trying to record incoming keywords from google searches.
So far I have this code (Im new)
Code: Select all
<?php
echo $_SERVER['HTTP_REFERER'];
?>
For example, this is where I would grab the keyword from -
http://search.yahoo.com/search?p=KEYWOR ... -8&fr=moz2
Can someone provide me with the code to do this?
Posted: Fri Jan 26, 2007 11:16 pm
by superdezign
HTTP_REFERER is an attribute defined by which website people come to yours from. Printing it to your screen will, at most, show you where you came from, which is useless information to you.
You could use cookies or sessions and log your visitor's referers into a log file or, preferably, a database. You say you're new so I don't expect you to automatically know how to do this, but I hope I've at least got you thinking on the right track.
I'd help you with the code, but I'm busy with a client's site. I'm sure someone will respond with either more detail or a better, more sensible method.
Posted: Fri Jan 26, 2007 11:19 pm
by superdezign
By the way, when attempting to test it, make sure you're aware that there doesn't always have to be a referrer. You must come from elsewhere (I believe it doesn't matter whether or not you arrive through a link).
Posted: Sat Jan 27, 2007 5:04 am
by onion2k
Ignore superdezign, he clearly didn't understand what you're after. "You could use cookies or sessions and log your visitor's referers into a log file" just doesn't make any sense. Cookies and sessions have nothing whatsoever to do with logging someone's referer.
To get the search information out of the refering string from the users request header you'll need to use a couple of functions that PHP provides for breaking down URLs, namely parse_url() and parse_str().
Code: Select all
<?php
$referer = $_SERVER['HTTP_REFERER'];
$arrRefererComponents = parse_url($referer);
parse_str($arrRefererComponents['query'],$arrGetVars);
switch($arrRefererComponents['host']) {
case "yahoo.com":
case "www.yahoo.com":
case "search.yahoo.com":
echo $arrGetVars['p'];
break;
case "google.co.uk":
case "www.google.co.uk":
case "google.com":
case "www.google.com":
echo $arrGetVars['q'];
break;
}
?>
You might want to expand that code to work more flexibly with the host names. Perhaps you could write something to strip the TLD (.com, .co.uk, etc) off so you don't have to worry about that part of it. Obviously you'll also need to write something to log the search term rather than echoing it too.
Posted: Sat Jan 27, 2007 5:36 am
by louie35
You could use the stristr function if you don't care about the TLD
Code: Select all
$referer = $_SERVER['HTTP_REFERER'];
if(stristr($referer, 'google.')) {
$query = $_GET['q'];
//do you insert or something else here
}elseif(stristr($referer, 'yahoo.')){
$query = $_GET['p'];
//do you insert or something else here
}
Posted: Sat Jan 27, 2007 6:24 am
by onion2k
You can't use $_GET for variables in the referer.
Posted: Sat Jan 27, 2007 6:50 am
by louie35
my mistake there. I wasn't thinking straight having some trouble with some arrays.
also shouldn't you code be:
Code: Select all
$referer = $_SERVER['HTTP_REFERER'];
//instead of
$referer = $_SERVER['HTTP_HOST'];
Posted: Sat Jan 27, 2007 6:58 am
by onion2k
I have no idea what you mean.

Posted: Sat Jan 27, 2007 7:04 am
by louie35
No hard feelings.
I know how it feels.

Posted: Sat Jan 27, 2007 2:44 pm
by thud
Wonderful.
Now my only difficulty is recording the links... I'd like to write/read the links to/from a flat txt file on the server.
Posted: Sat Jan 27, 2007 2:50 pm
by louie35
if you have a database wouldn't be easier to add them to a table there, by using an INSERT statement?
Posted: Sat Jan 27, 2007 3:36 pm
by onion2k
thud wrote:Now my only difficulty is recording the links... I'd like to write/read the links to/from a flat txt file on the server.
Try to work it out for yourself.
Posted: Sat Jan 27, 2007 3:45 pm
by superdezign
Bleh.
Yeah, I was thinking you take the entire referer URL, put it into a session variable, and store the value into a database. I was thinking "session" because I thought it'd be the only way to prevent you from getting the referer more than once per user.
However, it does make sense to just check for if it came from a search engine first. I wasn't even thinking that much. I was emulating what webstats do for you... Yeah, ignore superdezign. He didn't know what he was talking about.
