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 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
89 changes: 30 additions & 59 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,10 +41,6 @@ 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
Expand Down Expand Up @@ -80,32 +74,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 @@ -176,15 +144,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 +321,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 +369,7 @@ export const spec = {
}
}

let bidResponse = {
return {
bidderCode: bidRequest.bidderCode,
requestId: bidRequest.bidId,
ad: bidData.adm,
Expand All @@ -394,24 +383,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
60 changes: 8 additions & 52 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', () => {
let bidResponse;
let bidRequest;
let logWarnSpy;
let formatPixelsStub;
let isOneMobileBidderStub;

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

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

Expand All @@ -140,17 +137,6 @@ describe('AolAdapter', () => {
});
});

it('should add pixels to ad content when pixels are present in the response', () => {
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: {
Expand Down Expand Up @@ -492,67 +478,37 @@ describe('AolAdapter', () => {
});

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

beforeEach(() => {
$$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"', () => {
let userSyncs = spec.getUserSyncs({}, [bidResponse], bidRequest);
it('should return user syncs if pixels are present in the response', () => {
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', () => {
$$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', () => {
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()', () => {
it('should return pixels wrapped for dropping them once and within nested frames ', () => {
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()', () => {
it('should return false when when bidderCode is not present', () => {
expect(spec.isOneMobileBidder(null)).to.be.false;
Expand Down