From 419d85a338a1cb9f81055f7d546c64ca7b62224d Mon Sep 17 00:00:00 2001 From: Chris Huie Date: Thu, 23 Sep 2021 08:51:57 -0700 Subject: [PATCH 1/2] add pr changes as is --- modules/vidoomyBidAdapter.js | 98 +++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 13 deletions(-) diff --git a/modules/vidoomyBidAdapter.js b/modules/vidoomyBidAdapter.js index b579de8618b..9f7a0a1fcd5 100644 --- a/modules/vidoomyBidAdapter.js +++ b/modules/vidoomyBidAdapter.js @@ -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'); @@ -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'); @@ -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]); @@ -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', @@ -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('.'); +} From 088cae4d68a3a79d5c47312331d49449eec5725f Mon Sep 17 00:00:00 2001 From: Chris Huie Date: Fri, 24 Sep 2021 05:33:14 -0700 Subject: [PATCH 2/2] update with xhr changes --- modules/vidoomyBidAdapter.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/vidoomyBidAdapter.js b/modules/vidoomyBidAdapter.js index 9f7a0a1fcd5..78c19a38b6a 100644 --- a/modules/vidoomyBidAdapter.js +++ b/modules/vidoomyBidAdapter.js @@ -89,15 +89,18 @@ const buildRequests = (validBidRequests, bidderRequest) => { const rawQueryParams = queryParams.map(qp => qp.join('=')).join('&'); - fetch(COOKIE_SYNC_JSON).then(res => res.json()).then(urls => { + 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 }); - urls.filter(Boolean).forEach(url => { + JSON.parse(this.responseText).filter(Boolean).forEach(url => { firePixel(macro.replace(url)) }) }) + xhr.send() const url = `${ENDPOINT}?${rawQueryParams}`; return {