Showing Multiple Ads from a Single OpenX Zone

Published by John on August 29, 2012 Under OpenX

While setting up OpenX for a client, I wanted to be able to display multiple ads from a single zone on the same page. The default behavior is to show only one banner ad per zone.

However, we wanted to have a scrolling list of banner ads and to be able to place them in a single zone, so they could be logically organized.

On OpenX’s website, they suggested creating multiple zones for each banner/campaign, then one single zone that is large enough to accommodate all the zones, pasting the invocation code from each smaller zone into it. That seemed like way too much work, not to mention confusing and awkward to work with, especially for a non-technical client. I also came across a few OpenX plugins, but didn’t feel like paying extra for such a simple task.

I ended up doing a bit of playing with OpenX’s Local Mode Tag Invocation Code and came up with a solution to display multiple banners from the same zone.

Setup / Prerequisites

Before continuing, you will need to have a zone setup. You should be able to copy the default Javascript Tag Invocation Code from this zone to your website and correctly display a single banner ad.

This means you will need to have already added advertisers, campaigns, and banners, adding multiple banners to the zone.

How to Display Multiple Ads From the Same Zone

The following function is used to gets a predetermined number of adverts and return the html of each banner ad in an array.

$zone_id is the numeric ID of the zone created within OpenX. If you goto Inventory->Zones->Click on Desired Zone, you will see the id in the URL as “zoneid”

$ad_count is the max number of banner ads to get from the zone.

MAX_PATH needs to be correctly set to the path of your OpenX installation, If you are not sure, you can copy the path from the code generated automatically via OpenX when you select the Local Mode Tag Invocation Code.

**You will need to update the path(MAX_PATH) on line #7, and possibly line #9 before using this function**


function kcr_get_ads_by_zone($zone_id=1, $ad_count=20){

	$ad_place = 0;
	$not_finished = true;
	$ads = array();

	define('MAX_PATH', '/public_html/openx');
	
	if (@include_once(MAX_PATH . '/www/delivery/alocal.php')) {
	
		if (!isset($php_ads_context)) {
			$php_ads_context = array();
		}

		while($ad_place < $ad_count && $not_finished){

			$php_ad = view_local('', $zone_id, 0, 0, '', '', '0', $php_ads_context, '');
			$php_ads_context[] = array('!=' => 'bannerid:'.$php_ad['bannerid']);
			$php_ads_context[] = array('!=' => 'campaignid:'.$php_ad['campaignid']);

			//print_r($php_ad);

			if(!is_array($php_ad) || !isset($php_ad['html']) || empty($php_ad['html']))
				$not_finished = false;
			else
				$ads[] = $php_ad['html'];

			$ad_place++;


		}

	}

	return $ads;

}

$my_ads = kcr_get_ads_by_zone();

echo "<ul>";

foreach($my_ads as $ad)
  echo "<li>{$ad}</li>";

echo "</ul>";

Explanation of Above Code

The above function accepts the zoneid of an OpenX zone, as well as the number of ads to show.

It then uses a while loop and OpenX’s view_local function to retrieve ads from the zone. It continues until the view_local function has been called a set number of times, or an empty banner add is returned from view_local.

Other information is returned from view_local, so uncomment line #21 to view the entire banner ad array.

Line #18 and Line #19 ensure that the same ad is not displayed twice, nor are ads from the same campaign displayed in this grouping.

This example shows getting the up to 20 banners from zoneid #1 and then displaying them in an ul element.

A Word of Caution Regarding IAB Guidelines

When you select the Local Mode Tag Invocation Code, OpenX prints a warning that states “Note: Impression data generated from using Local Mode invocation tags are not compliant with IAB guidelines for ad impression measurements.”

IAB, or the Interactive Advertising Bureau is an organization of advertisers that, among other things, publishes a set of guidelines for publishers. So, depending on what types of ads you display and how you use/report the impression/click data, keep in mind the above warning.


9 Comments |

Comments:

  1. Rehman on Feb 20, 2013

    I’ve tried this code but it is still showing banner at a time. Can you help me on this?

    There is also one missing variable in above code. The parameter $zone_id should be in “view_local” function. But after entering correct zone ID it still display one banner.

  2. John on Feb 20, 2013

    Thanks for the heads up about the $zone_id variable, I have updated it above.

    Regarding your issue, most likely the if statement on line #23 is finding an empty or invalid ad, so the loop is stopped by setting $not_finished = false;

    You can try debugging it by uncommenting line #21. That will print the $php_ad variable and should tell you whether the call to view_local is in fact returning an invalid ad, which would be the most likely reason and would cause the loop to stop early.

    I would also recommend checking out how the zone and advertisements are setup within OpenX. It is possible that the way the zone or advertisers are setup could be causing issues as well.

    The above method is used on several sites, although it is modified a bit, and has worked quite well for displaying image based ads. If you need some more assistance, I would probably need to look at how the OpenX zone is setup in the backend, as well as file access to do some debugging of the php.

    Feel free to contact me using the contact form, or give me a call, and we can discuss it some more!

  3. TT on Jul 20, 2013

    I found this via Google and it works on first try.

    What I’m wondering is if it’s possible to randomize the output? At present it prints them in some order that doesn’t change.

    Thanks!

  4. John on Jul 20, 2013

    TT, thanks for the comment.

    I believe there is probably a way to get it from OpenX randomly, but would need to check the documentation on the view_local call.

    However, one easy way to randomize the results would be just to use PHP’s shuffle function.

    So, you could either just use shuffle after getting the ads, like shuffle($my_ads);

    You could also do it within the kcr_get_ads_by_zone function, so something like: return shuffle($ads);

    One downside to doing it that way, however, is that if you had 20 ads in the zone, but you only wanted to show 10 at a time. In that case, you would likely need to do the randomization via OpenX.

  5. Brano on Dec 01, 2014

    Hi,

    thanks for nice guide. I want to pass variables for local mode, I used XML-RPC tag, and one specify something like this:
    $url = “/1/www/delivery/axmlrpc.php?lang=en”;
    $oaXmlRpc = new OA_XmlRpc(‘ads.server.com’, $url, 80, false, 10);
    $adArray = $oaXmlRpc->view(“zone:1″, 0, ”, ”, 0, $OA_context, ”);

    with local mode tag I dont know where (and if it is possible) to add site variables (I got filters for banners which uses site variables).

  6. MH on Apr 08, 2016

    Hi John,
    How can I display all banners linked to a zone?i.e. I want to get all banners from a zone. What code I need to modify?

    Where do I call kcr_get_ads_by_zone function? within javascript?

    Thanks

  7. john on Aug 22, 2016

    MH, kcr_get_ads_by_zone is a php function that prints the ads from Revive Adserver(OpenX) as HTML. It provides an array of Adverts, so you could use it to generate javascript, but it would need to be modified, probably using json_encode or something similar.

  8. Vernon G. on Mar 04, 2017

    Hi John,

    So I’ve ran into the same issue as Rehman with only getting an output of one banner. Can you advise what particular settings in a Zone or otherwise might cause this behavior. I’m using Revive Adserver 4.01 (Formerly OpenX)

    I’ve uncommented line 21 and it only shows output for one banner.

  9. john on Mar 07, 2017

    Vernon G, it may be a setting within Revive. I believe there is at least one that deals with allowing more than one advert to be shown per zone.

    This post is quite old at this point, going on 4 years, and while I did just check one of my old clients and verify it is still working/running, I haven’t done much work with Revive recently.

    If you want, I am available for consultation at a reasonable rate and can likely get it setup/working for you.

Add a Comment