i'm searching for solution, that can expand SafeFrame custom ad from inside of custom creative in Google DFP, is that possible somehow?
Google DFP - Resize SafeFrame custom creative outer Iframe container from inside (expand ad)
6.8k views Asked by PayteR At
2
There are 2 answers
0
On
I couldn't find any solid documentation on this so the example from PayteR helped me immensely! I had to play with the new dimension to get the ad to expand the right way.
Here is some sample code I created from PayteR's example:
/* Main object for namespacing */
var Ad = function(obj) {};
/*
Register your ad with the dimensions of the current ad.
- This is basically an event handler for the the ad frame.
- 728 initial ad width
- 90 initial ad height
*/
Ad.prototype.registerExpand = function() {
$sf.ext.register(728, 90, function(status, data){});
};
/* Expand function to be called on click or load */
Ad.prototype.expandAd = function() {
/* Get the current geometry of your ad */
var self = $sf.ext.geom().self;
/*
Set the new geometry
- Increase height 315 pixels
- Expand downward by setting the bottom to the new height minus the current height
*/
$sf.ext.expand({
h: 315,
b: 315 - self.h
});
};
/* Intialize, register, and expand */
var ad = new Ad();
ad.registerExpand();
ad.expandAd();
There are 2 possible solutions:
1) using
SafeFrame API
pros:
cons:
2) code your own API with
window.postMessage()
javascript methodcons:
pros:
1) using
SafeFrame API
This API is realatively easy to use, you can see some examples in GPT Safeframe preview tool.
First you need to update your DFP initialization script in
<head>
of your websiteThis will allow to expand SafeFrame ads on your website. More about this in Control SafeFrame Container behavior through GPT.
Now you can create custom creative and serve it as SafeFrame on your website. Here is my one example. This Example can "wait" util it's visible, and then will expand to height of
<div id="container">
that is inside of SafeFrame:2. code your own API with
window.postMessage()
javascript methodFirst, you need to put this code, to your DFP initialization script in
<head>
of your website. This code will add an ID of Ad slot as#hash-tag
to<iframe>
's src so you can get it from inside of your creative.Second, you need to add this code to your website, better as separated .js file.
And third thing is your custom code in custom type of creative in DFP. Here is example, that is similar to that in first example, but here this script can wait until all content and image is loaded and then will expand/shrink your iframe with creative:
That's all. When you want to change anything on your website from inside of iframe, you can code your own cmd on your website and call this command from inside of the iframe.
Edit 1: just noticed now, that
var divGptAdId = '%%PATTERN:url%%;
will not return correct id of div on the page in #hash way, so now it's needed to give him a correct container div id change:to