Grabbing the referrer search engine keywords for a site


A couple of weeks back I had to write a solution for a client to track the referrer search engine from where the user came to his sites contact page, without using Google Analytics. If a user was to fill the contact form on the website, the referring search engine name and the keyword for which it was refereed was to be emailed along with the contact information. The following is a solution for the same.

To get the referrer information we need to use the $_SERVER['HTTP_REFERER'] variable and store it in a session so that it is not overwritten as the user browses the site before he submits the contact information. If the users comes to the website from a search engine he could land on any page, so we need to add some code to capture the referrer url on each page of the site. We only need to save the referrer if the referrer is from outside the current domain.

The following is the code that need to be added to each web page on the website. The referrer url will then be stored in a session and carried along as the user browses the site.

<?php
/* saveReferer.php */
 
$my_site = "http://www.codediesel.com";
 
/* If the referrer is from outside the domain, store the url in a session */
if(!substr($_SERVER['HTTP_REFERER'], 0, strlen($my_site)) !== $my_site) {
    if(!isset($_SESSION['my_referer'])) {
        $_SESSION['my_referer'] = $_SERVER['HTTP_REFERER'];
    }
} 
?>

You can now include the above file in all your web pages.

<?php
 
session_start();
require_once('saveReferer.php');
.
.
?>

To grab the search keyword from the referrer url we can use the get_keyword function given below.

<?php
 
function get_keyword($referer)
{
    $search_phrase = '';
    $engines = array('dmoz'     => 'q=',
                     'aol'      => 'q=',
                     'ask'      => 'q=',
                     'google'   => 'q=',
                     'bing'     => 'q=',
                     'hotbot'   => 'q=',
                     'teoma'    => 'q=',
                     'yahoo'    => 'p=',
                     'altavista'=> 'p=',
                     'lycos'    => 'query=',
                     'kanoodle' => 'query='
                     );
 
    foreach($engines as $engine => $query_param) {
        // Check if the referer is a search engine from our list.
        // Also check if the query parameter is valid.
        if (strpos($referer, $engine.".") !==  false && 
            strpos($referer, $query_param) !==  false) {
 
            // Grab the keyword from the referer url
            $referer .= "&";
            $pattern = "/[?&]{$query_param}(.*?)&/si";
            preg_match($pattern, $referer, $matches);
            $search_phrase = urldecode($matches[1]);
            return array($engine, $search_phrase);
        }   
    }
    return;
}
 
?>

The following is a sample contact page which sends the referrer search engine name and the keyword to the website admin.

<?php
 
/* contact.php */
session_start();
 
$engine = '';
$search_keyword = '';
 
$data = get_keyword($_SESSION['my_referer']);
 
if($data) {
   $engine = $data[0];
   $search_keyword = $data[1];
}
 
$message  = "SOME MESSAGE HERE\r\n";
$message .= "Engine : $engine \n";
$message .= "Keyword: $search_keyword \n";
 
$headers .= "From: test@test.com ". "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail("admin@test.com", "Test Email", $message, $headers);
 
/* Make sure you reset the referrer session variable */
unset($_SESSION['my_referer']);
 
?>

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

11 Responses

1

Sameer Borate Blog: Wir packen die Referrer Suchmaschinen-Keywords für eine Website | PHP Boutique

October 18th, 2011 at 2:04 pm

[...] Blog heute Sameer Borate hat einen neuen Beitrag mit einem handlichen bisschen Code, den Sie auf find die Schlüsselwörter aus einer Suchmaschine Verweis , um mit Tracking, wie die Besucher auf Ihre Website kommen helfen. Ein paar Wochen zuvor hatte ich [...]

2

Anonymous Coward

October 19th, 2011 at 8:54 am

This won’t be useful for too much longer:
http://googleblog.blogspot.com/2011/10/making-search-more-secure.html
and
After they move all logged in users to https you’ll only get referer search terms per thousand visitors

3

Managed Forex Accounts

November 6th, 2011 at 12:09 pm

i really get a good info from your blog, thank so much

4

Allan Slogan

December 1st, 2011 at 1:31 pm

I too enjoyed this article, as I’ve been trying to figure out how to properly dig for more referrer code when people come to the site. In particular, I’d like to discover how to monitor traffic that is too fast. In other words, a particluar IP will cruise dozens of pages in about 20 seconds. Then I could know that it’s a bot snagging information and cut it off.

5

Sudarsono

August 31st, 2012 at 4:07 am

Can i grap https:// ?

sameer

August 31st, 2012 at 4:48 am

It should.

7

Martin

October 3rd, 2012 at 1:55 am

Hi Does this still work?

8

Facebook Cerca

October 12th, 2012 at 3:18 am

Probabely google changed the url referrarl structure :-(

9

prestashop.com

October 29th, 2012 at 3:10 am

Thanks

10

Petras

January 24th, 2013 at 3:15 am

That’s what I was looking for a long time. Thank you very much.

11

David

January 31st, 2013 at 6:43 am

You will NOT be able to grab the search terms used when a user is logged into his Google Account. So that means that you wont be able to grab the search term when the Google search page has https:// in the URL. It’s secured, so forget it!

Your thoughts