Skip to content
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
33 changes: 19 additions & 14 deletions libraries/intentIqUtils/getRefferer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand All @@ -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;
}

Expand All @@ -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)
);
}

/**
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions modules/intentIqAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
38 changes: 34 additions & 4 deletions test/spec/modules/intentIqAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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/");
});

Expand All @@ -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/");
});
Expand All @@ -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(
Expand Down Expand Up @@ -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);

Expand Down
Loading