HOW TO: Create Intelligent Blog Ads

If you want to squeeze every last drop of revenue potential out of your blog without annoying your loyal visitors then you need to be a bit clever about the way you display advertising. I’m going to show you some simple PHP template code you can use to add some ad’ logic to your template.

First thing we would want to do is not show adsense advertising to registered users. I would still show CPM adverts as you need the page views to gain advertisers and get paid, and can slot those ads into areas where they will not be an annoyance.

Next you might want to show additional Google ad units for visitors arriving from search engines. As we have discussed before, these visitors are least likely to become regular readers and most likely to click your ads – two good reasons to at least try it, eh?

Most people will want to use channels for their adsense and chitika ads, also chitika allows you to use keywords. We can add these pieces of info dynamically. For the channels my example uses a scenario you might want to consider if you are building a multi author blog. In my case I want the bloggers who contribute to earn from their own work so I am going to use channels to determine their payment, the channel name is their own name and what the blog earns goes to them.

We should also look at how you can display different ads depending on the date, seeing as right now as I post this we are in the final throws of the Christmas season!

Lastly, for this post, we will perform some logic based on age of post.

I am going to use WordPress and Drupal for these examples but you should be able to translate to any PHP based blog with some tweaks. Let’s get on with the code.

Template Tags

Each blogging system has a set of template tags that provide bare minimum set of information that you can use for changing the display of your template at run time. If there is not the information you need already available then you need to start hacking code.

Take a look at the WordPress list and the Drupal PHPTemplate. As you can see they don’t provide much in the way of fancy information, just the basics. That is fine, just means we have to work a little if we want to do something really advanced.

Ad display code

The basic Chitika code is as follows




As you can see I have modified the default to provide variables for “channel” and “keyword”. We can provide the channel for Adsense in a similar fashion.

Registered User?

In Drupal to see if the person viewing your blog is logged in we can simply check the $user->uid property. In WordPress we have a $user_ID variable

If we want to show something or not in WordPress based on user status we can simply use something like the following.

0)
{
echo “Logged in”;
}
else
{
echo “Not logged in”;
}
?>

Here is the full Drupal code

uid < 1) { showAd($channel,$keyword); } } function showAd($channel, $keyword) { ?>




As you can see I have created a function for displaying Chitika which takes channel and keyword as parameters. I only call this function if the person is not a logged in user (user id would be less than 1).

While displaying the HTML this way is not the most efficient it does mean you can paste any HTML there instead of my Chitika code so you can replace banners or adsense if you wish.

Search Users

So how about displaying ads only to visitors from search engines? To do this we simply use the PHP environment variable $HTTP_REFERER

Obviously you would need to check for Yahoo!, MSN, etc too.

Set the channel

We can use anything we like for the channel within reason, some people set the channel to where on the page the ad unit was displayed, sometimes based on topic etc. I set the channel to the author using $channel = $node->name; in Drupal. In WordPress it would be the_author('login',false) which says “return the login of the author of this post, do not output as html”.

Date Sensitive Ads

Around Christmas time you might want to push gift advertising, while in summer you might want to show vacation or “back to school” type products. All you have to do is check the month or date, for example..

Post Age

If you want to only show advertising (or show more advertising) on old posts, you can work out the days since the post was created and determine what you want to do.

In Drupal we can work out days since post creation using

In WordPress the full code is

post_date)) / (60 * 60 * 24);

if ($days > 30)
{
showAd($channel,$keyword);
}
else
{
echo “Posted $days days ago”;
};

?>

Summary

This is just a taste of some of the things you can do. I will post a follow-up with more complicated code (such as database reading and writing) soon so do let me know if there are things you would like me to cover in part two.

7 thoughts on “HOW TO: Create Intelligent Blog Ads

  1. I am using norton antivirus and it doesn’t look like my referrer is blocked? I know some people do block referrers but my own stats tell me it is a minority

  2. Norton blocks the google, yahoo and msn HTTP_REFERER by default doesn’t it?

    It seems relying on HTTP_REFERER to display ads to people who are blocking HTTP_REFERER and not even knowing they are blocking it (90% of people with Norton or other anti-virus software) is a little risky, wouldn’t you say?

    Any ideas on getting past the default block?

Comments are closed.