From c834f8f7ed25e66a3da22b61504a20cda8353fa0 Mon Sep 17 00:00:00 2001 From: rizhang Date: Tue, 21 Mar 2017 23:20:06 +0300 Subject: [PATCH] Rz/ajax (#5) (#1042) update sharethrough adapter to use ajax for bid requests --- src/adapters/sharethrough.js | 38 +++++-------------------- test/spec/adapters/sharethrough_spec.js | 18 ++++++------ 2 files changed, 16 insertions(+), 40 deletions(-) diff --git a/src/adapters/sharethrough.js b/src/adapters/sharethrough.js index a9d26ecf23e6..1fa799fd6633 100644 --- a/src/adapters/sharethrough.js +++ b/src/adapters/sharethrough.js @@ -1,9 +1,10 @@ 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.1.0"; var SharethroughAdapter = function SharethroughAdapter() { @@ -11,60 +12,35 @@ var SharethroughAdapter = function SharethroughAdapter() { 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, $$PREBID_GLOBAL$$.strcallback); } } 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) { + bidResponse = JSON.parse(bidResponse); const bidId = bidResponse.bidId; const bidObj = utils.getBidRequest(bidId); try { diff --git a/test/spec/adapters/sharethrough_spec.js b/test/spec/adapters/sharethrough_spec.js index 0678690370ec..03eb7aaba49d 100644 --- a/test/spec/adapters/sharethrough_spec.js +++ b/test/spec/adapters/sharethrough_spec.js @@ -51,20 +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&'); + expect(firstBidUrl).to.contain(adapter.str.STR_BTLR_HOST + '/header-bid/v1?bidId=bidId1&placement_key=aaaa1111&hbVersion=%24prebid.version%24&strVersion=1.1.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.1.0&hbSource=prebid&'); }); }); @@ -117,8 +117,8 @@ describe('sharethrough adapter', () => { "stxUserId": "" }; - pbjs.strcallback(bidderReponse1); - pbjs.strcallback(bidderReponse2); + pbjs.strcallback(JSON.stringify(bidderReponse1)); + pbjs.strcallback(JSON.stringify(bidderReponse2)); firstBid = bidManager.addBidResponse.firstCall.args[1]; secondBid = bidManager.addBidResponse.secondCall.args[1];