Is it possible to capture search term from Google search?
Asked Answered
W

8

18

This may be a stupid question, but is it possible to capture what a user typed into a Google search box, so that this can then be used to generate a dynamic page on the landing page on my Web site?

For example, let's say someone searches Google for "hot dog", and my site comes up as one of the search result links. If the user clicks the link that directs them to my Web site, is it possible for me to somehow know or capture the "hot dog" text from the Google search box, so that I can call a script that searches my local database for content related to hot dogs, and then display that? It seems totally impossible to me, but I don't really know. Thanks.

Wier answered 2/6, 2009 at 19:33 Comment(4)
Be aware that the referrer can be faked or blocked entirely at the whims of the client.Freezer
just a note, since you're new: don't be so quick to accept the first answer. just cause it has 1 upvote, doesn't mean its rightUnwieldy
@TStamper: Good point and thanks for the tip. I was overly excited at how quickly I got an answer.Wier
Due to late-2011 Google security changes, this is no longer possible when the search was performed by a signed-in Google user: googleblog.blogspot.com/2011/10/making-search-more-secure.html and analytics.blogspot.com/2011/10/…Heifetz
H
13

Yes, it is possible. See HTTP header Referer. The Referer header will contain URL of Google search result page.

When user clicks a link on a Google search result page, the browser will make a request to your site with this kind of HTTP header:

Referer: http://www.google.fi/search?hl=en&q=http+header+referer&btnG=Google-search&meta=&aq=f&oq=

Just parse URL from request header, the search term used by user will be in q -parameter. Search term used in above example is "http header referer".

Same kind of approach usually works also for other search engines, they just have different kind of URL in Referer header.

This answer shows how to implement this in PHP.


Referer header is only available with HTTP 1.1, but that covers just about any somewhat modern browser. Browser may also forge Referer header or the header might be missing altogether, so do not make too serious desicions based on Referer header.

Habitation answered 2/6, 2009 at 19:37 Comment(2)
Excepting that, to have your page turn up in search results it already needs to have relevant contentPaulin
Due to late-2011 Google security changes, this is no longer possible when the search was performed by a signed-in Google user: googleblog.blogspot.com/2011/10/making-search-more-secure.html and analytics.blogspot.com/2011/10/…Heifetz
T
24

I'd do it like this

$referringPage = parse_url( $_SERVER['HTTP_REFERER'] );
if ( stristr( $referringPage['host'], 'google.' ) )
{
  parse_str( $referringPage['query'], $queryVars );
  echo $queryVars['q']; // This is the search term used
}
Thai answered 2/6, 2009 at 19:43 Comment(5)
+1 for making code compatible with international google domain names (google.fi, google.de etc) and with google image search.Zamir
thanks! I figure there's still a potential here for a false positive - somebody with google.domain.com - but this was only an example. You could tighten it up by also checking the value of $referringPage['path'] and, after that, a simple isset() check on $queryVars['q']Thai
Instead of stristr() use preg_match("/google\.[a-z]{2,4}$/i", $referringPage['host']) to prevent the google.domain.com case mentioned aboveJowett
That's a good attempt Swish, and I actually though of that, but then I remembered this: google.co.uk ;)Thai
@Peter Bailey, I don't think that false positives matter too much. Browser can fake this header anyways.Zamir
I
17

This is an old question and the answer has changed since the original question was asked and answered. As of October 2011 Google is encrypting this referral information for anyone who is logged into a Google account: http://googleblog.blogspot.com/2011/10/making-search-more-secure.html

For users not logged into Google, the search keywords are still found in the referral URL and the answers above still apply. However, for authenticated Google users, there is no way to for a website to see their search keywords.

However, by creating dedicated landing pages it might still be possible to make an intelligent guess. (Visitors to the "Dignified charcoal sketches of Jabba the Hutt" page are probably...well, insane.)

Iden answered 6/4, 2012 at 23:7 Comment(0)
H
13

Yes, it is possible. See HTTP header Referer. The Referer header will contain URL of Google search result page.

When user clicks a link on a Google search result page, the browser will make a request to your site with this kind of HTTP header:

Referer: http://www.google.fi/search?hl=en&q=http+header+referer&btnG=Google-search&meta=&aq=f&oq=

Just parse URL from request header, the search term used by user will be in q -parameter. Search term used in above example is "http header referer".

Same kind of approach usually works also for other search engines, they just have different kind of URL in Referer header.

This answer shows how to implement this in PHP.


Referer header is only available with HTTP 1.1, but that covers just about any somewhat modern browser. Browser may also forge Referer header or the header might be missing altogether, so do not make too serious desicions based on Referer header.

Habitation answered 2/6, 2009 at 19:37 Comment(2)
Excepting that, to have your page turn up in search results it already needs to have relevant contentPaulin
Due to late-2011 Google security changes, this is no longer possible when the search was performed by a signed-in Google user: googleblog.blogspot.com/2011/10/making-search-more-secure.html and analytics.blogspot.com/2011/10/…Heifetz
E
6

This is an old question but I found out that google no more gives out the query term because it by default redirects every user to https which will not give you the "q"parameter. Unless someone manually enters the google url with http (http://google.com) and then searches, there is no way as of now to get the "q" parameter.

Entryway answered 15/11, 2012 at 11:23 Comment(1)
This is the answer I was afraid I'd find. I'd noticed my search tracking now has none of the search terms and this must be why.Cracksman
A
1

Yes, it comes in the url:

http://www.google.com/search?hl=es&q=hot+dog&lr=&aq=f&oq=

here is an example:

Google sends many visitors to your site, if you want to get the keywords they used to come to your site, maybe to impress them by displaying it back on the page, or just to store the keyword in a database, here's the PHP code I use :

// take the referer
$thereferer = strtolower($_SERVER['HTTP_REFERER']);
// see if it comes from google
if (strpos($thereferer,"google")) {
    // delete all before q=
    $a = substr($thereferer, strpos($thereferer,"q="));     
    // delete q=
    $a = substr($a,2);
    // delete all FROM the next & onwards
    if (strpos($a,"&")) {
        $a = substr($a, 0,strpos($a,"&"));
    }   
    // we have the results.
    $mygooglekeyword = urldecode($a);
}

and we can use <?= $mygooglekeywords ?> when we want to output the
keywords.
Apologetic answered 2/6, 2009 at 19:37 Comment(2)
Wow, thanks everyone. I am one humbled newbie. I posted my question FOUR MINUTES ago and got back three helpful replies. Stack Overflow rules!Wier
There are much better ways to do it than this. PHP has a built in function to parse a url. parse_url, parse_str makes this 3 lines.Lavish
C
0

You can grab the referring URL and grab the search term from the query string. The search will be in the query as "q=searchTerm" where searchTerm is the text you want.

Charily answered 2/6, 2009 at 19:38 Comment(0)
P
0

Same thing, but with some error handling

<?php
if (@$_SERVER['HTTP_REFERER']) {
    $referringPage = parse_url($_SERVER['HTTP_REFERER']);
    if (stristr($referringPage['host'], 'google.')) {
        parse_str( $referringPage['query'], $queryVars );
        $google = $queryVars['q'];
        $google = str_replace("+"," ",$google); }
    else { $google = false; }}
else { $google = false; }

if ($google) { echo "You searched for ".$google." at Google then came here!"; }
else { echo "You didn't come here from Google"; }
?>
Pronunciamento answered 20/9, 2011 at 0:23 Comment(0)
P
0

Sorry, a little more
Adds support for Bing, Yahoo and Altavista

<?php
if (@$_SERVER['HTTP_REFERER']) {
    $referringPage = parse_url($_SERVER['HTTP_REFERER']);
    if (stristr($referringPage['host'], 'google.')
        || stristr($referringPage['host'], 'bing.')
        || stristr($referringPage['host'], 'yahoo.')) {
            parse_str( $referringPage['query'], $queryVars );
            if (stristr($referringPage['host'], 'google.')
                || stristr($referringPage['host'], 'bing.')) { $search = $queryVars['q']; }
                        else if (stristr($referringPage['host'], 'yahoo.')) { $search =     $queryVars['p']; }
                        else { $search = false; }
            if ($search) { $search = str_replace("+"," ",$search); }}
            else { $search = false; }}
else { $search = false; }
if ($search) { echo "You're in the right place for ".$search; }
?>
Pronunciamento answered 20/9, 2011 at 0:44 Comment(1)
NB: altavista seems to be owned by yahoo, when you search with it you get redirected to yahoo search page, so the search for yahoo. in the header covers both search enginesPronunciamento

© 2022 - 2024 — McMap. All rights reserved.