Skip to content

Commit

Permalink
Added user sync support for undertone bid adapter (#3172)
Browse files Browse the repository at this point in the history
* Added user sync support for undertone bid adapter (new pull request)

* Added user sync support for undertone bid adapter

* fix indentation

* Changed utils.getWindowTop() with the newer prebid utilities
  • Loading branch information
omerko authored and jaiminpanchal27 committed Oct 29, 2018
1 parent fda63ec commit 67b24c9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 17 deletions.
77 changes: 63 additions & 14 deletions modules/undertoneBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,44 @@
* Adapter to send bids to Undertone
*/

import * as utils from 'src/utils';
import * as urlUtils from 'src/url';
import { registerBidder } from 'src/adapters/bidderFactory';

const BIDDER_CODE = 'undertone';
const URL = '//hb.undertone.com/hb';
const FRAME_USER_SYNC = '//cdn.undertone.com/js/usersync.html';
const PIXEL_USER_SYNC_1 = '//usr.undertone.com/userPixel/syncOne?id=1&of=2';
const PIXEL_USER_SYNC_2 = '//usr.undertone.com/userPixel/syncOne?id=2&of=2';

function getCanonicalUrl() {
try {
let doc = window.top.document;
let element = doc.querySelector("link[rel='canonical']");
if (element !== null) {
return element.href;
}
} catch (e) {
}
return null;
}

function extractDomainFromHost(pageHost) {
let domain = null;
try {
let domains = /[-\w]+\.([-\w]+|[-\w]{3,}|[-\w]{1,3}\.[-\w]{2})$/i.exec(pageHost);
if (domains != null && domains.length > 0) {
domain = domains[0];
for (let i = 1; i < domains.length; i++) {
if (domains[i].length > domain.length) {
domain = domains[i];
}
}
}
} catch (e) {
domain = null;
}
return domain;
}

export const spec = {
code: BIDDER_CODE,
Expand All @@ -16,21 +49,14 @@ export const spec = {
return true;
}
},
buildRequests: function(validBidRequests) {
buildRequests: function(validBidRequests, bidderRequest) {
const payload = {
'x-ut-hb-params': []
};
const location = utils.getTopWindowLocation();
let domains = /[-\w]+\.([-\w]+|[-\w]{3,}|[-\w]{1,3}\.[-\w]{2})$/i.exec(location.host);
let domain = null;
if (domains != null && domains.length > 0) {
domain = domains[0];
for (let i = 1; i < domains.length; i++) {
if (domains[i].length > domain.length) {
domain = domains[i];
}
}
}
const referer = bidderRequest.refererInfo.referer;
const hostname = urlUtils.parse(referer).hostname;
let domain = extractDomainFromHost(hostname);
const pageUrl = getCanonicalUrl() || referer;

const pubid = validBidRequests[0].params.publisherId;
const REQ_URL = `${URL}?pid=${pubid}&domain=${domain}`;
Expand All @@ -39,7 +65,7 @@ export const spec = {
const bid = {
bidRequestId: bidReq.bidId,
hbadaptor: 'prebid',
url: location.href,
url: pageUrl,
domain: domain,
placementId: bidReq.params.placementId != undefined ? bidReq.params.placementId : null,
publisherId: bidReq.params.publisherId,
Expand Down Expand Up @@ -78,6 +104,29 @@ export const spec = {
});
}
return bids;
},
getUserSyncs: function(syncOptions, serverResponses, gdprConsent) {
const syncs = [];
if (gdprConsent && gdprConsent.gdprApplies === true) {
return syncs;
}

if (syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: FRAME_USER_SYNC
});
} else if (syncOptions.pixelEnabled) {
syncs.push({
type: 'image',
url: PIXEL_USER_SYNC_1
},
{
type: 'image',
url: PIXEL_USER_SYNC_2
});
}
return syncs;
}
};
registerBidder(spec);
37 changes: 34 additions & 3 deletions test/spec/modules/undertoneBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const bidReq = [{
auctionId: '6c22f5a5-59df-4dc6-b92c-f433bcf0a874'
}];

const bidderReq = {
refererInfo: {
referer: 'http://prebid.org/dev-docs/bidder-adaptor.html'
}
};

const validBidRes = {
ad: '<div>Hello</div>',
publisherId: 12345,
Expand Down Expand Up @@ -98,14 +104,16 @@ describe('Undertone Adapter', function () {
});
describe('build request', function () {
it('should send request to correct url via POST', function () {
const request = spec.buildRequests(bidReq);
const domain = null;
const request = spec.buildRequests(bidReq, bidderReq);
const domainStart = bidderReq.refererInfo.referer.indexOf('//');
const domainEnd = bidderReq.refererInfo.referer.indexOf('/', domainStart + 2);
const domain = bidderReq.refererInfo.referer.substring(domainStart + 2, domainEnd);
const REQ_URL = `${URL}?pid=${bidReq[0].params.publisherId}&domain=${domain}`;
expect(request.url).to.equal(REQ_URL);
expect(request.method).to.equal('POST');
});
it('should have all relevant fields', function () {
const request = spec.buildRequests(bidReq);
const request = spec.buildRequests(bidReq, bidderReq);
const bid1 = JSON.parse(request.data)['x-ut-hb-params'][0];
expect(bid1.bidRequestId).to.equal('263be71e91dd9d');
expect(bid1.sizes.length).to.equal(2);
Expand Down Expand Up @@ -150,4 +158,27 @@ describe('Undertone Adapter', function () {
expect(spec.interpretResponse({ body: bidResArray }).length).to.equal(1);
});
});

describe('getUserSyncs', () => {
it('verifies gdpr consent checked', () => {
const options = ({ iframeEnabled: true, pixelEnabled: true });
expect(spec.getUserSyncs(options, {}, { gdprApplies: true }).length).to.equal(0);
});

it('Verifies sync iframe option', function () {
const result = spec.getUserSyncs({ iframeEnabled: true, pixelEnabled: true });
expect(result).to.have.lengthOf(1);
expect(result[0].type).to.equal('iframe');
expect(result[0].url).to.equal('//cdn.undertone.com/js/usersync.html');
});

it('Verifies sync image option', function () {
const result = spec.getUserSyncs({ pixelEnabled: true });
expect(result).to.have.lengthOf(2);
expect(result[0].type).to.equal('image');
expect(result[0].url).to.equal('//usr.undertone.com/userPixel/syncOne?id=1&of=2');
expect(result[1].type).to.equal('image');
expect(result[1].url).to.equal('//usr.undertone.com/userPixel/syncOne?id=2&of=2');
});
});
});

0 comments on commit 67b24c9

Please sign in to comment.