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#69 in AOLP_ADS_JS/prebid.js from release/1.…
Browse files Browse the repository at this point in the history
…11.0 to master

* commit 'e0e25bc31e66fe7c89003741d654ea6b25fb1a21':
  Mistype fix.
  Removed conflict message leftover.
  Change method names, simplified regex, DOMReady logic introduced.
  Minor changes in the code.
  Small changes.
  Removed adapter.
  Adding functionality of cookie syncing upon bid request
  • Loading branch information
Pereira, Marcio committed Feb 16, 2017
2 parents 7d144bb + e0e25bc commit ac34b02
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
AOL Prebid 1.11.0
----------------
Added functionality of cookie syncing upon bid response.


AOL Prebid 1.10.0
----------------
Updated to Prebid 0.16.0
Expand Down
115 changes: 114 additions & 1 deletion src/adapters/aol.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const utils = require('../utils.js');
const ajax = require('../ajax.js').ajax;
const bidfactory = require('../bidfactory.js');
const bidmanager = require('../bidmanager.js');
const constants = require('../constants.json');

const AolAdapter = function AolAdapter() {

Expand All @@ -14,6 +15,114 @@ const AolAdapter = function AolAdapter() {
as: 'adserver-as.adtech.advertising.com'
};

const SYNC_TYPES = {
iframe: 'IFRAME',
img: 'IMG'
};

let DOMReady = (() => {
let readyEventFired = false;
return fn => {
let idempotentFn = () => {
if (readyEventFired) {
return;
}
readyEventFired = true;
return fn();
};
let doScrollCheck = () => {
if (readyEventFired) {
return;
}
try {
document.documentElement.doScroll('left');
} catch (e) {
setTimeout(doScrollCheck, 1);
return;
}
return idempotentFn();
};
if (document.readyState === "complete") {
return idempotentFn();
}
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", idempotentFn, false);
window.addEventListener("load", idempotentFn, false);
} else if (document.attachEvent) {
document.attachEvent("onreadystatechange", idempotentFn);
window.attachEvent("onload", idempotentFn);
let topLevel = false;
try {
topLevel = window.frameElement === null;
} catch (e) {
}
if (document.documentElement.doScroll && topLevel) {
return doScrollCheck();
}
}
};
})();

function dropSyncCookies(pixels) {
let pixelElements = parsePixelItems(pixels);
renderPixelElements(pixelElements);
}

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) {
pixels.match(itemsRegExp).forEach(item => {
let tagNameMatches = item.match(tagNameRegExp);
let sourcesPathMatches = item.match(srcRegExp);

if (tagNameMatches && sourcesPathMatches) {
pixelsItems.push({
tagName: tagNameMatches[0].toUpperCase(),
src: sourcesPathMatches[2]
});
}
});
}

return pixelsItems;
}

function renderPixelElements(pixelsElements) {
pixelsElements.forEach((element) => {
switch (element.tagName) {
case SYNC_TYPES.img:
return renderPixelImage(element);
case SYNC_TYPES.iframe:
return renderPixelIframe(element);
}
});
}

function renderPixelImage(pixelsItem) {
let image = new Image();
image.src = pixelsItem.src;
}

function renderPixelIframe(pixelsItem) {
let iframe = document.createElement('iframe');
iframe.width = 1;
iframe.height = 1;
iframe.style = 'display: none';
iframe.src = pixelsItem.src;
if (document.readyState === 'interactive' ||
document.readyState === 'complete') {
document.body.appendChild(iframe);
} else {
DOMReady(() => {
document.body.appendChild(iframe);
});
}
}

function template(strings, ...keys) {
return function(...values) {
let dict = values[values.length - 1] || {};
Expand Down Expand Up @@ -94,7 +203,11 @@ const AolAdapter = function AolAdapter() {

let ad = bidData.adm;
if (response.ext && response.ext.pixels) {
ad += response.ext.pixels;
if (bid.params.userSyncOn === constants.EVENTS.BID_RESPONSE) {
dropSyncCookies(response.ext.pixels);
} else {
ad += response.ext.pixels;
}
}

const bidResponse = bidfactory.createBid(1, bid);
Expand Down
75 changes: 75 additions & 0 deletions test/spec/adapters/aol_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,80 @@ describe('AolAdapter', () => {
var bidResponse = bidmanager.addBidResponse.firstCall.args[1];
expect(bidResponse.cpm).to.equal('a9334987');
});

it('should not render pixels on pubapi response when no parameter is set', () => {
server.respondWith(JSON.stringify({
"id": "245730051428950632",
"cur": "USD",
"seatbid": [{
"bid": [{
"id": 1,
"impid": "245730051428950632",
"price": 0.09,
"adm": "<script>console.log('ad');</script>",
"crid": "12345",
"h": 90,
"w": 728,
"ext": {"sizeid": 225}
}]
}],
"ext": {
"pixels": "<script>document.write('<iframe src=\"pixels.org\"></iframe>');</script>"
}
}));
adapter.callBids(DEFAULT_BIDDER_REQUEST);
server.respond();
expect(bidmanager.addBidResponse.calledOnce).to.be.true;
expect(document.body.querySelectorAll('iframe[src="pixels.org"]').length).to.equal(0);
});

it('should render pixels from pubapi response when param userSyncOn is set with \'bidResponse\'', () => {
server.respondWith(JSON.stringify({
"id": "245730051428950632",
"cur": "USD",
"seatbid": [{
"bid": [{
"id": 1,
"impid": "245730051428950632",
"price": 0.09,
"adm": "<script>console.log('ad');</script>",
"crid": "12345",
"h": 90,
"w": 728,
"ext": {"sizeid": 225}
}]
}],
"ext": {
"pixels": "<script>document.write('<iframe src=\"pixels.org\"></iframe>" +
"<iframe src=\"pixels1.org\"></iframe>');</script>"
}
}));
adapter.callBids({
bidderCode: 'aol',
requestId: 'd3e07445-ab06-44c8-a9dd-5ef9af06d2a6',
bidderRequestId: '7101db09af0db2',
start: new Date().getTime(),
bids: [{
bidder: 'aol',
bidId: '84ab500420319d',
bidderRequestId: '7101db09af0db2',
requestId: 'd3e07445-ab06-44c8-a9dd-5ef9af06d2a6',
placementCode: 'foo',
params: {
placement: 1234567,
network: '9599.1',
userSyncOn: 'bidResponse'
}
}]
});
server.respond();
expect(bidmanager.addBidResponse.calledOnce).to.be.true;
expect(document.body.querySelectorAll('iframe[src="pixels.org"]')[0].outerHTML).to.equal
('<iframe width="1" height="1" src="pixels.org" style="display: none;"></iframe>');
expect(document.body.querySelectorAll('iframe[src="pixels1.org"]')[0].outerHTML).to.equal
('<iframe width="1" height="1" src="pixels1.org" style="display: none;"></iframe>');
});

});

describe('when bidCpmAdjustment is set', () => {
Expand Down Expand Up @@ -463,4 +537,5 @@ describe('AolAdapter', () => {
});
});
});

});

0 comments on commit ac34b02

Please sign in to comment.