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

firing new adRenderFailed event when renderAd() fails #2210

Merged
merged 4 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/AnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const BID_RESPONSE = CONSTANTS.EVENTS.BID_RESPONSE;
const BID_WON = CONSTANTS.EVENTS.BID_WON;
const BID_ADJUSTMENT = CONSTANTS.EVENTS.BID_ADJUSTMENT;
const SET_TARGETING = CONSTANTS.EVENTS.SET_TARGETING;
const AD_RENDER_FAILED = CONSTANTS.EVENTS.AD_RENDER_FAILED;

const LIBRARY = 'library';
const ENDPOINT = 'endpoint';
Expand Down Expand Up @@ -105,6 +106,7 @@ export default function AnalyticsAdapter({ url, analyticsType, global, handler }
[BID_ADJUSTMENT]: args => this.enqueue({ eventType: BID_ADJUSTMENT, args }),
[SET_TARGETING]: args => this.enqueue({ eventType: SET_TARGETING, args }),
[AUCTION_END]: args => this.enqueue({ eventType: AUCTION_END, args }),
[AD_RENDER_FAILED]: args => this.enqueue({ eventType: AD_RENDER_FAILED, args }),
[AUCTION_INIT]: args => {
args.config = config.options; // enableAnaltyics configuration object
this.enqueue({ eventType: AUCTION_INIT, args });
Expand Down
10 changes: 9 additions & 1 deletion src/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@
"BID_WON": "bidWon",
"SET_TARGETING": "setTargeting",
"REQUEST_BIDS": "requestBids",
"ADD_AD_UNITS": "addAdUnits"
"ADD_AD_UNITS": "addAdUnits",
"AD_RENDER_FAILED" : "adRenderFailed"
},
"AD_RENDER_FAILED_REASON" : {
"PREVENT_WRITING_ON_MAIN_DOCUMENT": "preventWritingOnMainDocuemnt",
"NO_AD": "noAd",
"EXCEPTION": "exception",
"CANNOT_FIND_AD": "cannotFindAd",
"MISSING_DOC_OR_ADID": "missingDocOrAdid"
},
"EVENT_ID_PATHS": {
"bidWon": "adUnitCode"
Expand Down
31 changes: 25 additions & 6 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const { triggerUserSyncs } = userSync;
/* private variables */

const RENDERED = 'rendered';
const { ADD_AD_UNITS, BID_WON, REQUEST_BIDS, SET_TARGETING } = CONSTANTS.EVENTS;
const { ADD_AD_UNITS, BID_WON, REQUEST_BIDS, SET_TARGETING, AD_RENDER_FAILED } = CONSTANTS.EVENTS;
const { PREVENT_WRITING_ON_MAIN_DOCUMENT, NO_AD, EXCEPTION, CANNOT_FIND_AD, MISSING_DOC_OR_ADID } = CONSTANTS.AD_RENDER_FAILED_REASON;

var eventValidators = {
bidWon: checkDefinedPlacement
Expand Down Expand Up @@ -196,6 +197,18 @@ $$PREBID_GLOBAL$$.setTargetingForAst = function() {
events.emit(SET_TARGETING);
};

function emitAdRenderFail(reason, message, bid) {
const data = {};

data.reason = reason;
data.message = message;
if (data.bid) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wanted to check if this logic is properly configured. It seems like this if condition would never be met because the data is initialized as empty each time the function is called, so the data.bid property would always be undefined. Can you please take a look and clarify?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed. Changing that

data.bid = bid;
}

utils.logError(message);
events.emit(AD_RENDER_FAILED, data);
}
/**
* This function will render the ad (based on params) in the given iframe document passed through.
* Note that doc SHOULD NOT be the parent document page as we can't doc.write() asynchronously
Expand All @@ -206,6 +219,7 @@ $$PREBID_GLOBAL$$.setTargetingForAst = function() {
$$PREBID_GLOBAL$$.renderAd = function (doc, id) {
utils.logInfo('Invoking $$PREBID_GLOBAL$$.renderAd', arguments);
utils.logMessage('Calling renderAd with adId :' + id);

if (doc && id) {
try {
// lookup ad by ad Id
Expand All @@ -229,7 +243,8 @@ $$PREBID_GLOBAL$$.renderAd = function (doc, id) {
if (renderer && renderer.url) {
renderer.render(bid);
} else if ((doc === document && !utils.inIframe()) || mediaType === 'video') {
utils.logError(`Error trying to write ad. Ad render call ad id ${id} was prevented from writing to the main document.`);
const message = `Error trying to write ad. Ad render call ad id ${id} was prevented from writing to the main document.`;
emitAdRenderFail(PREVENT_WRITING_ON_MAIN_DOCUMENT, message, bid);
} else if (ad) {
doc.write(ad);
doc.close();
Expand All @@ -245,16 +260,20 @@ $$PREBID_GLOBAL$$.renderAd = function (doc, id) {
utils.insertElement(iframe, doc, 'body');
setRenderSize(doc, width, height);
} else {
utils.logError('Error trying to write ad. No ad for bid response id: ' + id);
const message = `Error trying to write ad. No ad for bid response id: ${id}`;
emitAdRenderFail(NO_AD, message, bid);
}
} else {
utils.logError('Error trying to write ad. Cannot find ad by given id : ' + id);
const message = `Error trying to write ad. Cannot find ad by given id : ${id}`;
emitAdRenderFail(CANNOT_FIND_AD, message);
}
} catch (e) {
utils.logError('Error trying to write ad Id :' + id + ' to the page:' + e.message);
const message = `Error trying to write ad Id :${id} to the page:${e.message}`;
emitAdRenderFail(EXCEPTION, message);
}
} else {
utils.logError('Error trying to write ad Id :' + id + ' to the page. Missing document or adId');
const message = `Error trying to write ad Id :${id} to the page. Missing document or adId`;
emitAdRenderFail(MISSING_DOC_OR_ADID, message);
}
};

Expand Down
13 changes: 13 additions & 0 deletions test/spec/AnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const BID_REQUESTED = CONSTANTS.EVENTS.BID_REQUESTED;
const BID_RESPONSE = CONSTANTS.EVENTS.BID_RESPONSE;
const BID_WON = CONSTANTS.EVENTS.BID_WON;
const BID_TIMEOUT = CONSTANTS.EVENTS.BID_TIMEOUT;
const AD_RENDER_FAILED = CONSTANTS.EVENTS.AD_RENDER_FAILED;

const AnalyticsAdapter = require('src/AnalyticsAdapter').default;
const config = {
url: 'http://localhost:9999/src/adapters/analytics/libraries/example.js',
Expand Down Expand Up @@ -90,6 +92,17 @@ FEATURE: Analytics Adapters API
assert.deepEqual(spyTestGlobal.args[0][2], args, `with expected event data\n`);
});

it('SHOULD call global when a adRenderFailed event occurs', () => {
const eventType = AD_RENDER_FAILED;
const args = { call: 'adRenderFailed' };

adapter.enableAnalytics();
events.emit(eventType, args);

assert.ok(spyTestGlobal.args[0][1] === eventType, `with expected event type\n`);
assert.deepEqual(spyTestGlobal.args[0][2], args, `with expected event data\n`);
});

it('SHOULD call global when a bidRequest event occurs', () => {
const eventType = BID_REQUESTED;
const args = { call: 'request' };
Expand Down