Recording incoming keywords from google searches.
Moderator: General Moderators
Recording incoming keywords from google searches.
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?
Ive seen websites that do this but I am unsure about how they are doing it.
Are any of you familiar with this?
I am trying to record incoming keywords from google searches.
So far I have this code (Im new)
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?
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?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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().
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.
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;
}
?>
Last edited by onion2k on Sat Jan 27, 2007 6:57 am, edited 1 time in total.
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
}my mistake there. I wasn't thinking straight having some trouble with some arrays.
also shouldn't you code be:
also shouldn't you code be:
Code: Select all
$referer = $_SERVER['HTTP_REFERER'];
//instead of
$referer = $_SERVER['HTTP_HOST'];- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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.