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

OFF-305 Create bidAdapter for Prebid.JS #1

Merged
merged 10 commits into from
Feb 14, 2023
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
110 changes: 110 additions & 0 deletions modules/flippBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import {isEmpty, parseUrl} from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
const NETWORK_ID = 11090;
const AD_TYPES = [4309, 641];
const TARGET_NAME = 'inline';
const BIDDER_CODE = 'flipp';
const ENDPOINT = 'https://gateflipp.flippback.com/flyer-locator-service/prebid_campaigns';
const DEFAULT_TTL = 30;
const DEFAULT_CURRENCY = 'USD';

const generateUUID = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};

export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
/**
* 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.siteId) && !!(bid.params.publisherNameIdentifier);

Choose a reason for hiding this comment

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

Can the bid be null here?

Copy link
Author

Choose a reason for hiding this comment

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

@jpanduro-blackbird I think it's guaranteed that bid.params is always an object according to prebid.js docs and other adapter implementations in this repo

},
/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} validBidRequests[] an array of bids
* @param {BidderRequest} bidderRequest master bidRequest object
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(validBidRequests, bidderRequest) {
const urlParams = parseUrl(bidderRequest.refererInfo.page).search;
const contentCode = urlParams['flipp-content-code'];
const userKey = isEmpty(validBidRequests[0]?.params.userKey) ? generateUUID() : validBidRequests[0]?.params.userKey;
const placements = validBidRequests.map((bid, index) => {
return {
divName: TARGET_NAME,
networkId: NETWORK_ID,
siteId: bid.params.siteId,
adTypes: AD_TYPES,
count: 1,
...(!isEmpty(bid.params.zoneIds) && {zoneIds: bid.params.zoneIds}),
properties: {
...(!isEmpty(contentCode) && {contentCode: contentCode.slice(0, 32)}),
},
prebid: {
requestId: bid.bidId,
publisherNameIdentifier: bid.params.publisherNameIdentifier,
height: bid.mediaTypes.banner.sizes[index][0],
width: bid.mediaTypes.banner.sizes[index][1],
}
}
});
return {
method: 'POST',
url: ENDPOINT,
data: {
placements,
url: bidderRequest.refererInfo.page,
user: {
key: userKey,
},
},
}
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @param {BidRequest} bidRequest A bid request object
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse, bidRequest) {
if (!serverResponse?.body) return [];
const res = serverResponse.body;
if (!isEmpty(res) && !isEmpty(res.decisions) && !isEmpty(res.decisions.inline)) {
return res.decisions.inline.map(decision => ({
bidderCode: BIDDER_CODE,
requestId: decision.prebid?.requestId,
cpm: decision.prebid?.cpm,
width: decision.width,
height: decision.height,
creativeId: decision.adId,
currency: DEFAULT_CURRENCY,
netRevenue: true,
ttl: DEFAULT_TTL,
ad: decision.prebid?.creative,
}));
}
return [];
},

/**
* 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: (syncOptions, serverResponses) => [],
}
registerBidder(spec);
40 changes: 40 additions & 0 deletions modules/flippBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Overview

```
Module Name: Flipp Bid Adapter
Module Type: Bidder Adapter
Maintainer: prebid@flipp.com
```

# Description

This module connects publishers to Flipp's Shopper Experience via Prebid.js.


# Test parameters

```javascript
var adUnits = [
{
code: 'flipp-scroll-ad-content',
mediaTypes: {
banner: {
sizes: [
[300, 600]
]
}
},
bids: [
{
bidder: 'flipp',
params: {
publisherNameIdentifier: 'wishabi-test-publisher', // Required
siteId: 1192075, // Required
zoneIds: [260678], // Optional
userKey: "", // Optional
}
}
]
}
]
```
170 changes: 170 additions & 0 deletions test/spec/modules/flippBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import {expect} from 'chai';
import {spec} from 'modules/flippBidAdapter';
import {newBidder} from 'src/adapters/bidderFactory';
const ENDPOINT = 'https://gateflipp.flippback.com/flyer-locator-service/prebid_campaigns';
describe('flippAdapter', function () {
const adapter = newBidder(spec);

describe('inherited functions', function () {
it('exists and is a function', function () {
expect(adapter.callBids).to.exist.and.to.be.a('function');
});
});

describe('isBidRequestValid', function () {
const bid = {
bidder: 'flipp',
params: {
publisherNameIdentifier: 'random',
siteId: 1234,
zoneIds: [1, 2, 3, 4],
}
};
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 invalidBid = Object.assign({}, bid);
invalidBid.params = { siteId: 1234 }
expect(spec.isBidRequestValid(invalidBid)).to.equal(false);
});
});

describe('buildRequests', function () {
const bidRequests = [{
bidder: 'flipp',
params: {
siteId: 1234,
},
adUnitCode: '/10000/unit_code',
sizes: [[300, 600]],
mediaTypes: {banner: {sizes: [[300, 600]]}},
bidId: '237f4d1a293f99',
bidderRequestId: '1a857fa34c1c96',
auctionId: 'a297d1aa-7900-4ce4-a0aa-caa8d46c4af7',
transactionId: '00b2896c-2731-4f01-83e4-7a3ad5da13b6',
}];
const bidderRequest = {
refererInfo: {
referer: 'http://example.com'
}
};

it('sends bid request to ENDPOINT via POST', function () {
const request = spec.buildRequests(bidRequests, bidderRequest);
expect(request.method).to.equal('POST');
});

it('sends bid request to ENDPOINT with query parameter', function () {
const request = spec.buildRequests(bidRequests, bidderRequest);
expect(request.url).to.equal(ENDPOINT);
});
});

describe('interpretResponse', function() {
it('should get correct bid response', function() {
const bidRequest = {
method: 'POST',
url: ENDPOINT,
data: {
placements: [{
divName: 'slot',
networkId: 12345,
siteId: 12345,
adTypes: [12345],
count: 1,
prebid: {
requestId: '237f4d1a293f99',
publisherNameIdentifier: 'bid.params.publisherNameIdentifier',
height: 600,
width: 300,
},
user: '10462725-da61-4d3a-beff-6d05239e9a6e"',
}],
url: 'http://example.com',
},
};

const serverResponse = {
body: {
'decisions': {
'inline': [{
'bidCpm': 1,
'adId': 262838368,
'height': 600,
'width': 300,
'storefront': { 'flyer_id': 5435567 },
'prebid': {
'requestId': '237f4d1a293f99',
'cpm': 1.11,
'creative': 'Returned from server',
}
}]
},
'location': {'city': 'Oakville'},
},
};

const expectedResponse = [
{
bidderCode: 'flipp',
requestId: '237f4d1a293f99',
currency: 'USD',
cpm: 1.11,
netRevenue: true,
width: 300,
height: 600,
creativeId: 262838368,
ttl: 30,
ad: 'Returned from server',
}
];

const result = spec.interpretResponse(serverResponse, bidRequest);
expect(result).to.have.lengthOf(1);
expect(result).to.deep.have.same.members(expectedResponse);
});

it('should get empty bid response when no ad is returned', function() {
const bidRequest = {
method: 'POST',
url: ENDPOINT,
data: {
placements: [{
divName: 'slot',
networkId: 12345,
siteId: 12345,
adTypes: [12345],
count: 1,
prebid: {
requestId: '237f4d1a293f99',
publisherNameIdentifier: 'bid.params.publisherNameIdentifier',
height: 600,
width: 300,
},
user: '10462725-da61-4d3a-beff-6d05239e9a6e"',
}],
url: 'http://example.com',
},
};

const serverResponse = {
body: {
'decisions': {
'inline': []
},
'location': {'city': 'Oakville'},
},
};

const result = spec.interpretResponse(serverResponse, bidRequest);
expect(result).to.have.lengthOf(0);
expect(result).to.deep.have.same.members([]);
})

it('should get empty response when bid server returns 204', function() {
expect(spec.interpretResponse({})).to.be.empty;
});
});
});