|
| 1 | +/** |
| 2 | + * This module translates iab category to freewheel industry using translation mapping file |
| 3 | + * Publisher can set translation file by using setConfig method |
| 4 | + * |
| 5 | + * Example: |
| 6 | + * config.setConfig({ |
| 7 | + * 'brandCategoryTranslation': { |
| 8 | + * 'translationFile': 'http://sample.com' |
| 9 | + * } |
| 10 | + * }); |
| 11 | + * If publisher has not defined translation file than prebid will use default prebid translation file provided here //cdn.jsdelivr.net/gh/prebid/category-mapping-file@1/freewheel-mapping.json |
| 12 | + */ |
| 13 | + |
| 14 | +import { config } from '../src/config'; |
| 15 | +import { setupBeforeHookFnOnce, hook } from '../src/hook'; |
| 16 | +import { ajax } from '../src/ajax'; |
| 17 | +import { timestamp, logError, setDataInLocalStorage, getDataFromLocalStorage } from '../src/utils'; |
| 18 | +import { addBidResponse } from '../src/auction'; |
| 19 | + |
| 20 | +const DEFAULT_TRANSLATION_FILE_URL = '//cdn.jsdelivr.net/gh/prebid/category-mapping-file@1/freewheel-mapping.json'; |
| 21 | +const DEFAULT_IAB_TO_FW_MAPPING_KEY = 'iabToFwMappingkey'; |
| 22 | +const DEFAULT_IAB_TO_FW_MAPPING_KEY_PUB = 'iabToFwMappingkeyPub'; |
| 23 | +const refreshInDays = 1; |
| 24 | + |
| 25 | +export const registerAdserver = hook('async', function(adServer) { |
| 26 | + let url; |
| 27 | + if (adServer === 'freewheel') { |
| 28 | + url = DEFAULT_TRANSLATION_FILE_URL; |
| 29 | + } |
| 30 | + initTranslation(url, DEFAULT_IAB_TO_FW_MAPPING_KEY); |
| 31 | +}, 'registerAdserver'); |
| 32 | +registerAdserver(); |
| 33 | + |
| 34 | +export function getAdserverCategoryHook(fn, adUnitCode, bid) { |
| 35 | + if (!bid) { |
| 36 | + return fn.call(this, adUnitCode); // if no bid, call original and let it display warnings |
| 37 | + } |
| 38 | + |
| 39 | + if (!config.getConfig('adpod.brandCategoryExclusion')) { |
| 40 | + return fn.call(this, adUnitCode, bid); |
| 41 | + } |
| 42 | + |
| 43 | + let localStorageKey = (config.getConfig('brandCategoryTranslation.translationFile')) ? DEFAULT_IAB_TO_FW_MAPPING_KEY_PUB : DEFAULT_IAB_TO_FW_MAPPING_KEY; |
| 44 | + |
| 45 | + if (bid.meta && !bid.meta.adServerCatId) { |
| 46 | + let mapping = getDataFromLocalStorage(localStorageKey); |
| 47 | + if (mapping) { |
| 48 | + try { |
| 49 | + mapping = JSON.parse(mapping); |
| 50 | + } catch (error) { |
| 51 | + logError('Failed to parse translation mapping file'); |
| 52 | + } |
| 53 | + if (bid.meta.iabSubCatId && mapping['mapping'] && mapping['mapping'][bid.meta.iabSubCatId]) { |
| 54 | + bid.meta.adServerCatId = mapping['mapping'][bid.meta.iabSubCatId]['id']; |
| 55 | + } else { |
| 56 | + // This bid will be automatically ignored by adpod module as adServerCatId was not found |
| 57 | + bid.meta.adServerCatId = undefined; |
| 58 | + } |
| 59 | + } else { |
| 60 | + logError('Translation mapping data not found in local storage'); |
| 61 | + } |
| 62 | + } |
| 63 | + fn.call(this, adUnitCode, bid); |
| 64 | +} |
| 65 | + |
| 66 | +export function initTranslation(url, localStorageKey) { |
| 67 | + setupBeforeHookFnOnce(addBidResponse, getAdserverCategoryHook, 50); |
| 68 | + let mappingData = getDataFromLocalStorage(localStorageKey); |
| 69 | + if (!mappingData || timestamp() < mappingData.lastUpdated + refreshInDays * 24 * 60 * 60 * 1000) { |
| 70 | + ajax(url, |
| 71 | + { |
| 72 | + success: (response) => { |
| 73 | + try { |
| 74 | + response = JSON.parse(response); |
| 75 | + response['lastUpdated'] = timestamp(); |
| 76 | + setDataInLocalStorage(localStorageKey, JSON.stringify(response)); |
| 77 | + } catch (error) { |
| 78 | + logError('Failed to parse translation mapping file'); |
| 79 | + } |
| 80 | + }, |
| 81 | + error: () => { |
| 82 | + logError('Failed to load brand category translation file.') |
| 83 | + } |
| 84 | + }, |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function setConfig(config) { |
| 90 | + if (config.translationFile) { |
| 91 | + // if publisher has defined the translation file, preload that file here |
| 92 | + initTranslation(config.translationFile, DEFAULT_IAB_TO_FW_MAPPING_KEY_PUB); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +config.getConfig('brandCategoryTranslation', config => setConfig(config.brandCategoryTranslation)); |
0 commit comments