Skip to content

Commit

Permalink
Merge pull request #5 from 33Across/user_sync_refactor
Browse files Browse the repository at this point in the history
Sync based on unique site id
  • Loading branch information
curlyblueeagle authored Jul 24, 2018
2 parents b80e2aa + d711c7c commit dfe216a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 59 deletions.
53 changes: 26 additions & 27 deletions modules/33acrossBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { userSync } from 'src/userSync'
import { uniques } from 'src/utils';
const { registerBidder } = require('../src/adapters/bidderFactory');
const { config } = require('../src/config');

const BIDDER_CODE = '33across';
const END_POINT = 'https://ssc.33across.com/api/v1/hb';
const SYNC_ENDPOINT = 'https://de.tynt.com/deb/v2?m=xch&rt=html';

const adapterState = {};

// All this assumes that only one bid is ever returned by ttx
function _createBidResponse(response) {
return {
Expand Down Expand Up @@ -42,9 +43,11 @@ function _createServerRequest(bidRequest) {
}
}
ttxRequest.site = { id: params.siteId };

// Go ahead send the bidId in request to 33exchange so it's kept track of in the bid response and
// therefore in ad targetting process
ttxRequest.id = bidRequest.bidId;

// Finally, set the openRTB 'test' param if this is to be a test bid
if (params.test === 1) {
ttxRequest.test = 1;
Expand All @@ -70,6 +73,17 @@ function _createServerRequest(bidRequest) {
}
}

// Sync object will always be of type iframe for TTX
function _createSync(siteId) {
const ttxSettings = config.getConfig('ttxSettings');
const syncUrl = (ttxSettings && ttxSettings.syncUrl) || SYNC_ENDPOINT;

return {
type: 'iframe',
url: `${syncUrl}&id=${siteId}`
}
}

function _getFormatSize(sizeArr) {
return {
w: sizeArr[0],
Expand All @@ -78,24 +92,6 @@ function _getFormatSize(sizeArr) {
}
}

// Register one sync per bid since each ad unit may potenitally be linked to a uniqe guid
// Sync type will always be 'iframe' for 33Across
function _registerUserSyncs(requestData) {
let ttxRequest;
try {
ttxRequest = JSON.parse(requestData);
} catch (err) {
// No point in trying to register sync since the requisite data cannot be parsed.
return;
}
const ttxSettings = config.getConfig('ttxSettings');

let syncUrl = (ttxSettings && ttxSettings.syncUrl) || SYNC_ENDPOINT;

syncUrl = `${syncUrl}&id=${ttxRequest.site.id}`;
userSync.registerSync('iframe', BIDDER_CODE, syncUrl);
}

function isBidRequestValid(bid) {
if (bid.bidder !== BIDDER_CODE || typeof bid.params === 'undefined') {
return false;
Expand All @@ -108,18 +104,15 @@ function isBidRequestValid(bid) {
return true;
}

// NOTE: At this point, 33exchange only accepts request for a single impression
// NOTE: At this point, TTX only accepts request for a single impression
function buildRequests(bidRequests) {
adapterState.uniqueSiteIds = bidRequests.map(req => req.params.siteId).filter(uniques);

return bidRequests.map(_createServerRequest);
}

// NOTE: At this point, the response from 33exchange will only ever contain one bid i.e. the highest bid
function interpretResponse(serverResponse, bidRequest) {
// Register user sync first
if (bidRequest && bidRequest.data) {
_registerUserSyncs(bidRequest.data);
}

const bidResponses = [];

// If there are bids, look at the first bid of the first seatbid (see NOTE above for assumption about ttx)
Expand All @@ -130,11 +123,17 @@ function interpretResponse(serverResponse, bidRequest) {
return bidResponses;
}

// Register one sync per unique guid
function getUserSyncs(syncOptions) {
return (syncOptions.iframeEnabled) ? adapterState.uniqueSiteIds.map(_createSync) : ([]);
}

const spec = {
code: BIDDER_CODE,
isBidRequestValid,
buildRequests,
interpretResponse
interpretResponse,
getUserSyncs
}

registerBidder(spec);
Expand Down
91 changes: 59 additions & 32 deletions test/spec/modules/33acrossBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,43 +404,70 @@ describe('33acrossBidAdapter:', function () {
expect(interpretResponse({ body: serverResponse }, this.serverRequest)).to.deep.equal([ bidResponse ]);
});
});
});

context('and register user sync', function() {
it('via the production endpoint', function() {
const spy = this.sandbox.spy(userSync, 'registerSync');
const serverResponse = {
cur: 'USD',
ext: {},
id: 'b1',
seatbid: []
describe('getUserSyncs', function() {
beforeEach(function() {
this.syncs = [
{
type: 'iframe',
url: 'https://de.tynt.com/deb/v2?m=xch&rt=html&id=id1'
},
{
type: 'iframe',
url: 'https://de.tynt.com/deb/v2?m=xch&rt=html&id=id2'
},
];
this.bidRequests = [
{
bidId: 'b1',
bidder: '33across',
bidderRequestId: 'b1a',
params: {
siteId: 'id1',
productId: 'foo'
},
adUnitCode: 'div-id',
auctionId: 'r1',
sizes: [
[ 300, 250 ]
],
transactionId: 't1'
},
{
bidId: 'b2',
bidder: '33across',
bidderRequestId: 'b2a',
params: {
siteId: 'id2',
productId: 'foo'
},
adUnitCode: 'div-id',
auctionId: 'r1',
sizes: [
[ 300, 250 ]
],
transactionId: 't2'
}
interpretResponse({ body: serverResponse }, this.serverRequest);
const syncUrl = `${SYNC_ENDPOINT}&id=${this.ttxRequest.site.id}`;
];
});

const registerSyncCalled = spy.calledWith('iframe', '33across', syncUrl);
expect(registerSyncCalled).to.be.true;
context('when iframe is not enabled', function() {
it('returns empty sync array', function() {
const syncOptions = {};
buildRequests(this.bidRequests);
expect(getUserSyncs(syncOptions)).to.deep.equal([]);
});
});

it('via the test endpoint', function() {
const spy = this.sandbox.spy(userSync, 'registerSync');

this.sandbox.stub(config, 'getConfig').callsFake(() => {
return {
'syncUrl': 'https://foo.com/deb/v2?m=xch'
}
});

const serverResponse = {
cur: 'USD',
ext: {},
id: 'b1',
seatbid: []
}
interpretResponse({ body: serverResponse }, this.serverRequest);
const syncUrl = `https://foo.com/deb/v2?m=xch&id=${this.ttxRequest.site.id}`;

const registerSyncCalled = spy.calledWith('iframe', '33across', syncUrl);
expect(registerSyncCalled).to.be.true;
context('when iframe is enabled', function() {
it('returns sync array equal to number of unique siteIDs', function() {
const syncOptions = {
iframeEnabled: true
};
buildRequests(this.bidRequests);
const syncs = getUserSyncs(syncOptions);
expect(syncs).to.deep.equal(this.syncs);
});
});
});
Expand Down

0 comments on commit dfe216a

Please sign in to comment.