Previous Section  < Day Day Up >  Next Section

Hack 85. Serve Backup Ads

Use AdSense's built-in (and rather thoughtful) ability to serve ads from alternate URLs when there are no targeted ads to offer.

There's a time and place for public service announcements. You just might not think your web site is the place and certainly not if it happens more than occasionally. When you signed up for AdSense (while you're no doubt a good citizen who pays their public radio and television dues), your intent was to reap a revenue stream from all the hard work that you've put into your content.

Yet there are times when a new section of your site hasn't yet been noticed and indexed by Google, AdSense has nothing appropriately targeted in its inventory, or there's a temporary outage of some kind. The net result is that you'll be running public service ads for the Red Cross or the like rather than revenue-generating, targeted advertising. Google AdSense doesn't get paid and so doesn't pay you for click-throughs on public service advertisements.

Now, you can either simply be OK with this coming up every so often—I know I am—or you can make use of a backup system Google AdSense provides: alternate ad URLs.

Point your browser at Google AdSense ( and click the Ad Settings tab at the top of the page. Then, scroll down until you see "Alternate ad URL or color," as shown in Figure 7-11.

Figure 7-11. Provide an alternate URL for ads when AdSense has only public service advertisements to offer your site

Google AdSense suggests ( four backup options:


Paste in the URL of an image somewhere on the Web, ad or not, static or dynamically generated. This can be an alternate image that you've created and are serving from your own site, one produced on-the-fly by another advertising service, or any other image that either has some revenue stream associated with it or simply tickles your fancy. For example, to serve up a static image named advert1.jpg residing on your web site, you'd provide a URL like

Clickable image

Provide the URL of an HTML page somewhere out on the Web that contains only a snippet of markup for a hyperlinked image. For example, you might have a file on your site called adsense_alternate.html that contains the following line:

<a href=""><img src="

images/advert2.jpg border="0" /></a>

That's all the file should have in it, mind you; leave off all the opening <html><head></head><body> and closing </body></html> bits and everything else you usually pack into your pages.

The URL you'd provide as an alternate would then be a pointer to that partial page, something like

HTML color code

If you have nothing to display as an alternative and are dead set against running public service ads, blank out the space where the AdSense ad would have gone by, providing the hexadecimal HTML color code of your page's background or that particular bit of real estate. For example, if your page had a background color of #160B35, a lovely dark blue that I use on my own site, you'd type that color code right into the "Alternate ad URL or color" field.

Collapse your ad

Google provides an HTML file you can download to and serve from your own web site that calls a bit of JavaScript to collapse your ad so that it doesn't show in the event you'd otherwise have seen a public service ad. For instructions and a link to download the file, visit

Whichever you choose, when you click the "Update code" button, a smidgeon of JavaScript (the third line in Figure 7-12) will be added to the AdSense code that you paste into your web page. This additional line provides all AdSense needs to serve up your alternate ad choice when it has no targeted ad to run on your site.

Figure 7-12. An alternate ad URL embedded in Google AdSense JavaScript code

Then again, there is a fifth alternative...

Amazon/Google Ad Replacement (AGAR;; GNU Public License) is a Perl script that supplements your Google AdSense ads with product advertisement drawn from Amazon's Web Services (AWS; and Associates ( programs. Not only does it supplement AdSense, but it also mimics it in appearance, supports all the AdSense ad sizes, and allows you to customize your color scheme to match what you've chosen for AdSense.

For AGAR to be useful (and financially rewarding), you'll need to have signed up as an Amazon Associate ( through which you make money on purchases resulting from click-throughs on your site.

Download AGAR and get it running as a CGI script ["How to Run the Hacks" in the Preface]. There's not much at all you need to change in the script itself, save replacing the default Amazon Associates ID with your own:

my $associate_id = "insert your amazon associates id here";

If you're in the United Kingdom rather than the United States, you'll also want to change the locale in my $locale = "us"; to uk and my $uk_associate_id = "coolstufftoown"; to your U.K. Amazon Associates ID. If you're neither in the U.S. nor the U.K., there is some further adjustment necessary, but we leave that as an exercise for the reader.

Point your browser directly at the CGI script to test it out and you should see an ad banner, as shown in Figure 7-13, easily confused for an AdSense ad at first blush, but clearly linked to Amazon products.

Figure 7-13. An AGAR-generated AdSense-like Amazon banner ad

The product category is chosen at random by default (go ahead and reload the page a few times to see this in action), but this can be customized either by altering the settings baked into the script itself or embedding settings into the agar.cgi URL. For example, instead of just pointing at agar.cgi, try agar.cgi??input_mode=kitchen&input_id=491864&ad_format=125x125_as. This produces a 125 by 125 pixel ad drawing from Amazon's "kitchen" category.

While the concept of mode and id, as expressed in the preceding URL is beyond the scope of this book, suffice it to say that you need to pass matching textual and numerical browse IDs. You'll find a detailed description of browse nodes and IDs in the Amazon Web Services documentation and a list of some of the text/number pairs in the AGAR code itself—look for %browse_ids =.

For an introduction to Amazon Web Services and all other things Amazon, pick up this book's cousin, Amazon Hacks (; O'Reilly) by Paul Bausch.

(If I may, I'd like to end with a pitch to at least consider letting the AdSense public service advertisements run. Sorry, I just couldn't help myself.)

    Previous Section  < Day Day Up >  Next Section