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

MediaGo Bid Adapter : add pmguid, title, description, keywords and synchronize mguid from third party cookie to first party cookie. #10923

Merged
merged 10 commits into from
Jan 25, 2024
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
149 changes: 123 additions & 26 deletions modules/mediagoBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,105 @@ import { registerBidder } from '../src/adapters/bidderFactory.js';
const BIDDER_CODE = 'mediago';
// const PROTOCOL = window.document.location.protocol;
const ENDPOINT_URL = 'https://gbid.mediago.io/api/bid?tn=';
const COOKY_SYNC_URL = 'https://gtrace.mediago.io/ju/cs/eplist';
// const COOKY_SYNC_URL = 'https://gtrace.mediago.io/ju/cs/eplist';
const COOKY_SYNC_IFRAME_URL = 'https://cdn.mediago.io/js/cookieSync.html';
export const THIRD_PARTY_COOKIE_ORIGIN = 'https://cdn.mediago.io';

const TIME_TO_LIVE = 500;
const GVLID = 1020;
// const ENDPOINT_URL = '/api/bid?tn=';
const storage = getStorageManager({ bidderCode: BIDDER_CODE });
export const storage = getStorageManager({bidderCode: BIDDER_CODE});
let globals = {};
let itemMaps = {};

/* ----- mguid:start ------ */
const COOKIE_KEY_MGUID = '__mguid_';
const STORE_MAX_AGE = 1000 * 60 * 60 * 24 * 365;
export const COOKIE_KEY_MGUID = '__mguid_';
const COOKIE_KEY_PMGUID = '__pmguid_';
const COOKIE_RETENTION_TIME = 365 * 24 * 60 * 60 * 1000; // 1 year
let reqTimes = 0;
/**
* get page title
* @returns {string}
*/

export function getPageTitle(win = window) {
try {
const ogTitle = win.top.document.querySelector('meta[property="og:title"]')
return win.top.document.title || (ogTitle && ogTitle.content) || '';
} catch (e) {
const ogTitle = document.querySelector('meta[property="og:title"]')
return document.title || (ogTitle && ogTitle.content) || '';
}
}

/**
* 获取用户id
* @return {string}
* get page description
*
* @returns {string}
*/
export function getPageDescription(win = window) {
let element;

try {
element = win.top.document.querySelector('meta[name="description"]') ||
win.top.document.querySelector('meta[property="og:description"]')
} catch (e) {
element = document.querySelector('meta[name="description"]') ||
document.querySelector('meta[property="og:description"]')
}

return (element && element.content) || '';
}

/**
* get page keywords
* @returns {string}
*/
export function getPageKeywords(win = window) {
let element;

try {
element = win.top.document.querySelector('meta[name="keywords"]');
} catch (e) {
element = document.querySelector('meta[name="keywords"]');
}

return (element && element.content) || '';
}

/**
* get connection downlink
* @returns {number}
*/
const getUserID = () => {
const i = storage.getCookie(COOKIE_KEY_MGUID);
export function getConnectionDownLink(win = window) {
const nav = win.navigator || {};
return nav && nav.connection && nav.connection.downlink >= 0 ? nav.connection.downlink.toString() : undefined;
}

if (i === null) {
const uuid = utils.generateUUID();
storage.setCookie(COOKIE_KEY_MGUID, uuid, STORE_MAX_AGE);
return uuid;
/**
* get pmg uid
* 获取并生成用户的id
*
* @return {string}
*/
export const getPmgUID = () => {
if (!storage.cookiesAreEnabled()) return;

let pmgUid = storage.getCookie(COOKIE_KEY_PMGUID);
if (!pmgUid) {
pmgUid = utils.generateUUID();
try {
storage.setCookie(COOKIE_KEY_PMGUID, pmgUid, getCurrentTimeToUTCString());
} catch (e) {}
}
return i;
return pmgUid;
};

/* ----- mguid:end ------ */
/* ----- pmguid:end ------ */

/**
* 获取一个对象的某个值,如果没有则返回空字符串
*
* @param {Object} obj 对象
* @param {...string} keys 键名
* @return {any}
Expand Down Expand Up @@ -283,6 +348,16 @@ function getReferrer(bidRequest = {}, bidderRequest = {}) {
return pageUrl;
}

/**
* get current time to UTC string
* @returns utc string
*/
export function getCurrentTimeToUTCString() {
const date = new Date();
date.setTime(date.getTime() + COOKIE_RETENTION_TIME);
return date.toUTCString();
}

/**
* 获取rtb请求参数
*
Expand Down Expand Up @@ -317,6 +392,10 @@ function getParam(validBidRequests, bidderRequest) {

const timeout = bidderRequest.timeout || 2000;
const firstPartyData = bidderRequest.ortb2;
const topWindow = window.top;
const title = getPageTitle();
const desc = getPageDescription();
const keywords = getPageKeywords();

if (items && items.length) {
let c = {
Expand Down Expand Up @@ -344,11 +423,23 @@ function getParam(validBidRequests, bidderRequest) {
firstPartyData,
content,
cat,
reqTimes
reqTimes,
pmguid: getPmgUID(),
page: {
title: title ? title.slice(0, 100) : undefined,
desc: desc ? desc.slice(0, 300) : undefined,
keywords: keywords ? keywords.slice(0, 100) : undefined,
hLen: topWindow.history?.length || undefined,
},
device: {
nbw: getConnectionDownLink(),
hc: topWindow.navigator?.hardwareConcurrency || undefined,
dm: topWindow.navigator?.deviceMemory || undefined,
}
},
user: {
buyeruid: getUserID(),
id: sharedid || pubcid
buyeruid: storage.getCookie(COOKIE_KEY_MGUID) || undefined,
id: sharedid || pubcid,
},
eids,
site: {
Expand Down Expand Up @@ -407,13 +498,12 @@ export const spec = {
return {
method: 'POST',
url: ENDPOINT_URL + globals['token'],
data: payloadString
data: payloadString,
};
},

/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
Expand Down Expand Up @@ -471,19 +561,26 @@ export const spec = {
}

if (syncOptions.iframeEnabled) {
window.addEventListener('message', function handler(event) {
if (!event.data || event.origin != THIRD_PARTY_COOKIE_ORIGIN) {
return;
}

this.removeEventListener('message', handler);

event.stopImmediatePropagation();

const response = event.data;
if (!response.optout && response.mguid) {
storage.setCookie(COOKIE_KEY_MGUID, response.mguid, getCurrentTimeToUTCString());
}
}, true);
return [
{
type: 'iframe',
url: `${COOKY_SYNC_IFRAME_URL}?${syncParamUrl}`
}
];
} else {
return [
{
type: 'image',
url: `${COOKY_SYNC_URL}?${syncParamUrl}`
}
];
}
},

Expand Down
Loading