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

Unit Tests for Container, DFP Win Scenarios #16

Merged
merged 2 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
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
122 changes: 64 additions & 58 deletions modules/openxAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ 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"
const SLOT_LOADED = 'slotOnload'

let googletag = window.googletag || {};
googletag.cmd = googletag.cmd || [];
Expand Down Expand Up @@ -245,34 +245,31 @@ function removeads(info) {
}

function getAuctionIdByAdId(adId) {

let auctionId;
utils._map(eventStack, value => value).forEach( function(auctionInfo) {
if(auctionInfo && auctionInfo.events){
utils._map(eventStack, value => value).forEach(function(auctionInfo) {
if (auctionInfo && auctionInfo.events) {
let bidWonEvent;
bidWonEvent = auctionInfo.events.filter(function(eventsInfo) {
return eventsInfo.eventType === "bidWon";
return eventsInfo.eventType === 'bidWon';
});

if(bidWonEvent.length > 0) {
if (bidWonEvent.length > 0) {
bidWonEvent.forEach(function(bidWon) {
if(bidWon.args && bidWon.args.adId && bidWon.args.adId === adId) {
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) {

if (eventStack[auctionId] && eventStack[auctionId].events) {
eventStack[auctionId].events.forEach(function(eventsInfo) {
if(eventsInfo.eventType === "auctionEnd") {
if (eventsInfo.eventType === 'auctionEnd') {
adUnitCodes = eventsInfo.args.adUnitCodes;
}
})
Expand All @@ -282,13 +279,13 @@ function getAllAdUnitCodesByAuctionId(auctionId) {

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)){
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;
}
}
Expand All @@ -301,34 +298,33 @@ function getAuctionIdByAdUnitCode(adUnitCode) {
}

function onSlotLoaded({ slot }) {

const adId = slot.getTargeting('hb_adid')[0];
const slotElementId = slot.getSlotElementId();
const adUnitPath = slot.getAdUnitPath();

let auctionId = getAuctionIdByAdId(adId);
if(!auctionId) {
if (!auctionId) {
auctionId = getAuctionIdByAdUnitCode(slotElementId);
if(!auctionId) {
if (!auctionId) {
auctionId = getAuctionIdByAdUnitCode(adUnitPath);
}
}

let allSlotsLoaded = false;
if(auctionId) {
if(!loadedAdSlots[auctionId]) {
if (auctionId) {
if (!loadedAdSlots[auctionId]) {
loadedAdSlots[auctionId] = []
}
loadedAdSlots[auctionId].push(slotElementId);
let allAdUnitCodes = getAllAdUnitCodesByAuctionId(auctionId);
if(loadedAdSlots[auctionId].length === allAdUnitCodes.length) {
if (loadedAdSlots[auctionId].length === allAdUnitCodes.length) {
allSlotsLoaded = true;
}
}

if(auctionId && eventStack[auctionId] && allSlotsLoaded) {
setTimeout(function(){
if(eventStack[auctionId]) {
if (auctionId && eventStack[auctionId] && allSlotsLoaded) {
setTimeout(function() {
if (eventStack[auctionId]) {
send(SLOT_LOADED, eventStack, auctionId);
eventStack[auctionId] = null;
}
Expand All @@ -339,14 +335,13 @@ function onSlotLoaded({ slot }) {

googletag.cmd.push(function() {
googletag.pubads().addEventListener(SLOT_LOADED, function(args) {
utils.logInfo("OX: SlotOnLoad event triggered");
utils.logInfo('OX: SlotOnLoad event triggered');
onSlotLoaded(args);
});
});

let openxAdapter = Object.assign(adapter({ urlParam, analyticsType }), {
track({ eventType, args }) {

if (!checkInitOptions()) {
send(eventType, {}, null);
return;
Expand All @@ -370,34 +365,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);
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() {
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;
// utils.logInfo('OX: Deleted Auction Info for auctionId', auctionId);
}, AUCTION_END_WAIT_TIME);
}
}
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 @@ -407,8 +402,8 @@ openxAdapter.enableAnalytics = function(config) {
initOptions = config.options;
initOptions.testCode = getTestCode();
initOptions.utmTagData = this.buildUtmTagData();
if(!initOptions.slotLoadWaitTime) {

if (!initOptions.slotLoadWaitTime) {
initOptions.slotLoadWaitTime = DEFAULT_SLOT_LOAD_BUFFER_TIME
}
utils.logInfo('OpenX Analytics enabled with config', initOptions);
Expand Down Expand Up @@ -622,13 +617,24 @@ function pushEvent(eventType, args, auctionId) {
}
} else {
if (isValidEvent(eventType, args.adUnitCode)) {
eventStack[auctionId].events.push({ eventType: eventType, args: args });
eventStack[auctionId].events.push({ eventType: eventType, args: args });
}
}
}

openxAdapter.reset = function() {
eventStack = {};
loadedAdSlots = {};
};

openxAdapter.slotOnLoad = onSlotLoaded;

adapterManager.registerAnalyticsAdapter({
adapter: openxAdapter,
code: 'openx'
});

export default openxAdapter;
export default Object.assign({
adapter: openxAdapter,
auctionEndWaitTime: AUCTION_END_WAIT_TIME
});
Loading