July 18, 2017
One of our clients requested to integrate AppNexus ads with their Drupal 8 site. The requirements were very simple:
- Ability to display ads as blocks.
- Ad blocks should be configurable: tagId, Sizes. And pass those variables via apntag.defineTag().
- Ability to pass global page options via apntag.setPageOpts().
Since there wasn't any module that would support this I decided to create a module that will be very easy to use and at the same time will be extendable and useful in other projects.
The module comes with a simple PHP class that helps to generate pageOpts from hook_page_attachments(). You may create your own class using that class as an example.
use Drupal\appnexus\PageOpts;
/**
* Implements hook_page_attachments().
*/
function mymodule_page_attachments(array &$page) {
$page_opts = (new PageOpts())
->setKeywordQuotes()
->setMember('YOUR_MEMBER_ID')
->setKeyword('var1', 'value')
->setKeyword('var2', ['val1', 'val2'])
->setKeyword('var3', 1234);
if ($opts = $page_opts->build()) {
$page['#attached']['drupalSettings']['appnexus']['opts'] = json_encode($opts);
}
}
Please make sure you specify Member ID in order to properly pass all the parameters.