Skip to content

Instantly share code, notes, and snippets.

@djrosenbaum
Last active October 25, 2022 12:35
Show Gist options
  • Save djrosenbaum/1635870ba65f878f8a8d414599f24e63 to your computer and use it in GitHub Desktop.
Save djrosenbaum/1635870ba65f878f8a8d414599f24e63 to your computer and use it in GitHub Desktop.
Google Publisher Tag: Define Request Display
<!DOCTYPE html>
<html>
<head>
<title>GPT DEFINE REQUEST DISPLAY</title>
<script async="async" src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
<script>
window.googletag = window.googletag || { cmd: [] };
googletag.cmd.push(() => {
googletag.pubads().enableSingleRequest();
googletag.pubads().disableInitialLoad();
googletag.pubads().addEventListener('slotRenderEnded', function(e) {
console.log('slot render ended!');
console.log('e:', e);
console.log('slot render ended!', e.slot.getSlotElementId());
// renderAd(e.slot.getSlotElementId());
});
googletag.pubads().addEventListener('slotOnload', function(e) {
console.log('slot on load!');
});
googletag.pubads().addEventListener('slotRequested', (e) => {
console.log('Slot Requested:', e);
});
googletag.pubads().addEventListener('slotResponseReceived', (e) => {
console.log('Slot Response Received:', e);
console.log('Response Information:', e.slot.getResponseInformation());
});
googletag.enableServices();
});
var adSlot;
function defineAdSlot() {
var adUnitPath = '/3379/qa';
var adSizes = [300, 250];
var divId = 'my_ad_slot';
adSlot = window.googletag.defineSlot(adUnitPath, adSizes, divId).addService(googletag.pubads());
console.log('ad slot defined:', adSlot);
}
function requestAd() {
googletag.cmd.push(() => {
googletag.pubads().refresh([adSlot]);
});
}
function displayAd() {
var el = document.querySelector('#ad-slot');
var id = 'my_ad_slot';
googletag.cmd.push(() => {
var div = document.createElement('div');
div.id = id;
el.appendChild(div);
googletag.display(id);
});
}
</script>
</head>
<body>
<button onClick="defineAdSlot()">Define Ad Slot</button>
<button onClick="requestAd()">Request Ad Slot</button>
<button onClick="displayAd()">Display Ad Slot</button>
<div id="ad-slot"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment