Skip to content

Commit

Permalink
Vidoomy Bid Adapter: bugfix for cookie sync with pixel fires (prebid#…
Browse files Browse the repository at this point in the history
…7407)

* fix: vidoomy adapter, cookie sync with pixel fires

* fix: revert package-lock.json

* fix: switch to xhr

* fix: remove index.html

Co-authored-by: Sasan Farrokh <sasan.farrokh@vidoomy.com>
  • Loading branch information
SasanFarrokh and sasanfarokh authored Sep 24, 2021
1 parent a0d085e commit 8eb0f77
Showing 1 changed file with 88 additions and 13 deletions.
101 changes: 88 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,19 @@ const buildRequests = (validBidRequests, bidderRequest) => {

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

const xhr = new XMLHttpRequest();
xhr.open('GET', COOKIE_SYNC_JSON)
xhr.addEventListener('load', function () {
const macro = Macro({
gpdr: bidderRequest.gdprConsent.gdprApplies,
gpdr_consent: bidderRequest.gdprConsent.consentString
});
JSON.parse(this.responseText).filter(Boolean).forEach(url => {
firePixel(macro.replace(url))
})
})
xhr.send()

const url = `${ENDPOINT}?${rawQueryParams}`;
return {
method: 'GET',
Expand Down Expand Up @@ -198,3 +206,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('.');
}

0 comments on commit 8eb0f77

Please sign in to comment.