How to make AuctionAd$ dynamic with PHP

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

imagesauction-ads.jpg

I had decided to try out AuctionAd$ as an alternative to Google AdSense because Google wasn’t giving me decent enough adverts to display. When I signed up for AuctionAd$ I quickly realised that although I had more control over the adverts by specifying the keywords it also meant I was the one responsible for making it work.

Before you read the rest of the article (you may not be interested in the coding side) the end result is that I have built an AuctionAd$ display system that adjusts the keywords it uses based upon the visitor clicking adverts. This means that your visitors will automatically adjust what is displayed for them by clicking or not clicking on the adverts shown. You still have control over the keywords you use but, instead of a supplying a single keyword, you supply a list that it initially selects from and then adapts based on your visitors interaction.

If you wish to just download it and install (you do at this point need some technical knowledge) please scroll to the bottom of the article for instructions.

How it was done

As with all my programming exercises I reversed the problem and made it my computer’s problem. What if I could see which adverts were being clicked on and then dynamically choose which ones were getting better click-through-ratios? AuctionAd$, like Google AdSense, uses a lump of JavaScript that creates an iFrame to then stick the advert in. This means you cannot use ‘onclick’ on a DIV around the iFrame because those clicks are not sent to the outer frame. But I did discover that you can use ‘onmouseover’.

Here is what I wrapped the advert in. (Just a quick note to say that I do not think that this breaches any terms, as I am not adjusting the advert I am just adding the ability to see if it’s being clicked.)

<div onmouseover="dynamicadvert('<?php echo $da_keyword ?>')" 
style="width: 160pxheight:600px">
{ADVERT GOES HERE}
</div>

The ‘onmouseover’ calls a function which in turns calls a AJAX function to tell the server what has happened. I am using prototype purely for ease of use.

Below is the JavaScript code.

var mouseover = false;
function dynamicadvert(keyword)
{
if(mouseover == false) {
	new Ajax.Request('/index.php', {
				method: 'get',
			parameters: {da_keyword: keyword}
 						});
        		mouseover = true;
	}
}

This is just a very simple AJAX request (made even easier by prototype) plus a global var to stop the request being made more than once.

What next?

So I now I had a way to record if my readers were putting their cursors over the advert. Now although it doesn’t mean they clicked any of them, it does at least show interest in them (good enough for me).

The basic plan was to then have a list of keywords that I would then keep track of to see how successful they were in getting attention. To do this I built a simple SQL database with two columns.

keyword (a varchar)
weight (a integer)

Every time someone triggered an ‘onmouseover’ for a particular advert that a keyword that had generated it would get an extra number of weighting points. At the same time all the other keywords would lose some points.

Here is the code.

$result = $this->dbQuery("SELECT count(*) as count FROM keywords");
$row = mysql_fetch_assoc($result);
$count = $row['count'];
 
if($count > 1) {
	$lower = intval(FIXEDPOINT/($count - 1));
	$this->dbQuery("UPDATE keywords SET weight=weight-$lower
				WHERE keyword!='$keyword'");
	$this->dbQuery("UPDATE keywords SET weight=".FIXEDPOINT."
				WHERE weight<".FIXEDPOINT);
}
 
$this->dbQuery("UPDATE keywords SET weight=weight+".FIXEDPOINT." 
			WHERE keyword='$keyword'");

I count the total number of keywords, and then I divide a fixed value (set to 1000) by the number of keywords minus one. This lowers every keyword by the same amount as the other keyword is going up. Values may never go below the 1000 mark so all adverts still gain some chance of showing. I also put in a cap so that one particular advert could never become (n)x more popular than any other. This was done with this code.

if(rand(0,$this->cap) == 0) {
	$this->dbQuery("UPDATE keywords SET weight=".(FIXEDPOINT*$this->cap).
				" WHERE weight>".(FIXEDPOINT*$this->cap));
}

The reason for the ‘rand’ was so that this query did not have to be called every time, but so that it at least trimmed the cap enough to stop it getting out of hand.

To choose which keyword to display I have this code.

$total = 0;
$picks = array();
foreach($this->weighting as $keyword => $weight) {
	$total += $weight;
	$picks[$total] = $keyword;
}
$value = rand(0,$total);
foreach($picks as $pos => $keyword) {
	if($value <= $pos) {
		return $keyword;
	}
}

This takes each of the keywords, counts the total weighting from all of them. It then creates an array based upon the keyword and the running total. I then create a random number between 0 and the total value. Then step through the newly created array until I find an index that matches our random number.

The end result is that keywords that get clicked on more will show up more. But all adverts have at least a chance of being shown.

Demo Application

If you first want to try the code out before putting it live on a website. I have included an index.php in the installation .ZIP that gives an example of how it works.

Installation

Firstly let me say that I plan on turning this project into a WordPress plug-in and you should avoid attempting any of the following steps unless you are mildly competent with PHP.

The code can be used with any PHP 4 (and above) website that already uses AuctionAd$. But the installation process I have set out is for WordPress; although it is simple enough to install for any purpose.

First download the dynamicadvert.zip from the code-bank unzip it somewhere.

In the .zip file you’ll find instructions.txt which has detailed instructions (I decided not to include them as they made the post very long.)

License

All I would ask is that anyone who uses the code puts some form of recognition (not hidden 4 levels deep.) on their blog/website. If you do make stacks load of extra cash from the better click-through-ratios that this may/may not generate then any donations are gratefully accepted plus they will pay for more innovation on my part.

Already running

The code is already running on my site. Feel free to hover over my advert and screw my stats, but please only click on adverts you actually are interested in.

Just as a quick disclaimer, as I state in the installation instructions this is something that does require technical knowledge to install (for now) and therefore I do not give any assurance that it will not cause your computer/desk/home to burn down.

Other Ideas

I realise that this code could be used in a whole host of other ways to manipulate advert listings. Instead of choosing amongst keywords, you could have it choose between different advert providers (I suspect Google would not allow this). It may also be possible to dynamically pull down the most popular Ebay keywords (there are ways…) and then supply those keywords to the system.

If you have any feedback, ideas or you want a custom version of this for your site please get in touch (I do have to get paid sometimes).

There Are 8 Responses So Far. »

  1. [...] has put together a little PHP to make AuctionAds more dynamic (don’t forget to get your free $5 by signing up to test [...]

  2. [...] Little PHP to make your AA more dynamic from Nicholas Thank you for reading this post. You can now Leave A Comment (0) or Leave A Trackback. [...]

  3. [...] Nik has post an interessant articles about How to make AuctionAd$ dynamic with PHP [...]

  4. Nick Halstead’s Blog: How to make AuctionAd$ dynamic with PHP…

  5. [...] the Programming and Management blog today, Nick Halstead has posted a quick look at how to make the AuctionAd$ you include on your site a bit more [...]

  6. This is a magnificient product. It would actually help to categorise the performing / non-prforming ads on ones blog.

  7. Thanks for the support Seema, it has already proven its worth on my own blog - I already have some fascinating results which I have included in an update

    I hope to extend the functionality to allow you to display from more than one advert supplier. It would also be very useful to even adapt what types are shown at what time of day, because its possible that users from different countries prefer different types of adverts.

  8. Looks like you’re onto something here. I look forward to trying your plugin when it’s ready and you can bet I’ll give credit where credit is due!

Post a Response