Ad Slot

Constructs a new adSlot in the page.

Params
opts Object
opts.name string Slot name, ex) RP01
opts.id string Slot's container id, ex) ad-div-RP01
opts.sizes Array.<number, number> Array.<Array.<number, number>> ex) [300, 250] or [[88, 31], [300, 600]].
opts.adunit string Full ad unit code.
[opts.group] string Slot group name.
[opts.targeting] Object.<string, string> Slot-specific targeting.
[opts.companion] boolean True if companion ad.
[opts.mapping] SizeMapping Size mapping.
[opts.outofpage] boolean True if out-of-page ad.
[opts.preserveId] boolean True to never mangle the container id.
[opts.enabled] boolean False if ineligible to make ad calls.
[opts.on] Object.<string, function> Dictionary of callbacks.
[opts.one] Object.<string, function> Dictionary of single-run callbacks.
Returns
AdSlot
See
var log = require('../modules/log.js'),
    SlotCache = require('../modules/slot-cache.js'),
    EventHandler = require('./event-handler.js'),
    googletag = require('../modules/googletag.js'),
    concatLeft = require('../util/list-concat-left.js'),
    mergeLeft = require('../util/map-merge-left.js');

module.exports = function (opts) {
    var slot, name,
        cache = SlotCache(opts.name),
        events = EventHandler({
            events: concatLeft(opts.on, cache.get.events()),
            singles: concatLeft(opts.one, cache.get.singles())
        }),
        targeting = mergeLeft(opts.targeting, cache.get.targeting());

Create the GPT slot instance.

    if (opts.outofpage) {
        slot = googletag.defineOutOfPageSlot(opts.adunit, opts.id);
    } else {
        slot = googletag.defineSlot(opts.adunit, opts.sizes, opts.id);
    }

Apply any slot-level targeting.

    for (name in targeting) {
        slot.setTargeting(name, targeting[name]);
    }

Apply the size mapping if set.

    if (opts.mapping) {
        slot.defineSizeMapping(opts.mapping);
    }

event.slot._name

Attach the name to the native googletag.Slot object for use during GPT events.

harmony.on('slotRenderEnded', function (event) {
    var name = event.slot._name;
});
Type
string
    slot._name = opts.name;

Deprecated Do not use event.slot.name. Scheduled for removal.

    slot.name = opts.name;

event.slot._id

Attach the slot id to the native googletag.Slot object for use during GPT events.

harmony.on('slotRenderEnded', function (event) {
    var id = event.slot._id;
});
Type
string
    slot._id = opts.id;

Slot

Type
Slot
    return {

name

Name of this slot in the system.

Type
string
        name: opts.name,

id

DOM element id of this slot's container.

Type
string
        id: opts.id,

sizes

List of possible sizes this slot can accept.

Type
Array.<number, number> Array.<Array.<number, number>>
        sizes: opts.sizes,

adunit

Fully qualified adunit this slot is targeting.

Type
string
        adunit: opts.adunit,

group

Type
Group
See
Group
        group: opts.group || null,

companion

Type
boolean
        companion: opts.companion || false,

outofpage

Type
boolean
        outofpage: opts.outofpage || false,

enabled

True when this slot is eligible to make ad calls.

Type
boolean
        enabled: opts.enabled === false ? false : true,

on()

Type
function(string, function)
See
EventHandler
        on: events.on,

one()

Type
function(string, function)
See
EventHandler
        one: events.one,

off()

Type
function()
See
EventHandler
        off: events.off,

trigger()

Type
function(string, ?)
See
EventHandler
        trigger: events.trigger,

active

Danger Zone True when this slot has already been displayed.

Type
boolean
        active: false,

gpt

Danger Zone The registered GPT Slot.

        gpt: slot,

activate()

Danger Zone

        activate: function () {
            if (!this.active) {
                var pubads = googletag.pubads();

slot.on('slotRenderEnded', callback)

Params
callback function(googletag.events.SlotRenderEndedEvent)
See
                pubads.addEventListener('slotRenderEnded', function (event) {
                    if (event.slot === slot) {
                        events.trigger('slotRenderEnded', event);
                    }
                });

slot.on('impressionViewable', callback)

Params
callback function(googletag.events.ImpressionViewableEvent)
See
                pubads.addEventListener('impressionViewable', function (event) {
                    if (event.slot === slot) {
                        events.trigger('impressionViewable', event);
                    }
                });
                slot.addService(pubads);
                if (this.companion) {
                    slot.addService(googletag.companionAds());
                }
                this.active = true;
            }
        }
    };
};