February 12th, 2007

How to track referrers to your website

When running marketing campaigns, it's great to know where your visitors and customers are coming from, so that you can track conversion based on source, and determine ROI for various advertising link campaigns that you're running.

This code will capture the visitor's referring URL when they first enter your site. This could be a query string from a major search engine, it could be the URL of a website linking to yours, and you could even force it to be something specific. For example, if you want to track how many visitors are clicking on your email signature, you can use the URL www.yourdomain.com?source=email.

Then in any form or conversion point on your site, have a hidden field where you echo the $_SESSION['source'] variable. Then when you get the lead information, you can see where that lead came from.

<?php
//set the source of this visitor
if (!isset($_SESSION['source'])) {
        if((isset($_GET['source']))&&($_GET['source']!=")) { // ?source= in querystring, such as email sig
                $_SESSION['source'] = $_GET['source'];
        } else {
                if($_SERVER['HTTP_REFERER']) { // referred from another web page
                        $_SESSION['source'] = $_SERVER['HTTP_REFERER'];
                } else {
                        $_SESSION['source'] = 'direct'; // typed yourdomain.com directly into browser
                }
        }
}
?>

One Response to ' How to track referrers to your website '

Subscribe to comments with RSS or TrackBack to ' How to track referrers to your website '.

  1. Paul G. said,

    on September 14th, 2008 at 8:49 pm

    Justin,

    This is great info. Can you point me to a website where this tracking method is implemented? If I can see an example maybe I can implement this. My programming knowledge is limited.

    Thx.

    Paul G.

Leave a reply

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>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word