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

Nativo Bid Adapter: update to adUnit param usage #7517

Merged
merged 15 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
73 changes: 44 additions & 29 deletions modules/nativoBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { isEmpty } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
import { deepAccess, isEmpty } from '../src/utils.js'
import { registerBidder } from '../src/adapters/bidderFactory.js'
import { BANNER } from '../src/mediaTypes.js'
// import { config } from 'src/config'

const BIDDER_CODE = 'nativo';
const BIDDER_ENDPOINT = 'https://exchange.postrelease.com/prebid';
const BIDDER_CODE = 'nativo'
const BIDDER_ENDPOINT = 'https://exchange.postrelease.com/prebid'

const GVLID = 263;
const GVLID = 263

const TIME_TO_LIVE = 360;
const TIME_TO_LIVE = 360

const SUPPORTED_AD_TYPES = [BANNER];
const SUPPORTED_AD_TYPES = [BANNER]

const bidRequestMap = {};
const adUnitsRequested = {};
const bidRequestMap = {}
const adUnitsRequested = {}

// Prebid adapter referrence doc: https://docs.prebid.org/dev-docs/bidder-adaptor.html

Expand All @@ -30,7 +30,7 @@ export const spec = {
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function (bid) {
return bid.params && !!bid.params.placementId
return true
},

/**
Expand All @@ -42,27 +42,37 @@ export const spec = {
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests, bidderRequest) {
const placementIds = []
const placementIds = new Set()
const placmentBidIdMap = {}
let placementId, pageUrl
validBidRequests.forEach((request) => {
pageUrl = pageUrl || request.params.url // Use the first url value found
placementId = request.params.placementId
placementIds.push(placementId)
placmentBidIdMap[placementId] = {
pageUrl = deepAccess(
request,
'params.url',
bidderRequest.refererInfo.referer
)
placementId = deepAccess(request, 'params.placementId')

if (placementId) {
placementIds.add(placementId)
}

var key = placementId || request.adUnitCode
placmentBidIdMap[key] = {
bidId: request.bidId,
size: getLargestSize(request.sizes),
}
})
bidRequestMap[bidderRequest.bidderRequestId] = placmentBidIdMap

if (!pageUrl) pageUrl = bidderRequest.refererInfo.referer

// Build adUnit data
const adUnitData = {
adUnits: validBidRequests.map((adUnit) => {
// Track if we've already requested for this ad unit code
adUnitsRequested[adUnit.adUnitCode] = adUnitsRequested[adUnit.adUnitCode] !== undefined ? adUnitsRequested[adUnit.adUnitCode]++ : 0
adUnitsRequested[adUnit.adUnitCode] =
adUnitsRequested[adUnit.adUnitCode] !== undefined
? adUnitsRequested[adUnit.adUnitCode]++
: 0
return {
adUnitCode: adUnit.adUnitCode,
mediaTypes: adUnit.mediaTypes,
Expand All @@ -72,22 +82,25 @@ export const spec = {

// Build QS Params
let params = [
{ key: 'ntv_ptd', value: placementIds.toString() },
{ key: 'ntv_pb_rid', value: bidderRequest.bidderRequestId },
{
key: 'ntv_ppc',
value: btoa(JSON.stringify(adUnitData)), // Convert to Base 64
},
{
key: 'ntv_dbr',
value: btoa(JSON.stringify(adUnitsRequested))
value: btoa(JSON.stringify(adUnitsRequested)),
},
{
key: 'ntv_url',
value: encodeURIComponent(pageUrl),
}
},
]

if (placementIds.size > 0) {
params.unshift({ key: 'ntv_ptd', value: [...placementIds].join(',') })
}

if (bidderRequest.gdprConsent) {
// Put on the beginning of the qs param array
params.unshift({
Expand Down Expand Up @@ -135,7 +148,7 @@ export const spec = {
let bidResponse, adUnit
seatbids.forEach((seatbid) => {
seatbid.bid.forEach((bid) => {
adUnit = this.getRequestId(body.id, bid.impid)
adUnit = this.getAdUnitData(body.id, bid)
bidResponse = {
requestId: adUnit.bidId,
cpm: bid.price,
Expand Down Expand Up @@ -268,14 +281,16 @@ export const spec = {
/**
* Maps Prebid's bidId to Nativo's placementId values per unique bidderRequestId
* @param {String} bidderRequestId - The unique ID value associated with the bidderRequest
* @param {String} placementId - The placement ID value from Nativo
* @param {Object} bid - The placement ID value from Nativo
* @returns {String} - The bidId value associated with the corresponding placementId
*/
getRequestId: function (bidderRequestId, placementId) {
return (
bidRequestMap[bidderRequestId] &&
bidRequestMap[bidderRequestId][placementId]
)
getAdUnitData: function (bidderRequestId, bid) {
var data = deepAccess(bidRequestMap, `${bidderRequestId}.${bid.impid}`)

if (data) return data

var unitCode = deepAccess(bid, 'ext.ad_unit_id')
return deepAccess(bidRequestMap, `${bidderRequestId}.${unitCode}`)
},
}
registerBidder(spec)
Expand Down
1 change: 0 additions & 1 deletion modules/nativoBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ var adUnits = [
bids: [{
bidder: 'nativo',
params: {
placementId: 1125609,
url: 'https://test-sites.internal.nativo.net/testing/prebid_adpater.html'
}
}]
Expand Down
53 changes: 50 additions & 3 deletions test/spec/modules/nativoBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ describe('nativoBidAdapterTests', function () {
expect(spec.isBidRequestValid(bid)).to.equal(true)
})

it('should return false when required params are not passed', function () {
it('should return true when params are not passed', function () {
let bid2 = Object.assign({}, bid)
delete bid2.params
bid2.params = {}
expect(spec.isBidRequestValid(bid2)).to.equal(false)
expect(spec.isBidRequestValid(bid2)).to.equal(true)
})
})

Expand Down Expand Up @@ -129,7 +129,12 @@ describe('interpretResponse', function () {
}

// mock
spec.getRequestId = () => 123456
spec.getAdUnitData = () => {
return {
bidId: 123456,
sizes: [300, 250],
}
}

let result = spec.interpretResponse({ body: response }, { bidderRequest })
expect(Object.keys(result[0])).to.have.deep.members(
Expand Down Expand Up @@ -232,3 +237,45 @@ describe('getUserSyncs', function () {
)
})
})

describe('getAdUnitData', () => {
afterEach(() => {
if (window.bidRequestMap) delete window.bidRequestMap
})

it('Matches placementId value', () => {
const adUnitData = {
bidId: 123456,
sizes: [300, 250],
}

window.bidRequestMap = {
9876543: {
12345: adUnitData,
},
}

const data = spec.getAdUnitData(9876543, { impid: 12345 })
expect(Object.keys(data)).to.have.deep.members(
Object.keys(adUnitData)
)
})

it('Falls back to ad unit code value', () => {
const adUnitData = {
bidId: 123456,
sizes: [300, 250],
}

window.bidRequestMap = {
9876543: {
'#test-code': adUnitData,
},
}

const data = spec.getAdUnitData(9876543, { impid: 12345, ext: { ad_unit_code: '#test-code' } })
expect(Object.keys(data)).to.have.deep.members(
Object.keys(adUnitData)
)
})
})