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
Router Script
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);
?>
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>