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

[PBID-71] Added testCode, bidTimeout events, send individual auctions payloads #6

Merged
merged 1 commit into from
Feb 26, 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
143 changes: 94 additions & 49 deletions modules/openxAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ const bidTimeoutConst = CONSTANTS.EVENTS.BID_TIMEOUT;
let initOptions = {
publisherPlatformId: '',
publisherAccountId: -1,
testCode: 'default',
utmTagData: [],
adUnits: []
};
let bidWon = { options: {}, events: [] };
let eventStack = { options: {}, events: [] };

let auctionStatus = 'not_started';
let eventStack = {};

let localStoragePrefix = 'openx_analytics_';
let utmTags = [
Expand Down Expand Up @@ -107,47 +105,64 @@ function buildUtmLocalStorageTimeoutKey() {
function buildUtmLocalStorageKey(utmMarkKey) {
return localStoragePrefix.concat(utmMarkKey);
}

function checkPublisherPlatformId() {
if (initOptions.publisherPlatformId !== undefined) {
if (typeof initOptions.publisherPlatformId === 'string') {
if (initOptions.publisherPlatformId !== '') {
return initOptions.publisherPlatformId;
} else {
utils.logError('SOX: Invalid PublisherPlatformId');
utils.logError('OX: Invalid PublisherPlatformId');
return null;
}
} else {
utils.logError('SOX: Invalid datatype for PublisherPlatformId');
utils.logError('OX: Invalid datatype for PublisherPlatformId');
return null;
}
} else {
utils.logError('SOX : PublisherPlatformId not defined');
utils.logError('OX: PublisherPlatformId not defined');
return null;
}
}

function checkPublisherAccountId() {
if (initOptions.publisherAccountId !== undefined) {
if (typeof initOptions.publisherAccountId === 'number') {
if (initOptions.publisherAccountId > -1) {
return initOptions.publisherAccountId;
} else {
utils.logError('SOX: Invalid PublisherAccountId');
utils.logError('OX: Invalid PublisherAccountId');
return null;
}
} else {
utils.logError('SOX: Invalid datatype for PublisherAccountId');
utils.logError('OX: Invalid datatype for PublisherAccountId');
return null;
}
} else {
utils.logError('SOX : PublisherAccountId not defined');
utils.logError('OX: PublisherAccountId not defined');
return null;
}
}

function checkTestCode() {
if (initOptions.testCode !== undefined) {
if (typeof initOptions.testCode === 'string') {
return initOptions.testCode;
} else {
utils.logError('OX: Invalid datatype for testCode');
return null;
}
} else {
utils.logInfo('OX: testCode not defined');
return 'default';
}
}

function checkInitOptions() {
let publisherPlatformId = checkPublisherPlatformId();
let publisherAccountId = checkPublisherAccountId();
if (publisherPlatformId && publisherAccountId) {
let testCode = checkTestCode();
if (publisherPlatformId && publisherAccountId && testCode) {
return true;
}
return false;
Expand All @@ -160,8 +175,9 @@ function checkAdUnitConfig() {
return initOptions.adUnits.length > 0;
}

function buildEventStack() {
eventStack.options = initOptions;
function buildEventStack(auctionId) {
eventStack[auctionId].options = initOptions;
utils.logInfo('OX: Options Initialized', eventStack);
}

function filterBidsByAdUnit(bids) {
Expand All @@ -176,7 +192,7 @@ function filterBidsByAdUnit(bids) {

function isValidEvent(eventType, adUnitCode) {
if (checkAdUnitConfig()) {
let validationEvents = [bidAdjustmentConst, bidResponseConst, bidWonConst, bidTimeoutConst];
let validationEvents = [bidAdjustmentConst, bidResponseConst, bidWonConst];
if (
!includes(initOptions.adUnits, adUnitCode) &&
includes(validationEvents, eventType)
Expand All @@ -187,11 +203,13 @@ function isValidEvent(eventType, adUnitCode) {
return true;
}

function isValidEventStack() {
if (eventStack.events.length > 0) {
return eventStack.events.some(function(event) {
function isValidEventStack(auctionId) {
utils.logInfo('OX: Validating eventStack for', auctionId)
if (eventStack[auctionId].events.length > 0) {
return eventStack[auctionId].events.some(function(event) {
// utils.logInfo('OX: EventType of event ', event.eventType)
return (
bidRequestConst === event.eventType || bidWonConst === event.eventType
bidRequestConst === event.eventType || bidResponseConst === event.eventType || bidAdjustmentConst === event.eventType || auctionEndConst === event.eventType || bidTimeoutConst === event.eventType
);
});
}
Expand All @@ -216,8 +234,9 @@ function removeads(info) {

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

if (!checkInitOptions()) {
send(eventType, {}, null, null);
send(eventType, {}, null);
return;
}

Expand All @@ -227,31 +246,44 @@ let openxAdapter = Object.assign(adapter({ urlParam, analyticsType }), {
info.ad = '';
}

if (eventType === auctionInitConst) {
auctionStatus = 'started';
}
let auctionId = info.auctionId
// utils.logInfo('OX: Got auctionId', auctionId);

if (eventType === bidWonConst && auctionStatus === 'not_started') {
pushEvent(eventType, info);
utils.logInfo('SOX:Bid won called... ');
if (eventType === auctionInitConst) {
eventStack[auctionId] = { options: {}, events: [] };
// utils.logInfo('OX: Event Stack updated after AuctionInit', eventStack);
}
else if (eventType === bidWonConst) { // && auctionStatus[auctionId] !== 'started'
pushEvent(eventType, info, auctionId);
// utils.logInfo('OX: Bid won called for', auctionId);
return;
}

if (eventType === auctionEndConst) {
pushEvent(eventType, removeads(info));
utils.logInfo('SOX:Auction end called... ');
else if (eventType === auctionEndConst) {
pushEvent(eventType, removeads(info), auctionId);
// utils.logInfo('OX: Auction end called for', auctionId);
updateSessionId();
buildEventStack();
if (isValidEventStack()) {
auctionStatus = 'not_started';
buildEventStack(auctionId);
if (isValidEventStack(auctionId)) {
setTimeout(function() {
let publisherPlatformId = eventStack.options.publisherPlatformId;
let publisherAccountId = eventStack.options.publisherAccountId;
send(eventType, eventStack, publisherPlatformId, publisherAccountId);
// utils.logInfo('OX: Sending data', eventStack);
send(
eventType,
eventStack,
auctionId
);
delete eventStack[auctionId];
// utils.logInfo('OX: Deleted Auction Info for auctionId', auctionId);
}, AUCTION_END_WAIT_TIME);
} else {
setTimeout(function() {
delete eventStack[auctionId];
// utils.logInfo('OX: Deleted Auction Info for auctionId', auctionId);
}, AUCTION_END_WAIT_TIME);
}
} else if (eventType === bidRequestConst || eventType === bidTimeoutConst) {
pushEvent(eventType, info);
}
else if (eventType === bidTimeoutConst) {
// utils.logInfo('SA: Bid Timedout for', auctionId);
pushEvent(eventType, info, auctionId);
}
}
});
Expand All @@ -260,6 +292,7 @@ openxAdapter.originEnableAnalytics = openxAdapter.enableAnalytics;

openxAdapter.enableAnalytics = function(config) {
initOptions = config.options;
initOptions.testCode = checkTestCode();
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: checkTestCode() should be broken out to something like getValidTestCode or default. The action to check, doesn't tell us what actions are taking place.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! I'll update it.

initOptions.utmTagData = this.buildUtmTagData();
utils.logInfo('OpenX Analytics enabled with config', initOptions);
openxAdapter.originEnableAnalytics(config);
Expand Down Expand Up @@ -301,28 +334,31 @@ function buildPayload(
eventType,
publisherPlatformId,
publisherAccountId,
testCode,
sourceUrl
) {
return {
data: data,
eventType: eventType,
publisherPlatformId: publisherPlatformId,
publisherAccountId: publisherAccountId,
testCode: testCode,
sourceUrl: sourceUrl
};
}
function apiCall(url, MAX_RETRIES, payload) {

function apiCall(url, MAX_RETRIES, payload, auctionId) {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState !== 4) return;
if (xhr.status >= 200 && xhr.status < 300) {
utils.logInfo('SOX: Data sent for event :', payload.eventType);
utils.logInfo('OX: Data sent for event:', payload.eventType);
} else {
if (MAX_RETRIES == 0) {
utils.logError('SOX:Retries Exhausted, Data could not be Sent!!');
utils.logError('OX: Retries Exhausted, Data could not be Sent!!');
return;
}
utils.logInfo('SOX:Retrying.....', MAX_RETRIES);
utils.logInfo('OX: Retrying ...', MAX_RETRIES);
url = getRandomUrl(url);
apiCall(url, MAX_RETRIES - 1, payload);
}
Expand Down Expand Up @@ -404,11 +440,16 @@ function detectBrowser() {
return 'Others';
}

function send(eventType, data, publisherPlatformId, publisherAccountId) {
function send(eventType, eventStack, auctionId) {
var ua = window.navigator.userAgent;
var sourceUrl = window.location.href;
var sourceBrowser = detectBrowser();
var sourceOs = detectOS();
// utils.logInfo('OX: AuctionId', auctionId);
var data = eventStack[auctionId];
var publisherPlatformId = eventStack[auctionId].options.publisherPlatformId;
var publisherAccountId = eventStack[auctionId].options.publisherPlatformId;
var testCode = eventStack[auctionId].options.testCode;
data['user_agent'] = ua;
data['source_url'] = sourceUrl;
data['source_browser'] = sourceBrowser;
Expand All @@ -420,7 +461,6 @@ function send(eventType, data, publisherPlatformId, publisherAccountId) {
}
if (typeof data === 'object') {
const stringData = JSON.stringify(data);
console.log(stringData);
if (typeof stringData === 'string') {
const compressedData = zlib.gzipSync(stringData);
let urlGenerated = getRandomUrl(null);
Expand All @@ -429,29 +469,34 @@ function send(eventType, data, publisherPlatformId, publisherAccountId) {
eventType,
publisherPlatformId,
publisherAccountId,
testCode,
sourceUrl
);
apiCall(urlGenerated, MAX_RETRIES, payload);
apiCall(urlGenerated, MAX_RETRIES, payload, auctionId);
} else {
utils.logError('SOX:Invalid data format');
utils.logError('OX: Invalid data format');
delete eventStack[auctionId];
// utils.logInfo('OX: Deleted Auction Info for auctionId', auctionId);
return;
}
} else {
utils.logError('SOX:Invalid data format');
utils.logError('OX: Invalid data format');
delete eventStack[auctionId];
// utils.logInfo('OX: Deleted Auction Info for auctionId', auctionId);
return;
}
}
function pushEvent(eventType, args) {
function pushEvent(eventType, args, auctionId) {
if (eventType === bidRequestConst) {
if (checkAdUnitConfig()) {
args.bids = filterBidsByAdUnit(args.bids);
}
if (args.bids.length > 0) {
eventStack.events.push({ eventType: eventType });
eventStack[auctionId].events.push({ eventType: eventType });
}
} else {
if (isValidEvent(eventType, args.adUnitCode)) {
eventStack.events.push({ eventType: eventType, args: args });
eventStack[auctionId].events.push({ eventType: eventType, args: args });
}
}
}
Expand Down