diff --git a/libraries/intentIqUtils/getRefferer.js b/libraries/intentIqUtils/getRefferer.js index 20c6a6a5b47..2ed8a668a56 100644 --- a/libraries/intentIqUtils/getRefferer.js +++ b/libraries/intentIqUtils/getRefferer.js @@ -4,19 +4,25 @@ import { getWindowTop, logError, getWindowLocation, getWindowSelf } from '../../ * Determines if the script is running inside an iframe and retrieves the URL. * @return {string} The encoded vrref value representing the relevant URL. */ -export function getReferrer() { + +export function getCurrentUrl() { + let url = ''; try { - const url = getWindowSelf() === getWindowTop() - ? getWindowLocation().href - : getWindowTop().location.href; + if (getWindowSelf() === getWindowTop()) { + // top page + url = getWindowLocation().href || ''; + } else { + // iframe + url = getWindowTop().location.href || ''; + } if (url.length >= 50) { - const { origin } = new URL(url); - return origin; - } + return new URL(url).origin; + }; return url; } catch (error) { + // Handling access errors, such as cross-domain restrictions logError(`Error accessing location: ${error}`); return ''; } @@ -31,12 +37,12 @@ export function getReferrer() { * @return {string} The modified URL with appended `vrref` or `fui` parameters. */ export function appendVrrefAndFui(url, domainName) { - const fullUrl = encodeURIComponent(getReferrer()); + const fullUrl = getCurrentUrl(); if (fullUrl) { return (url += '&vrref=' + getRelevantRefferer(domainName, fullUrl)); } url += '&fui=1'; // Full Url Issue - url += '&vrref=' + encodeURIComponent(domainName || ''); + if (domainName) url += '&vrref=' + encodeURIComponent(domainName); return url; } @@ -47,10 +53,9 @@ export function appendVrrefAndFui(url, domainName) { * @return {string} The relevant referrer */ export function getRelevantRefferer(domainName, fullUrl) { - if (domainName && isDomainIncluded(fullUrl, domainName)) { - return fullUrl; - } - return domainName ? encodeURIComponent(domainName) : fullUrl; + return encodeURIComponent( + domainName && isDomainIncluded(fullUrl, domainName) ? fullUrl : (domainName || fullUrl) + ); } /** @@ -61,7 +66,7 @@ export function getRelevantRefferer(domainName, fullUrl) { */ export function isDomainIncluded(fullUrl, domainName) { try { - return fullUrl.includes(domainName); + return new URL(fullUrl).hostname === domainName; } catch (error) { logError(`Invalid URL provided: ${error}`); return false; diff --git a/modules/intentIqAnalyticsAdapter.js b/modules/intentIqAnalyticsAdapter.js index 03f1aa65654..2122d918e44 100644 --- a/modules/intentIqAnalyticsAdapter.js +++ b/modules/intentIqAnalyticsAdapter.js @@ -5,7 +5,7 @@ import { ajax } from '../src/ajax.js'; import { EVENTS } from '../src/constants.js'; import { detectBrowser } from '../libraries/intentIqUtils/detectBrowserUtils.js'; import { appendSPData } from '../libraries/intentIqUtils/urlUtils.js'; -import { appendVrrefAndFui, getReferrer } from '../libraries/intentIqUtils/getRefferer.js'; +import { appendVrrefAndFui, getCurrentUrl, getRelevantRefferer } from '../libraries/intentIqUtils/getRefferer.js'; import { getCmpData } from '../libraries/intentIqUtils/getCmpData.js'; import { VERSION, @@ -267,9 +267,10 @@ function getRandom(start, end) { export function preparePayload(data) { const result = getDefaultDataObject(); + const fullUrl = getCurrentUrl(); result[PARAMS_NAMES.partnerId] = iiqAnalyticsAnalyticsAdapter.initOptions.partner; result[PARAMS_NAMES.prebidVersion] = prebidVersion; - result[PARAMS_NAMES.referrer] = getReferrer(); + result[PARAMS_NAMES.referrer] = getRelevantRefferer(iiqAnalyticsAnalyticsAdapter.initOptions.domainName, fullUrl); result[PARAMS_NAMES.terminationCause] = iiqAnalyticsAnalyticsAdapter.initOptions.terminationCause; result[PARAMS_NAMES.clientType] = iiqAnalyticsAnalyticsAdapter.initOptions.clientType; result[PARAMS_NAMES.siteId] = iiqAnalyticsAnalyticsAdapter.initOptions.siteId; diff --git a/test/spec/modules/intentIqAnalyticsAdapter_spec.js b/test/spec/modules/intentIqAnalyticsAdapter_spec.js index c103aff65a0..987dec98f51 100644 --- a/test/spec/modules/intentIqAnalyticsAdapter_spec.js +++ b/test/spec/modules/intentIqAnalyticsAdapter_spec.js @@ -21,7 +21,7 @@ import { } from "../../../libraries/intentIqConstants/intentIqConstants.js"; import * as detectBrowserUtils from "../../../libraries/intentIqUtils/detectBrowserUtils.js"; import { - getReferrer, + getCurrentUrl, appendVrrefAndFui, } from "../../../libraries/intentIqUtils/getRefferer.js"; import { @@ -483,7 +483,7 @@ describe("IntentIQ tests all", function () { .stub(utils, "getWindowLocation") .returns({ href: "http://localhost:9876/" }); - const referrer = getReferrer(); + const referrer = getCurrentUrl(); expect(referrer).to.equal("http://localhost:9876/"); }); @@ -494,7 +494,7 @@ describe("IntentIQ tests all", function () { .stub(utils, "getWindowTop") .returns({ location: { href: "http://example.com/" } }); - const referrer = getReferrer(); + const referrer = getCurrentUrl(); expect(referrer).to.equal("http://example.com/"); }); @@ -506,7 +506,7 @@ describe("IntentIQ tests all", function () { .stub(utils, "getWindowTop") .throws(new Error("Access denied")); - const referrer = getReferrer(); + const referrer = getCurrentUrl(); expect(referrer).to.equal(""); expect(logErrorStub.calledOnce).to.be.true; expect(logErrorStub.firstCall.args[0]).to.contain( @@ -594,6 +594,36 @@ describe("IntentIQ tests all", function () { expect(request.url).to.include("general=Lee"); }); + it("should include domainName in both query and payload when fullUrl is empty (cross-origin)", function () { + const domainName = "mydomain-frame.com"; + + enableAnalyticWithSpecialOptions({ domainName }); + + getWindowTopStub = sinon.stub(utils, "getWindowTop").throws(new Error("cross-origin")); + + events.emit(EVENTS.BID_WON, getWonRequest()); + + const request = server.requests[0]; + + // Query contain vrref=domainName + const parsedUrl = new URL(request.url); + const vrrefParam = parsedUrl.searchParams.get("vrref"); + + // Payload contain vrref=domainName + const payloadEncoded = parsedUrl.searchParams.get("payload"); + const payloadDecoded = JSON.parse(atob(JSON.parse(payloadEncoded)[0])); + + expect(server.requests.length).to.be.above(0); + expect(vrrefParam).to.not.equal(null); + expect(decodeURIComponent(vrrefParam)).to.equal(domainName); + expect(parsedUrl.searchParams.get("fui")).to.equal("1"); + + expect(payloadDecoded).to.have.property("vrref"); + expect(decodeURIComponent(payloadDecoded.vrref)).to.equal(domainName); + + restoreReportList(); + }); + it("should not send additionalParams in report if value is too large", function () { const longVal = "x".repeat(5000000);