I preface this with a note that there are actually a lot of WordPress plugins that purport to do this.

Stable, Topical, Content

Sometimes a blog is just a blog, and you write what you write. Other times you have a site that covers an ever expanding, diverse plethora of topics. When you have topical content that can be easily identified and codified, the game changes. You no longer are forced to rely on generic ads, you can pick and chose what works best for your specific content.

There are two general ways to achieve this. The first way is adding a specific ad to each page, manually, and the second is figuring out how to automate it.

Using an API

Amazon has a Product Advertising API which gives developers access to the entire product catalog, which can let you programmatically determine what links to call and when. Like a lot of Amazon APIs, it’s not exactly written in low-geek levels of usage. This is my constant complaint about Amazon and their services: they were not written to be easily understood.

Thankfully, unlike my experience with making a custom Alexa skill, I did not actually have to invent the wheel. This time there is a fully functional, if poorly documented, Amazon Product Adverstising Library based on PHP REST and SOAP using the Product Advertising API..

A ‘Simple’ Application

The API is best installed via composer, which isn’t my favorite method. Nothing against composer, it’s great to pull down a library and it’s dependancies. My issues with it are that people don’t properly flag their archives so even if you use --prefer-dist on your build, you still get all the tests and documentation and that annoys me.

Anyway. Use composer, get the files, and then you can use it to call Amazon searches and build links:

include_once( 'vendor/autoload.php' );

use ApaiIO\Configuration\GenericConfiguration;
use ApaiIO\Operations\Search;
use ApaiIO\ApaiIO;

$conf = new GenericConfiguration();
$client = new \GuzzleHttp\Client();
$request = new \ApaiIO\Request\GuzzleRequest($client);

    ->setCountry( 'com' )
    ->setAccessKey( AWS_API_KEY )
    ->setSecretKey( AWS_API_SECRET_KEY )
    ->setAssociateTag( AWS_ASSOCIATE_TAG )
    ->setRequest( $request );
$apaiIO = new ApaiIO( $conf );

$search = new Search();
$search->setCategory( 'DVD' );
$search->setActor( 'Lucy Lawless' );
$search->setKeywords( 'Xena' );

$formattedResponse = $apaiIO->runOperation( $search );

var_dump( $formattedResponse );

Obviously you don’t ‘var_dump’ for real, but it’s a good way to get an idea of what kind of data you’ll be getting back. You can also reformat the output by changing the response transformer. For example, if you want an array, you can add this to your $conf setting:

  ->setResponseTransformer( new \ApaiIO\ResponseTransformer\XmlToArray() )

Customizing Keywords

Of course not everyone wants every search result to be about a warrior princess. Pity, but that’s the world for you. These three aspects are the ones most people will care about:

$search->setCategory( 'DVD' );
$search->setActor( 'Lucy Lawless' );
$search->setKeywords( 'Xena' );

All of that data was actually saved in the post, so I wrote my code to extract it. That is the actual magic, though. On pages for characters, we had their actors. On pages for shows, we had the show title and genre. Using that, we were able to logically extract the information to generate the appropriate ads.

Why Not a Plugin?

I did mention there were plugins that do this. The problem was that I needed to mess with that customization so much. I had to hand code in the logic (which was not perfect) to show the ‘right’ links on the right pages. No plugin met all my needs nor permitted enough customization in the right ways.

Reader Interactions

%d bloggers like this: