Welcome Guest, Not a member yet? Register   Sign In
How to display the search terms people used to get on my site
#6

[eluser]Jay Logan[/eluser]
I store all my hits in my database - including referral URL which is sometimes a Google search query. When I retrieve the hits, I run each referrer URL through this helper I made that checks to see if it is from a search engine. Here is the code.

Code:
function clean_referrer($referrer) {

    if ($referrer) {

        preg_match("/[\&\?]q=([^&]*)/", $referrer, $matches);

        if (isset($matches[1])) {

            $search_query = rawurldecode($matches[1]);
            $search_query = str_replace("+", " ", $search_query);
            return "Search engine query for ".$search_query;

        } else {

            return $referrer;

        }

    } else {

        return $referrer;

    }

}

Technically, this doesn't check to see if the referrer is specifically from Google. Rather just if the user came from a URL that has "?q=" is the URL (which is generally a search engine). You could edit the code to specify Google. I don't know regex that much though.

If you don't store the hits in the DB, then you could just use a $SERVER['HTTP REFERER'] variable.


Messages In This Thread
How to display the search terms people used to get on my site - by El Forum - 09-12-2009, 01:34 PM



Theme © iAndrew 2016 - Forum software by © MyBB