Skip to content

Commit

Permalink
Merge pull request #7 from PubMatic-OpenWrap/kargo
Browse files Browse the repository at this point in the history
added kargo adapter
  • Loading branch information
PubMatic-OpenWrap authored Nov 3, 2017
2 parents 15dadc2 + 3777dc4 commit cbcc883
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions adapters.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"hiromedia",
"indexExchange",
"innity",
"kargo",
"kruxlink",
"getintent",
"inneractive",
Expand Down
168 changes: 168 additions & 0 deletions src/adapters/kargo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
const bidfactory = require('../bidfactory.js');
const bidmanager = require('../bidmanager.js');
const adloader = require('../adloader.js');
const utils = require('../utils.js');
const adaptermanager = require('../adaptermanager');
const CONSTANTS = require('../constants.json');
const HOST = $$PREBID_GLOBAL$$.kargo_kraken_host || 'https://krk.kargo.com';

const KargoAdapter = function KargoAdapter() {
function _handleBid(bids) {
return function wrappedHandleBid(adUnits) {
utils._map(bids, bid => {
let adUnit = adUnits[bid.params.placementId];

if (adUnit) {
bidmanager.addBidResponse(bid.placementCode, _createBid(adUnit));

if (adUnit.receivedTracker) {
var el = document.createElement('img');
el.src = adUnit.receivedTracker;
document.body.appendChild(el);
}
}
});
};
}

function _createBid(adUnit) {
let bidObject = bidfactory.createBid(CONSTANTS.STATUS.GOOD);
bidObject.bidderCode = 'kargo';
bidObject.cpm = Number(adUnit.cpm);
bidObject.ad = adUnit.adm;
bidObject.width = adUnit.width;
bidObject.height = adUnit.height;
return bidObject;
}

function _callBids(params) {
const transformedParams = Object.assign({}, {
timeout: params.timeout,
currency: 'USD',
cpmGranularity: 1,
cpmRange: {
floor: 0,
ceil: 20
},
adSlotIds: utils._map(params.bids, bid => bid.params.placementId)
}, _getAllMetadata());
const encodedParams = encodeURIComponent(JSON.stringify(transformedParams));
const callbackName = `kargo_prebid_${params.requestId.replace(/-/g, '_')}`;

$$PREBID_GLOBAL$$[callbackName] = _handleBid(params.bids);

adloader.loadScript(`${HOST}/api/v1/bid?json=${encodedParams}&cb=window.${preBidNameSpace}.${callbackName}`);
}

function _readCookie(name) {
let nameEquals = `${name}=`;
let cookies = document.cookie.split(';');

for (let key in cookies) {
let cookie = cookies[key];
while (cookie.charAt(0) === ' ') {
cookie = cookie.substring(1, cookie.length);
}

if (cookie.indexOf(nameEquals) === 0) {
return cookie.substring(nameEquals.length, cookie.length);
}
}

return null;
}

function _getCrbIds() {
try {
const crb = JSON.parse(decodeURIComponent(_readCookie('krg_crb')));
var syncIds = {};

if (crb && crb.v) {
var vParsed = JSON.parse(atob(crb.v));

if (vParsed && vParsed.syncIds) {
syncIds = vParsed.syncIds;
}
}

return syncIds;
} catch (e) {
return {};
}
}

function _getUid() {
try {
const uid = JSON.parse(decodeURIComponent(_readCookie('krg_uid')));
var vData = {};

if (uid && uid.v) {
vData = uid.v;
}

return vData;
} catch (e) {
return {};
}
}

function _getKruxUserId() {
return _getLocalStorageSafely('kxkar_user');
}

function _getKruxSegments() {
return _getLocalStorageSafely('kxkar_segs');
}

function _getKrux() {
const segmentsStr = _getKruxSegments();
var segments = [];

if (segmentsStr) {
segments = segmentsStr.split(',');
}

return {
userID: _getKruxUserId(),
segments: segments
};
}

function _getLocalStorageSafely(key) {
try {
return localStorage.getItem(key);
} catch (e) {
return null;
}
}

function _getUserIds() {
const uid = _getUid();
const crbIds = _getCrbIds();

return {
kargoID: uid.userId,
clientID: uid.clientId,
crbIDs: crbIds,
optOut: uid.optOut
};
}

function _getAllMetadata() {
return {
userIDs: _getUserIds(),
krux: _getKrux(),
pageURL: window.location.href
};
}

// Export the callBids function, so that prebid.js can execute
// this function when the page asks to send out bid requests.
return {
callBids: _callBids
};
};

//adaptermanager.registerBidAdapter(new KargoAdapter(), 'kargo');

module.exports = KargoAdapter;

0 comments on commit cbcc883

Please sign in to comment.