diff --git a/modules/sovrnBidAdapter.js b/modules/sovrnBidAdapter.js index d13331b9179..f8c10fd8605 100644 --- a/modules/sovrnBidAdapter.js +++ b/modules/sovrnBidAdapter.js @@ -22,6 +22,7 @@ export const spec = { * @return object of parameters for Prebid AJAX request */ buildRequests: function(bidReqs) { + const loc = utils.getTopWindowLocation(); let sovrnImps = []; let iv; utils._each(bidReqs, function (bid) { @@ -37,8 +38,8 @@ export const spec = { id: utils.getUniqueIdentifierStr(), imp: sovrnImps, site: { - domain: window.location.host, - page: window.location.host + window.location.pathname + location.search + location.hash + domain: loc.host, + page: loc.host + loc.pathname + loc.search + loc.hash } }; if (iv) sovrnBidReq.iv = iv; diff --git a/src/utils.js b/src/utils.js index 7126f293c02..9fd0fb72ac5 100644 --- a/src/utils.js +++ b/src/utils.js @@ -157,17 +157,50 @@ export function parseGPTSingleSizeArray(singleSize) { } }; -exports.getTopWindowLocation = function () { - let location; +export function getTopWindowLocation() { + if (this.inIframe()) { + return getIframeParentLoc(); + } else { + return window.location; + } +} + +const getIframeParentLoc = function() { + let loc = window.location; try { - // force an exception in x-domain enviornments. #1509 - window.top.location.toString(); - location = window.top.location; + if (window.$sf) { + loc = window.document.referrer; + } else { + if (window.document.location && window.document.location.ancestorOrigins && + window.document.location.ancestorOrigins.length >= 1) { + loc = window.document.location.ancestorOrigins[window.document.location.ancestorOrigins.length - 1]; + } else if (window.document.location) { + loc = getNonWebKitIframeParentLoc(); + } + } + loc = parseFullUrl(loc); } catch (e) { - location = window.location; + this.logMessage('getTopParentLoc failure', e); + } + return loc; +}; + +const getNonWebKitIframeParentLoc = function() { + let referrerLoc = ''; + let currentWindow; + do { + currentWindow = currentWindow ? currentWindow.parent : window; + if (currentWindow.document && currentWindow.document.referrer) { + referrerLoc = currentWindow.document.referrer; + } } + while (currentWindow !== window.top); + return referrerLoc; +}; - return location; +const parseFullUrl = function(locString) { + const match = locString.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/); + return match && {protocol: match[1], host: match[2], hostname: match[3], port: match[4], pathname: match[5], search: match[6], hash: match[7], href: match.input}; }; exports.getTopWindowUrl = function () {