From bacedfbaf0b98f1210e0ed20aa750ab2225a74c8 Mon Sep 17 00:00:00 2001 From: Eyvaz Ahmadzada Date: Mon, 19 Aug 2024 15:35:24 +0200 Subject: [PATCH 1/2] improve referrer for more accurate reporting --- modules/intentIqAnalyticsAdapter.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/modules/intentIqAnalyticsAdapter.js b/modules/intentIqAnalyticsAdapter.js index 10ce8097bf1..134ce0adee1 100644 --- a/modules/intentIqAnalyticsAdapter.js +++ b/modules/intentIqAnalyticsAdapter.js @@ -217,7 +217,16 @@ function constructFullUrl(data) { } export function getReferrer() { - return document.referrer; + try { + if (window.self === window.top) { + return window.location.href; + } else { + return window.top.location.href; + } + } catch (error) { + logError(`Error accessing location: ${error}`); + return ''; + } } iiqAnalyticsAnalyticsAdapter.originEnableAnalytics = iiqAnalyticsAnalyticsAdapter.enableAnalytics; From 99cb1e20d9c4300b1b531f99c32c6ef3e93c9599 Mon Sep 17 00:00:00 2001 From: Eyvaz Ahmadzada Date: Mon, 2 Sep 2024 12:59:22 +0200 Subject: [PATCH 2/2] add unit tests --- modules/intentIqAnalyticsAdapter.js | 8 ++--- .../modules/intentIqAnalyticsAdapter_spec.js | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/modules/intentIqAnalyticsAdapter.js b/modules/intentIqAnalyticsAdapter.js index 134ce0adee1..5e51c1c2c8a 100644 --- a/modules/intentIqAnalyticsAdapter.js +++ b/modules/intentIqAnalyticsAdapter.js @@ -1,4 +1,4 @@ -import { logInfo, logError } from '../src/utils.js'; +import { logInfo, logError, getWindowSelf, getWindowTop, getWindowLocation } from '../src/utils.js'; import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js'; import adapterManager from '../src/adapterManager.js'; import { ajax } from '../src/ajax.js'; @@ -218,10 +218,10 @@ function constructFullUrl(data) { export function getReferrer() { try { - if (window.self === window.top) { - return window.location.href; + if (getWindowSelf() === getWindowTop()) { + return getWindowLocation().href; } else { - return window.top.location.href; + return getWindowTop().location.href; } } catch (error) { logError(`Error accessing location: ${error}`); diff --git a/test/spec/modules/intentIqAnalyticsAdapter_spec.js b/test/spec/modules/intentIqAnalyticsAdapter_spec.js index 9a204dde31b..9209c758cd6 100644 --- a/test/spec/modules/intentIqAnalyticsAdapter_spec.js +++ b/test/spec/modules/intentIqAnalyticsAdapter_spec.js @@ -66,6 +66,9 @@ let wonRequest = { describe('IntentIQ tests all', function () { let logErrorStub; + let getWindowSelfStub; + let getWindowTopStub; + let getWindowLocationStub; beforeEach(function () { logErrorStub = sinon.stub(utils, 'logError'); @@ -93,6 +96,9 @@ describe('IntentIQ tests all', function () { afterEach(function () { logErrorStub.restore(); + if (getWindowSelfStub) getWindowSelfStub.restore(); + if (getWindowTopStub) getWindowTopStub.restore(); + if (getWindowLocationStub) getWindowLocationStub.restore(); config.getConfig.restore(); events.getEvents.restore(); iiqAnalyticsAnalyticsAdapter.disableAnalytics(); @@ -169,4 +175,34 @@ describe('IntentIQ tests all', function () { expect(iiqAnalyticsAnalyticsAdapter.initOptions.currentGroup).to.equal('B'); expect(iiqAnalyticsAnalyticsAdapter.initOptions.fpid).to.be.not.null; }); + + it('should return window.location.href when window.self === window.top', function () { + // Stub helper functions + getWindowSelfStub = sinon.stub(utils, 'getWindowSelf').returns(window); + getWindowTopStub = sinon.stub(utils, 'getWindowTop').returns(window); + getWindowLocationStub = sinon.stub(utils, 'getWindowLocation').returns({ href: 'http://localhost:9876/' }); + + const referrer = getReferrer(); + expect(referrer).to.equal('http://localhost:9876/'); + }); + + it('should return window.top.location.href when window.self !== window.top and access is successful', function () { + // Stub helper functions to simulate iframe + getWindowSelfStub = sinon.stub(utils, 'getWindowSelf').returns({}); + getWindowTopStub = sinon.stub(utils, 'getWindowTop').returns({ location: { href: 'http://example.com/' } }); + + const referrer = getReferrer(); + expect(referrer).to.equal('http://example.com/'); + }); + + it('should return an empty string and log an error when accessing window.top.location.href throws an error', function () { + // Stub helper functions to simulate error + getWindowSelfStub = sinon.stub(utils, 'getWindowSelf').returns({}); + getWindowTopStub = sinon.stub(utils, 'getWindowTop').throws(new Error('Access denied')); + + const referrer = getReferrer(); + expect(referrer).to.equal(''); + expect(logErrorStub.calledOnce).to.be.true; + expect(logErrorStub.firstCall.args[0]).to.contain('Error accessing location: Error: Access denied'); + }); });