Skip to content

Commit

Permalink
Intentiq Analytics: Referrer Info Update (prebid#12155)
Browse files Browse the repository at this point in the history
* improve referrer for more accurate reporting

* add unit tests
  • Loading branch information
eyvazahmadzada authored Sep 5, 2024
1 parent d9676ce commit a6fc47e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
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

0 comments on commit a6fc47e

Please sign in to comment.