From d3f4d28aed6ca78da829cd20592e3eca92fe28a2 Mon Sep 17 00:00:00 2001 From: AlessandroDG Date: Mon, 1 Apr 2019 17:51:38 +0200 Subject: [PATCH] Rvr 2369 Refactor events handling (#3683) * Rvr 2369 Refactor events handling (#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 (#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 https://github.com/prebid/Prebid.js/pull/3435 --- modules/rivrAnalyticsAdapter.js | 26 ++------ .../spec/modules/rivrAnalyticsAdapter_spec.js | 61 +++++-------------- 2 files changed, 19 insertions(+), 68 deletions(-) diff --git a/modules/rivrAnalyticsAdapter.js b/modules/rivrAnalyticsAdapter.js index c5ff7757f82..e1cfa5c70bc 100644 --- a/modules/rivrAnalyticsAdapter.js +++ b/modules/rivrAnalyticsAdapter.js @@ -1,6 +1,5 @@ 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'; @@ -8,24 +7,9 @@ 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 }); } } }); @@ -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); } }; @@ -46,4 +30,4 @@ adapterManager.registerAnalyticsAdapter({ code: 'rivr' }); -export default rivrAnalytics +export default rivrAnalytics; diff --git a/test/spec/modules/rivrAnalyticsAdapter_spec.js b/test/spec/modules/rivrAnalyticsAdapter_spec.js index c2c9d0ae9e8..2676c3a59b6 100644 --- a/test/spec/modules/rivrAnalyticsAdapter_spec.js +++ b/test/spec/modules/rivrAnalyticsAdapter_spec.js @@ -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(() => { @@ -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'); @@ -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 = [