Skip to content

Commit

Permalink
Media.net Adapter Improvements (#2634)
Browse files Browse the repository at this point in the history
* Media.net adapter: passing slot visibility to server

* Media.net adapter: changed slot visibility logic and removed mocks on window properties

* Media.net adapter: refactored slot-visibility tests to use beforeEach and afterEach hooks

* create only one sandbox and restore for medianet
  • Loading branch information
vedantseta authored and snapwich committed Jun 8, 2018
1 parent 15b017b commit 1d318f9
Show file tree
Hide file tree
Showing 2 changed files with 288 additions and 12 deletions.
93 changes: 92 additions & 1 deletion modules/medianetBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { config } from 'src/config';

const BIDDER_CODE = 'medianet';
const BID_URL = '//prebid.media.net/rtb/prebid';
const SLOT_VISIBILITY = {
NOT_DETERMINED: 0,
ABOVE_THE_FOLD: 1,
BELOW_THE_FOLD: 2
};

$$PREBID_GLOBAL$$.medianetGlobals = {};

Expand Down Expand Up @@ -71,6 +76,31 @@ function getSize(size) {
}
}

function getWindowSize() {
return {
w: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || -1,
h: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || -1
}
}

function getCoordinates(id) {
const element = document.getElementById(id);
if (element && element.getBoundingClientRect) {
const rect = element.getBoundingClientRect();
let coordinates = {};
coordinates.top_left = {
y: rect.top,
x: rect.left
};
coordinates.bottom_right = {
y: rect.bottom,
x: rect.right
};
return coordinates
}
return null;
}

function extParams(params, gdpr) {
let ext = {
customer_id: params.cid,
Expand All @@ -80,6 +110,10 @@ function extParams(params, gdpr) {
if (ext.gdpr_applies) {
ext.gdpr_consent_string = gdpr.consentString || '';
}
let windowSize = spec.getWindowSize();
if (windowSize.w !== -1 && windowSize.h !== -1) {
ext.screen = windowSize;
}
return ext;
}

Expand All @@ -102,9 +136,64 @@ function slotParams(bidRequest) {
if (bidFloor) {
params.bidfloor = bidFloor;
}
const coordinates = getCoordinates(bidRequest.adUnitCode);
if (coordinates) {
let normCoordinates = normalizeCoordinates(coordinates);
params.ext.coordinates = normCoordinates;
params.ext.viewability = getSlotVisibility(coordinates.top_left, getMinSize(params.banner));
if (getSlotVisibility(normCoordinates.top_left, getMinSize(params.banner)) > 0.5) {
params.ext.visibility = SLOT_VISIBILITY.ABOVE_THE_FOLD;
} else {
params.ext.visibility = SLOT_VISIBILITY.BELOW_THE_FOLD;
}
} else {
params.ext.visibility = SLOT_VISIBILITY.NOT_DETERMINED;
}

return params;
}

function getMinSize(sizes) {
return sizes.reduce((min, size) => size.h * size.w < min.h * min.w ? size : min);
}

function getSlotVisibility(topLeft, size) {
let maxArea = size.w * size.h;
let windowSize = spec.getWindowSize();
let bottomRight = {
x: topLeft.x + size.w,
y: topLeft.y + size.h
};
if (maxArea === 0 || windowSize.w === -1 || windowSize.h === -1) {
return 0;
}

return getOverlapArea(topLeft, bottomRight, {x: 0, y: 0}, {x: windowSize.w, y: windowSize.h}) / maxArea;
}

// find the overlapping area between two rectangles
function getOverlapArea(topLeft1, bottomRight1, topLeft2, bottomRight2) {
// If no overlap, return 0
if ((topLeft1.x > bottomRight2.x || bottomRight1.x < topLeft2.x) || (topLeft1.y > bottomRight2.y || bottomRight1.y < topLeft2.y)) {
return 0;
}
// return overlapping area : [ min of rightmost/bottommost co-ordinates ] - [ max of leftmost/topmost co-ordinates ]
return ((Math.min(bottomRight1.x, bottomRight2.x) - Math.max(topLeft1.x, topLeft2.x)) * (Math.min(bottomRight1.y, bottomRight2.y) - Math.max(topLeft1.y, topLeft2.y)));
}

function normalizeCoordinates(coordinates) {
return {
top_left: {
x: coordinates.top_left.x + window.pageXOffset,
y: coordinates.top_left.y + window.pageYOffset,
},
bottom_right: {
x: coordinates.bottom_right.x + window.pageXOffset,
y: coordinates.bottom_right.y + window.pageYOffset,
}
}
}

function generatePayload(bidRequests, bidderRequests) {
return {
site: siteDetails(bidRequests[0].params.site),
Expand Down Expand Up @@ -204,6 +293,8 @@ export const spec = {
if (syncOptions.pixelEnabled) {
return filterUrlsByType(cookieSyncUrls, 'image');
}
}
},

getWindowSize,
};
registerBidder(spec);
Loading

0 comments on commit 1d318f9

Please sign in to comment.