Skip to content

Commit

Permalink
Add piano dmp analytics adapter (#8413)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitriy Mishutin <dmitriy.mishutin@cxense.com>
  • Loading branch information
DmitriyMishutin and Dmitriy Mishutin authored Jun 10, 2022
1 parent 6d66da7 commit 5584057
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
13 changes: 13 additions & 0 deletions modules/pianoDmpAnalyticcsAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Overview

Module Name: Piano DMP Analytics Adapter

Module Type: Analytics Adapter

Maintainer: [support@piano.com](mailto:support@piano.com)

# Description

Analytics adapter to be used with cx.js

Visit [https://piano.io/product/dmp/](https://piano.io/product/dmp/) for more information.
38 changes: 38 additions & 0 deletions modules/pianoDmpAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import adapter from '../src/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';

const pianoDmpAnalytics = adapter({ analyticsType: 'bundle', handler: 'on' });

const { enableAnalytics: _enableAnalytics } = pianoDmpAnalytics;

Object.assign(pianoDmpAnalytics, {
/**
* Save event in the global array that will be consumed later by cx.js
*/
track: ({ eventType, args: params }) => {
window.cX.callQueue.push([
'prebid',
{ eventType, params, time: Date.now() },
]);
},

/**
* Before forwarding the call to the original enableAnalytics function -
* create (if needed) the global array that is used to pass events to the cx.js library
* by the 'track' function above.
*/
enableAnalytics: function (...args) {
window.cX = window.cX || {};
window.cX.callQueue = window.cX.callQueue || [];

return _enableAnalytics.call(this, ...args);
},
});

adapterManager.registerAnalyticsAdapter({
adapter: pianoDmpAnalytics,
code: 'pianoDmp',
gvlid: 412,
});

export default pianoDmpAnalytics;
66 changes: 66 additions & 0 deletions test/spec/modules/pianoDmpAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import pianoDmpAnalytics from 'modules/pianoDmpAnalyticsAdapter.js';
import adapterManager from 'src/adapterManager';
import * as events from 'src/events';
import constants from 'src/constants.json';
import { expect } from 'chai';

describe('Piano DMP Analytics Adapter', () => {
let sandbox;

beforeEach(() => {
sandbox = sinon.createSandbox();

sandbox.stub(events, 'getEvents').returns([]);
});

afterEach(() => {
sandbox.restore();
});

describe('track', () => {
beforeEach(() => {
adapterManager.enableAnalytics({
provider: 'pianoDmp',
});
});

afterEach(() => {
delete window.cX;
pianoDmpAnalytics.disableAnalytics();
});

it('should pass events to call queue', () => {
const eventsList = [
constants.EVENTS.AUCTION_INIT,
constants.EVENTS.AUCTION_END,
constants.EVENTS.BID_ADJUSTMENT,
constants.EVENTS.BID_TIMEOUT,
constants.EVENTS.BID_REQUESTED,
constants.EVENTS.BID_RESPONSE,
constants.EVENTS.NO_BID,
constants.EVENTS.BID_WON,
];

// Given
const testEvents = eventsList.map((event) => ({
event,
args: { test: event },
}));

// When
testEvents.forEach(({ event, args }) => events.emit(event, args));

// Then
const callQueue = (window.cX || {}).callQueue;

expect(callQueue).to.be.an('array');
expect(callQueue.length).to.equal(testEvents.length);

callQueue.forEach(([method, params], index) => {
expect(method).to.equal('prebid');
expect(params.eventType).to.equal(testEvents[index].event);
expect(params.params).to.deep.equal(testEvents[index].args);
});
});
});
});

0 comments on commit 5584057

Please sign in to comment.