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

Connatix Bid Adapter: support viewability #12122

Merged
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
86 changes: 85 additions & 1 deletion modules/connatixBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import {
registerBidder
} from '../src/adapters/bidderFactory.js';

import { percentInView } from '../libraries/percentInView/percentInView.js';

import {
deepAccess,
isFn,
logError,
isArray,
formatQS,
getWindowTop,
isNumber,
isStr,
deepSetValue
} from '../src/utils.js';

Expand Down Expand Up @@ -62,6 +67,76 @@ export function validateVideo(mediaTypes) {
return video.context !== ADPOD;
}

export function _getMinSize(sizes) {
if (!sizes || sizes.length === 0) return undefined;
return sizes.reduce((minSize, currentSize) => {
const minArea = minSize.w * minSize.h;
const currentArea = currentSize.w * currentSize.h;
return currentArea < minArea ? currentSize : minSize;
});
}

export function _canSelectViewabilityContainer() {
try {
window.top.document.querySelector('#viewability-container');
return true;
} catch (e) {
return false;
}
}

export function _isViewabilityMeasurable(element) {
if (!element) return false;
return _canSelectViewabilityContainer(element);
}

export function _getViewability(element, topWin, { w, h } = {}) {
return topWin.document.visibilityState === 'visible'
? percentInView(element, topWin, { w, h })
: 0;
}

export function detectViewability(bid) {
const { params, adUnitCode } = bid;

const viewabilityContainerIdentifier = params.viewabilityContainerIdentifier;

let element = null;
let bidParamSizes = null;
let minSize = [];

if (isStr(viewabilityContainerIdentifier)) {
try {
element = document.querySelector(viewabilityContainerIdentifier) || window.top.document.querySelector(viewabilityContainerIdentifier);
if (element) {
bidParamSizes = [element.offsetWidth, element.offsetHeight];
minSize = _getMinSize(bidParamSizes)
}
} catch (e) {
logError(`Error while trying to find viewability container element: ${viewabilityContainerIdentifier}`);
}
}

if (!element) {
// Get the sizes from the mediaTypes object if it exists, otherwise use the sizes array from the bid object
bidParamSizes = bid.mediaTypes && bid.mediaTypes.banner && bid.mediaTypes.banner.sizes ? bid.mediaTypes.banner.sizes : bid.sizes;
bidParamSizes = typeof bidParamSizes === 'undefined' && bid.mediaType && bid.mediaType.video && bid.mediaType.video.playerSize ? bid.mediaType.video.playerSize : bidParamSizes;
bidParamSizes = typeof bidParamSizes === 'undefined' && bid.mediaType && bid.mediaType.video && isNumber(bid.mediaType.video.w) && isNumber(bid.mediaType.h) ? [bid.mediaType.video.w, bid.mediaType.video.h] : bidParamSizes;
minSize = _getMinSize(bidParamSizes ?? [])
element = document.getElementById(adUnitCode);
}

if (_isViewabilityMeasurable(element)) {
const minSizeObj = {
w: minSize[0],
h: minSize[1]
}
return Math.round(_getViewability(element, getWindowTop(), minSizeObj))
}

return null;
}

/**
* Get ids from Prebid User ID Modules and add them to the payload
*/
Expand Down Expand Up @@ -91,9 +166,10 @@ export const spec = {
const hasMediaTypes = Boolean(mediaTypes) && (Boolean(mediaTypes[BANNER]) || Boolean(mediaTypes[VIDEO]));
const isValidBanner = validateBanner(mediaTypes);
const isValidVideo = validateVideo(mediaTypes);
const isValidViewability = typeof params.viewabilityPercentage === 'undefined' || (isNumber(params.viewabilityPercentage) && params.viewabilityPercentage >= 0 && params.viewabilityPercentage <= 1);
const hasRequiredBidParams = Boolean(params.placementId);

const isValid = hasBidId && hasMediaTypes && isValidBanner && isValidVideo && hasRequiredBidParams;
const isValid = hasBidId && hasMediaTypes && isValidBanner && isValidVideo && hasRequiredBidParams && isValidViewability;
if (!isValid) {
logError(
`Invalid bid request:
Expand All @@ -120,10 +196,18 @@ export const spec = {
params,
sizes,
} = bid;

let detectedViewabilityPercentage = detectViewability(bid);
if (isNumber(detectedViewabilityPercentage)) {
detectedViewabilityPercentage = detectedViewabilityPercentage / 100;
}

return {
bidId,
mediaTypes,
sizes,
detectedViewabilityPercentage,
declaredViewabilityPercentage: bid.params.viewabilityPercentage ?? null,
placementId: params.placementId,
floor: getBidFloor(bid),
};
Expand Down
Loading