Skip to content

Commit

Permalink
Relevant Yield analytics adapter (#6195)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-palmer-relevant-digital authored Jan 28, 2021
1 parent a926dee commit 524efda
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
33 changes: 33 additions & 0 deletions modules/relevantAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import adapter from '../src/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';

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

const { enableAnalytics: orgEnableAnalytics } = relevantAnalytics;

Object.assign(relevantAnalytics, {
/**
* Save event in the global array that will be consumed later by the Relevant Yield library
*/
track: ({ eventType: ev, args }) => {
window.relevantDigital.pbEventLog.push({ ev, args, ts: new Date() });
},

/**
* Before forwarding the call to the original enableAnalytics function -
* create (if needed) the global array that is used to pass events to the Relevant Yield library
* by the 'track' function above.
*/
enableAnalytics: function(...args) {
window.relevantDigital = window.relevantDigital || {};
window.relevantDigital.pbEventLog = window.relevantDigital.pbEventLog || [];
return orgEnableAnalytics.call(this, ...args);
},
});

adapterManager.registerAnalyticsAdapter({
adapter: relevantAnalytics,
code: 'relevant',
});

export default relevantAnalytics;
13 changes: 13 additions & 0 deletions modules/relevantAnalyticsAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Overview

Module Name: Relevant Yield Analytics Adapter

Module Type: Analytics Adapter

Maintainer: [support@relevant-digital.com](mailto:support@relevant-digital.com)

# Description

Analytics adapter to be used with [Relevant Yield](https://www.relevant-digital.com/relevantyield)

Contact [sales@relevant-digital.com](mailto:sales@relevant-digital.com) for information.
43 changes: 43 additions & 0 deletions test/spec/modules/relevantAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import relevantAnalytics from '../../../modules/relevantAnalyticsAdapter.js';
import adapterManager from 'src/adapterManager';
import events from 'src/events';
import constants from 'src/constants.json'
import { expect } from 'chai';

describe('Relevant Analytics Adapter', () => {
beforeEach(() => {
adapterManager.enableAnalytics({
provider: 'relevant'
});
});

afterEach(() => {
relevantAnalytics.disableAnalytics();
});

it('should pass all events to the global array', () => {
// Given
const testEvents = [
{ ev: constants.EVENTS.AUCTION_INIT, args: { test: 1 } },
{ ev: constants.EVENTS.BID_REQUESTED, args: { test: 2 } },
];

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

// Then
const eventQueue = (window.relevantDigital || {}).pbEventLog;
expect(eventQueue).to.be.an('array');
expect(eventQueue.length).to.be.at.least(testEvents.length);

// The last events should be our test events
const myEvents = eventQueue.slice(-testEvents.length);
testEvents.forEach(({ ev, args }, idx) => {
const actualEvent = myEvents[idx];
expect(actualEvent.ev).to.eql(ev);
expect(actualEvent.args).to.eql(args);
});
});
});

0 comments on commit 524efda

Please sign in to comment.