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

RVR-2177 - Refactor events handling #7

Merged
merged 1 commit into from
Jan 8, 2019
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
22 changes: 3 additions & 19 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 adaptermanager from 'src/adaptermanager';
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 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