Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from scm-spain/feature/add_bidderSettings_method
Browse files Browse the repository at this point in the history
Feature/add bidder settings method
  • Loading branch information
rafamarquezv authored Jan 24, 2019
2 parents 16d2b39 + 6096222 commit 69d3c65
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 60 deletions.
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ const appNexusConnector = AppNexusConnector.init({
member: 4242
},
prebidConfig: {
config: {
core: {
bidderTimeout: 1000,
priceGranularity: "dense",
enableSendAllBids: false
},
bidderSettings: {
...
}
}
})
Expand All @@ -47,6 +50,68 @@ const openAds = OpenAds.init({config:{
}
}})
```
## Configuration

Configuration object is divided in two objects:
* **config**: related with AppNexus (ast.js)
* **prebidConfig**: related with PreBid (pbjs.js)
* **core** : (Optional) Settings to override the default Prebid core configuration.
* **bidderSettings**: (Optional) Settings to override the default Prebid settings used by the bidders.

### bidderSettings

An example of **bidderSettings** overriding the standard configuration applied to all bidders:

```json
{
standard: {
adserverTargeting: [{
key: "hb_bidder",
val: function(bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: "hb_adid",
val: function(bidResponse) {
return bidResponse.adId;
}
}, {
key: "hb_pb",
val: function(bidResponse) {
return bidResponse.pbMg;
}
}, {
key: 'hb_size',
val: function (bidResponse) {
return bidResponse.size;
}
}, {
key: 'hb_source',
val: function (bidResponse) {
return bidResponse.source;
}
}, {
key: 'hb_format',
val: function (bidResponse) {
return bidResponse.mediaType;
}
}, {
key: 'hb_cache_id',
val: function (bidResponse) {
return bidResponse.videoCacheKey;
}
}, {
key: 'hb_uuid',
val: function (bidResponse) {
return bidResponse.videoCacheKey;
}
}]
}
}

```

More info can be found [here](http://prebid.org/dev-docs/publisher-api-reference.html#module_pbjs.bidderSettings).

# Preconditions

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@schibstedspain/openads-appnexus-prebid",
"version": "1.2.0",
"version": "2.0.0",
"description": "OpenAds AppNexus connector with Prebid features",
"main": "dist/",
"scripts": {
Expand Down
11 changes: 8 additions & 3 deletions src/openads-appnexus/AppNexusConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ export default class AppNexusConnector {
this._prebidClient = prebidClient
this._pageOpts = pageOpts
this._prebidConfig = prebidConfig
if (this._pageOpts) this._astClient.setPageOpts(this._pageOpts)

if (this._pageOpts) {
this._astClient.setPageOpts(this._pageOpts)
}

this._prebidClient.setConfig({config: this._prebidConfig})

this._loadAdDebouncer = new Debouncer({
onDebounce: this._onLoadAdDebounce.bind(this),
debounceTimeout: TIMEOUT_DEBOUNCE
Expand Down Expand Up @@ -108,8 +114,7 @@ export default class AppNexusConnector {
)
if (normalizedInputs.adUnits.length > 0 && this._prebidClient) {
this._prebidClient.addAdUnits({adUnits: normalizedInputs.adUnits})
if (this._prebidConfig)
this._prebidClient.setConfig(this._prebidConfig)

this._prebidClient.requestBids({
timeout: TIMEOUT_PREBID,
bidsBackHandler: () => {
Expand Down
2 changes: 1 addition & 1 deletion src/openads-appnexus/PrebidClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class PrebidClient {
* Defines setConfig.
* @param config
*/
setConfig(config) {
setConfig({config}) {
throw new Error('AppNexusConnector#setConfig must be implemented')
}
}
7 changes: 5 additions & 2 deletions src/openads-appnexus/PrebidClientImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ export default class PrebidClientImpl {
return this
}

setConfig(config) {
setConfig({config = {}} = {}) {
this._logger.debug(this._logger.name, '| setConfig | config:', config)
this._pbjs.que.push(() => this._pbjs.setConfig(config))
this._pbjs.que.push(() => {
this._pbjs.setConfig(config.core)
this._pbjs.bidderSettings = config.bidderSettings
})
return this
}
}
106 changes: 59 additions & 47 deletions src/test/openads-appnexus/AppNexusConnectorTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,58 @@ describe('AppNexus Connector', function() {
debugMode: () => null
})

const getValidPrebidConfig = () => {
return {
core: {
bidderTimeout: 1500,
priceGranularity: 'dense',
enableSendAllBids: false
},
bidderSettings: {
standard: {
adserverTargeting: [
{
key: 'hb_bidder',
val: function(bidResponse) {
return bidResponse.bidderCode
}
},
{
key: 'pt0',
val: function(bidResponse) {
return bidResponse.adId
}
},
{
key: 'hb_pb',
val: function(bidResponse) {
var bucket = (parseFloat(bidResponse.pbCg) * 100).toFixed()
return isNaN(bucket) ? '0' : bucket
}
}
]
}
}
}
}

describe('constructor', () => {
it('should call the setPageOpts if options are given', done => {
const givenPageOpts = {
member: 1000
}
const astClientMock = createAstClientMock()
const setPageOptsSpy = sinon.spy(astClientMock, 'setPageOpts')
const prebidClientMock = createPrebidClientMock()
const setConfigSpy = sinon.spy(prebidClientMock, 'setConfig')

Promise.resolve()
.then(
() =>
new AppNexusConnector({
pageOpts: givenPageOpts,
astClient: astClientMock
astClient: astClientMock,
prebidClient: prebidClientMock
})
)
.then(() => {
Expand All @@ -100,6 +139,7 @@ describe('AppNexus Connector', function() {
setPageOptsSpy.args[0][0],
'setPageOpts should receive the pageOpts'
).to.deep.equal(givenPageOpts)
expect(setConfigSpy.calledOnce).to.be.true
done()
})
.catch(e => done(e))
Expand Down Expand Up @@ -146,11 +186,7 @@ describe('AppNexus Connector', function() {
pageOpts: {
member: 1000
},
prebidConfig: {
bidderTimeout: 1500,
priceGranularity: 'dense',
enableSendAllBids: false
},
prebidConfig: getValidPrebidConfig(),
logger: createLoggerMock(),
astClient: astClientMock,
prebidClient: prebidClientMock,
Expand Down Expand Up @@ -236,11 +272,7 @@ describe('AppNexus Connector', function() {
pageOpts: {
member: 1000
},
prebidConfig: {
bidderTimeout: 1500,
priceGranularity: 'dense',
enableSendAllBids: false
},
prebidConfig: getValidPrebidConfig(),
logger: createLoggerMock(),
astClient: astClientMock,
prebidClient: prebidClientMock,
Expand Down Expand Up @@ -326,11 +358,7 @@ describe('AppNexus Connector', function() {
pageOpts: {
member: 1000
},
prebidConfig: {
bidderTimeout: 1500,
priceGranularity: 'dense',
enableSendAllBids: false
},
prebidConfig: getValidPrebidConfig(),
logger: createLoggerMock(),
astClient: astClientMock,
prebidClient: prebidClientMock,
Expand Down Expand Up @@ -376,7 +404,8 @@ describe('AppNexus Connector', function() {
logger: createLoggerMock(),
astClient: createAstClientMock(),
adRepository: createAdRepositoryMock(),
loggerProvider: loggerProviderMock
loggerProvider: loggerProviderMock,
prebidClient: createPrebidClientMock()
})

appNexusConnector.enableDebug({debug: true})
Expand All @@ -397,7 +426,8 @@ describe('AppNexus Connector', function() {
logger: createLoggerMock(),
astClient: createAstClientMock(),
adRepository: createAdRepositoryMock(),
loggerProvider: createloggerProviderMock()
loggerProvider: createloggerProviderMock(),
prebidClient: createPrebidClientMock()
})
expect(appNexusConnector.display({})).to.be.a('promise')
})
Expand All @@ -410,7 +440,8 @@ describe('AppNexus Connector', function() {
logger: createLoggerMock(),
astClient: astClientMock,
adRepository: createAdRepositoryMock(),
loggerProvider: createloggerProviderMock()
loggerProvider: createloggerProviderMock(),
prebidClient: createPrebidClientMock()
})
const givenId = 1

Expand Down Expand Up @@ -650,7 +681,8 @@ describe('AppNexus Connector', function() {
addAdUnits: () => {},
requestBids: requestObj => requestObj.bidsBackHandler(),
setTargetingForAst: () => null,
setConfig: () => null
setConfig: () => null,
setBidderSettings: () => null
}

const adRepositoryMock = {
Expand All @@ -662,11 +694,7 @@ describe('AppNexus Connector', function() {
pageOpts: {
member: 1000
},
prebidConfig: {
bidderTimeout: 1500,
priceGranularity: 'dense',
enableSendAllBids: false
},
prebidConfig: getValidPrebidConfig(),
logger: createLoggerMock(),
astClient: astClientMock,
prebidClient: prebidClientMock,
Expand All @@ -675,10 +703,8 @@ describe('AppNexus Connector', function() {
})

const addAdUnitsSpy = sinon.spy(prebidClientMock, 'addAdUnits')
const setConfigSpy = sinon.spy(prebidClientMock, 'setConfig')
const requestBidsSpy = sinon.spy(prebidClientMock, 'requestBids')
const loadTagsSpy = sinon.spy(astClientMock, 'loadTags')

const expectedprebidUnitsArray = [
{
code: 1,
Expand All @@ -695,13 +721,10 @@ describe('AppNexus Connector', function() {
.then(() => {
setTimeout(() => {
expect(addAdUnitsSpy.calledOnce).to.be.true
expect(setConfigSpy.called).to.be.true
expect(requestBidsSpy.calledOnce).to.be.true

expect(addAdUnitsSpy.args[0][0].adUnits).to.deep.equal(
expectedprebidUnitsArray
)

expect(loadTagsSpy.calledOnce, 'should have loaded the tag').to.be
.true
done()
Expand Down Expand Up @@ -750,7 +773,8 @@ describe('AppNexus Connector', function() {
addAdUnits: () => {},
requestBids: requestObj => requestObj.bidsBackHandler(),
setTargetingForAst: () => null,
setConfig: () => null
setConfig: () => null,
setBidderSettings: () => null
}

const adRepositoryMock = {
Expand All @@ -762,11 +786,7 @@ describe('AppNexus Connector', function() {
pageOpts: {
member: 1000
},
prebidConfig: {
bidderTimeout: 1500,
priceGranularity: 'dense',
enableSendAllBids: false
},
prebidConfig: getValidPrebidConfig(),
logger: createLoggerMock(),
astClient: astClientMock,
prebidClient: prebidClientMock,
Expand All @@ -775,10 +795,8 @@ describe('AppNexus Connector', function() {
})

const addAdUnitsSpy = sinon.spy(prebidClientMock, 'addAdUnits')
const setConfigSpy = sinon.spy(prebidClientMock, 'setConfig')
const requestBidsSpy = sinon.spy(prebidClientMock, 'requestBids')
const loadTagsSpy = sinon.spy(astClientMock, 'loadTags')

const expectedprebidUnitsArray = [
{
code: 1,
Expand All @@ -804,13 +822,10 @@ describe('AppNexus Connector', function() {
.then(() => {
setTimeout(() => {
expect(addAdUnitsSpy.calledOnce).to.be.true
expect(setConfigSpy.called).to.be.true
expect(requestBidsSpy.calledOnce).to.be.true

expect(addAdUnitsSpy.args[0][0].adUnits).to.deep.equal(
expectedprebidUnitsArray
)

expect(loadTagsSpy.calledOnce, 'should have loaded the tag').to.be
.true
done()
Expand All @@ -828,7 +843,8 @@ describe('AppNexus Connector', function() {
logger: createLoggerMock(),
astClient: createAstClientMock(),
adRepository: adRepositoryMock,
loggerProvider: createloggerProviderMock()
loggerProvider: createloggerProviderMock(),
prebidClient: createPrebidClientMock()
})
const givenId = 1

Expand All @@ -840,11 +856,7 @@ describe('AppNexus Connector', function() {
segmentation: {a: 5},
native: {b: 6}
},
prebid: {
code: 1,
mediaTypes: [[3, 4]],
bids: [100, 101]
}
prebidConfig: getValidPrebidConfig()
}
appNexusConnector
.loadAd({
Expand Down
Loading

0 comments on commit 69d3c65

Please sign in to comment.