Skip to content

Instantly share code, notes, and snippets.

@stuwilli
Last active September 18, 2015 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuwilli/483e94de00b762ab1ba4 to your computer and use it in GitHub Desktop.
Save stuwilli/483e94de00b762ab1ba4 to your computer and use it in GitHub Desktop.
GPT Resize Freewheel Frame
<!--nosandbox-->
<div id='div-gpt-ad-1234567891234-728x90'></div><!-- change this elements id so it's unique -->
<script type="text/javascript">
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);
})();
(function() {
var conf = {
baseName: '', //network id and ad unit here e.g /1234/site.com
formats: [728,90], //sizes here, can support multiples eg. [[728,90],[970,250]]
div: 'div-gpt-ad-1234567891234-728x90', //change this to match the div id above
pageMatch: { //URL regex patterns for setting second level ad units
'homepage': '^\/$' //detects homapge and sets a second level ad unit e.g /1234/site.com/homapge
},
};
function resizeSlot(width, height) {
if (window.self === window.top) {
return;
}
var slot, fid = window.frameElement.getAttribute('id');
slot = window.top.document.getElementById(fid);
slot.setAttribute('width', width);
slot.setAttribute('height', height);
return;
}
function path() {
if (window.self === window.top) {
return window.location.pathname;
} else {
var a = document.createElement('a');
a.href = document.referrer;
return (typeof a.hostname !== 'undefined') ? a.pathname : '';
}
}
function pageMatcher() {
if (typeof conf.pageMatch !== 'undefined' &&
Object.prototype.toString.call(conf.pageMatch) === '[object Object]') {
for (var section in conf.pageMatch) {
try {
if (path().match(conf.pageMatch[section])) {
return conf.baseName + '/' + section;
}
} catch (err) {}
}
}
return conf.baseName;
}
googletag.cmd.push(function() {
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
console.log('Creative with id: ' + event.creativeId +
' is rendered to slot of size: ' + event.size[0] + 'x' + event.size[1]);
resizeSlot(event.size[0], event.size[1]);
});
googletag.pubads().display(pageMatcher(), conf.formats, conf.div);
});
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment