Repository URL to install this package:
|
Version:
0.1.4 ▾
|
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var cmd = [];
var exec = function exec(fn) {
if (self.googletag && self.googletag.apiReady) {
fn(self.googletag);
} else if (self.googletag && self.googletag.cmd) {
self.googletag.cmd.push(function () {
fn(self.googletag);
});
} else {
cmd.push(function () {
fn(self.googletag);
});
}
};
var registered = false;
var registerListeners = function registerListeners() {
if (!registered) {
self.googletag.pubads().addEventListener('slotRenderEnded', function (event) {});
}
};
var configured = false;
var configureLibrary = function configureLibrary() {
if (!configured) {
var lib = self.googletag;
lib.pubads().enableSingleRequest();
lib.pubads().collapseEmptyDivs(true);
lib.pubads().disableInitialLoad();
lib.enableServices();
}
};
var disable = function disable() {
console.log('disabling ads');
if (configured) {
self.googletag.destroySlots();
}
};
var loadLibrary = function loadLibrary() {
return new Promise(function (resolve, reject) {
if (self.googletag) {
resolve(self.googletag);
} else {
self.googletag = self.googletag || {};
self.googletag.cmd = self.googletag.cmd || [];
var script = document.createElement('script');
var head = document.getElementsByTagName('head')[0];
script.async = true;
script.src = 'https://www.googletagservices.com/tag/js/gpt.js';
script.onload = function () {
self.googletag.cmd.push(resolve);
};
script.onerror = reject;
head.appendChild(script);
}
});
};
var init = function init() {
return loadLibrary().then(function () {
registerListeners();
configureLibrary();
}).catch(function () {
console.log('loadLibrary failed');
});
};
var buildFullAdUnitPath = function buildFullAdUnitPath(suffix) {
var _self$config = self.config,
accountId = _self$config.accountId,
region = _self$config.region;
return ['', accountId, region, suffix].join('/');
};
var defineSlot = function defineSlot(_ref) {
var id = _ref.id,
adUnitPathSuffix = _ref.adUnitPathSuffix,
sizeMapping = _ref.sizeMapping;
if (id && adUnitPathSuffix && sizeMapping) {
var adUnitPath = self.buildFullAdUnitPath(adUnitPathSuffix);
exec(function (lib) {
console.log('[ads] define slot ' + id);
var size = sizeMapping.reduce(function (builder, mapping) {
return builder.addSize(mapping.viewport, mapping.sizes);
}, lib.sizeMapping()).build();
if (lib.pubads().getSlots().filter(function (slot) {
return slot.getSlotElementId() === id;
}).length === 0) lib.defineSlot(adUnitPath, [0, 0], id).addService(lib.pubads()).defineSizeMapping(size);
});
}
};
var destroySlot = function destroySlot(id) {
console.log('[ads] destroy slot ' + id);
exec(function (lib) {
lib.destroySlots([id]);
});
};
var refreshSlot = function refreshSlot(id) {
console.log('[ads] refresh slot ' + id);
};
var setTargeting = function setTargeting() {
var config = self.config;
exec(function (lib) {
lib.pubads().set('adsense_keywords', config.adsense_keywords);
lib.pubads().set('adsense_keyword_type', config.adsense_keyword_type);
lib.pubads().set('page_url', config.page_url);
lib.pubads().setTargeting('dcats', config.dcats);
lib.pubads().setTargeting('dcategory', config.dcategory);
lib.pubads().setTargeting('dlanguage', config.dlanguage);
lib.pubads().setTargeting('dcountry', config.dcountry);
lib.pubads().setTargeting('dplacement', config.dplacement);
});
};
var setConfig = function setConfig(config) {
self.config = config;
};
var getConfig = function getConfig() {
return self.config;
};
var refresh = function refresh() {
return init().then(function () {
console.log('[ads] initialised');
var lib = self.googletag;
cmd.push(function () {
console.log('[ads] render');
var slots = lib.pubads().getSlots();
slots.forEach(function (slot) {
return lib.display(slot.getSlotElementId());
});
setTargeting();
lib.pubads().refresh();
});
cmd.splice(0, cmd.length).forEach(function (c) {
return lib.cmd.push(c);
});
});
};
exports.default = {
config: {},
init: init,
disable: disable,
defineSlot: defineSlot,
destroySlot: destroySlot,
refreshSlot: refreshSlot,
refresh: refresh,
setTargeting: setTargeting,
setConfig: setConfig
};