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

Add TapSense Header Bidding Adapter and tests #1004

Merged
merged 3 commits into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"vertoz",
"widespace",
"admixer",
"tapsense",
{
"appnexus": {
"alias": "brealtime"
Expand Down
89 changes: 89 additions & 0 deletions src/adapters/tapsense.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//v0.0.1
var bidfactory = require('../bidfactory.js');
Copy link
Member

Choose a reason for hiding this comment

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

please use const/let instead of var as appropriate.

var bidmanager = require('../bidmanager.js');
var adloader = require('../adloader');
var utils = require('../utils.js');

var TapSenseAdapter = function TapSenseAdapter() {
var version = "0.0.1";
var creativeSizes = [
"320x50"
Copy link
Member

Choose a reason for hiding this comment

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

This is the only valid bid size?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For header bidding we only currently only support banner requests

Copy link
Member

Choose a reason for hiding this comment

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

ok, probably want to make a note about this in your bidder documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to be sure, to create our bidder documentation I will need to make a PR for this page with our information correct?? http://prebid.org/dev-docs/bidders.html

];
var validParams = [
"ufid",
"refer",
"ad_unit_id",
"device_id",
"lat",
"long",
"user",
"price_floor",
"test",
"jsonp"
];
var bids;
window.tapsense = {};
function _callBids(params) {
bids = params.bids || [];
for (var i = 0; i < bids.length; i++) {
var bid = bids[i];
var isValidSize = false;
if (!bid.sizes) {
return;
}
for (var k = 0; k < bid.sizes.length; k++) {
if (creativeSizes.indexOf(bid.sizes[k].join("x")) > -1) {
Copy link
Member

Choose a reason for hiding this comment

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

This fails if the bid.sizes is a single array and not a nested array (ie [300,250]; instead of [[300,250]];)

isValidSize = true;
break;
}
}
if (isValidSize) {
if (!bid.params.scriptURL) {
continue;
}
var queryString = "?price=true&callback=tapsense.callback_with_price_" + bid.bidId + "&version=" + version + "&";
Copy link
Member

Choose a reason for hiding this comment

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

Prefer string templates here instead of concatenation.

window.tapsense["callback_with_price_" + bid.bidId] = generateCallback(bid.bidId);
Copy link
Member

Choose a reason for hiding this comment

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

use the the prebid global object $$PREBID_GLOBAL$$ instead of window.

var keys = Object.keys(bid.params);
for (var j = 0; j < keys.length; j++) {
if (validParams.indexOf(keys[j]) < 0) continue;
queryString += encodeURIComponent(keys[j]) + "=" + encodeURIComponent(bid.params[keys[j]]) + "&";
}
var scriptURL = bid.params.scriptURL;
Copy link
Member

Choose a reason for hiding this comment

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

can you make scriptUrl static or is it required to be passed in?

_requestBids(scriptURL + queryString);
}
}
}

function generateCallback(bidId){
return function(response, price) {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason this is an anonymous function?

var bidObj;
if (response && price) {
var bidReq = utils.getBidRequest(bidId);
if (response.status.value === "ok" && response.count_ad_units > 0) {
bidObj = bidfactory.createBid(1, bidObj);
bidObj.cpm = price;
bidObj.width = response.width;
bidObj.height = response.height;
bidObj.ad = response.ad_units[0].html;
} else {
bidObj = bidfactory.createBid(2, bidObj);
}
bidObj.bidderCode = bidReq.bidder;
bidmanager.addBidResponse(bidReq.placementCode, bidObj);

} else {
utils.logMessage('No prebid response');
}
};
}

function _requestBids(scriptURL) {
adloader.loadScript(scriptURL);
}

return {
callBids: _callBids
};
};

module.exports = TapSenseAdapter;
237 changes: 237 additions & 0 deletions test/spec/adapters/tapsense_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
import { expect } from 'chai';
import Adapter from 'src/adapters/tapsense';
import bidmanager from 'src/bidmanager';
import adloader from "src/adloader";
import * as utils from "src/utils";

const DEFAULT_BIDDER_REQUEST = {
"bidderCode": "tapsense",
"bidderRequestId": "141ed07a281ca3",
"requestId": "b202e550-b0f7-4fb9-bfb4-1aa80f1795b4",
"start": new Date().getTime(),
"bids": [
{
"sizes": undefined, //set values in tests
"bidder": "tapsense",
"bidId": "2b211418dd0575",
"bidderRequestId": "141ed07a281ca3",
"placementCode": "thisisatest",
"params": {
"ufid": "thisisaufid",
"refer": "thisisarefer",
"version": "0.0.1",
"jsonp": 1,
"ad_unit_id": "thisisanadunitid",
"device_id": "thisisadeviceid",
"lat": "thisislat",
"long": "thisisalong",
"user": "thisisanidfa",
"price_floor": 0.01
}
}
]
}

const SUCCESSFUL_RESPONSE = {
"count_ad_units": 1,
"status": {
"value": "ok",
},
"ad_units": [
{
html: "<html><head></head><body></body></html>",
imp_url: "https://i.tapsense.com"
}
],
"id": "thisisanid",
"width": 320,
"height": 50,
"time": new Date().getTime()
}

const UNSUCCESSFUL_RESPONSE = {
"count_ad_units": 0,
"status": {
"value": "nofill" //will be set in test
},
"time": new Date().getTime()
}

function duplicate(obj) {
return JSON.parse(JSON.stringify(obj));
}

function makeSuccessfulRequest(adapter){
let modifiedReq = duplicate(DEFAULT_BIDDER_REQUEST);
modifiedReq.bids[0].sizes = [[320,50], [500,500]];
modifiedReq.bids[0].params.scriptURL = "https://ads.tapsense.com";
adapter.callBids(modifiedReq);
return modifiedReq.bids;
}

describe ("TapSenseAdapter", () => {
let adapter, sandbox;

beforeEach(() => {
adapter = new Adapter;
sandbox = sinon.sandbox.create();
});
afterEach(() => {
sandbox.restore();
})

describe('request function', () => {

beforeEach(() => {
sandbox.stub(adloader, 'loadScript');
});

afterEach(() => {
sandbox.restore();
});

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

it('requires parameters to make request', () => {
adapter.callBids({});
sinon.assert.notCalled(adloader.loadScript);
});

it('does not make a request if missing sizes', () => {
adapter.callBids(DEFAULT_BIDDER_REQUEST);
sinon.assert.notCalled(adloader.loadScript);
});

it('does not make a request if ad sizes are invalid/incorrect', () => {
let modifiedReq = duplicate(DEFAULT_BIDDER_REQUEST);
modifiedReq.bids[0].sizes = [[500,500]];
adapter.callBids(modifiedReq);
sinon.assert.notCalled(adloader.loadScript);
});

it('does not make a request if no scriptURL is provided in bid params', () => {
let modifiedReq = duplicate(DEFAULT_BIDDER_REQUEST);
modifiedReq.bids[0].sizes = [[320,50]];
adapter.callBids(modifiedReq);
sinon.assert.notCalled(adloader.loadScript);
})

describe("requesting an ad", () => {
beforeEach(() => {
makeSuccessfulRequest(adapter);
});
afterEach(() => {
sandbox.restore();
})
it("makes a request if both valid sizes and scriptURL are provided", () => {
sinon.assert.calledOnce(adloader.loadScript);
expect(adloader.loadScript.firstCall.args[0]).to.contain(
"ads.tapsense.com"
);
});
it("appends bid params as a query string when requesting ad", () => {
sinon.assert.calledOnce(adloader.loadScript);
expect(adloader.loadScript.firstCall.args[0]).to.match(
/ufid=thisisaufid&/
);
expect(adloader.loadScript.firstCall.args[0]).to.match(
/refer=thisisarefer&/
);
expect(adloader.loadScript.firstCall.args[0]).to.match(
/version=[^&]+&/
);
expect(adloader.loadScript.firstCall.args[0]).to.match(
/jsonp=1&/
);
expect(adloader.loadScript.firstCall.args[0]).to.match(
/ad_unit_id=thisisanadunitid&/
);
expect(adloader.loadScript.firstCall.args[0]).to.match(
/device_id=thisisadeviceid&/
);
expect(adloader.loadScript.firstCall.args[0]).to.match(
/lat=thisislat&/
);
expect(adloader.loadScript.firstCall.args[0]).to.match(
/long=thisisalong&/
);
expect(adloader.loadScript.firstCall.args[0]).to.match(
/user=thisisanidfa&/
);
expect(adloader.loadScript.firstCall.args[0]).to.match(
/price_floor=0\.01&/
);
expect(adloader.loadScript.firstCall.args[0]).to.not.contain(
"scriptUrl"
);
})
})
});

describe("generateCallback", () => {
beforeEach(() => {
sandbox.stub(adloader, 'loadScript');
});
afterEach(() => {
sandbox.restore();
});
it("generates callback in namespaced object with correct bidder id", () => {
makeSuccessfulRequest(adapter);
expect(tapsense.callback_with_price_2b211418dd0575).to.exist.and.to.be.a('function');
})
});

describe("response", () => {
beforeEach(() => {
sandbox.stub(bidmanager, 'addBidResponse');
sandbox.stub(adloader, 'loadScript');
let bids = makeSuccessfulRequest(adapter);
sandbox.stub(utils, "getBidRequest", (id) => {
return bids.find((item) => { return item.bidId === id});
})
});
afterEach(() => {
sandbox.restore();
});
describe("successful response", () => {
beforeEach(() => {
tapsense.callback_with_price_2b211418dd0575(SUCCESSFUL_RESPONSE, 1.2);
});
it("called the bidmanager and registers a bid", () => {
sinon.assert.calledOnce(bidmanager.addBidResponse);
expect(bidmanager.addBidResponse.firstCall.args[1].getStatusCode()).to.equal(1);
});
it("should have the correct placementCode", () => {
sinon.assert.calledOnce(bidmanager.addBidResponse);
expect(bidmanager.addBidResponse.firstCall.args[0]).to.equal("thisisatest");
});
});
describe("unsuccessful response", () => {
beforeEach(() => {
tapsense.callback_with_price_2b211418dd0575(UNSUCCESSFUL_RESPONSE, 1.2);
})
it("should call the bidmanger and register an invalid bid", () => {
sinon.assert.calledOnce(bidmanager.addBidResponse);
expect(bidmanager.addBidResponse.firstCall.args[1].getStatusCode()).to.equal(2);
});
it("should have the correct placementCode", () => {
expect(bidmanager.addBidResponse.firstCall.args[0]).to.equal("thisisatest");
})
});
describe("no response/timeout", () => {
it("should not register any bids", () => {
sinon.assert.notCalled(bidmanager.addBidResponse);
})
});
describe("edge cases", () => {
it("does not register a bid if no price is supplied", () => {
sandbox.stub(utils, "logMessage");
tapsense.callback_with_price_2b211418dd0575(SUCCESSFUL_RESPONSE);
sinon.assert.notCalled(bidmanager.addBidResponse);
});
});
});

})