forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Prebid/slot on load #15
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ac8b0aa
added slotOnLoad event listener to publish data before auction end ti…
suribabuox 9b6d608
added slotOnLoad event listener to publish data before auction end ti…
suribabuox 200af63
Merge branch 'prebid/slot-on-load' of https://github.com/openx/Prebid…
suribabuox 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ const SCHEMA_VERSION = '0.1'; | |
const MAX_RETRIES = 2; | ||
const MAX_TIMEOUT = 10000; | ||
const AUCTION_END_WAIT_TIME = 2000; | ||
const DEFAULT_SLOT_LOAD_BUFFER_TIME = 100; | ||
|
||
const auctionInitConst = CONSTANTS.EVENTS.AUCTION_INIT; | ||
const auctionEndConst = CONSTANTS.EVENTS.AUCTION_END; | ||
|
@@ -25,15 +26,21 @@ const bidRequestConst = CONSTANTS.EVENTS.BID_REQUESTED; | |
const bidAdjustmentConst = CONSTANTS.EVENTS.BID_ADJUSTMENT; | ||
const bidResponseConst = CONSTANTS.EVENTS.BID_RESPONSE; | ||
const bidTimeoutConst = CONSTANTS.EVENTS.BID_TIMEOUT; | ||
const SLOT_LOADED = "slotOnload" | ||
|
||
let googletag = window.googletag || {}; | ||
googletag.cmd = googletag.cmd || []; | ||
|
||
let initOptions = { | ||
publisherPlatformId: '', | ||
publisherAccountId: -1, | ||
testCode: 'default', | ||
utmTagData: [], | ||
adUnits: [] | ||
adUnits: [], | ||
slotLoadWaitTime: 0 | ||
}; | ||
let eventStack = {}; | ||
let loadedAdSlots = {}; | ||
|
||
let localStoragePrefix = 'openx_analytics_'; | ||
let utmTags = [ | ||
|
@@ -237,6 +244,106 @@ function removeads(info) { | |
} | ||
} | ||
|
||
function getAuctionIdByAdId(adId) { | ||
|
||
let auctionId; | ||
utils._map(eventStack, value => value).forEach( function(auctionInfo) { | ||
if(auctionInfo && auctionInfo.events){ | ||
let bidWonEvent; | ||
bidWonEvent = auctionInfo.events.filter(function(eventsInfo) { | ||
return eventsInfo.eventType === "bidWon"; | ||
}); | ||
|
||
if(bidWonEvent.length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: don't have to do a check here, just chain them for happy fun times. forEach on an emtpy list is just a no-op |
||
bidWonEvent.forEach(function(bidWon) { | ||
if(bidWon.args && bidWon.args.adId && bidWon.args.adId === adId) { | ||
auctionId = bidWon.args.auctionId; | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
return auctionId; | ||
} | ||
|
||
function getAllAdUnitCodesByAuctionId(auctionId) { | ||
|
||
let adUnitCodes; | ||
if(eventStack[auctionId] && eventStack[auctionId].events) { | ||
|
||
eventStack[auctionId].events.forEach(function(eventsInfo) { | ||
if(eventsInfo.eventType === "auctionEnd") { | ||
adUnitCodes = eventsInfo.args.adUnitCodes; | ||
} | ||
}) | ||
} | ||
return adUnitCodes; | ||
} | ||
|
||
function getAuctionIdByAdUnitCode(adUnitCode) { | ||
let auctionId; | ||
utils._map(eventStack, value => value).forEach( function(auctionInfo) { | ||
if(auctionId === undefined) { | ||
if(auctionInfo && auctionInfo.events) { | ||
auctionInfo.events.forEach(function(eventsInfo){ | ||
if(eventsInfo.eventType === auctionEndConst) { | ||
if(eventsInfo.args && eventsInfo.args.adUnitCodes) { | ||
if(eventsInfo.args.adUnitCodes.includes(adUnitCode)){ | ||
auctionId = eventsInfo.args.auctionId; | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
}); | ||
return auctionId; | ||
} | ||
|
||
function onSlotLoaded({ slot }) { | ||
|
||
const adId = slot.getTargeting('hb_adid')[0]; | ||
const slotElementId = slot.getSlotElementId(); | ||
const adUnitPath = slot.getAdUnitPath(); | ||
|
||
let auctionId = getAuctionIdByAdId(adId); | ||
if(!auctionId) { | ||
auctionId = getAuctionIdByAdUnitCode(slotElementId); | ||
if(!auctionId) { | ||
auctionId = getAuctionIdByAdUnitCode(adUnitPath); | ||
} | ||
} | ||
|
||
let allSlotsLoaded = false; | ||
if(auctionId) { | ||
if(!loadedAdSlots[auctionId]) { | ||
loadedAdSlots[auctionId] = [] | ||
} | ||
loadedAdSlots[auctionId].push(slotElementId); | ||
let allAdUnitCodes = getAllAdUnitCodesByAuctionId(auctionId); | ||
if(loadedAdSlots[auctionId].length === allAdUnitCodes.length) { | ||
allSlotsLoaded = true; | ||
} | ||
} | ||
|
||
if(auctionId && eventStack[auctionId] && allSlotsLoaded) { | ||
setTimeout(function(){ | ||
if(eventStack[auctionId]) { | ||
send(SLOT_LOADED, eventStack, auctionId); | ||
eventStack[auctionId] = null; | ||
} | ||
delete loadedAdSlots[auctionId]; | ||
}, initOptions.slotLoadWaitTime); | ||
} | ||
} | ||
|
||
googletag.cmd.push(function() { | ||
googletag.pubads().addEventListener(SLOT_LOADED, function(args) { | ||
utils.logInfo("OX: SlotOnLoad event triggered"); | ||
onSlotLoaded(args); | ||
}); | ||
}); | ||
|
||
let openxAdapter = Object.assign(adapter({ urlParam, analyticsType }), { | ||
track({ eventType, args }) { | ||
|
||
|
@@ -263,31 +370,34 @@ let openxAdapter = Object.assign(adapter({ urlParam, analyticsType }), { | |
pushEvent(eventType, info, auctionId); | ||
// utils.logInfo('OX: Bid won called for', auctionId); | ||
} else if (eventType === auctionEndConst) { | ||
pushEvent(eventType, removeads(info), auctionId); | ||
// utils.logInfo('OX: Auction end called for', auctionId); | ||
updateSessionId(); | ||
buildEventStack(auctionId); | ||
if (isValidEventStack(auctionId)) { | ||
setTimeout(function() { | ||
// utils.logInfo('OX: Sending data', eventStack); | ||
send( | ||
eventType, | ||
eventStack, | ||
auctionId | ||
); | ||
eventStack[auctionId] = null; | ||
// utils.logInfo('OX: Deleted Auction Info for auctionId', auctionId); | ||
}, AUCTION_END_WAIT_TIME); | ||
} else { | ||
setTimeout(function() { | ||
eventStack[auctionId] = null; | ||
// utils.logInfo('OX: Deleted Auction Info for auctionId', auctionId); | ||
}, AUCTION_END_WAIT_TIME); | ||
} | ||
pushEvent(eventType, removeads(info), auctionId); | ||
// utils.logInfo('OX: Auction end called for', auctionId); | ||
updateSessionId(); | ||
buildEventStack(auctionId); | ||
if (isValidEventStack(auctionId)) { | ||
setTimeout(function() { | ||
// utils.logInfo('OX: Sending data', eventStack); | ||
if(eventStack[auctionId]) { | ||
send( | ||
eventType, | ||
eventStack, | ||
auctionId | ||
); | ||
eventStack[auctionId] = null; | ||
} | ||
delete loadedAdSlots[auctionId]; | ||
// utils.logInfo('OX: Deleted Auction Info for auctionId', auctionId); | ||
}, AUCTION_END_WAIT_TIME); | ||
} else { | ||
setTimeout(function() { | ||
eventStack[auctionId] = null; | ||
// utils.logInfo('OX: Deleted Auction Info for auctionId', auctionId); | ||
}, AUCTION_END_WAIT_TIME); | ||
} | ||
} else if (eventType === bidTimeoutConst) { | ||
// utils.logInfo('SA: Bid Timedout for', auctionId); | ||
pushEvent(eventType, info, auctionId); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
|
@@ -297,6 +407,10 @@ openxAdapter.enableAnalytics = function(config) { | |
initOptions = config.options; | ||
initOptions.testCode = getTestCode(); | ||
initOptions.utmTagData = this.buildUtmTagData(); | ||
|
||
if(!initOptions.slotLoadWaitTime) { | ||
initOptions.slotLoadWaitTime = DEFAULT_SLOT_LOAD_BUFFER_TIME | ||
} | ||
utils.logInfo('OpenX Analytics enabled with config', initOptions); | ||
|
||
// set default sampling rate to 5% | ||
|
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.
Just a nitpick: you dont have to do a _map, when there is a _each function in the utils that does the same thing as a map/forEach.