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

Contxtful Rtd Provider - Feature Ad Unit Positions #12792

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
165 changes: 165 additions & 0 deletions modules/contxtfulRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ import {
buildUrl,
isArray,
generateUUID,
canAccessWindowTop,
deepAccess,
getSafeframeGeometry,
getWindowSelf,
getWindowTop,
inIframe,
isSafeFrameWindow,
} from '../src/utils.js';
import { loadExternalScript } from '../src/adloader.js';
import { getStorageManager } from '../src/storageManager.js';
import { MODULE_TYPE_RTD } from '../src/activities/modules.js';
import { getGptSlotInfoForAdUnitCode } from '../libraries/gptUtils/gptUtils.js';

const MODULE_NAME = 'contxtful';
const MODULE = `${MODULE_NAME}RtdProvider`;
Expand Down Expand Up @@ -225,6 +233,161 @@ function getTargetingData(adUnits, config, _userConsent) {
}
}

function getVisibilityStateElement(domElement, windowTop) {
if ('checkVisibility' in domElement) {
return domElement.checkVisibility();
}

const elementCss = windowTop.getComputedStyle(domElement, null);
return elementCss.display !== 'none';
}

function getElementFromTopWindowRecurs(element, currentWindow) {
try {
if (getWindowTop() === currentWindow) {
return element;
} else {
const frame = currentWindow.frameElement;
const frameClientRect = frame.getBoundingClientRect();
const elementClientRect = element.getBoundingClientRect();
if (frameClientRect.width !== elementClientRect.width || frameClientRect.height !== elementClientRect.height) {
return undefined;
}
return getElementFromTopWindowRecurs(frame, currentWindow.parent);
}
} catch (err) {
logError(MODULE, err);
return undefined;
}
}

function getDivIdPosition(divId) {
if (!isSafeFrameWindow() && !canAccessWindowTop()) {
return {};
}

const position = {};

if (isSafeFrameWindow()) {
const { self } = getSafeframeGeometry() ?? {};

if (!self) {
return {};
}

position.x = Math.round(self.t);
position.y = Math.round(self.l);
} else {
try {
// window.top based computing
const wt = getWindowTop();
const d = wt.document;

let domElement;

if (inIframe() === true) {
const ws = getWindowSelf();
const currentElement = ws.document.getElementById(divId);
domElement = getElementFromTopWindowRecurs(currentElement, ws);
} else {
domElement = wt.document.getElementById(divId);
}

if (!domElement) {
return {};
}

let box = domElement.getBoundingClientRect();
const docEl = d.documentElement;
const body = d.body;
const clientTop = (d.clientTop ?? body.clientTop) ?? 0;
const clientLeft = (d.clientLeft ?? body.clientLeft) ?? 0;
const scrollTop = (wt.scrollY ?? docEl.scrollTop) ?? body.scrollTop;
const scrollLeft = (wt.scrollX ?? docEl.scrollLeft) ?? body.scrollLeft;

position.visibility = getVisibilityStateElement(domElement, wt);
position.x = Math.round(box.left + scrollLeft - clientLeft);
position.y = Math.round(box.top + scrollTop - clientTop);
} catch (err) {
logError(MODULE, err);
return {};
}
}

return position;
}

function tryGetDivIdPosition(divIdMethod) {
let divId = divIdMethod();
if (divId) {
const divIdPosition = getDivIdPosition(divId);
if (divIdPosition.x !== undefined && divIdPosition.y !== undefined) {
return divIdPosition;
}
}
return undefined;
}

function tryMultipleDivIdPositions(adUnit) {
let divMethods = [
// ortb2\
() => {
adUnit.ortb2Imp = adUnit.ortb2Imp || {};
const ortb2Imp = deepAccess(adUnit, 'ortb2Imp');
return deepAccess(ortb2Imp, 'ext.data.divId');
},
// gpt
() => getGptSlotInfoForAdUnitCode(adUnit.code).divId,
// adunit code
() => adUnit.code
];

for (const divMethod of divMethods) {
let divPosition = tryGetDivIdPosition(divMethod);
if (divPosition) {
return divPosition;
}
}
}

function tryGetAdUnitPosition(adUnit) {
let adUnitPosition = {};
adUnit.ortb2Imp = adUnit.ortb2Imp || {};

// Try to get position with the divId
const divIdPosition = tryMultipleDivIdPositions(adUnit);
if (divIdPosition) {
adUnitPosition.p = { x: divIdPosition.x, y: divIdPosition.y };
adUnitPosition.v = divIdPosition.visibility;
adUnitPosition.t = 'div';
return adUnitPosition;
}

// Try to get IAB position
const iabPos = adUnit?.mediaTypes?.banner?.pos;
if (iabPos !== undefined) {
adUnitPosition.p = iabPos;
adUnitPosition.t = 'iab';
return adUnitPosition;
}

return undefined;
}

function getAdUnitPositions(bidReqConfig) {
const adUnits = bidReqConfig.adUnits || [];
let adUnitPositions = {};

for (const adUnit of adUnits) {
let adUnitPosition = tryGetAdUnitPosition(adUnit);
if (adUnitPosition) {
adUnitPositions[adUnit.code] = adUnitPosition;
}
}

return adUnitPositions;
}

/**
* @param {Object} reqBidsConfigObj Bid request configuration object
* @param {Function} onDone Called on completion
Expand All @@ -237,6 +400,7 @@ function getBidRequestData(reqBidsConfigObj, onDone, config, userConsent) {
}

logInfo(MODULE, 'getBidRequestData');
const adUnitsPositions = getAdUnitPositions(reqBidsConfigObj);
const bidders = config?.params?.bidders || [];
if (isEmpty(bidders) || !isArray(bidders)) {
onReturn();
Expand All @@ -262,6 +426,7 @@ function getBidRequestData(reqBidsConfigObj, onDone, config, userConsent) {
ext: {
rx: rxBatch[bidderCode],
events: singlePointEvents,
pos: btoa(JSON.stringify(adUnitsPositions)),
sm: sm(),
params: {
ev: config.params?.version,
Expand Down
2 changes: 1 addition & 1 deletion modules/contxtfulRtdProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

The Contxtful RTD module offers a unique feature—Receptivity. Receptivity is an efficiency metric, enabling the qualification of any instant in a session in real time based on attention. The core idea is straightforward: the likelihood of an ad’s success increases when it grabs attention and is presented in the right context at the right time.

To utilize this module, you need to register for an account with [Contxtful](https://contxtful.com). For inquiries, please reach out to [[email protected]](mailto:[email protected]).
To utilize this module, you need to register for an account with [Contxtful](https://contxtful.com). For inquiries, please reach out to [[email protected]](mailto:[email protected]).

## Build Instructions

Expand Down
Loading