-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Category translation module for adpod #3513
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e1ddfc4
category translation module
53a94c4
Merge branch 'master' of github.com:prebid/Prebid.js into translation…
759018b
update function name, refactor code and add getIabSubCategory function
ebe6e7e
Merge branch 'master' of github.com:prebid/Prebid.js into translation…
cbd7df4
move hook init and import adpod
d2d8313
bugfix in getting adapter spec
37391e4
add hook for adserver in use
2b81856
updated getting iab subcategory code
e70ba4e
bugfix
230e6c1
Use individual mapping file for translation
632f4da
Update default mapping url
8bebe7c
add hook only once
37695d7
update mapping url
d9a818f
Update brandCategoryExclusion logic
f7f65c2
wrap in try catch
e334120
update todos and add default refreshInDays
e7e485e
Merge branch 'master' of github.com:prebid/Prebid.js into translation…
d31d8d9
udpate hooks
2e8fe49
update hook
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* 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'; | ||
import { setupBeforeHookFnOnce, hook } from '../src/hook'; | ||
import { ajax } from '../src/ajax'; | ||
import { timestamp, logError, setDataInLocalStorage, getDataFromLocalStorage } from '../src/utils'; | ||
import { addBidResponse } from '../src/auction'; | ||
|
||
const DEFAULT_TRANSLATION_FILE_URL = '//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'); | ||
registerAdserver(); | ||
|
||
export function getAdserverCategoryHook(fn, adUnitCode, bid) { | ||
if (!bid) { | ||
return fn.call(this, adUnitCode); // if no bid, call original and let it display warnings | ||
} | ||
|
||
if (!config.getConfig('adpod.brandCategoryExclusion')) { | ||
return fn.call(this, adUnitCode, bid); | ||
} | ||
|
||
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 = getDataFromLocalStorage(localStorageKey); | ||
if (mapping) { | ||
try { | ||
mapping = JSON.parse(mapping); | ||
} catch (error) { | ||
logError('Failed to parse translation mapping file'); | ||
} | ||
if (bid.meta.iabSubCatId && mapping['mapping'] && mapping['mapping'][bid.meta.iabSubCatId]) { | ||
bid.meta.adServerCatId = mapping['mapping'][bid.meta.iabSubCatId]['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); | ||
} | ||
|
||
export function initTranslation(url, localStorageKey) { | ||
setupBeforeHookFnOnce(addBidResponse, getAdserverCategoryHook, 50); | ||
let mappingData = getDataFromLocalStorage(localStorageKey); | ||
if (!mappingData || timestamp() < mappingData.lastUpdated + refreshInDays * 24 * 60 * 60 * 1000) { | ||
ajax(url, | ||
{ | ||
success: (response) => { | ||
try { | ||
response = JSON.parse(response); | ||
response['lastUpdated'] = timestamp(); | ||
setDataInLocalStorage(localStorageKey, JSON.stringify(response)); | ||
} catch (error) { | ||
logError('Failed to parse translation mapping file'); | ||
} | ||
}, | ||
error: () => { | ||
logError('Failed to load brand category translation 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)); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we increase default to 7?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have kept it 1 because if publisher changes the mapping file, localStorage will be updated in everyone's browser within 24 hours. So per day 1 request.