Skip to content

Commit

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

* 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

* Rvr 2369 prevent duplicate events (prebid#10)

## Type of change
- [x] Refactoring (no functional changes, no api changes)

## Description of change

Refactor rivrAnalyticsAdapter.js events handling.


## History

* RVR-2087 - update adapterManager dependency

* RVR-2369 - Update package-lock.json

* RVR-2369 - Revert changes in main Analytics adapter

It will be handled in a separate PR

* RVR-2369 - Use relative import paths

Needed for prebid#3435
  • Loading branch information
AlessandroDG authored and jacekburys-quantcast committed May 15, 2019
1 parent fef159c commit ea0570e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 68 deletions.
26 changes: 5 additions & 21 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 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;
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 ea0570e

Please sign in to comment.