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 Sharethrough adapter #865

Merged
merged 16 commits into from Jan 12, 2017
Merged
1 change: 1 addition & 0 deletions adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"underdogmedia",
"memeglobal",
"centro",
"sharethrough",
"roxot",
"vertoz",
{
Expand Down
149 changes: 149 additions & 0 deletions src/adapters/sharethrough.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
var utils = require('../utils.js');
var bidmanager = require('../bidmanager.js');
var bidfactory = require('../bidfactory.js');

const STR_BIDDER_CODE = "sharethrough";
const STR_VERSION = "0.1.0";

var SharethroughAdapter = function SharethroughAdapter() {

const str = {};
str.STR_BTLR_HOST = document.location.protocol + "//btlr.sharethrough.com";
str.STR_TEST_HOST = document.location.protocol + "//btlr-prebid-test.sharethrough.com";
Copy link
Member

Choose a reason for hiding this comment

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

Using a different endpoint for test is not ideal as real issues are discovered in prod. Is there a way to not use a test endpoint here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have to use a test endpoint here to guarantee that a creative is always return from our exchange. If we used the real endpoint there's no way to guarantee that a DSP will always bid on the impression request.

Copy link
Member

Choose a reason for hiding this comment

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

Usually other bidders setup a test creative /placement on the live exchange. Is this possible?

str.STR_BEACON_HOST = document.location.protocol + "//b.sharethrough.com/butler?";
str.placementCodeSet = {};

function _callBids(params) {
const bids = params.bids;

$$PREBID_GLOBAL$$.onEvent('bidWon', str.bidWon);
Copy link
Member

Choose a reason for hiding this comment

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

This line can be removed now that you added your analytics adapter.


addEventListener("message", _receiveMessage, false);

// cycle through bids
for (let i = 0; i < bids.length; i += 1) {
const bidRequest = bids[i];
str.placementCodeSet[bidRequest.placementCode] = bidRequest;
const scriptUrl = _buildSharethroughCall(bidRequest);
str.loadIFrame(scriptUrl);
Copy link
Member

Choose a reason for hiding this comment

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

Don't load JSONP inside an iframe. Use ajax.js or adLoader.loadScript() instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to put the url inside an iframe so we can drop tracking cookies on publisher's page. This method work for iOS Safari since they block 3rd party cookies.

Copy link
Member

Choose a reason for hiding this comment

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

Ok

}
}

function _buildSharethroughCall(bid) {
const testPkey = 'test';
const pkey = utils.getBidIdParameter('pkey', bid.params);

let host = (pkey === testPkey) ? str.STR_TEST_HOST : str.STR_BTLR_HOST;

let url = host + "/header-bid/v1?";
url = utils.tryAppendQueryString(url, 'bidId', bid.bidId);

if(pkey !== testPkey) {
url = utils.tryAppendQueryString(url, 'placement_key', pkey);
url = utils.tryAppendQueryString(url, 'ijson', '$$PREBID_GLOBAL$$.strcallback');
url = appendEnvFields(url);
} else {
url = url.substring(0, url.length - 1);
}

return url;
}

str.loadIFrame = function(url) {
const iframe = document.createElement("iframe");
iframe.src = url;
iframe.style.cssText = 'display:none;';

document.body.appendChild(iframe);
};

function _receiveMessage(event) {
if(event.origin === str.STR_BTLR_HOST || event.origin === str.STR_TEST_HOST) {
$$PREBID_GLOBAL$$.strcallback(JSON.parse(event.data).response);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please wrap JSON.parse in a try/catch block

}
}

$$PREBID_GLOBAL$$.strcallback = function(bidResponse) {
const bidId = bidResponse.bidId;
const bidObj = utils.getBidRequest(bidId);
try {
const bid = bidfactory.createBid(1, bidObj);

bid.bidderCode = 'sharethrough';
Copy link
Collaborator

Choose a reason for hiding this comment

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

'sharethrough' is defined in STR_BIDDER_CODE, could use that here to avoid duplicating the string

bid.cpm = bidResponse.creatives[0].cpm;
const size = bidObj.sizes[0];
bid.width = size[0];
bid.height = size[1];
bid.adserverRequestId = bidResponse.adserverRequestId;
str.placementCodeSet[bidObj.placementCode].adserverRequestId = bidResponse.adserverRequestId;
bid.winId = bidResponse.creatives[0].auctionWinId;

bid.pkey = utils.getBidIdParameter('pkey', bidObj.params);

const windowLocation = `str_response_${bidId}`;
const bidJsonString = JSON.stringify(bidResponse);
bid.ad = `<div data-str-native-key="${bid.pkey}" data-stx-response-name='${windowLocation}'>
</div>
<script>var ${windowLocation} = ${bidJsonString}</script>
<script src="//native.sharethrough.com/assets/sfp-set-targeting.js"></script>
<script type='text/javascript'>
(function() {
var sfp_js = document.createElement('script');
sfp_js.src = "//native.sharethrough.com/assets/sfp.js";
sfp_js.type = 'text/javascript';
sfp_js.charset = 'utf-8';
window.top.document.getElementsByTagName('body')[0].appendChild(sfp_js);
Copy link
Member

Choose a reason for hiding this comment

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

window.top isn't safe to call directly, it should be in a try/catch. Additionally it should be avoided if possible to support SafeFrame in the future.

})();
</script>`;
bidmanager.addBidResponse(bidObj.placementCode, bid);
} catch (e) {
_handleInvalidBid(bidObj);
}
};

function _handleInvalidBid(bidObj) {
const bid = bidfactory.createBid(2, bidObj);
bidmanager.addBidResponse(bidObj.placementCode, bid);
}

str.bidWon = function() {
Copy link
Member

Choose a reason for hiding this comment

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

We currently do not allow bidders to listen to other bids. If a bidder wants to do this, they should add an analytics adapter. This is a publisher opt in only process. https://github.com/prebid/Prebid.js/blob/master/src/adapters/analytics/example2.js

const curBidderCode = arguments[0].bidderCode;

if(curBidderCode !== STR_BIDDER_CODE && (arguments[0].adUnitCode in str.placementCodeSet)) {
let strBid = str.placementCodeSet[arguments[0].adUnitCode];
str.fireLoseBeacon(curBidderCode, arguments[0].cpm, strBid.adserverRequestId, "headerBidLose");
}
};

str.fireLoseBeacon = function(winningBidderCode, winningCPM, arid, type) {
let loseBeaconUrl = str.STR_BEACON_HOST;
loseBeaconUrl = utils.tryAppendQueryString(loseBeaconUrl, "winnerBidderCode", winningBidderCode);
loseBeaconUrl = utils.tryAppendQueryString(loseBeaconUrl, "winnerCpm", winningCPM);
loseBeaconUrl = utils.tryAppendQueryString(loseBeaconUrl, "arid", arid);
loseBeaconUrl = utils.tryAppendQueryString(loseBeaconUrl, "type", type);
loseBeaconUrl = appendEnvFields(loseBeaconUrl);

str.fireBeacon(loseBeaconUrl);
};

function appendEnvFields(url) {
url = utils.tryAppendQueryString(url, 'hbVersion', '$prebid.version$');
url = utils.tryAppendQueryString(url, 'strVersion', STR_VERSION);
url = utils.tryAppendQueryString(url, 'hbSource', 'prebid');

return url;
}

str.fireBeacon = function(theUrl) {
const img = new Image();
img.src = theUrl;
};

return {
callBids: _callBids,
str : str,
};
};

module.exports = SharethroughAdapter;

Loading