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

Added user sync support for undertone bid adapter #3172

Merged
merged 5 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
59 changes: 51 additions & 8 deletions modules/undertoneBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ 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 = utils.getWindowTop().document;
let element = doc.querySelector("link[rel='canonical']");
if (element !== null) {
return element.href;
}
} catch (e) {
}
return null;
}

export const spec = {
code: BIDDER_CODE,
Expand All @@ -20,26 +35,31 @@ export const spec = {
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];
try {
const location = utils.getWindowTop().location;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use http://prebid.org/dev-docs/bidder-adaptor.html#referrers. You can use referer property to get top level host
Very soon we will be deprecating utils.getWindowTop()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi
Simply getting the referrer will not help me in this case.
I'm trying to retrieve the canonical url from the <link rel="canonical".. > element <link (https://www.w3.org/TR/capability-urls/#canonical-urls)
Only in case it does not exist, I need to use the full referrer url.

Will you support such functionality to access the top level document?

Copy link
Contributor Author

@omerko omerko Oct 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try to access the top level document myself.

But regarding the new API to get the referer, I need to get the hostname out of it.
since it's a string I cannot do location.host, but can I use the URL api: https://developer.mozilla.org/en-US/docs/Web/API/URL
it does not support older IE versions

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@omerko
To get host you can use https://github.com/prebid/Prebid.js/blob/master/src/url.js#L27

To get canonical url, your current solution is fine. I will talk to team and try to add this feature. Can you create an issue with this feature request ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I used the host extraction the way you suggested and for canonical I replaced getWindowTop with direct access to the window.top from adapter code.

Also I created a feature request for the canonical URL extraction here: #3221

let domains = /[-\w]+\.([-\w]+|[-\w]{3,}|[-\w]{1,3}\.[-\w]{2})$/i.exec(location.host);
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;
}

const pubid = validBidRequests[0].params.publisherId;
const REQ_URL = `${URL}?pid=${pubid}&domain=${domain}`;
const pageUrl = getCanonicalUrl() || location.href;

validBidRequests.map(bidReq => {
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 +98,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);
23 changes: 23 additions & 0 deletions test/spec/modules/undertoneBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,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');
});
});
});