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

Adagio Bid Adapter: add capability to pass ext-data from localStorage in bidRequest #6415

Merged
merged 1 commit into from
Mar 26, 2021
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
68 changes: 65 additions & 3 deletions modules/adagioBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { OUTSTREAM } from '../src/video.js';

export const BIDDER_CODE = 'adagio';
export const LOG_PREFIX = 'Adagio:';
export const VERSION = '2.7.0';
export const VERSION = '2.8.0';
export const FEATURES_VERSION = '1';
export const ENDPOINT = 'https://mp.4dex.io/prebid';
export const SUPPORTED_MEDIA_TYPES = [BANNER, NATIVE, VIDEO];
Expand All @@ -23,7 +23,7 @@ export const ADAGIO_LOCALSTORAGE_KEY = 'adagioScript';
export const GVLID = 617;
export const storage = getStorageManager(GVLID, 'adagio');
export const RENDERER_URL = 'https://script.4dex.io/outstream-player.js';

export const MAX_SESS_DURATION = 30 * 60 * 1000;
export const ADAGIO_PUBKEY = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9el0+OEn6fvEh1RdVHQu4cnT0
jFSzIbGJJyg3cKqvtE6A0iaz9PkIdJIvSSSNrmJv+lRGKPEyRA/VnzJIieL39Ngl
Expand Down Expand Up @@ -62,6 +62,8 @@ export const ORTB_VIDEO_PARAMS = {

let currentWindow;

const EXT_DATA = {}

export function adagioScriptFromLocalStorageCb(ls) {
try {
if (!ls) {
Expand Down Expand Up @@ -108,6 +110,9 @@ export function getAdagioScript() {
// It's an antipattern regarding the TCF2 enforcement logic
// but it's the only way to respect the user choice update.
window.localStorage.removeItem(ADAGIO_LOCALSTORAGE_KEY);
// Extra data from external script.
// This key is removed only if localStorage is not accessible.
window.localStorage.removeItem('adagio');
}
});
}
Expand All @@ -131,6 +136,37 @@ function isSafeFrameWindow() {
return !!(ws.$sf && ws.$sf.ext);
}

// Get localStorage "adagio" data to be passed to the request
export function prepareExchange(storageValue) {
const adagioStorage = JSON.parse(storageValue, function(name, value) {
if (!name.startsWith('_') || name === '') {
return value;
}
});
let random = utils.deepAccess(adagioStorage, 'session.rnd');
let newSession = false;

if (internal.isNewSession(adagioStorage)) {
newSession = true;
random = Math.random();
}

const data = {
session: {
new: newSession,
rnd: random
}
}

utils.mergeDeep(EXT_DATA, adagioStorage, data);

internal.enqueue({
action: 'session',
ts: Date.now(),
data: EXT_DATA
});
}

function initAdagio() {
if (canAccessTopWindow()) {
currentWindow = (canAccessTopWindow()) ? utils.getWindowTop() : utils.getWindowSelf();
Expand All @@ -146,6 +182,14 @@ function initAdagio() {
w.ADAGIO.versions.adagioBidderAdapter = VERSION;
w.ADAGIO.isSafeFrameWindow = isSafeFrameWindow();

storage.getDataFromLocalStorage('adagio', (storageData) => {
try {
internal.prepareExchange(storageData);
} catch (e) {
utils.logError(LOG_PREFIX, e);
}
});

getAdagioScript();
}

Expand Down Expand Up @@ -561,6 +605,21 @@ function isRendererPreferredFromPublisher(bidRequest) {
);
}

/**
*
* @param {object} adagioStorage
* @returns {boolean}
*/
function isNewSession(adagioStorage) {
const now = Date.now();
const { lastActivityTime, vwSmplg } = utils.deepAccess(adagioStorage, 'session', {});
return (
!utils.isNumber(lastActivityTime) ||
!utils.isNumber(vwSmplg) ||
(now - lastActivityTime) > MAX_SESS_DURATION
)
}

export const internal = {
enqueue,
getOrAddAdagioAdUnit,
Expand All @@ -577,7 +636,9 @@ export const internal = {
getCurrentWindow,
supportIObs,
canAccessTopWindow,
isRendererPreferredFromPublisher
isRendererPreferredFromPublisher,
isNewSession,
prepareExchange
};

function _getGdprConsent(bidderRequest) {
Expand Down Expand Up @@ -918,6 +979,7 @@ export const spec = {
site: site,
pageviewId: pageviewId,
adUnits: groupedAdUnits[organizationId],
data: EXT_DATA,
regs: {
gdpr: gdprConsent,
coppa: coppa,
Expand Down
3 changes: 2 additions & 1 deletion test/spec/modules/adagioBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ describe('Adagio bid adapter', () => {
'schain',
'prebidVersion',
'adapterVersion',
'featuresVersion'
'featuresVersion',
'data'
];

it('groups requests by organizationId', function() {
Expand Down