Skip to content

Commit

Permalink
PubMatic to support DigiTrust Id passing (#3160)
Browse files Browse the repository at this point in the history
* in-dev changes

* included config

* Unit test cases for DigitrustId passing in PubMatic bid adapter

* eslint fixes

* removed a comment

* replaced "() => {" with "() => {"
  • Loading branch information
pm-harshad-mane authored and jsnellbaker committed Oct 11, 2018
1 parent 18107d8 commit 2903eb0
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 0 deletions.
43 changes: 43 additions & 0 deletions modules/pubmaticBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import * as utils from 'src/utils';
import { registerBidder } from 'src/adapters/bidderFactory';
import { BANNER, VIDEO } from 'src/mediaTypes';
import {config} from 'src/config';
const constants = require('src/constants.json');

const BIDDER_CODE = 'pubmatic';
const ENDPOINT = '//hbopenbid.pubmatic.com/translator?source=prebid-client';
const USYNCURL = '//ads.pubmatic.com/AdServer/js/showad.js#PIX&kdntuid=1&p=';
const DEFAULT_CURRENCY = 'USD';
const AUCTION_TYPE = 1;
const PUBMATIC_DIGITRUST_KEY = 'nFIn8aLzbd';
const UNDEFINED = undefined;
const CUSTOM_PARAMS = {
'kadpageurl': '', // Custom page url
Expand Down Expand Up @@ -275,6 +277,45 @@ function _createImpressionObject(bid, conf) {
return impObj;
}

function _getDigiTrustObject(key) {
function getDigiTrustId() {
let digiTrustUser = window.DigiTrust && (config.getConfig('digiTrustId') || window.DigiTrust.getUser({member: key}));
return (digiTrustUser && digiTrustUser.success && digiTrustUser.identity) || null;
}
let digiTrustId = getDigiTrustId();
// Verify there is an ID and this user has not opted out
if (!digiTrustId || (digiTrustId.privacy && digiTrustId.privacy.optout)) {
return null;
}
return digiTrustId;
}

function _handleDigitrustId(eids) {
let digiTrustId = _getDigiTrustObject(PUBMATIC_DIGITRUST_KEY);
if (digiTrustId !== null) {
eids.push({
'source': 'digitru.st',
'uids': [
{
'id': digiTrustId.id || '',
'atype': 1,
'ext': {
'keyv': parseInt(digiTrustId.keyv) || 0
}
}
]
});
}
}

function _handleEids(payload) {
let eids = [];
_handleDigitrustId(eids);
if (eids.length > 0) {
payload.user.eids = eids;
}
}

export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER, VIDEO],
Expand Down Expand Up @@ -414,6 +455,8 @@ export const spec = {
utils.logWarn(BIDDER_CODE + ': dctr value not found in 1st adunit, ignoring values from subsequent adunits');
}

_handleEids(payload);

return {
method: 'POST',
url: ENDPOINT,
Expand Down
192 changes: 192 additions & 0 deletions test/spec/modules/pubmaticBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {expect} from 'chai';
import {spec} from 'modules/pubmaticBidAdapter';
import * as utils from 'src/utils';
import {config} from 'src/config';
const constants = require('src/constants.json');

describe('PubMatic adapter', function () {
Expand Down Expand Up @@ -448,6 +449,197 @@ describe('PubMatic adapter', function () {
expect(data.imp[0].ext.pmZoneId).to.equal(bidRequests[0].params.pmzoneid.split(',').slice(0, 50).map(id => id.trim()).join()); // pmzoneid
});

it('Request should have digitrust params', function() {
window.DigiTrust = {
getUser: function () {
}
};
var bidRequest = {};
let sandbox = sinon.sandbox.create();
sandbox.stub(window.DigiTrust, 'getUser').callsFake(() =>
({
success: true,
identity: {
privacy: {optout: false},
id: 'testId',
keyv: 4
}
})
);

let request = spec.buildRequests(bidRequests, bidRequest);
let data = JSON.parse(request.data);
expect(data.user.eids).to.deep.equal([{
'source': 'digitru.st',
'uids': [{
'id': 'testId',
'atype': 1,
'ext': {
'keyv': 4
}
}]
}]);
sandbox.restore();
delete window.DigiTrust;
});

it('Request should not have digitrust params when DigiTrust not loaded', function() {
let request = spec.buildRequests(bidRequests, {});
let data = JSON.parse(request.data);
expect(data.user.eids).to.deep.equal(undefined);
});

it('Request should not have digitrust params due to optout', function() {
window.DigiTrust = {
getUser: function () {
}
};
let sandbox = sinon.sandbox.create();
sandbox.stub(window.DigiTrust, 'getUser').callsFake(() =>
({
success: true,
identity: {
privacy: {optout: true},
id: 'testId',
keyv: 4
}
})
);

let request = spec.buildRequests(bidRequests, {});
let data = JSON.parse(request.data);
expect(data.user.eids).to.deep.equal(undefined);
sandbox.restore();
delete window.DigiTrust;
});

it('Request should not have digitrust params due to failure', function() {
window.DigiTrust = {
getUser: function () {
}
};
let sandbox = sinon.sandbox.create();
sandbox.stub(window.DigiTrust, 'getUser').callsFake(() =>
({
success: false,
identity: {
privacy: {optout: false},
id: 'testId',
keyv: 4
}
})
);

let request = spec.buildRequests(bidRequests, {});
let data = JSON.parse(request.data);
expect(data.user.eids).to.deep.equal(undefined);
sandbox.restore();
delete window.DigiTrust;
});

describe('DigiTrustId from config', function() {
var origGetConfig;
let sandbox;
beforeEach(() => {
sandbox = sinon.sandbox.create();
window.DigiTrust = {
getUser: sandbox.spy()
};
});

afterEach(() => {
sandbox.restore();
delete window.DigiTrust;
});

it('Request should have digiTrustId config params', function() {
sandbox.stub(config, 'getConfig').callsFake((key) => {
var config = {
digiTrustId: {
success: true,
identity: {
privacy: {optout: false},
id: 'testId',
keyv: 4
}
}
};
return config[key];
});

let request = spec.buildRequests(bidRequests, {});
let data = JSON.parse(request.data);
expect(data.user.eids).to.deep.equal([{
'source': 'digitru.st',
'uids': [{
'id': 'testId',
'atype': 1,
'ext': {
'keyv': 4
}
}]
}]);
// should not have called DigiTrust.getUser()
expect(window.DigiTrust.getUser.notCalled).to.equal(true);
});

it('Request should not have digiTrustId config params due to optout', function() {
sandbox.stub(config, 'getConfig').callsFake((key) => {
var config = {
digiTrustId: {
success: true,
identity: {
privacy: {optout: true},
id: 'testId',
keyv: 4
}
}
}
return config[key];
});
let request = spec.buildRequests(bidRequests, {});
let data = JSON.parse(request.data);
expect(data.user.eids).to.deep.equal(undefined);
// should not have called DigiTrust.getUser()
expect(window.DigiTrust.getUser.notCalled).to.equal(true);
});

it('Request should not have digiTrustId config params due to failure', function() {
sandbox.stub(config, 'getConfig').callsFake((key) => {
var config = {
digiTrustId: {
success: false,
identity: {
privacy: {optout: false},
id: 'testId',
keyv: 4
}
}
}
return config[key];
});

let request = spec.buildRequests(bidRequests, {});
let data = JSON.parse(request.data);
expect(data.user.eids).to.deep.equal(undefined);
// should not have called DigiTrust.getUser()
expect(window.DigiTrust.getUser.notCalled).to.equal(true);
});

it('Request should not have digiTrustId config params if they do not exist', function() {
sandbox.stub(config, 'getConfig').callsFake((key) => {
var config = {};
return config[key];
});

let request = spec.buildRequests(bidRequests, {});
let data = JSON.parse(request.data);
expect(data.user.eids).to.deep.equal(undefined);
// should have called DigiTrust.getUser() once
expect(window.DigiTrust.getUser.calledOnce).to.equal(true);
});
});

it('Request params check for video ad', function () {
let request = spec.buildRequests(videoBidRequests);
let data = JSON.parse(request.data);
Expand Down

0 comments on commit 2903eb0

Please sign in to comment.