Rotating Ads

Sometimes you want to be able to rotate ads without any fuss.

Sometimes you want to be able to rotate ads without any fuss.

Today I thought I’d look at rotating ads or anything else on your site without resorting to a full blown adserver.

I’ve been a bit busy the last six months with personal life and working on some new sites, so not had much time to post anything on here. But hopefully will get something up next week.

I’ve been working quite a lot on casual games sites with some success so will have some ideas and tips on that.

Anyway, typically you might want to rotate some ads or code in order do some testing or even spread some impressions.

An example of non-advert rotation might be article promotion services like Zergnet or CrowdIgnite, where you might want to avoid users becoming blind to the same articles.

More often it is some advertising network code that you want to rotate. Now you could do this with something like Google’s adserver solution, a plugin for WordPress or something like OpenX.

However, Often I just want something simple and often turn to a little bit of PHP or javascript.

Google, before they had their experiment option in Adsense, gave an example of some simple javascript you could use to do some testing. It is for A-B testing Adsense but you could use it for anything.

<script type=”text/javascript”>
var random_number = Math.random();
if (random_number < .5){
//your first ad unit code goes here
} else {
//your second ad unit code goes here
<script type=”text/javascript”     src=””></script>

I personally quite like this PHP code, I originally got it from here but made a few changes based on what I’d read as it was not randomising properly with the original code.

<?php $splitIt = mt_rand(0,2); echo “<!– $splitIt –>”; ?>
<?php if ($splitIt == 0) { ?>
//you first code here
<?php } ?>

<?php if ($splitIt == 1) { ?>
//you second code here
<?php } ?>

<?php if ($splitIt == 2) { ?>
//you third code here
<?php } ?>

You can increase this number up and I’ve been using it on a site with around 20-60,000 page views per day with no problems on a shared server with Liquid Web.

Anyway, hope it is handy and if you have some better examples or cleaner code please feel free to leave a comment.