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

AOL adapter - switched to native Prebid user syncs support. #3032

Merged
merged 3 commits into from
Sep 20, 2018
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
107 changes: 30 additions & 77 deletions modules/aolBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as utils from 'src/utils';
import { registerBidder } from 'src/adapters/bidderFactory';
import { config } from 'src/config';
import { EVENTS } from 'src/constants.json';
import { BANNER } from 'src/mediaTypes';

const AOL_BIDDERS_CODES = {
Expand Down Expand Up @@ -43,31 +41,11 @@ const NEXAGE_SERVER = 'hb.nexage.com';
const ONE_DISPLAY_TTL = 60;
const ONE_MOBILE_TTL = 3600;

$$PREBID_GLOBAL$$.aolGlobals = {
pixelsDropped: false
};

const NUMERIC_VALUES = {
TRUE: 1,
FALSE: 0
};

let showCpmAdjustmentWarning = (function() {
let showCpmWarning = true;

return function() {
let bidderSettings = $$PREBID_GLOBAL$$.bidderSettings;
if (showCpmWarning && bidderSettings && bidderSettings.aol &&
typeof bidderSettings.aol.bidCpmAdjustment === 'function') {
utils.logWarn(
'bidCpmAdjustment is active for the AOL adapter. ' +
'As of Prebid 0.14, AOL can bid in net – please contact your accounts team to enable.'
);
showCpmWarning = false; // warning is shown at most once
}
};
})();

function template(strings, ...keys) {
return function(...values) {
let dict = values[values.length - 1] || {};
Expand All @@ -80,32 +58,6 @@ function template(strings, ...keys) {
};
}

function parsePixelItems(pixels) {
let itemsRegExp = /(img|iframe)[\s\S]*?src\s*=\s*("|')(.*?)\2/gi;
let tagNameRegExp = /\w*(?=\s)/;
let srcRegExp = /src=("|')(.*?)\1/;
let pixelsItems = [];

if (pixels) {
let matchedItems = pixels.match(itemsRegExp);
if (matchedItems) {
matchedItems.forEach(item => {
let tagName = item.match(tagNameRegExp)[0];
let url = item.match(srcRegExp)[2];

if (tagName && tagName) {
pixelsItems.push({
type: tagName === SYNC_TYPES.IMAGE.TAG ? SYNC_TYPES.IMAGE.TYPE : SYNC_TYPES.IFRAME.TYPE,
url: url
});
}
});
}
}

return pixelsItems;
}

function _isMarketplaceBidder(bidder) {
return bidder === AOL_BIDDERS_CODES.AOL || bidder === AOL_BIDDERS_CODES.ONEDISPLAY;
}
Expand Down Expand Up @@ -164,8 +116,6 @@ export const spec = {
});
},
interpretResponse({body}, bidRequest) {
showCpmAdjustmentWarning();

if (!body) {
utils.logError('Empty bid response', bidRequest.bidderCode, body);
} else {
Expand All @@ -176,15 +126,11 @@ export const spec = {
}
}
},
getUserSyncs(options, bidResponses) {
let bidResponse = bidResponses[0];
getUserSyncs(options, serverResponses) {
const bidResponse = !utils.isEmpty(serverResponses) && serverResponses[0].body;

if (config.getConfig('aol.userSyncOn') === EVENTS.BID_RESPONSE) {
if (!$$PREBID_GLOBAL$$.aolGlobals.pixelsDropped && bidResponse && bidResponse.ext && bidResponse.ext.pixels) {
$$PREBID_GLOBAL$$.aolGlobals.pixelsDropped = true;

return parsePixelItems(bidResponse.ext.pixels);
}
if (bidResponse && bidResponse.ext && bidResponse.ext.pixels) {
return this.parsePixelItems(bidResponse.ext.pixels);
}

return [];
Expand Down Expand Up @@ -357,6 +303,31 @@ export const spec = {

return params;
},
parsePixelItems(pixels) {
let itemsRegExp = /(img|iframe)[\s\S]*?src\s*=\s*("|')(.*?)\2/gi;
let tagNameRegExp = /\w*(?=\s)/;
let srcRegExp = /src=("|')(.*?)\1/;
let pixelsItems = [];

if (pixels) {
let matchedItems = pixels.match(itemsRegExp);
if (matchedItems) {
matchedItems.forEach(item => {
let tagName = item.match(tagNameRegExp)[0];
let url = item.match(srcRegExp)[2];

if (tagName && tagName) {
pixelsItems.push({
type: tagName === SYNC_TYPES.IMAGE.TAG ? SYNC_TYPES.IMAGE.TYPE : SYNC_TYPES.IFRAME.TYPE,
url: url
});
}
});
}
}

return pixelsItems;
},

_parseBidResponse(response, bidRequest) {
let bidData;
Expand All @@ -380,7 +351,7 @@ export const spec = {
}
}

let bidResponse = {
return {
bidderCode: bidRequest.bidderCode,
requestId: bidRequest.bidId,
ad: bidData.adm,
Expand All @@ -394,24 +365,6 @@ export const spec = {
netRevenue: true,
ttl: bidRequest.ttl
};

if (response.ext && response.ext.pixels) {
if (config.getConfig('aol.userSyncOn') !== EVENTS.BID_RESPONSE) {
bidResponse.ad += this.formatPixels(response.ext.pixels);
}
}

return bidResponse;
},
formatPixels(pixels) {
let formattedPixels = pixels.replace(/<\/?script( type=('|")text\/javascript('|")|)?>/g, '');

return '<script>var w=window,prebid;' +
'for(var i=0;i<10;i++){w = w.parent;prebid=w.$$PREBID_GLOBAL$$;' +
'if(prebid && prebid.aolGlobals && !prebid.aolGlobals.pixelsDropped){' +
'try{prebid.aolGlobals.pixelsDropped=true;' + formattedPixels + 'break;}' +
'catch(e){continue;}' +
'}}</script>';
},
isOneMobileBidder: _isOneMobileBidder,
isSecureProtocol() {
Expand Down
72 changes: 9 additions & 63 deletions test/spec/modules/aolBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ describe('AolAdapter', function () {
let bidResponse;
let bidRequest;
let logWarnSpy;
let formatPixelsStub;
let isOneMobileBidderStub;

beforeEach(function () {
Expand All @@ -111,14 +110,12 @@ describe('AolAdapter', function () {
body: getDefaultBidResponse()
};
logWarnSpy = sinon.spy(utils, 'logWarn');
formatPixelsStub = sinon.stub(spec, 'formatPixels');
isOneMobileBidderStub = sinon.stub(spec, 'isOneMobileBidder');
});

afterEach(function () {
$$PREBID_GLOBAL$$.bidderSettings = bidderSettingsBackup;
logWarnSpy.restore();
formatPixelsStub.restore();
isOneMobileBidderStub.restore();
});

Expand All @@ -139,27 +136,6 @@ describe('AolAdapter', function () {
ttl: bidRequest.ttl
});
});

it('should add pixels to ad content when pixels are present in the response', function () {
bidResponse.body.ext = {
pixels: 'pixels-content'
};

formatPixelsStub.returns('pixels-content');
let formattedBidResponse = spec.interpretResponse(bidResponse, bidRequest);

expect(formattedBidResponse.ad).to.equal(DEFAULT_AD_CONTENT + 'pixels-content');
});

it('should show warning in the console', function() {
$$PREBID_GLOBAL$$.bidderSettings = {
aol: {
bidCpmAdjustment: function() {}
}
};
spec.interpretResponse(bidResponse, bidRequest);
expect(utils.logWarn.calledOnce).to.be.true;
});
});

describe('buildRequests()', function () {
Expand Down Expand Up @@ -492,69 +468,39 @@ describe('AolAdapter', function () {
});

describe('getUserSyncs()', function () {
let serverResponses;
let bidResponse;
let bidRequest;

beforeEach(function () {
$$PREBID_GLOBAL$$.aolGlobals.pixelsDropped = false;
config.setConfig({
aol: {
userSyncOn: 'bidResponse'
},
});
bidResponse = getDefaultBidResponse();
bidResponse.ext = {
pixels: getPixels()
};

serverResponses = [
{body: bidResponse}
];
});

it('should return user syncs only if userSyncOn equals to "bidResponse"', function () {
let userSyncs = spec.getUserSyncs({}, [bidResponse], bidRequest);
it('should return user syncs if pixels are present in the response', function () {
let userSyncs = spec.getUserSyncs({}, serverResponses);

expect($$PREBID_GLOBAL$$.aolGlobals.pixelsDropped).to.be.true;
expect(userSyncs).to.deep.equal([
{type: 'image', url: 'img.org'},
{type: 'iframe', url: 'pixels1.org'}
]);
});

it('should not return user syncs if it has already been returned', function () {
$$PREBID_GLOBAL$$.aolGlobals.pixelsDropped = true;

let userSyncs = spec.getUserSyncs({}, [bidResponse], bidRequest);

expect($$PREBID_GLOBAL$$.aolGlobals.pixelsDropped).to.be.true;
expect(userSyncs).to.deep.equal([]);
});

it('should not return user syncs if pixels are not present', function () {
bidResponse.ext.pixels = null;
let userSyncs = spec.getUserSyncs({}, serverResponses);

let userSyncs = spec.getUserSyncs({}, [bidResponse], bidRequest);

expect($$PREBID_GLOBAL$$.aolGlobals.pixelsDropped).to.be.false;
expect(userSyncs).to.deep.equal([]);
});
});

describe('formatPixels()', function () {
it('should return pixels wrapped for dropping them once and within nested frames ', function () {
let pixels = '<script>document.write(\'<pixels-dom-elements/>\');</script>';
let formattedPixels = spec.formatPixels(pixels);

expect(formattedPixels).to.equal(
'<script>var w=window,prebid;' +
'for(var i=0;i<10;i++){w = w.parent;prebid=w.$$PREBID_GLOBAL$$;' +
'if(prebid && prebid.aolGlobals && !prebid.aolGlobals.pixelsDropped){' +
'try{prebid.aolGlobals.pixelsDropped=true;' +
'document.write(\'<pixels-dom-elements/>\');break;}' +
'catch(e){continue;}' +
'}}</script>');
});
});

describe('isOneMobileBidder()', function () {
it('should return false when when bidderCode is not present', function () {
it('should return false when when bidderCode is not present', () => {
expect(spec.isOneMobileBidder(null)).to.be.false;
});

Expand Down