Skip to content

Commit

Permalink
updated getting iab subcategory code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaimin Panchal committed Feb 11, 2019
1 parent 37391e4 commit 2b81856
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
29 changes: 20 additions & 9 deletions modules/appnexusBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Renderer } from '../src/Renderer';
import * as utils from '../src/utils';
import { registerBidder } from '../src/adapters/bidderFactory';
import { BANNER, NATIVE, VIDEO } from '../src/mediaTypes';
import { registerBidder, getIabSubCategory } from '../src/adapters/bidderFactory';
import { BANNER, NATIVE, VIDEO, ADPOD } from '../src/mediaTypes';
import find from 'core-js/library/fn/array/find';
import includes from 'core-js/library/fn/array/includes';

Expand Down Expand Up @@ -32,10 +32,7 @@ const NATIVE_MAPPING = {
displayUrl: 'displayurl'
};
const SOURCE = 'pbjs';
const mappingFileInfo = Object.freeze({
mappingFileUrl: 'http://sample.com/mappnig.json',
uniqueKey: utils.getUniqueIdentifierStr()
});
const mappingFileUrl = 'https://api.myjson.com/bins/11f7yo';

export const spec = {
code: BIDDER_CODE,
Expand Down Expand Up @@ -226,9 +223,8 @@ export const spec = {
*/
getMappingFileInfo: function() {
return {
url: mappingFileInfo.mappingFileUrl,
refreshInDays: 7,
localStorageKey: `${spec.code}_${mappingFileInfo.uniqueKey}`
url: mappingFileUrl,
refreshInDays: 7
}
},

Expand Down Expand Up @@ -339,6 +335,21 @@ function newBid(serverBid, rtbBid, bidderRequest) {
vastImpUrl: rtbBid.notify_url,
ttl: 3600
});

const videoContext = utils.deepAccess(bidRequest, 'mediaTypes.video.context');
if (videoContext === ADPOD) {
const iabSubCatId = getIabSubCategory(bidRequest.bidder, rtbBid.brand_category_id);

bid.meta = {
iabSubCatId
};

bid.video = {
context: ADPOD,
durationSeconds: Math.ceil(rtbBid.rtb.video.duration_ms / 1000),
};
}

// This supports Outstream Video
if (rtbBid.renderer_url) {
const rendererOptions = utils.deepAccess(
Expand Down
27 changes: 25 additions & 2 deletions src/adapters/bidderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ export function preloadBidderMappingFile(fn, adUnits) {
let bidderSpec = adapterManager.getBidAdapter(bidder);
if (bidderSpec.getSpec().getMappingFileInfo) {
let info = bidderSpec.getSpec().getMappingFileInfo();
let mappingData = getDataFromLocalStorage(info.localStorageKey);
let key = (info.localStorageKey) ? info.localStorageKey : bidderSpec.getSpec().code;
let mappingData = getDataFromLocalStorage(key);
if (!mappingData || timestamp() < mappingData.lastUpdated + info.refreshInDays * 24 * 60 * 60 * 1000) {
ajax(info.url,
{
Expand All @@ -369,7 +370,7 @@ export function preloadBidderMappingFile(fn, adUnits) {
lastUpdated: timestamp(),
mapping: response.mapping
}
setDataInLocalStorage(info.localStorageKey, JSON.stringify(mapping));
setDataInLocalStorage(key, JSON.stringify(mapping));
} catch (error) {
logError(`Failed to parse ${bidder} bidder translation mapping file`);
}
Expand All @@ -385,6 +386,28 @@ export function preloadBidderMappingFile(fn, adUnits) {
fn.call(this, adUnits);
}

/**
* Reads the data stored in localstorage and returns iab subcategory
* @param {string} bidderCode bidderCode
* @param {string} category bidders category
*/
export function getIabSubCategory(bidderCode, category) {
let bidderSpec = adapterManager.getBidAdapter(bidderCode);
if (bidderSpec.getSpec().getMappingFileInfo) {
let info = bidderSpec.getSpec().getMappingFileInfo();
let key = (info.localStorageKey) ? info.localStorageKey : bidderSpec.getBidderCode();
let data = getDataFromLocalStorage(key);
if (data) {
try {
data = JSON.parse(data);
} catch (error) {
logError(`Failed to parse ${bidderCode} mapping data stored in local storage`);
}
return (data.mapping[category]) ? data.mapping[category] : null;
}
}
}

// check that the bid has a width and height set
function validBidSize(adUnitCode, bid, bidRequests) {
if ((bid.width || bid.width === 0) && (bid.height || bid.height === 0)) {
Expand Down
17 changes: 0 additions & 17 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1203,23 +1203,6 @@ export function hasLocalStorage() {
return !!window.localStorage;
}

/**
* Reads the data stored in localstorage and returns iab subcategory
* @param {string} localStorageKey key to get data from local storage
* @param {string} category bidders category
*/
export function getIabSubCategory(localStorageKey, category) {
let data = getDataFromLocalStorage(localStorageKey);
if (data) {
try {
data = JSON.parse(data);
} catch (error) {
logError(`Failed to parse translation data stored in local storage`);
}
return (data.mapping[category]) ? data.mapping[category] : null;
}
}

export function isArrayOfNums(val, size) {
return (isArray(val)) && ((size) ? val.length === size : true) && (val.every(v => isInteger(v)));
}

0 comments on commit 2b81856

Please sign in to comment.