Configuring Drupal to be an ad server

Date: Sat Apr 26 2014 Banner Advertisements »»»» Drupal Tutorial

The Advertisement module for Drupal is pretty darn powerful and is quite adequate for serving ads on the site on which it's running. That is, you can configure the module, add in a bunch of ad nodes, configure a block to select ads from given ad groups, and they show up on the page. This module also supports "remote ads". This is a bad name for the functionality but "remote ads" generates a bit of javascript which can be embedded into other websites to display advertisements hosted by the Advertisement module. I've been using the Advertisement module to host ads within the Drupal instance on which the ad's are running, however I am partway through converting to using the remote ads feature and am liking the result so far.

The advantages I hope to gain include:-

  • Centralized management of my advertising assets
  • Simplify running the same ad on multiple domains
  • Run ads on 3rd party sites such as blogspot based blogs
  • Distribute the CPU, bandwidth, and memory requirements to run the ad module

The overview is to set up a Drupal site, enable the 'ad' module and some of its submodules (especially 'Remote Ads'), set up some ad groups, set up some advertising, then in 'Remote Ads' generate code snippets for embedding elsewhere. It's pretty straightforward but there are a couple gotcha's including one which will stop you dead in the water until a bug is fixed. So let's look at the details.

First step is to set up a fresh Drupal instance and set up the modules. In my case I've set up a Drupal instance solely for running ads. You may not want to do so, or you may. The advantage I see with this is a fresh Drupal instance with the minimum number of required modules takes very little memory and should run faster. I'm certainly impressed by performance with the one I just set up. And since this is going to be serving ads for several domains it had better have good performance.

I enabled most of the Ad modules. I do not enable the Ad Channel module because that feature only confuses things and I don't perceive any usefulness to that module. The only critical one for this purpose is "Remote Ads".

Next step is setting up in the "Ad Groups" vocabulary appropriate slots for your ads. This vocabulary is automatically created while enabling the Advertisements module. One sets up vocabulary terms to match the areas into which ads are to be displayed. There are two dimensions to naming these terms: 1) The website the ad is displayed on, and 2) the page region. For example ads displayed in sidebars have to be skinny enough for the sidebar, ads displayed at the top or bottom can be wide, and you may wish to show some ads on one site, some ads on another site, and some ads on all sites. The Ad Groups terms you could choose are:

  • site1 side
  • site2 side
  • site1 bottom
  • site2 bottom
  • site1 top
  • site2 top

With the Ad Groups terms set up you can now enter some ads. When adding the ad simply select taxonomy terms based on the site and region to display the ad. You may think this is a bit of a hack, shouldn't one instead select the site separately from the region? Maybe. This is simple however and it works for me very fine. It's also more flexible than simply "site" and "region" but you could add terms for more refined categories such as "web hosting providers" to show only web hosting provider advertisements on specific pages.

If after entering a bunch of ads you add another site it's easy to cause some of the ads to display on the new site. First set up taxonomy terms "site3 side" and the like, then edit the ads you want to show on site3 to select the terms for the appropriate site3 Ad Groups.

You'll notice that in the Ad config page (admin/content/ad) the middle column shows the selected terms from "Ad Groups" you've chosen. This is a quick way to scan down the ads to see which are shown on what sites. Unfortunately this isn't the cleanest user interface choice.

When adding ads make sure to set them in "Active" state or they won't show.

When adding HTML ads that include img tags make sure that Filtered HTML allows the img tag to be displayed. If not the image won't show up.

In user permissions make sure to enable the "show advertisements" permission for anonymous. I really don't understand why this isn't the default setting because isn't that the default expectation?

With this much set up you can go to the Blocks admin screen and see a bunch of potential blocks, one for each of the "Ad Group" terms, named "ad group: XXXXX". Those blocks are there in case you want to display the ads on the site where the ads are being hosted. However the purpose of this tutorial is to display the ads remotely, so let's look at that.

Go to the Ads config screen (admin/content/ad) and click on "Remote Ads" (admin/content/ad/ad_remote). There are two things to select, the Ad Group to be displayed and the number of ads to select from the ad group. This will select 'n' randomly chosen ads from the ones with the given term. Click on Generate Code Snippet. You'll be shown a bit of javascript which you can copy and then paste into a page on another website.

You've probably already had this experience of copying and pasting code snippets. For example any video on can be embedded elsewhere by copying and pasting code snippets. The process is exactly the same. You click in the code snippet box, press CTRL-C (or COMMAND-C), go to the other web site, select the correct region of the template for that site, then press CTRL-V (or COMMAND-V) to paste the code snippet.

If the other site is a Drupal instance one would create a new block, and paste the code snippet into the block body. It's important to select a plain input format as in my experience even the "Full HTML" input format in Drupal doesn't handle the code snippet properly. You may have to create a new input format, named 'plain', with zero filters and no roles who are allowed to use this input format. You, as the site Administrator, on the uid=1 account, will be able to use this input format anyway. By having zero filters the code snippet won't be munged before display allowing the javascript to execute properly in the web browser.

If the other site is a account one would click on the 'Layout' link, then 'Add New Gadget', then select an HTML gadget, then paste the code snippet into the new gadget. Many other blogging and website platforms have a similar gadget paradigm allowing you to paste in HTML.

I should mention the bug which will stop you in your tracks. As of this writing with version 6.x-2.2 of the module the code snippet it generates is broken. (See Remote Ad Code Not Working for a discussion). The code snippet relies on "document.write" to insert some HTML but they've totally mangled the the job and there's no way the generated code snippet is correct. The document.write function requires a string and the generated code snippet is anything but a string. I've written a patch to the module, which you can find on the above issue, and which works for me on my ad server. With any luck the powers that be will accept my patch (or a version thereof) and churn out 6.x-2.3 with the fix.