-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add piano dmp analytics adapter (#8413)
Co-authored-by: Dmitriy Mishutin <dmitriy.mishutin@cxense.com>
- Loading branch information
1 parent
6d66da7
commit 5584057
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); |