Reputation: 89
I'm setting up A/B tests for a site that is running several adspaces through Google Ad Manager (aka Double Click for Publishers). Part of the test involves moving and resizing the adspaces' iframes, thus we need to serve up different images within the ads. We're trying to avoid having to do this through the Ad Manager dashboard, but the fact that Google's ads are served in an iframe make it so that our front-end Javascript on our test site can't change the img src to the new ad creative.
Is there any way to have the Google Ad iframe accept Javascript changes as the page is loading?
Upvotes: 0
Views: 1218
Reputation: 859
First, it would depends on your safeframe setup (see here for more details about the safeframe API). If your goal is to trigger a function inside your "banner" ad iframe from your parent site :
case 1 : GPT safeframe API not enabled
creative script (custom template or custom script added to your 'basic' creative)
function changeBackground() {
var creative = document.querySelector('#creativeId');
creative.style.backgroundImage = 'url("yournewimage.jpeg")';
}
parent site DOM script :
document.querySelector('#bannerId iframe').contentWindow.changeBackground();
case 2 : GPT safeframe API enabled you would need to create a dedicated communication protocol through a window postmessage => see here for details
Also, keep in mind the following :
Then, few questions are arising :
That is why I would recommend to handle the "responsive" rendering directly from your Google Publisher Tag setup :
This solution has the advantage to cover all of creative types from your site adserver integration :
<script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
<script>
window.googletag = window.googletag || {cmd: []};
googletag.cmd.push(function() {
// Define a responsive ad slot.
// [START responsive_ad]
responsiveAdSlot =
googletag.defineSlot('/6355419/Travel/Europe',
[[300, 250], [728, 90], [750, 200]], 'responsive-ad')
.addService(googletag.pubads());
var mapping =
googletag.sizeMapping()
.addSize([1024, 768], [[750, 200], [728, 90]])
.addSize([640, 480], [300, 250])
.addSize([0, 0], [])
.build();
responsiveAdSlot.defineSizeMapping(mapping);
// [END responsive_ad]
// Enable SRA and services.
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
function refreshSlot() {
googletag.cmd.push(function() {
googletag.pubads().refresh([responsiveAdSlot]);
})
}
//widow being resized (end of event to prevent multiple adcalls)
var timeOutFunctionId;
window.addEventListener('resize', function() {
clearTimeout(timeOutFunctionId);
timeOutFunctionId = setTimeout(refreshSlot, 500)
});
</script>
<div class="ad-container">
<div id="responsive-ad" class="ad-slot"></div>
</div>
<script>
googletag.cmd.push(function() {
googletag.display('responsive-ad');
});
</script>
Credit for the "wait resize end event".
Upvotes: 1