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']);
 
?>


11 thoughts on “Grabbing the referrer search engine keywords for a site

  1. 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.

  2. 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!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>