Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intentiq Analytics: Referrer Info Update #12155

Merged
merged 5 commits into from
Sep 5, 2024
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
13 changes: 11 additions & 2 deletions modules/intentIqAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -229,7 +229,16 @@ function constructFullUrl(data) {
}

export function getReferrer() {
return document.referrer;
try {
if (getWindowSelf() === getWindowTop()) {
return getWindowLocation().href;
} else {
return getWindowTop().location.href;
}
} catch (error) {
logError(`Error accessing location: ${error}`);
return '';
}
}

iiqAnalyticsAnalyticsAdapter.originEnableAnalytics = iiqAnalyticsAnalyticsAdapter.enableAnalytics;
Expand Down
36 changes: 36 additions & 0 deletions test/spec/modules/intentIqAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ let wonRequest = {

describe('IntentIQ tests all', function () {
let logErrorStub;
let getWindowSelfStub;
let getWindowTopStub;
let getWindowLocationStub;
let detectBrowserStub;

beforeEach(function () {
Expand Down Expand Up @@ -96,6 +99,9 @@ describe('IntentIQ tests all', function () {

afterEach(function () {
logErrorStub.restore();
if (getWindowSelfStub) getWindowSelfStub.restore();
if (getWindowTopStub) getWindowTopStub.restore();
if (getWindowLocationStub) getWindowLocationStub.restore();
if (detectBrowserStub) detectBrowserStub.restore();
config.getConfig.restore();
events.getEvents.restore();
Expand Down Expand Up @@ -176,6 +182,36 @@ describe('IntentIQ tests all', function () {
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');
});

it('should not send request if the browser is in blacklist (chrome)', function () {
const USERID_CONFIG_BROWSER = [...USERID_CONFIG];
USERID_CONFIG_BROWSER[0].params.browserBlackList = 'ChrOmE';
Expand Down