Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Amp resize #14

Merged
merged 5 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/creative.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const DEFAULT_CACHE_PATH = '/pbc/v1/cache';
*/
pbjs.renderAd = function(doc, adId, dataObject) {
if (environment.isAmp(dataObject)) {
renderAmpAd(dataObject.cacheHost, dataObject.cachePath, dataObject.uuid);
renderAmpAd(dataObject.cacheHost, dataObject.cachePath, dataObject.uuid, dataObject.size);
} else if (environment.isCrossDomain()) {
renderCrossDomain(adId, dataObject.pubUrl);
} else {
Expand Down Expand Up @@ -126,7 +126,7 @@ function getCacheEndpoint(cacheHost, cachePath) {
return `https://${host}${path}`;
}

function renderAmpAd(cacheHost, cachePath, uuid) {
function renderAmpAd(cacheHost, cachePath, uuid, size) {
let adUrl = `${getCacheEndpoint(cacheHost, cachePath)}?uuid=${uuid}`;

let handler = function(response) {
Expand All @@ -149,6 +149,39 @@ function renderAmpAd(cacheHost, cachePath, uuid) {
let nurl = bidObject.nurl;
utils.writeAdUrl(nurl, bidObject.h, bidObject.w);
}

};
//register creative right away to not miss initial geom-update
if (typeof size !== 'undefined' && size !== "") {
let sizeArr = size.split('x').map(Number);
resizeIframe(sizeArr[0], sizeArr[1]);
} else {
console.log('Targeting key hb_size not found to resize creative');
}

utils.sendRequest(adUrl, handler);
}

function resizeIframe(width, height) {
if (environment.isSafeFrame()) {
const iframeWidth = window.innerWidth;
const iframeHeight = window.innerHeight;

function resize(status) {
let newWidth = width - iframeWidth;
let newHeight = height - iframeHeight;
$sf.ext.expand({r:newWidth, b:newHeight, push: true});
}

if (iframeWidth !== width || iframeHeight !== height) {
$sf.ext.register(width, height, resize);
// we need to resize the DFP container as well
window.parent.postMessage({
sentinel: 'amp',
type: 'embed-size',
width: width,
height: height
}, '*');
}
}
}
2 changes: 1 addition & 1 deletion src/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function isAmp(dataObject) {
/**
* @returns true if the environment is a SafeFrame.
*/
function isSafeFrame() {
export function isSafeFrame() {
return window.$sf && window.$sf.ext;
}

Expand Down
3 changes: 2 additions & 1 deletion template/amp/dfp-creative.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
var uuid = "%%PATTERN:hb_cache_id%%";
var mediaType = "%%PATTERN:hb_format%%";
var pubUrl = "%%PATTERN:url%%";
var size = "%%PATTERN:hb_size%%";

try {
pbjs.renderAd(document, adId, {cacheHost: cacheHost, cachePath: cachePath, uuid: uuid, mediaType: mediaType, pubUrl: pubUrl});
pbjs.renderAd(document, adId, {cacheHost: cacheHost, cachePath: cachePath, uuid: uuid, mediaType: mediaType, pubUrl: pubUrl, size: size});
} catch(e) {
console.log(e);
}
Expand Down