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

Vidoomy Bid Adapter: bugfix for cookie sync with pixel fires #7407

Merged
merged 4 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
98 changes: 85 additions & 13 deletions modules/vidoomyBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { INSTREAM, OUTSTREAM } from '../src/video.js';
const ENDPOINT = `https://d.vidoomy.com/api/rtbserver/prebid/`;
const BIDDER_CODE = 'vidoomy';
const GVLID = 380;

const COOKIE_SYNC_JSON = 'https://vpaid.vidoomy.com/sync/urls.json';

const isBidRequestValid = bid => {
if (!bid.params) {
utils.logError(BIDDER_CODE + ': bid.params should be non-empty');
Expand Down Expand Up @@ -58,17 +61,9 @@ const buildRequests = (validBidRequests, bidderRequest) => {
adType = VIDEO;
}

let host = '';
try {
host = bidderRequest.refererInfo.referer.split('#')[0].replace(/^(https\:\/\/|http\:\/\/)|(\/)$/g, '').split('/')[0];
} catch (eBidRequest) {
try {
host = window.location.href.replace(/^(https\:\/\/|http\:\/\/)|(\/)$/g, '').split('/')[0];
} catch (eLocationHref) {
host = window.location.href;
}
}
const hostname = host.split(':')[0];
const aElement = document.createElement('a');
aElement.href = (bidderRequest.refererInfo && bidderRequest.refererInfo.referer) || top.location.href;
const hostname = aElement.hostname

const videoContext = utils.deepAccess(bid, 'mediaTypes.video.context');

Expand All @@ -83,8 +78,8 @@ const buildRequests = (validBidRequests, bidderRequest) => {
queryParams.push(['dt', /Mobi/.test(navigator.userAgent) ? 2 : 1]);
queryParams.push(['pid', bid.params.pid]);
queryParams.push(['requestId', bid.bidId]);
queryParams.push(['d', hostname]);
queryParams.push(['sp', encodeURIComponent(bidderRequest.refererInfo.referer)]);
queryParams.push(['d', getDomainWithoutSubdomain(hostname)]);
queryParams.push(['sp', encodeURIComponent(aElement.href)]);
if (bidderRequest.gdprConsent) {
queryParams.push(['gdpr', bidderRequest.gdprConsent.gdprApplies]);
queryParams.push(['gdprcs', bidderRequest.gdprConsent.consentString]);
Expand All @@ -94,6 +89,16 @@ const buildRequests = (validBidRequests, bidderRequest) => {

const rawQueryParams = queryParams.map(qp => qp.join('=')).join('&');

fetch(COOKIE_SYNC_JSON).then(res => res.json()).then(urls => {
const macro = Macro({
gpdr: bidderRequest.gdprConsent.gdprApplies,
gpdr_consent: bidderRequest.gdprConsent.consentString
});
urls.filter(Boolean).forEach(url => {
firePixel(macro.replace(url))
})
})

const url = `${ENDPOINT}?${rawQueryParams}`;
return {
method: 'GET',
Expand Down Expand Up @@ -198,3 +203,70 @@ export const spec = {
};

registerBidder(spec);

function firePixel(url) {
const img = document.createElement('img');
img.width = 1;
img.height = 1;
img.src = url;
document.body.appendChild(img);
setTimeout(() => {
img.remove();
}, 10000)
}

function normalizeKey (x) {
return x.replace(/_/g, '').toLowerCase();
}

function Macro (obj) {
const macros = {};
for (const key in obj) {
macros[normalizeKey(key)] = obj[key];
}

const set = (key, value) => {
macros[normalizeKey(key)] = typeof value === 'function' ? value : String(value);
};

return {
set,
setAll (obj) {
for (const key in obj) {
macros[normalizeKey(key)] = set(obj[key]);
}
},
replace (string, extraMacros = {}) {
const allMacros = {
...macros,
...extraMacros,
};
const regexes = [
/{{\s*([a-zA-Z0-9_]+)\s*}}/g,
/\$\$\s*([a-zA-Z0-9_]+)\s*\$\$/g,
/\[\s*([a-zA-Z0-9_]+)\s*\]/g,
];
regexes.forEach(regex => {
string = string.replace(regex, (str, x) => {
x = normalizeKey(x);
let value = allMacros[x];
value = typeof value === 'function' ? value(allMacros) : value;
return !value && value !== 0 ? '' : value;
});
});
return string;
},
};
}

function getDomainWithoutSubdomain (hostname) {
const parts = hostname.split('.');
const newParts = [];
for (let i = parts.length - 1; i >= 0; i--) {
newParts.push(parts[i]);
if (newParts.length !== 1 && parts[i].length > 3) {
break;
}
}
return newParts.reverse().join('.');
}
Loading