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 Module: support ui events #12398

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
151 changes: 113 additions & 38 deletions modules/contxtfulRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const CONTXTFUL_RECEPTIVITY_DOMAIN = 'api.receptivity.io';

const storageManager = getStorageManager({
moduleType: MODULE_TYPE_RTD,
moduleName: MODULE_NAME
moduleName: MODULE_NAME,
});

let rxApi = null;
Expand All @@ -43,14 +43,11 @@ function getRxEngineReceptivity(requester) {
}

function getItemFromSessionStorage(key) {
let value = null;
try {
// Use the Storage Manager
value = storageManager.getDataFromSessionStorage(key, null);
return storageManager.getDataFromSessionStorage(key);
} catch (error) {
logError(MODULE, error);
}

return value;
}

function loadSessionReceptivity(requester) {
Expand Down Expand Up @@ -102,6 +99,9 @@ function init(config) {

try {
initCustomer(config);

observeLastCursorPosition();

return true;
} catch (error) {
logError(MODULE, error);
Expand Down Expand Up @@ -170,6 +170,9 @@ function addConnectorEventListener(tagId, prebidConfig) {
}
config['prebid'] = prebidConfig || {};
rxApi = await rxApiBuilder(config);

// Remove listener now that we can use rxApi.
removeListeners();
}
);
}
Expand All @@ -189,8 +192,11 @@ function getTargetingData(adUnits, config, _userConsent) {
logInfo(MODULE, 'getTargetingData');

const requester = config?.params?.customer;
const rx = getRxEngineReceptivity(requester) ||
loadSessionReceptivity(requester) || {};
const rx =
getRxEngineReceptivity(requester) ||
loadSessionReceptivity(requester) ||
{};

if (isEmpty(rx)) {
return {};
}
Expand All @@ -215,56 +221,42 @@ function getBidRequestData(reqBidsConfigObj, onDone, config, userConsent) {
function onReturn() {
if (isFirstBidRequestCall) {
isFirstBidRequestCall = false;
};
}
onDone();
}

logInfo(MODULE, 'getBidRequestData');
const bidders = config?.params?.bidders || [];
if (isEmpty(bidders) || !isArray(bidders)) {
onReturn();
return;
}

let fromApiBatched = () => rxApi?.receptivityBatched?.(bidders);
let fromApiSingle = () => prepareBatch(bidders, getRxEngineReceptivity);
let fromStorage = () => prepareBatch(bidders, loadSessionReceptivity);

function tryMethods(methods) {
for (let method of methods) {
try {
let batch = method();
if (!isEmpty(batch)) {
return batch;
}
} catch (error) { }
}
return {};
let fromApi = rxApi?.receptivityBatched?.(bidders) || {};
let fromStorage = prepareBatch(bidders, (bidder) => loadSessionReceptivity(`${config?.params?.customer}_${bidder}`));

let sources = [fromStorage, fromApi];
if (isFirstBidRequestCall) {
sources.reverse();
}
let rxBatch = {};
try {
if (isFirstBidRequestCall) {
rxBatch = tryMethods([fromStorage, fromApiBatched, fromApiSingle]);
} else {
rxBatch = tryMethods([fromApiBatched, fromApiSingle, fromStorage])
}
} catch (error) { }

let rxBatch = Object.assign(...sources);

let singlePointEvents;
if (isEmpty(rxBatch)) {
onReturn();
return;
singlePointEvents = btoa(JSON.stringify({ ui: getUiEvents() }));
}

bidders
.map((bidderCode) => ({ bidderCode, rx: rxBatch[bidderCode] }))
.filter(({ rx }) => !isEmpty(rx))
.forEach(({ bidderCode, rx }) => {
.forEach(bidderCode => {
const ortb2 = {
user: {
data: [
{
name: MODULE_NAME,
ext: {
rx,
rx: rxBatch[bidderCode],
events: singlePointEvents,
params: {
ev: config.params?.version,
ci: config.params?.customer,
Expand All @@ -274,13 +266,96 @@ function getBidRequestData(reqBidsConfigObj, onDone, config, userConsent) {
],
},
};

mergeDeep(reqBidsConfigObj.ortb2Fragments?.bidder, {
[bidderCode]: ortb2,
});
});

onReturn();
};
}

function getUiEvents() {
return {
position: lastCursorPosition,
screen: getScreen(),
};
}

function getScreen() {
function getInnerSize() {
let w = window?.innerWidth;
let h = window?.innerHeight;

if (w && h) {
return [w, h];
}
}

function getDocumentSize() {
let body = window?.document?.body;
let w = body.clientWidth;
let h = body.clientHeight;

if (w && h) {
return [w, h];
}
}

// If we cannot access or cast the window dimensions, we get None.
// If we cannot collect the size from the window we try to use the root document dimensions
let [width, height] = getInnerSize() || getDocumentSize() || [0, 0];
let topLeft = { x: window.scrollX, y: window.scrollY };

return {
topLeft,
width,
height,
timestampMs: performance.now(),
};
}

let lastCursorPosition;

function observeLastCursorPosition() {
function pointerEventToPosition(event) {
lastCursorPosition = {
x: event.clientX,
y: event.clientY,
timestampMs: performance.now()
};
}

function touchEventToPosition(event) {
let touch = event.touches.item(0);
if (!touch) {
return;
}

lastCursorPosition = {
x: touch.clientX,
y: touch.clientY,
timestampMs: performance.now()
};
}

addListener('pointermove', pointerEventToPosition);
addListener('touchmove', touchEventToPosition);
}

let listeners = {};
function addListener(name, listener) {
listeners[name] = listener;

window.addEventListener(name, listener);
}

function removeListeners() {
for (const name in listeners) {
window.removeEventListener(name, listeners[name]);
delete listeners[name];
}
}

export const contxtfulSubmodule = {
name: MODULE_NAME,
Expand Down
4 changes: 2 additions & 2 deletions 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 contact [contact@contxtful.com](mailto:contact@contxtful.com).
To utilize this module, you need to register for an account with [Contxtful](https://contxtful.com). For inquiries, please reach out to [contact@contxtful.com](mailto:contact@contxtful.com).

## Build Instructions

Expand Down Expand Up @@ -72,7 +72,7 @@ pbjs.setConfig({
| `customer` | `String` | Required | Your unique customer identifier. |
| `hostname` | `String` | Optional | Target URL for CONTXTFUL external JavaScript file. Default is "api.receptivity.io". Changing default behaviour is not recommended. Please reach out to contact@contxtful.com if you experience issues. |
| `adServerTargeting` | `Boolean`| Optional | Enables the `getTargetingData` to inject targeting value in ad units. Setting to true enables the feature, false disables the feature. Default is true |
| `bidders` | `Array` | Optional | Setting this array enables Receptivity in the `ortb2` object through `getBidRequestData` for all the listed `bidders`. Default is `[]` (an empty array). RECOMMENDED : Add all the bidders active like this `["bidderCode1", "bidderCode", "..."]` |
| `bidders` | `Array` | Optional | Setting this array enables Receptivity in the `ortb2` object through `getBidRequestData` for all the listed `bidders`. Default is `[]` (an empty array). RECOMMENDED : Add all the active bidders like this `["bidderCode1", "bidderCode", "..."]` |

## Usage: Injection in Ad Servers

Expand Down
Loading