Skip to content

Commit

Permalink
[33Across adapter] Map ad unit path to element id (prebid#3920)
Browse files Browse the repository at this point in the history
* fix return of _mapAdUnitPathToElementId()

* improve logging of _mapAdUnitPathToElementId()

* do not use Array.find()

* return id once element is found

* return id once element is found

* let -> const
  • Loading branch information
glebglushtsov authored and sa1omon committed Nov 28, 2019
1 parent fc1e5bd commit 8b4c78d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
48 changes: 35 additions & 13 deletions modules/33acrossBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { registerBidder } from '../src/adapters/bidderFactory';
import { config } from '../src/config';
import * as utils 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';
Expand Down Expand Up @@ -32,17 +31,43 @@ function _isViewabilityMeasurable(element) {
}

function _getViewability(element, topWin, { w, h } = {}) {
return utils.getWindowTop().document.visibilityState === 'visible'
return topWin.document.visibilityState === 'visible'
? _getPercentInView(element, topWin, { w, h })
: 0;
}

function _mapAdUnitPathToElementId(adUnitCode) {
if (utils.isGptPubadsDefined()) {
const adSlots = googletag.pubads().getSlots();
const isMatchingAdSlot = utils.isSlotMatchingAdUnitCode(adUnitCode);

for (let i = 0; i < adSlots.length; i++) {
if (isMatchingAdSlot(adSlots[i])) {
const id = adSlots[i].getSlotElementId();

utils.logInfo(`[33Across Adapter] Map ad unit path to HTML element id: '${adUnitCode}' -> ${id}`);

return id;
}
}
}

utils.logWarn(`[33Across Adapter] Unable to locate element for ad unit code: '${adUnitCode}'`);

return null;
}

function _getAdSlotHTMLElement(adUnitCode) {
return document.getElementById(adUnitCode) ||
document.getElementById(_mapAdUnitPathToElementId(adUnitCode));
}

// Infer the necessary data from valid bid for a minimal ttxRequest and create HTTP request
// NOTE: At this point, TTX only accepts request for a single impression
function _createServerRequest(bidRequest, gdprConsent) {
const ttxRequest = {};
const params = bidRequest.params;
const element = document.getElementById(bidRequest.adUnitCode);
const element = _getAdSlotHTMLElement(bidRequest.adUnitCode);
const sizes = _transformSizes(bidRequest.sizes);
const minSize = _getMinSize(sizes);

Expand All @@ -52,10 +77,6 @@ function _createServerRequest(bidRequest, gdprConsent) {

const contributeViewability = ViewabilityContributor(viewabilityAmount);

if (element === null) {
utils.logWarn(`Unable to locate element with id: '${bidRequest.adUnitCode}'`);
}

/*
* Infer data for the request payload
*/
Expand Down Expand Up @@ -264,13 +285,14 @@ function isBidRequestValid(bid) {
// - the server, at this point, also doesn't need the consent string to handle gdpr compliance. So passing
// value whether set or not, for the sake of future dev.
function buildRequests(bidRequests, bidderRequest) {
const gdprConsent = Object.assign({ consentString: undefined, gdprApplies: false }, bidderRequest && bidderRequest.gdprConsent);
const gdprConsent = Object.assign({
consentString: undefined,
gdprApplies: false
}, bidderRequest && bidderRequest.gdprConsent);

adapterState.uniqueSiteIds = bidRequests.map(req => req.params.siteId).filter(utils.uniques);

return bidRequests.map((req) => {
return _createServerRequest(req, gdprConsent);
});
return bidRequests.map(req => _createServerRequest(req, gdprConsent));
}

// NOTE: At this point, the response from 33exchange will only ever contain one bid i.e. the highest bid
Expand Down
2 changes: 1 addition & 1 deletion modules/33acrossBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Connects to 33Across's exchange for bids.
```
var adUnits = [
{
code: '33across-hb-ad-123456-1',
code: '33across-hb-ad-123456-1', // ad slot HTML element ID
sizes: [
[300, 250],
[728, 90]
Expand Down
4 changes: 2 additions & 2 deletions test/spec/modules/33acrossBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ describe('33acrossBidAdapter:', function () {

utils.getWindowTop.restore();
utils.getWindowSelf.restore();
sandbox.stub(utils, 'getWindowTop').returns(win);
sandbox.stub(utils, 'getWindowSelf').returns({});
sandbox.stub(utils, 'getWindowTop').returns({});
sandbox.stub(utils, 'getWindowSelf').returns(win);

expect(spec.buildRequests(bidRequests)).to.deep.equal([ serverRequest ]);
});
Expand Down

0 comments on commit 8b4c78d

Please sign in to comment.