Skip to content
This repository has been archived by the owner on Feb 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request prebid#81 in AOLP_ADS_JS/prebid.js from feature/PL…
Browse files Browse the repository at this point in the history
…MP-157-sharethrough-adapter to release/1.16.0

* commit '0ee6e1af865fd9ab7d7e07dbcf4b52e9b6d0c91d':
  Add creative key in bid response for Sharethrough adapter.
  fix specs
  update version
  fix jslint errors
  Change to using a closure for the callback from ajax to preserve bidObj
  Ensure cookies get sent in request headers (prebid#1069)
  Rz/ajax (#5) (prebid#1042)
  • Loading branch information
vzhukovsky committed Apr 12, 2017
2 parents 09f8847 + 0ee6e1a commit 3829bad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 52 deletions.
54 changes: 20 additions & 34 deletions src/adapters/sharethrough.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,53 @@
var utils = require('../utils.js');
var bidmanager = require('../bidmanager.js');
var bidfactory = require('../bidfactory.js');
var ajax = require('../ajax.js').ajax;

const STR_BIDDER_CODE = "sharethrough";
const STR_VERSION = "0.1.0"; //Need to check analytics too for version
const STR_VERSION = "1.2.0";

var SharethroughAdapter = function SharethroughAdapter() {

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

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

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);
str.ajax(scriptUrl, _createCallback(bidRequest), undefined, {withCredentials: true});
}
}

function _createCallback(bidRequest) {
return (bidResponse) => {
_strcallback(bidRequest, bidResponse);
};
}

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

let 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);
}
url = utils.tryAppendQueryString(url, 'placement_key', pkey);
url = appendEnvFields(url);

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) {
try {
$$PREBID_GLOBAL$$.strcallback(JSON.parse(event.data).response);
} catch(e) {
console.log(e);
}
}
}

$$PREBID_GLOBAL$$.strcallback = function(bidResponse) {
function _strcallback(bidObj, bidResponse) {
bidResponse = JSON.parse(bidResponse);
const bidId = bidResponse.bidId;
const bidObj = utils.getBidRequest(bidId);
try {
const bid = bidfactory.createBid(1, bidObj);
bid.bidderCode = STR_BIDDER_CODE;
Expand All @@ -79,6 +60,10 @@ var SharethroughAdapter = function SharethroughAdapter() {

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

if (bidResponse.creatives[0].creative) {
bid.creativeKey = bidResponse.creatives[0].creative.creative_key;
}

const windowLocation = `str_response_${bidId}`;
const bidJsonString = JSON.stringify(bidResponse);
bid.ad = `<div data-str-native-key="${bid.pkey}" data-stx-response-name='${windowLocation}'>
Expand All @@ -102,7 +87,7 @@ var SharethroughAdapter = function SharethroughAdapter() {
} catch (e) {
_handleInvalidBid(bidObj);
}
};
}

function _handleInvalidBid(bidObj) {
const bid = bidfactory.createBid(2, bidObj);
Expand All @@ -120,6 +105,7 @@ var SharethroughAdapter = function SharethroughAdapter() {
return {
callBids: _callBids,
str : str,
callback: _strcallback
};
};

Expand Down
27 changes: 9 additions & 18 deletions test/spec/adapters/sharethrough_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,20 @@ describe('sharethrough adapter', () => {
let secondBidUrl;

beforeEach(() => {
sandbox.spy(adapter.str, 'loadIFrame');
sandbox.spy(adapter.str, 'ajax');
});

it('should call loadIFrame on the adloader for each bid', () => {
it('should call ajax to make a request for each bid', () => {

adapter.callBids(bidderRequest);

firstBidUrl = adapter.str.loadIFrame.firstCall.args[0];
secondBidUrl = adapter.str.loadIFrame.secondCall.args[0];
firstBidUrl = adapter.str.ajax.firstCall.args[0];
secondBidUrl = adapter.str.ajax.secondCall.args[0];

sinon.assert.calledTwice(adapter.str.loadIFrame);
sinon.assert.calledTwice(adapter.str.ajax);

expect(firstBidUrl).to.contain(adapter.str.STR_BTLR_HOST + '/header-bid/v1?bidId=bidId1&placement_key=aaaa1111&ijson=pbjs.strcallback&hbVersion=%24prebid.version%24&strVersion=0.1.0&hbSource=prebid&');
expect(secondBidUrl).to.contain(adapter.str.STR_BTLR_HOST + '/header-bid/v1?bidId=bidId2&placement_key=bbbb2222&ijson=pbjs.strcallback&hbVersion=%24prebid.version%24&strVersion=0.1.0&hbSource=prebid&');
});

});

describe('strcallback', () => {

it('should exist and be a function', () => {
let shit = sandbox.stub(pbjs, 'strcallback');
expect(pbjs.strcallback).to.exist.and.to.be.a('function');
expect(firstBidUrl).to.contain(adapter.str.STR_BTLR_HOST + '/header-bid/v1?bidId=bidId1&placement_key=aaaa1111&hbVersion=%24prebid.version%24&strVersion=1.2.0&hbSource=prebid&');
expect(secondBidUrl).to.contain(adapter.str.STR_BTLR_HOST + '/header-bid/v1?bidId=bidId2&placement_key=bbbb2222&hbVersion=%24prebid.version%24&strVersion=1.2.0&hbSource=prebid&');
});

});
Expand Down Expand Up @@ -117,8 +108,8 @@ describe('sharethrough adapter', () => {
"stxUserId": ""
};

pbjs.strcallback(bidderReponse1);
pbjs.strcallback(bidderReponse2);
adapter.callback(bidderRequest.bids[0], JSON.stringify(bidderReponse1));
adapter.callback(bidderRequest.bids[1], JSON.stringify(bidderReponse2));

firstBid = bidManager.addBidResponse.firstCall.args[1];
secondBid = bidManager.addBidResponse.secondCall.args[1];
Expand Down

0 comments on commit 3829bad

Please sign in to comment.