How To Change Your Content Based On Traffic

In yesterday’s post, we spoke about why you would want to change your content based on traffic intent. In today’s post, I’m going to give you a basic framework about how to do it. This post is written as a Thesis Tutorial, because working with Thesis is just easier (see my Thesis review), but you can easily adapt the code to any website or theme.

OK. Like all Thesis customizations, we’re going to need to open up the custom_functions.php file. In this example, we are going to offer different social buttons based on where the user came from. If they came from a social site, we’ll show the interactive buttons with counts/votes. If they came from anyplace else, we’re going to show static graphic icon buttons.

For the website I’m using I put the buttons under the author byline, so my code will go in that function (if you are going to copy and paste, wait until the end for the final code so you get all the semicolons and parentheses).

//this is author byline
function uauthor_byline()

I’m going to make sure the buttons only appear on single pages so I’ll need the following bit of code:

if (is_single())

Ok now we get to the programming. We’ll need two variables and one array. The variables will hold the referring URL and a flag that tells whether that condition is true, and the array will hold the list of sites we are checking against.

$CUsocial = false; // this is the flag for social traffic
$CUref = $_SERVER['HTTP_REFERER']; //this is the referring URL

$CUsocialar = array('reddit', 'stumbleupon', 'digg', 'twitter', 'facebook', 'delicious'); // this is an array of social sites

So what we want to do next is take the list of social sites and see if any of them are in the referer. If they are, we set the flag to true.

//check all the social sites
$i=0;while ($i<=count($CUsocialar)){
if(stristr($CUref, $CUsocialar[$i])) {
$CUsocial = true;
}
$i++;

}

Now, if you’re a stickler, you could make the case that, if Twitter was in the filename and not the domain, we could get a false positive, and you would be correct. I just don’t think that’s going to happen often enough to be a real concern. OK so now we know whether the referring site is any of the social websites we want to trap for. If it is, the $CUsocial variable will be ‘true’ so we’ll need this bit of code:

if ($CUsocial){
//code for active social websites goes here
}else{
//code for default condition goes here
?>}

The code above has a placeholder for the buttons you can get from places like digg, stumbleupon and facebook. Since there are so many tutorials and instructions from the original websites, I just left placeholders. Here’s the full code:

function uauthor_byline() {
if (is_single()){
$CUsocial = false; // this is the flag for social traffic
$CUref = $_SERVER['HTTP_REFERER']; //this is the referring URL
$CUsocialar = array('reddit', 'stumbleupon', 'digg', 'twitter', 'facebook', 'delicious'); // this is an array of social sites
//check all the social sites
$i=0;while ($i<=count($CUsocialar)){ if(stristr($CUref, $CUsocialar[$i])) { $CUsocial = true; } $i++; }
if ($CUsocial){

//code for active social websites goes here
}else{
//code for default condition goes here
}
}

The following is just a starting point and can be re-used and expanded upon. For example, if you want to trap for search engines, here’s the extra code you would need:

$CUsearch = false; //this is the flag for search traffic
$CUsearchar = array('google', 'yahoo', 'bing'); //this is an array of search sites

//check all the search sites
$i=0;while ($i<=count($CUsocialar)){
if(stristr($CUref, $CUsearchar[$i])) {
$CUsearch = true;
}
$i++;
}

But you can use the code all over the site content, header, sidebar, etc. You can combine it with date based triggers, or there are many, many different possibilities, if you spend time playing with the code.

Comments

By Michael Gray

Michael Gray is SEO specialist and publishes a Search Engine Industry blog at www.Wolf-Howl.com. He has over 10 years experience in website development and internet marketing, helping both small and large companies increase their search engine visibility, traffic, and sales.

Leave a comment