Skip to content

Commit

Permalink
Rvr 2369 Refactor events handling (#9)
Browse files Browse the repository at this point in the history
* RVR-2177 - Refactor events handling

* RVR-2087 - Inject pbjsGlobalVariable into rivraddon

* RVR-2087 - update adapterManager dependency

* RVR-2087 - Add ADD_AD_UNITS to Prebid.JS trackable events

* RVR-2369 - Update package-lock.json
  • Loading branch information
AlessandroDG authored Mar 26, 2019
1 parent b9fb264 commit 5703176
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 72 deletions.
32 changes: 8 additions & 24 deletions modules/rivrAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import {ajax} from '../src/ajax';
import adapter from '../src/AnalyticsAdapter';
import CONSTANTS from '../src/constants.json';
import {ajax} from 'src/ajax';
import adapter from 'src/AnalyticsAdapter';
import adapterManager from '../src/adapterManager';
import * as utils from '../src/utils';
import * as utils from 'src/utils';

const analyticsType = 'endpoint';

let rivrAnalytics = Object.assign(adapter({analyticsType}), {
track({ eventType, args }) {
if (!window.rivraddon || !window.rivraddon.analytics || !window.rivraddon.analytics.getContext()) {
return;
}
utils.logInfo(`ARGUMENTS FOR TYPE: ============= ${eventType}`, args);
let handler = null;
switch (eventType) {
case CONSTANTS.EVENTS.AUCTION_INIT:
handler = window.rivraddon.analytics.trackAuctionInit;
break;
case CONSTANTS.EVENTS.AUCTION_END:
handler = window.rivraddon.analytics.trackAuctionEnd;
break;
case CONSTANTS.EVENTS.BID_WON:
handler = window.rivraddon.analytics.trackBidWon;
break;
}
if (handler) {
handler(args)
if (window.rivraddon && window.rivraddon.analytics && window.rivraddon.analytics.getContext() && window.rivraddon.analytics.trackPbjsEvent) {
utils.logInfo(`ARGUMENTS FOR TYPE: ============= ${eventType}`, args);
window.rivraddon.analytics.trackPbjsEvent({ eventType, args });
}
}
});
Expand All @@ -36,7 +20,7 @@ rivrAnalytics.originEnableAnalytics = rivrAnalytics.enableAnalytics;
// override enableAnalytics so we can get access to the config passed in from the page
rivrAnalytics.enableAnalytics = (config) => {
if (window.rivraddon && window.rivraddon.analytics) {
window.rivraddon.analytics.enableAnalytics(config, {utils, ajax});
window.rivraddon.analytics.enableAnalytics(config, {utils, ajax, pbjsGlobalVariable: $$PREBID_GLOBAL$$});
rivrAnalytics.originEnableAnalytics(config);
}
};
Expand All @@ -46,4 +30,4 @@ adapterManager.registerAnalyticsAdapter({
code: 'rivr'
});

export default rivrAnalytics
export default rivrAnalytics;
4 changes: 3 additions & 1 deletion src/AnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const {
BID_ADJUSTMENT,
BIDDER_DONE,
SET_TARGETING,
AD_RENDER_FAILED
AD_RENDER_FAILED,
ADD_AD_UNITS
}
} = CONSTANTS;

Expand Down Expand Up @@ -109,6 +110,7 @@ export default function AnalyticsAdapter({ url, analyticsType, global, handler }
[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 }),
[ADD_AD_UNITS]: args => this.enqueue({ eventType: ADD_AD_UNITS, args }),
[AUCTION_INIT]: args => {
args.config = typeof config === 'object' ? config.options || {} : {}; // enableAnaltyics configuration object
this.enqueue({ eventType: AUCTION_INIT, args });
Expand Down
12 changes: 12 additions & 0 deletions test/spec/AnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 ADD_AD_UNITS = CONSTANTS.EVENTS.ADD_AD_UNITS;

const AnalyticsAdapter = require('src/AnalyticsAdapter').default;
const config = {
Expand Down Expand Up @@ -86,6 +87,17 @@ FEATURE: Analytics Adapters API
expect(result).to.deep.equal({args: {call: 'adRenderFailed'}, eventType: 'adRenderFailed'});
});

it('SHOULD call global when an addAdUnits event occurs', function () {
const eventType = ADD_AD_UNITS;
const args = { call: 'addAdUnits' };

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

let result = JSON.parse(requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'addAdUnits'}, eventType: 'addAdUnits'});
});

it('SHOULD call global when a bidRequest event occurs', function () {
const eventType = BID_REQUESTED;
const args = { call: 'request' };
Expand Down
61 changes: 14 additions & 47 deletions test/spec/modules/rivrAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ describe('RIVR Analytics adapter', () => {
let sandbox;
let ajaxStub;
let rivraddonsEnableAnalyticsStub;
let rivraddonsTrackAuctionInitStub;
let rivraddonsTrackAuctionEndStub;
let rivraddonsTrackBidWonStub;
let rivraddonsTrackPbjsEventStub;
let timer;

before(() => {
Expand All @@ -46,9 +44,7 @@ describe('RIVR Analytics adapter', () => {
analytics: {
enableAnalytics: () => {},
getContext: () => { return MOCK_RIVRADDON_CONTEXT; },
trackAuctionInit: () => {},
trackAuctionEnd: () => {},
trackBidWon: () => {},
trackPbjsEvent: () => {},
}
};
rivraddonsEnableAnalyticsStub = sandbox.stub(window.rivraddon.analytics, 'enableAnalytics');
Expand Down Expand Up @@ -98,61 +94,32 @@ describe('RIVR Analytics adapter', () => {

it('Firing an event when rivraddon context is not defined it should do nothing', () => {
let rivraddonsGetContextStub = sandbox.stub(window.rivraddon.analytics, 'getContext');
rivraddonsTrackAuctionInitStub = sandbox.stub(window.rivraddon.analytics, 'trackAuctionInit');
rivraddonsTrackPbjsEventStub = sandbox.stub(window.rivraddon.analytics, 'trackPbjsEvent');

expect(rivraddonsTrackAuctionInitStub.callCount).to.be.equal(0);
expect(rivraddonsTrackPbjsEventStub.callCount).to.be.equal(0);

events.emit(CONSTANTS.EVENTS.AUCTION_INIT, {auctionId: EMITTED_AUCTION_ID, config: {}, timeout: 3000});

expect(rivraddonsTrackAuctionInitStub.callCount).to.be.equal(0);
expect(rivraddonsTrackPbjsEventStub.callCount).to.be.equal(0);

window.rivraddon.analytics.getContext.restore();
window.rivraddon.analytics.trackAuctionInit.restore();
window.rivraddon.analytics.trackPbjsEvent.restore();
});

it('Firing AUCTION_INIT should call rivraddon trackAuctionInit passing the parameters', () => {
rivraddonsTrackAuctionInitStub = sandbox.stub(window.rivraddon.analytics, 'trackAuctionInit');
it('Firing AUCTION_INIT should call rivraddon trackPbjsEvent passing the parameters', () => {
rivraddonsTrackPbjsEventStub = sandbox.stub(window.rivraddon.analytics, 'trackPbjsEvent');

expect(rivraddonsTrackAuctionInitStub.callCount).to.be.equal(0);
expect(rivraddonsTrackPbjsEventStub.callCount).to.be.equal(0);

events.emit(CONSTANTS.EVENTS.AUCTION_INIT, {auctionId: EMITTED_AUCTION_ID, config: {}, timeout: 3000});

expect(rivraddonsTrackAuctionInitStub.callCount).to.be.equal(1);
expect(rivraddonsTrackPbjsEventStub.callCount).to.be.equal(1);

const firstArgument = rivraddonsTrackAuctionInitStub.getCall(0).args[0];
expect(firstArgument.auctionId).to.be.equal(EMITTED_AUCTION_ID);
const firstArgument = rivraddonsTrackPbjsEventStub.getCall(0).args[0];
expect(firstArgument.eventType).to.be.equal(CONSTANTS.EVENTS.AUCTION_INIT);
expect(firstArgument.args.auctionId).to.be.equal(EMITTED_AUCTION_ID);

window.rivraddon.analytics.trackAuctionInit.restore();
});

it('Firing AUCTION_END should call rivraddon trackAuctionEnd passing the parameters', () => {
rivraddonsTrackAuctionEndStub = sandbox.stub(window.rivraddon.analytics, 'trackAuctionEnd');

expect(rivraddonsTrackAuctionEndStub.callCount).to.be.equal(0);

events.emit(CONSTANTS.EVENTS.AUCTION_END, {auctionId: EMITTED_AUCTION_ID});

expect(rivraddonsTrackAuctionEndStub.callCount).to.be.equal(1);

const firstArgument = rivraddonsTrackAuctionEndStub.getCall(0).args[0];
expect(firstArgument.auctionId).to.be.equal(EMITTED_AUCTION_ID);

window.rivraddon.analytics.trackAuctionEnd.restore();
});

it('Firing BID_WON should call rivraddon trackBidWon passing the parameters', () => {
rivraddonsTrackBidWonStub = sandbox.stub(window.rivraddon.analytics, 'trackBidWon');

expect(rivraddonsTrackBidWonStub.callCount).to.be.equal(0);

events.emit(CONSTANTS.EVENTS.BID_WON, {auctionId: EMITTED_AUCTION_ID});

expect(rivraddonsTrackBidWonStub.callCount).to.be.equal(1);

const firstArgument = rivraddonsTrackBidWonStub.getCall(0).args[0];
expect(firstArgument.auctionId).to.be.equal(EMITTED_AUCTION_ID);

window.rivraddon.analytics.trackBidWon.restore();
window.rivraddon.analytics.trackPbjsEvent.restore();
});

const BANNER_AD_UNITS_MOCK = [
Expand Down

0 comments on commit 5703176

Please sign in to comment.