diff --git a/src/secureCreatives.js b/src/secureCreatives.js index 415fbc17c63..67579bae76c 100644 --- a/src/secureCreatives.js +++ b/src/secureCreatives.js @@ -6,7 +6,7 @@ import events from './events'; import { fireNativeTrackers } from './native'; import { EVENTS } from './constants'; -import { isSlotMatchingAdUnitCode } from './utils'; +import { isSlotMatchingAdUnitCode, logWarn } from './utils'; import { auctionManager } from './auctionManager'; import find from 'core-js/library/fn/array/find'; import { isRendererRequired, executeRenderer } from './Renderer'; @@ -74,12 +74,38 @@ function sendAdToCreative(adObject, remoteDomain, source) { function resizeRemoteCreative({ adUnitCode, width, height }) { // resize both container div + iframe ['div', 'iframe'].forEach(elmType => { - let elementStyle = getElementByAdUnit(elmType).style; - elementStyle.width = width + 'px'; - elementStyle.height = height + 'px'; + let element = getElementByAdUnit(elmType); + if (element) { + let elementStyle = element.style; + elementStyle.width = width + 'px'; + elementStyle.height = height + 'px'; + } else { + logWarn(`Unable to locate matching page element for adUnitCode ${adUnitCode}. Can't resize it to ad's dimensions. Please review setup.`); + } }); + function getElementByAdUnit(elmType) { - return document.getElementById(find(window.googletag.pubads().getSlots().filter(isSlotMatchingAdUnitCode(adUnitCode)), slot => slot) - .getSlotElementId()).querySelector(elmType); + let id = getElementIdBasedOnAdServer(adUnitCode); + let parentDivEle = document.getElementById(id); + return parentDivEle && parentDivEle.querySelector(elmType); + } + + function getElementIdBasedOnAdServer(adUnitCode) { + if (window.googletag) { + return getDfpElementId(adUnitCode) + } else if (window.apntag) { + return getAstElementId(adUnitCode) + } else { + return adUnitCode; + } + } + + function getDfpElementId(adUnitCode) { + return find(window.googletag.pubads().getSlots().filter(isSlotMatchingAdUnitCode(adUnitCode)), slot => slot).getSlotElementId() + } + + function getAstElementId(adUnitCode) { + let astTag = window.apntag.getTag(adUnitCode); + return astTag && astTag.targetId; } }