Skip to content
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 3 commits into from
Apr 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 137 additions & 23 deletions modules/openxAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [
Expand Down Expand Up @@ -237,6 +244,106 @@ function removeads(info) {
}
}

function getAuctionIdByAdId(adId) {

let auctionId;
utils._map(eventStack, value => value).forEach( function(auctionInfo) {
Copy link

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.

if(auctionInfo && auctionInfo.events){
let bidWonEvent;
bidWonEvent = auctionInfo.events.filter(function(eventsInfo) {
return eventsInfo.eventType === "bidWon";
});

if(bidWonEvent.length > 0) {
Copy link

Choose a reason for hiding this comment

The 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 }) {

Expand All @@ -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);
}
}
}
});

Expand All @@ -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%
Expand Down