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