AdManager
A JavaScipt library for interacting with Google DFP.
Introduction
AdManager is a JavaScript library for interacting with Google Publisher Tags (GPT) and Google DFP. It handles the loading of the GPT library as well as the definition and request of ad inventory. Below you’ll find documentation on its configuration and usage.
- Installation
- Basic Usage
- Configuration
- Inventory
- Events
- Dynamic Insertion
- Contributing
- Dependencies
- References
Installation
Bower
Use the Bower package manager to install AdManager into your project. To do so you can either use the CLI:
$ bower install admanager --save
Or define it in your bower.json manifest:
"dependencies": "admanager": "latest"
npm
Similarly, AdManager can be installed using npm. To do so you can either use the CLI:
$ npm install admanager --save
Or define it in your package.json manifest:
"dependencies": "admanager": "latest"
Direct download
If package managers are not your thing, the library can be downloaded directly from GitHub using the Download ZIP button.
Basic Usage
AdManager Usage <!-- This is the ad unit container. AdManager looks for all of the [data-ad-unit] in the DOM and grabs the slot name to make a request from DFP to fill those units. -->
Configuration
A configuration object is required to initialize the AdManager.
key | type |
---|---|
account |
Integer |
autoload |
Boolean |
clientType |
String |
inventory |
Array |
context |
String |
enabled |
Boolean |
targeting |
Array |
insertionEnabled |
Array |
insertion |
Object |
Example Configuration:
account: 1234567 clientType: 'desktop' inventory: slot: 'Unit_Name_in_DFP' sizes: 728 90 970 250 1000 220
account
Type: Integer
Default: null
, must be specified
Description: Your network code, found in the “Admin” tab of DFP.
autoload
Type: Boolean
Default: true
Description: Whether to start the qualification process automatically.
clientType
Type: String
Default: 'default'
, optional
Description: This declares the client type (such as desktop, tablet, or mobile). The value can be set by an external client-detection script and will be used to compare against each inventory item to see whether the item should be displayed or not for that client.
For example, if a desktop device is detected, this value should be set to clientType: 'desktop'
and items in the inventory array that match (type: 'desktop'
) will be displayed. This allows you to include both desktop and mobile inventory items, but only show the appropriate ones according to what clientType
is set to at load time.
inventory
Type: Array
Default: []
, must be specified
Description: An array of one or more objects that define different ad types. See the Inventory section below. More information can be found in the inventory section below.
Example:
var config = // ... inventory: slot: 'Unit_Name_1' sizes: 728 90 970 250 1000 220 slot: 'Unit_Name_2' sizes: 728 90 970 250 1000 220 // ... ;
context
Type: String
Default: 'body'
, optional
Description: This is used as a jQuery selector that specifies the DOM context where ads are to be inserted. In standard cases this will be static since there will only be one page. In infinite scroll applications, there may exist multiple pages in a single window and this provides a way to distinguish one page/context from another.
enabled
Type: Boolean
Default: true
, optional
Description: This provides a way to disable the AdManager.
insertionEnabled
Type: Boolean
Default: false
, optional
Description: Whether to enable dynamic insertion.
insertion
Type: Object
Example Insertion:
pxBetweenUnits: 800 adHeightLimit: 1000 insertExclusion: 'img' 'iframe' 'video' 'audio' '.video' '.audio' '[data-ad-unit]'
insertion.insertExclusion
Type: Array
Default:
'img' 'iframe' 'video' 'audio' '.video' '.audio' '[data-ad-unit]'
Description: When using the dynamic insertion feature, this allows customization of what elements to exclude when looking for valid insertion points.
insertion.pxBetweenUnits
Type: Integer
Default: 800
, optional
Description: The minimum distance between dynamically inserted units.
insertion.adHeightLimit
Type: Integer
Default: 1000
, optional
Description: The max height for dynamically inserted units.
Inventory
The inventory array is a collection of objects that represent different ad positions.
property name | type | |
---|---|---|
slot |
String | |
sizes |
Array | |
type |
String | |
dynamic |
Boolean | |
localContext |
String | optional (required if dynamic: true ) |
Example Usage:
var config = // ... inventory: slot: 'Article_Leaderboard' sizes: 728 90 970 250 1000 220 type: 'desktop' dynamic: false slot: 'Article_Dynamic' sizes: 300 250 300 600 type: 'desktop' dynamic: true localContext: '.entry-content' // ... ;
slot
Type: String
Description: The slot name defined in DFP.
sizes
Type: Array
Description: An array of accepted sizes for this unit. Must match up to the sizes defined in DFP.
type
Type: String
Description: This can be used to categorize inventory. For example, it can be used to denote whether an unit is for desktop or mobile devices. This value is checked against clientType
.
dynamic
Type: Boolean
Default: false
Description: This enables/disables dynamic insertion. If set to false
, AdManager will expect a container on the page with the data-ad-unit
attribute value that corresponds to the slot name defined in the Inventory object.
localContext
Type: String
Description: This is needed only for dynamic insertion. The string is a jQuery selector that specifies an insertion point for the new ad.
Example:
var config = // ... inventory: // ... slot: 'Article_Dynamic' sizes: 300 250 300 600 type: 'desktop' dynamic: true localContext: '.entry-content' // ... ;
Events
Custom jQuery events prefixed with AdManager
.
event | trigger source |
---|---|
AdManager:libraryLoaded |
internal |
AdManager:adUnitRendered |
internal |
AdManager:slotsDefined |
internal |
AdManager:refresh |
external |
AdManager:runSequence |
both |
AdManager:emptySlots |
external |
AdManager:emptySlotsInContext |
external |
AdManager:importConfig |
both |
AdManager:libraryLoaded
Description: This is triggered once when the GPT library is loaded.
AdManager:adUnitRendered
Description: This is triggered each time an ad is rendered. Bind to this event to receive notification of a particular ad that has rendered.
Parameter: unit
{Object}
name | type | description |
---|---|---|
name |
String | The slot name defined in DFP. |
id |
String | HTML id for the current ad wrapper. |
size |
Array | Indicates the pixel size of the rendered creative. |
isEmpty |
Boolean | true if no ad was returned for the slot, false otherwise. |
creativeId |
String | Creative ID of the rendered ad. |
lineItemId |
String | Line item ID of the rendered ad. |
serviceName |
String | Name of the service that rendered the slot. |
AdManager:slotsDefined
Description: This is triggered when slots are successfully defined, but before ads are rendered.
AdManager:refresh
Description: Pass an array of slot names to be refreshed. Slots should already be in the DOM.
Example Usage:
$event;
AdManager:runSequence
Description: Trigger to run the full qualification sequence: the identification of positions in the DOM, define of DFP slots, targeting, request for creative, and display.
Example Usage:
$event;
AdManager:emptySlots
Description: Pass an array of slot names to be emptied.
Example Usage:
$event;
AdManager:emptySlotsInContext
Description: Pass an array of slot names to be emptied within a selection.
Example Usage:
$event;
AdManager:importConfig
Description: Pass an object to import new configuration values. The new config will override values in the current config.
Example Usage:
$event;
Dynamic Insertion
Overview
Note: This feature is optional.
This feature allows AdManager to dynamically insert a variable number of new ad positions on the fly, without predetermined ad containers. The Insertion.js module contains logic that analyzes the DOM nodes in a specified text area to determine the optimal places where to insert ads.
Instructions
- Add new inventory items that reflect the maximum possible number of dynamically inserted ads.
- Set the additional options
dynamic
andlocalContext
in the inventory config.
var config = // ... inventory: // ... slot: 'Dynamic_Unit_1' sizes: 300 250 300 425 300 600 type: 'desktop' dynamic: true localContext: '.entry-content' slot: 'Dynamic_Unit_2' sizes: 300 250 300 425 300 600 type: 'desktop' dynamic: true localContext: '.entry-content' slot: 'Dynamic_Unit_3' sizes: 300 250 300 425 300 600 type: 'desktop' dynamic: true localContext: '.entry-content' ; ;
- If a more specific outer/main context is needed (the default context is
body
), the propertycontext
can be added to the config. This is needed when keeping multiple contexts or articles separate, such as in infinite scroll applications. In the example below, it is.hentry
.
Paragraph 1 Paragraph 2 Paragraph 3
Contributing
Coding Style
AdManager follows the WordPress JavaScript Coding Standards. There is a .jscsrc
included in the project for automatic linting using JSCS.
The modules are written in the UMD pattern to support AMD, CommonJS, and global usage.
Development
The project contains a gulpfile.js for concatenation and minification. To use first install gulp and the dependencies (npm install
). The default gulp task (gulp
) will start the watch task.
Dependencies
AdManager requires jQuery.