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 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
134 changes: 134 additions & 0 deletions modules/bidglassBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
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) {
/*
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.

return window === window.top ? window.location.href : window.parent === window.top ? document.referrer : 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 bidResponses = [];

utils._each(serverResponse.body.bidResponses, function(bid) {
bidResponses.push({
requestId: bid.requestId,
cpm: parseFloat(bid.cpm),
width: parseInt(bid.width, 10),
height: parseInt(bid.height, 10),
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;
}

}

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'
}
}]
}]
```
103 changes: 103 additions & 0 deletions test/spec/modules/bidglassAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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);
});
});
});