Monetize Long Tail Search on Slpogs

Status
Not open for further replies.

Albus Finch

Don't bite your friends
Feb 6, 2008
181
9
0
Awestralia
I've lurked on here for a while and picked up some tips that have been very helpful to my business. I figured it was about time I at least tried to give something back.

I run a bunch of blogs that have new posts added automagically (another story for another time maybe) and they get good traffic but it is of course mostly long tail stuff. I struggled for quite a while on how to make the most of this traffic because the niches my blogs covered and the intent with which visitors are reaching my page with are so diverse. I read a post on xmcp123's blog (Slightly Shady SEO) a while back which provided the inspiration to build this script to monetize my splogs.

I've never considered myself a coder so I assume the code is rough as hell. It works for me though and will helpfully help someone out.

If anyone has any ideas what I could be doing better let me know.

These are the tables and columns that I use for this script:
tracking - visit_id, sub_id, domain, page, referrer, search_term, ip, user_agent, click, convert, offer_id, day, mday, mon, month, year, hour, minute, revenue
offers - offer_niche, offer_type, offer_description, offer_network, offer_payout, offer_url, offer_id
keywords - keyword_niche, keyword, offer_id

You need to populate the keywords table yourself. I fill it with one word keywords and an offer that relates to that keyword. For example, I might have 'diet' as the keyword and the latest diet pill craze as the offer. I was surprised how few entries I needed in this table to direct about 90% of my search traffic to relevant affiliate offers. The other 10% I send to peakclick.

Here is a quick breakdown on what each script below does.

WP Header Script
I whack this at the very top of header.php in all my wordpress themes.
1) Grabs all of the usual visitor data and dumps it into the 'tracking' table.
2) Checks if the referrer is a search engine. If it isn't it does nothing and just displays the blog.
3) If the referrer is a search engine it then grabs the $search_term that the visitor searched for to reach my site.
4) It splits that search term up into separate words, and looks for each of those separate words in 'keywords' table.
5) If it finds a match it then grabs the offer_id from the 'keywords' table for the affiliate offer that relates to that particular keyword. If it can't find a match I set the $offer_id to 0 which means they will be sent to peakclick.
6) We then redirect the user to another domain ($router_domain) which has the Router Script hosted on it along with their $sub_id, $offer_id and $search_term.

Router Script
1) Grabs the $sub_id, $offer_id and $search_term that was passed with the visitor.
2) Gets the $offer_url from the 'offers' table that relates to the $offer_id that was passed.
3) Does a double meta refresh and sends the visitor off to the relevant affiliate offer.

WP Header Script
PHP:
<?php

$router_domain = 'domain-you-bounce-visitors-through.com';
$dbhost = 'your-db-host';
$dbuser = 'your-db-username';
$dbpass = 'your-db-password';
$dbname = 'your-db-name';


//get time and date
putenv("TZ=Australia/Perth");
$my_t=getdate(date("U"));
$weekday = $my_t[weekday];
$mday = $my_t[mday];
$mon = $my_t[mon];
$month = $my_t[month];
$year = $my_t[year];
$hours = $my_t[hours];
$minutes = $my_t[minutes];

//create $sub_id
$sub_id_array = array('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 

'x', 'c', 'v', 'b', 'n', 'm', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 

'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
$sub_id = $sub_id_array[RAND(0, 61)] . $sub_id_array[RAND(0, 61)] . $sub_id_array[RAND(0, 61)] . $sub_id_array[RAND(0, 61)] 

. $sub_id_array[RAND(0, 61)] . $sub_id_array[RAND(0, 61)] . $sub_id_array[RAND(0, 61)] . $sub_id_array[RAND(0, 61)];

//connect to database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

//grab $ip of visitor
$ip=$_SERVER['REMOTE_ADDR'];

//grab $user_agent of visitor
$user_agent=$_SERVER['HTTP_USER_AGENT'];

//grab $referrer of visitor
$referrer=$_SERVER['HTTP_REFERER'];

//grab $domain
$domain = $_SERVER['HTTP_HOST'];

//remove http:// and www. from $domain
$patterns[0] = '|http://|';
$patterns[1] = '|www.|';
$replacements[2] = '';
$replacements[1] = '';
$domain = preg_replace($patterns, $replacements, $domain);

//grab $page
$page = $domain . $_SERVER['REQUEST_URI'];

//grab $search_term
function GetKeyword($str){
    # if search engine is either of MSN, GOOGLE and ASK
    $position = strpos($str,'q=');
    if($position === false){
        # if search engine is yahoo
        $position = strpos($str,'p=');
        if($position === false){
            $keyword = "";
        }
        else{
            $plitarr = split('&',substr($str, $position, strlen($str)));
            $keyword = substr($plitarr[0],2,strlen($plitarr[0]));
        }
    }
    else{
        $plitarr = split('&', substr($str, $position, strlen($str)));
        $keyword = substr($plitarr[0],2,strlen($plitarr[0]));
    }
    return $keyword;
}
if($_SERVER['HTTP_REFERER']){
    $referedby = $_SERVER['HTTP_REFERER'];
$search_term = GetKeyword($referedby);

//if there is a $search_term redirect
if (strlen($search_term) > 0)
{

//replace fullstops in $search_term with a +
//$patterns = '.';
//$replacements = '+';
//$clean_search_term = preg_replace($patterns, $replacements, $search_term);
$clean_search_term = $search_term;

//explode $clean_search_term into $keywords_array
$keywords_array = explode("+", $clean_search_term);

//find a word in $keywords_array that matches keyword in keywords table then grab the corresponding offer_id
$query = "SELECT * FROM keywords";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
if (in_array($row['keyword'], $keywords_array)) {
$offer_id = $row['offer_id'];
}
}


//if no match is found set offer_id to 0
if($offer_id>0){
}
else{
$offer_id=0;
}

//drop all details of visit into tracking table
mysql_select_db($mysql);
mysql_query("INSERT INTO tracking (sub_id, domain, page, referrer, ip, user_agent, day, mday, month, mon, year, hour, 

minute, search_term, click, offer_id) VALUES ('$sub_id', '$domain', '$page', '$referrer', '$ip', '$user_agent', '$day', 

'$mday', '$month', '$mon', '$year', '$hours', '$minutes', '$search_term', '1', '$offer_id')");

//redirect to router.php
header('Location: ' . $router_domain . '?b=' . $offer_id . '&a=' . $sub_id . '&c=' . $search_term);

}

//else just add the details to the tracking table
else{
//drop all details of visit into tracking table
mysql_select_db($mysql);
mysql_query("INSERT INTO tracking (sub_id, domain, page, referrer, ip, user_agent, day, mday, month, mon, year, hour, 

minute, search_term, offer_id) VALUES ('$sub_id', '$domain', '$page', '$referrer', '$ip', '$user_agent', '$day', '$mday', 

'$month', '$mon', '$year', '$hours', '$minutes', '$search_term', '$offer_id')");
}}

//close database
mysql_close($conn);

?>
Router Script

PHP:
<?php

$peakclick_url = 'your-peak-click-url.com';

//get offer_id and sub_id passed in URL
$sub_id = $_GET['a'];
$offer_id = $_GET['b'];
$search_term = $_GET['c'];


//if peakclick
if ($offer_id==0){
$redirect_url = $peakclick_url . $search_term;
}

//else if aff offer
else{
//connect to database
include 'library/config.php';
include 'library/opendb.php';

//get $offer_url from offers table
mysql_select_db($mysql);
$result = mysql_query("SELECT * FROM offers WHERE offer_id='$offer_id'");
$row = mysql_fetch_array($result);
$offer_url = $row[offer_url];

//close db connection
include 'library/closedb.php';
 
$redirect_url = $offer_url . $sub_id;
}

?>

<html>
<head>
<meta http-equiv="refresh" content="2;URL=<?php
echo $redirect_url;
?>">
<script>
url='<?php
echo $redirect_url;
?>';
if(document.images) { top.location.replace(url); }
else { top.location.href=url; }
</script>
</head>
<body></body></html>
 


+rep; thanks for the info. I have been working on something very similar, but got sidetracked on something else for a while. I have always thought that using the header info directly was kinda tacky (Find the best [YOUR SEARCH TERM] here), but using it like this shows a lot of potential.

I may have missed it, but have you thought about putting anything in place for misspellings, besides querying the db for the literal term? I know for single-term searches, it probably wouldn't make a difference, but the longer tails could end up with a large number of combinations to describe what amounts to the same thing.
 
have you thought about putting anything in place for misspellings

Yeah, but I haven't done much more than think about it:)

At the moment I just check my logs every couple of days and update my 'keywords' table manually with anything that I think is worth adding in.

Good to hear it might be of some use to you.
 
Good post. It's not often a lurker contributes like that. :)

It's definitely a very good script. You could build on it to identify what TYPE of keyword each user is searching. Are they trying to BUY [keyword], find cheap [keyword], research keyword? Check out this post: Search Analysis: Converting the Longtail into Sales : Slightly Shady SEO

And yes, the cloaking system is weak. You need to add some IP delivery in there. But if you put this on a bunch of blogspot splogs via some javascript, you don't need to worry about that. :)
 
Status
Not open for further replies.