.

Start Tracking Incoming Referers Better

We used to rely on our web stats and a ‘How did you hear about us’ field to understand how many people we’re clicking into our sites from where. If this sounds like you, do what we should have done years ago and add some simple referer tracking knowledge.

Here’s some of the really simply code I wrote in php to do so.

Step 1: Create a database table to store each time a visitor arrived at your site from another site

CREATE TABLE `table_name` (
  `date` datetime NOT NULL,
  `fqdn` varchar(127) NOT NULL,   #fqdn=fully qualified domain name
  `adn` varchar(127) NOT NULL,    #adn=absolute domain name
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Step 2: Identify when a visitor has just arrived from an external domain. This code should be run on every page. When it finds a visitor has just come from another domain, we set two cookies. One so we can stop checking them and a second that lasts for 30 days that we’ll refer to when they register.

if (empty($_COOKIE['ref_id'])) {      //this cookie set below.
  $referer = parse_url($_SERVER['HTTP_REFERER']);
  $host = $referer['host']; //extract the FQDN (www.example.com)
  $domain_parts = explode(’.',$host); //break it into pieces
  //if it starts with www remove the ‘www’ piece
  if ($domain_parts[0]==’www’ && count($domain_parts)>2) {
    array_shift($domain_parts);
  }
  $domain = implode(”.”, $domain_parts); //glue it back together
  if ($domain && !strstr($domain,’dogster.com’)
           && !strstr($domain,’catster.com’)) {
    $insert=”INSERT INTO table_name (date,fqdn,adn)
                 VALUES(NOW(),’$host’,'$domain’)”;
    $result=mysql_query($insert, $dbcon);
    //set session cookie
    setcookie(”ref_id”, $referer_site_id);
    //set 30 day cookie
    setcookie(”ref_domain”, $domain, time()+2592000);
  }
}

Step 3: Make a database table that simply stores unique domains. Ours is in MySQL

CREATE TABLE `table_name_2` (
  `refering_domain_id` mediumint(6) unsigned NOT NULL auto_increment,
  `absolute_domain` varchar(30) NOT NULL,
  PRIMARY KEY  (`refering_domain_id`)
)  ENGINE=MyISAM DEFAULT CHARSET=utf8  

Step 4: Whenever a new member registers, look for the 30 day cookie (’ref_domain’). If the cookie exists, see if that domain already exists in table_name_2. If yes, proceed, if not, add it.

Step 5: Create a database table that stores a row for each registrant that came to your domain from a referring domain. We also use this table to store an optional refering campaign so we categorize registrations if they were part of a particular promotion, ad campaign, event, etc.

CREATE TABLE `table_name_3` (
  `user_id` int(8) unsigned NOT NULL default '0',
  `refering_domain_id` mediumint(6) unsigned NOT NULL default '0',
  `refering_campaign` varchar(20) NOT NULL default '',
  UNIQUE KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Step 6: Add the newly joined referred registrants info to a row in ‘table_name_3′

Step 7: Start cranking out views and reports. We look at how many members joined from each domain over a period of time. Then it becomes endless. How many pet profiles did they add. How many friends did they make, forum postings, treats given, messages written. It then becomes quite obvious the quality of the referring domain to go along with the quantity which we roughly knew from our weblogs.

A required task that you can do at any point in the process is aggregate like domains, extract insignificant ones. You may find stripping off subdomains and working with the absolute domains helpful. us.f340.mail.yahoo.com, us.f608.mail.yahoo.com, us.f806.mail.yahoo.com, us.f816.mail.yahoo.com and us.f827.mail.yahoo.com can all be treated as mail.yahoo.com. Or you may want to ignore top-level domains. For example we are interested keeping www.google.com and image.google.com separate, but google.com.qa, google.gr, google.com.ph, google.co.nz, google.fi, google.pt are all treated as google.other to us for now.

For dog’s sake, it’s never too soon to know where your best members are coming from, and seeing over time how they are using your service.

Share it! These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Facebook
  • Google
  • Reddit
  • del.icio.us
  • StumbleUpon
  • NewsVine
  • SphereIt
  • Ma.gnolia
  • Live

Leave a Reply

fields marked with * are required

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