Skip to content

Commit

Permalink
BeOp Bid Adapter: prefer canonical URL when present & prepend protocol (
Browse files Browse the repository at this point in the history
#8391)

* [BeOp] prefer canonical URL when present & prepend protocol

* [BeOp] get the right page url in tracking events too

* [BeOp] improve page url resolver

* BeOp Bid Adapter: 'top.location.protocol' can also throw
  • Loading branch information
cyppan authored May 17, 2022
1 parent 7f6914c commit 49ea721
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
57 changes: 51 additions & 6 deletions modules/beopBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { deepAccess, isArray, logWarn, triggerPixel, buildUrl, logInfo, getValue, getBidIdParameter } from '../src/utils.js';
import { getRefererInfo } from '../src/refererDetection.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { config } from '../src/config.js';
const BIDDER_CODE = 'beop';
Expand Down Expand Up @@ -36,11 +37,11 @@ export const spec = {
*/
buildRequests: function(validBidRequests, bidderRequest) {
const slots = validBidRequests.map(beOpRequestSlotsMaker);
let pageUrl = deepAccess(window, 'location.href') || deepAccess(bidderRequest, 'refererInfo.canonicalUrl') || config.getConfig('pageUrl');
let fpd = config.getLegacyFpd(config.getConfig('ortb2'));
let gdpr = bidderRequest.gdprConsent;
let firstSlot = slots[0];
let payloadObject = {
const pageUrl = getPageUrl(bidderRequest.refererInfo, window);
const fpd = config.getLegacyFpd(config.getConfig('ortb2'));
const gdpr = bidderRequest.gdprConsent;
const firstSlot = slots[0];
const payloadObject = {
at: new Date().toString(),
nid: firstSlot.nid,
nptnid: firstSlot.nptnid,
Expand Down Expand Up @@ -100,6 +101,7 @@ export const spec = {

function buildTrackingParams(data, info, value) {
const accountId = data.params.accountId;
const pageUrl = getPageUrl(null, window);
return {
pid: accountId === undefined ? data.ad.match(/account: \“([a-f\d]{24})\“/)[1] : accountId,
nid: data.params.networkId,
Expand All @@ -110,7 +112,7 @@ function buildTrackingParams(data, info, value) {
se_ca: 'bid',
se_ac: info,
se_va: value,
url: window.location.href
url: pageUrl
};
}

Expand Down Expand Up @@ -141,4 +143,47 @@ function beOpRequestSlotsMaker(bid) {
}
}

const protocolRelativeRegExp = /^\/\//
function isProtocolRelativeUrl(url) {
return url && url.match(protocolRelativeRegExp) != null;
}

const withProtocolRegExp = /[a-z]{1,}:\/\//
function isNoProtocolUrl(url) {
return url && url.match(withProtocolRegExp) == null;
}

function ensureProtocolInUrl(url, defaultProtocol) {
if (isProtocolRelativeUrl(url)) {
return `${defaultProtocol}${url}`;
} else if (isNoProtocolUrl(url)) {
return `${defaultProtocol}//${url}`;
}
return url;
}

/**
* sometimes trying to access a field (protected?) triggers an exception
* Ex deepAccess(window, 'top.location.href') might throw if it crosses origins
* so here is a lenient version
*/
function safeDeepAccess(obj, path) {
try {
return deepAccess(obj, path)
} catch (_e) {
return null;
}
}

function getPageUrl(refererInfo, window) {
refererInfo = refererInfo || getRefererInfo();
let pageUrl = refererInfo.canonicalUrl || safeDeepAccess(window, 'top.location.href') || deepAccess(window, 'location.href');
// Ensure the protocol is present (looks like sometimes the extracted pageUrl misses it)
if (pageUrl != null) {
const defaultProtocol = safeDeepAccess(window, 'top.location.protocol') || deepAccess(window, 'location.protocol');
pageUrl = ensureProtocolInUrl(pageUrl, defaultProtocol);
}
return pageUrl;
}

registerBidder(spec);
17 changes: 15 additions & 2 deletions test/spec/modules/beopBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('BeOp Bid Adapter tests', () => {
},
'refererInfo':
{
'canonicalUrl': 'http://test.te'
'canonicalUrl': 'test.te'
}
};

Expand All @@ -124,7 +124,20 @@ describe('BeOp Bid Adapter tests', () => {
expect(payload.tc_string).to.exist;
expect(payload.tc_string).to.equal('BOJ8RZsOJ8RZsABAB8AAAAAZ+A==');
expect(payload.url).to.exist;
expect(payload.url).to.equal('http://localhost:9876/context.html');
// check that the protocol is added correctly
expect(payload.url).to.equal('http://test.te');
});

it('should not prepend the protocol in page url if already present', function () {
const bidderRequest = {
'refererInfo': {
'canonicalUrl': 'https://test.te'
}
};
const request = spec.buildRequests(bidRequests, bidderRequest);
const payload = JSON.parse(request.data);
expect(payload.url).to.exist;
expect(payload.url).to.equal('https://test.te');
});
});

Expand Down

0 comments on commit 49ea721

Please sign in to comment.