How to Implement AppNexus ads in Drupal 8

Parent Feed: 

AppNexus + Drupal 8

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())
    ->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.

Download AppNexus

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web