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

New Adapter: bidglass #3861

Merged
merged 3 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
187 changes: 187 additions & 0 deletions modules/bidglassBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import * as utils from 'src/utils';
// import {config} from 'src/config';
import {registerBidder} from 'src/adapters/bidderFactory';

const BIDDER_CODE = 'bidglass';

export const spec = {
code: BIDDER_CODE,
aliases: ['bg'], // short code
/**
* Determines whether or not the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function(bid) {
return !!(bid.params.adUnitId && !isNaN(parseFloat(bid.params.adUnitId)) && isFinite(bid.params.adUnitId));
},
/**
* Make a server request from the list of BidRequests.
*
* @param {validBidRequests[]} - an array of bids
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(validBidRequests, bidderRequest) {
/*
Use `bidderRequest.bids[]` to get bidder-dependent
request info.

If your bidder supports multiple currencies, use
`config.getConfig(currency)` to find which one the ad
server needs.
*/

/*
Sample array entry for validBidRequests[]:
[{
"bidder": "bidglass",
"bidId": "51ef8751f9aead",
"params": {
"adUnitId": 11,
...
},
"adUnitCode": "div-gpt-ad-1460505748561-0",
"transactionId": "d7b773de-ceaa-484d-89ca-d9f51b8d61ec",
"sizes": [[320,50],[300,250],[300,600]],
"bidderRequestId": "418b37f85e772c",
"auctionId": "18fd8b8b0bd757",
"bidRequestsCount": 1
}]
*/

let imps = [];
let getReferer = function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already pass this information in bidderRequest. Check http://prebid.org/dev-docs/bidder-adaptor.html#referrers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our rules for determining and reporting referer and origins vary slightly from prebid's built-in methods. We'd prefer to use our own methods for consistency's sake across our platform.

if (window === window.top) {
return window.location.href;
} else if (window.parent === window.top) {
return document.referrer;
} else {
return null;
}
};
let getOrigins = function() {
var ori = [window.location.protocol + '//' + window.location.hostname];

if (window.location.ancestorOrigins) {
for (var i = 0; i < window.location.ancestorOrigins.length; i++) {
ori.push(window.location.ancestorOrigins[i]);
}
} else if (window !== window.top) {
// Derive the parent origin
var parts = document.referrer.split('/');

ori.push(parts[0] + '//' + parts[2]);

if (window.parent !== window.top) {
// Additional unknown origins exist
ori.push('null');
}
}

return ori;
};

utils._each(validBidRequests, function(bid) {
bid.sizes = ((utils.isArray(bid.sizes) && utils.isArray(bid.sizes[0])) ? bid.sizes : [bid.sizes]);
bid.sizes = bid.sizes.filter(size => utils.isArray(size));

// Stuff to send: [bid id, sizes, adUnitId]
imps.push({
bidId: bid.bidId,
sizes: bid.sizes,
adUnitId: utils.getBidIdParameter('adUnitId', bid.params)
});
});

// Stuff to send: page URL
const bidReq = {
reqId: utils.getUniqueIdentifierStr(),
imps: imps,
ref: getReferer(),
ori: getOrigins()
};

let url = 'https://bid.glass/ad/hb.php?' +
`src=$$REPO_AND_VERSION$$`;

return {
method: 'POST',
url: url,
data: JSON.stringify(bidReq),
options: {
contentType: 'text/plain',
withCredentials: false
}
}
},

/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse) {
// const serverBody = serverResponse.body;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove dead code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do. Commit pending.

// const headerValue = serverResponse.headers.get('some-response-header');

const bidResponses = [];

utils._each(serverResponse.body.bidResponses, function(bid) {
bidResponses.push({
requestId: bid.requestId,
cpm: parseFloat(bid.cpm),
width: parseInt(bid.width),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseInt takes two parameters string and radix. I would suggest to add radix as it does not default to 10 and may return unexpected results.
More info here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt and interesting article on parseInt https://medium.com/dailyjs/parseint-mystery-7c4368ef7b21

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good call.

height: parseInt(bid.height),
creativeId: bid.creativeId,
dealId: bid.dealId || null,
currency: bid.currency || 'USD',
mediaType: bid.mediaType || 'banner',
netRevenue: true,
ttl: bid.ttl || 10,
ad: bid.ad
});
});

return bidResponses;
},

/**
* Register the user sync pixels which should be dropped after the auction.
*
* @param {SyncOptions} syncOptions Which user syncs are allowed?
* @param {ServerResponse[]} serverResponses List of server's responses.
* @return {UserSync[]} The user syncs which should be dropped.
*/
getUserSyncs: function(syncOptions, serverResponses) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getUserSyncs, onTimeout, onBidWon and onSetTargeting are all optional. You can remove from the spec if you do not plan to use these features

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks.

return [];
},

/**
* Register bidder specific code, which will execute if bidder timed out after an auction
* @param {data} Containing timeout specific data
*/
onTimeout: function(data) {
// Bidder specifc code
},

/**
* Register bidder specific code, which will execute if a bid from this bidder won the auction
* @param {Bid} The bid that won the auction
*/
onBidWon: function(bid) {
// Bidder specific code
},

/**
* Register bidder specific code, which will execute when the adserver targeting has been set for a bid from this bidder
* @param {Bid} The bid of which the targeting has been set
*/
onSetTargeting: function(bid) {
// Bidder specific code
}

}

registerBidder(spec);
34 changes: 34 additions & 0 deletions modules/bidglassBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Overview

```
Module Name: Bid Glass Bid Adapter
Module Type: Bidder Adapter
Maintainer: dliebner@gmail.com
```

# Description

Connects to Bid Glass and allows bids on ad units to compete within prebid.

# Sample Ad Unit: For Publishers
```
var adUnits = [{
code: 'bg-test-rectangle',
sizes: [[300, 250]],
bids: [{
bidder: 'bidglass',
params: {
adUnitId: '-1'
}
}]
},{
code: 'bg-test-leaderboard',
sizes: [[728, 90]],
bids: [{
bidder: 'bidglass',
params: {
adUnitId: '-1'
}
}]
}]
```
114 changes: 114 additions & 0 deletions test/spec/modules/bidglassAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { expect } from 'chai';
import { spec } from 'modules/bidglassBidAdapter';
import { newBidder } from 'src/adapters/bidderFactory';

describe('Bid Glass Adapter', function () {
const adapter = newBidder(spec);

describe('isBidRequestValid', function () {
let bid = {
'bidder': 'bidglass',
'params': {
'adUnitId': '3'
},
'adUnitCode': 'bidglass-testunit',
'sizes': [[300, 250], [300, 600]],
'bidId': '30b31c1838de1e',
'bidderRequestId': '22edbae2733bf6',
'auctionId': '1d1a030790a475',
};

it('should return true when required params found', function () {
expect(spec.isBidRequestValid(bid)).to.equal(true);
});

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

describe('buildRequests', function () {
const bidRequests = [{
'bidder': 'bidglass',
'params': {
'adUnitId': '3'
},
'adUnitCode': 'bidglass-testunit',
'sizes': [[300, 250], [300, 600]],
'bidId': '30b31c1838de1e',
'bidderRequestId': '22edbae2733bf6',
'auctionId': '1d1a030790a475',
}];

const request = spec.buildRequests(bidRequests);

it('sends bid request to our endpoint via POST', function () {
expect(request.method).to.equal('POST');
});

it('sets withCredentials to false', function () {
expect(request.options.withCredentials).to.equal(false);
});
});

describe('interpretResponse', function () {
let response;
beforeEach(function () {
response = {
body: {
'bidResponses': [{
'ad': '<!-- Creative -->',
'cpm': '0.01',
'creativeId': '-1',
'width': '300',
'height': '250',
'requestId': '30b31c1838de1e'
}]
}
};
});

it('should get the correct bid response', function () {
let expectedResponse = [{
'requestId': '30b31c1838de1e',
'cpm': 0.01,
'width': 300,
'height': 250,
'creativeId': '-1',
'dealId': null,
'currency': 'USD',
'mediaType': 'banner',
'netRevenue': true,
'ttl': 10,
'ad': '<!-- Creative -->'
}];

let result = spec.interpretResponse(response);
expect(Object.keys(result[0])).to.deep.equal(Object.keys(expectedResponse[0]));
});

it('handles empty bid response', function () {
let response = {
body: {
'bidResponses': []
}
};
let result = spec.interpretResponse(response);
expect(result.length).to.equal(0);
});
});

describe('getUserSyncs', function () {
let syncOptions = {iframeEnabled: true, pixelEnabled: true};
let emptyServerResponse = {
bidResponses: []
};
it('should return an array', function () {
let result = spec.getUserSyncs(syncOptions, emptyServerResponse);
expect(result).to.be.an('array');
});
});
});