What is Openx
OpenX is, in my opinion, the most efficient and hands-free open source ad server on the web today. It’s flexible, easy to use, and best of all, free.
What is WordPress?
WordPress is one of the leading platforms in blogging on the web today. Whether you are self-hosted (using wordpress.org) or being hosted on Automattic servers (using wordpress.com), WordPress is an amazing blogging platform which makes blogging easy and fun.
This OpenX and WordPress tutorial will outline how to more effectively use the targeting features of OpenX utilizing the built-in features of WordPress. It will explain how to do this by using WordPress tags and OpenX source parameters, both built-in features to WordPress and OpenX, respectively.
OpenX Solution Used: Hosted OpenX (http://hosted.openx.org/)
WordPress Version: 2.6.2
For this tutorial, I will be using tags are the “source” for OpenX.
Step 1: Setup tags so OpenX can view them as a “source.”
[sourcecode type=”php”]
<?php
while (have_posts()):
the_post();
$fsm_oxtags = get_the_tags();
if ($fsm_oxtags)
{
foreach ($fsm_oxtags as $tag)
{
$taglist .= $tag – >
name . ‘|’;
}
$taglist = rtrim($taglist, ‘|’);
$oxtags = rawurlencode($taglist);
}
endwhile;
?>
[/sourcecode]
I have these in the top of my single.php file within WordPress. This only creates the variable of $oxtags so it can be echo’d elsewhere.
Details of Step 1: The first bit of code will create the loop. then it will go through each tag and set it as “tag |” because I am using pipes to separate each tag for OpenX. Next, it will trim off the last pipe because it’s not needed, creating something like… “tag 1|tag 2|tag 3|tag 4” … and like I just mentioned, it will strip off the last pipe. Lastly, it will put that string, “tag 1|tag 2|tag 3|tag 4” … into a raw encoded URL string which we will be able to echo later in the page. Note: this is only being set at $oxtags so it can be used elsewhere, nothing is echo’d in this part of the script.
Step 2: Setup your OpenX invocation code
[sourcecode 1=”” 2=”” 3=”var” 4=”m3_u” 5=”=” 6=”(location.protocol=='https:'?'https://d.openx.org/ajs.php':'http://d.openx.org/ajs.php');” 7=”var” m3_r=”Math.floor(Math.random()*99999999999);” 8=”if” 9=”(!document.MAX_used)” 10=”document.MAX_used” 11=”=” 12=”',';” 13=”document.write” 14=”("<scr"+"ipt” type=”text/javascript” 15=”src='"+m3_u);” 16=”document.write” 17=”("?zoneid=1009");” 18=”document.write” 19=”('&cb='” 20=”+” 21=”m3_r);” 22=”if” 23=”(document.MAX_used” 24=”!=” 25=”',')” 26=”document.write” 27=”("&exclude="” 28=”+” 29=”document.MAX_used);” 30=”document.write” 31=”(document.charset” 32=”?” 33=”'&charset='+document.charset” 34=”:” 35=”(document.characterSet” 36=”?” 37=”'&charset='+document.characterSet” 38=”:” 39=”''));” 40=”document.write” 41=”("&loc="” 42=”+” 43=”escape(window.location));” 44=”document.write” 45=”” 46=”echo” 47=”$oxtags;” 48=”?></strong>");” 49=”if” 50=”(document.referrer)” 51=”document.write” 52=”("&referer="” 53=”+” 54=”escape(document.referrer));” 55=”if” 56=”(document.context)” 57=”document.write” 58=”("&context="” 59=”+” 60=”escape(document.context));” 61=”if” 62=”(document.mmm_fo)” 63=”document.write” 64=”("&mmm_fo=1");” 65=”document.write” 66=”("'></scr"+"ipt>");” 67=”/” language=”>”][/sourcecode]]>–>
[/sourcecode]
Now that we have the $oxtags variable ready, we can echo it where we need it. Simply add the next line, such as I have, document.write ("&source=<?php echo $oxtags; ?>");
, then we’re almost ready to start using advanced targeting within OpenX using your WordPress tags.
Step 3: Rewind the content loop
!!! IMPORTANT !!!
This is critical! You must do this if you want your posts’ content to display properly.
[sourcecode type=”php”]
<?php rewind_posts();
if (have_posts()):
while (have_posts()):
the_post();
?>
[/sourcecode]
The reason for this is that you need to rewind the posts so the loop is displayed properly. (documentation)
The line above is what my real loop looks like in my single.php file, ensuring that the content of the loop is displayed properly as it was originally.
Note: You only need to rewind_posts(); if you have multiple loops or you put the Step 1 code before the first loop, which I did for this tutorial.
Step 4: Setup OpenX Targetting to Deliver to Specific Tags
Now that we have sources setup within the JavaScript and PHP for WordPress, we need to get to work adding some banners using specific sources. Go to one of your campaigns, then select a banner you’ve already added or add a new banner. Go to the Delivery Options tab. This is where you can select your targeting limitations.
First, add the delivery option to limit the banner display for a specific source.
Next, set it to “contains” so you can specify which tagged posts you want to display this banner.
Lastly, specify the source you want to use fr banner delivery limitation.
Results
Here is what your final output should look like:
[sourcecode]
<script type=’text/javascript’><!–//<![CDATA[
var m3_u = (location.protocol==’https:’?’https://d.openx.org/ajs.php’:’http://d.openx.org/ajs.php’);
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ‘,’;
document.write ("<scr"+"ipt type=’text/javascript’ src=’"+m3_u);
document.write ("?zoneid=1009");
document.write (‘&cb=’ + m3_r);
if (document.MAX_used != ‘,’) document.write ("&exclude=" + document.MAX_used);
document.write (document.charset ? ‘&charset=’+document.charset : (document.characterSet ? ‘&charset=’+document.characterSet : ”));
document.write ("&loc=" + escape(window.location));
document.write ("&source=<strong>tag%201%7Ctag%202</strong>");
if (document.referrer) document.write ("&referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&mmm_fo=1");
document.write ("’></scr"+"ipt>");
//]]>–>&</script>
[/sourcecode]
I bolded the part that was used with the Step 1 code. Basically, it will output your tags for each WordPress post as a “source” so you can use them for targeting on your site.
Thanks goes out to Thomas and Steve Smith for helping me get this working
Jonathan says
Glad you like it. I’m hoping to release some more tutorials in the near future about OpenX, but hopefully broader tutorials.
Jean-Paul Horn says
Excellent tutorial, thanks so much for this! Bookmarked for future reference.
Mat says
Hi Jonathon, Interesting Article
Quick Question I have
Is openX able to target by inputted data from the user of a postcode search
Jonathan says
Mat, Unfortunately, hosted.openx.org doesn’t offer a postcode breakdown for delivery limitation. The best you’ll be able to get is by Geo: Continent and Geo: Country.
You might be able to achieve it by limitation delivery by IP address, using a range for known IP address ranges for a specific zip code, but it will be a lot harder to get 100% or near 100% accuracy for that.
Ruk says
Brilliant tutorial. Thanks
Time Is Money says
Hello Jonathan, Does this technique only work with hosted.openx.org accounts or can it be used on a self hosted openx installation?
Thanks.
Jonathan Dingman says
Yup! It should work just the same since you’re not actually changing any of the core OpenX code, only the delivery javascript code which you embed on your site.
Time Is Money says
Thanks works perfect! This helps a lot. I’m working on using this technique to pull publisher profile data and using it as site variables. This tutorial has opened up my mind and made the targeting process more flexible and understandable.
Thanks.