Skip to content

Instantly share code, notes, and snippets.

@ghankerson
Created December 9, 2014 18:19
Show Gist options
  • Save ghankerson/f9b885d783f57f2f65d9 to your computer and use it in GitHub Desktop.
Save ghankerson/f9b885d783f57f2f65d9 to your computer and use it in GitHub Desktop.
GPT Tags take 100 bazillion
/* Initialize Google Publisher Tags API - Docs: http://goo.gl/ZqOiy */
var gptadslots = [];
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
/* Initialize Push Function - Docs: http://goo.gl/EVjef */
googletag.cmd.push(function() {
var metas = document.getElementsByTagName('meta'),
mapping1,
mapping2,
busy = false,
resizer;
/*
* this function tries to prevent to many window resize events from firing too quickly
*/
resizer = function() {
if (busy) {
googletag.pubads().refresh();
clearTimeout(busy);
}
};
window.onresize = function() {
busy = setTimeout(resizer, 250);
};
/* Get value of a META tag by attribute name and return its value. If undefined, return default string. */
/* Santize string and replace all special characters with underscores. */
this.getTag = function(metaname, tagdefault) {
if (metaname === undefined) {
return false;
}
if (tagdefault === undefined) {
tagdefault = false;
}
var targets = [];
for (i = 0; i < metas.length; i++) {
/* Seek out the specific tag we need from all META tags available. Verify that it has a content attribute. */
if (metas[i].getAttribute("name") == metaname && metas[i].getAttribute("content")) {
targets.push(metas[i].getAttribute("content"));
}
}
var tag = (targets[0] !== undefined) ? targets[0] : tagdefault; /* Return only the first one if it exists, otherwise default */
if (tag === false) {
return false;
}
if (tag == "mpr") {
tag = "radio";
} /* We use "mpr" to tag institutional content, but IBSys calls it "radio" */
return tag.replace(/[^a-zA-Z0-9]/g, '_').toLowerCase(); /* Sanitize to accept only lowercase letters, numbers and underscores: http://stackoverflow.com/a/9705227 */
};
/* Defult Slot Properties - Should result in an ad slot like "/3468/ibs.mpr.news/health/todays_question" */
this.buildslot = [];
if (this.getTag("mpr-content-topic")) {
this.buildslot.push(this.getTag("mpr-content-topic"));
} /* append mpr-content-topic meta tag if it exists */
if (this.getTag("mpr-program")) {
this.buildslot.push(this.getTag("mpr-program"));
} /* append mpr-program meta tag if it exists */
if (this.getTag("mpr-subsite")) {
this.buildslot.push(this.getTag("mpr-subsite"));
} /* append mpr-subsite meta tag if it exists */
if (this.getTag("mpr-content-group")) {
this.buildslot.push(this.getTag("mpr-content-group"));
} /* append mpr-content-group meta tag if it exists */
this.buildslot = (this.buildslot.length) ? this.buildslot.join("/") : "default"; /* append "default" if ad slot is empty */
this.site = this.getTag("mpr-site", "radio"); /* assign site to adslot if meta tag exists, default to ibs.mpr.radio if not */
this.slot = "/3468/ibs.mpr." + this.site + "/" + this.buildslot; /* concatenate all properties in to ad slot request */
/* Define ad slots and map to element ID */
gptadslots[1] = googletag.defineSlot(this.slot, [
[300, 250],
[300, 600]
], 'mpr-ad-1').setTargeting('pos', ['mr_top']).addService(googletag.pubads()); /* multi */
gptadslots[2] = googletag.defineSlot(this.slot, [
[300, 250]
], 'mpr-ad-2').setTargeting('pos', ['mr_bottom']).addService(googletag.pubads());
gptadslots[3] = googletag.defineSlot(this.slot, [
[728, 90]
], 'mpr-ad-leader').setTargeting('pos', ['leader_top']).addService(googletag.pubads());
gptadslots[0] = googletag.defineOutOfPageSlot(this.slot, 'mpr-ad-oop').addService(googletag.pubads());
if (document.getElementById('membership-ad')) {
mapping1 = googletag.sizeMapping().
addSize([0, 0], []).
addSize([421, 0], [800, 100]). // Desktop/Tablet
build();
mapping2 = googletag.sizeMapping().
addSize([0, 0], [420, 50]).
addSize([421, 0], []). // Mobile
build();
gptadslots[4] = googletag.defineSlot('/31396736/mprnews_mbrshp_drive_banner_800x100', [800,100],
'membership-ad').defineSizeMapping(mapping1).addService(googletag.pubads());
gptadslots[5] = googletag.defineSlot('/31396736/mprnews_mbrshp_drive_banner_420x50', [420,50],
'membership-ad-mobile').defineSizeMapping(mapping2).addService(googletag.pubads());
}
/* Configure DFP publisher ads service - Docs: http://goo.gl/d6uBy */
googletag.pubads().enableSingleRequest();
googletag.pubads().enableAsyncRendering();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();
/* Push ads to their assigned element ID - Be sure ids match those defined above. */
/* Moved inside the first googletag.cmd.push method to prevent possible timing issues */
googletag.cmd.push(function() {
googletag.display('mpr-ad-1');
});
googletag.cmd.push(function() {
googletag.display('mpr-ad-2');
});
googletag.cmd.push(function() {
googletag.display('mpr-ad-leader');
});
googletag.cmd.push(function() {
googletag.display('mpr-ad-oop');
});
if (document.getElementById('membership-ad-frame-wrapper')) {
googletag.cmd.push(function() {
document.getElementById("membership-ad-frame-wrapper").className = "has-ad";
googletag.display('membership-ad');
googletag.display('membership-ad-mobile');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment