Skip to content

Commit

Permalink
added ability to pipe analytics events to customize values before log…
Browse files Browse the repository at this point in the history
…ging
  • Loading branch information
snapwich committed Mar 14, 2017
1 parent 25cc847 commit c377321
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/adapters/analytics/AnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function AnalyticsAdapter({ url, analyticsType, global, handler }
var _eventCount = 0;
var _enableCheck = true;
var _handlers;
var _pipe;

if (analyticsType === LIBRARY) {
loadScript(url, _emptyQueue);
Expand Down Expand Up @@ -61,6 +62,10 @@ export default function AnalyticsAdapter({ url, analyticsType, global, handler }
function _enqueue({ eventType, args }) {
const _this = this;

if(typeof _pipe === 'function') {
args = _pipe(eventType, args);
}

if (global && window[global] && eventType && args) {
this.track({ eventType, args });
} else {
Expand All @@ -74,8 +79,17 @@ export default function AnalyticsAdapter({ url, analyticsType, global, handler }
function _enable(config) {
var _this = this;

_sampled = typeof config === "undefined" || typeof config.options === "undefined" ||
typeof config.options.sampling === "undefined" || Math.random() < parseFloat(config.options.sampling);
if(typeof config === 'object' && typeof config.options === 'object') {
_sampled = typeof config.options.sampling === 'undefined' || Math.random() < parseFloat(config.options.sampling);

if(typeof config.options.pipe === 'function') {
_pipe = config.options.pipe;
}

} else {
_sampled = true;
}


if(_sampled) {
//first send all events fired before enableAnalytics called
Expand Down
37 changes: 37 additions & 0 deletions test/spec/unit/adapters/analytics/AnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@ FEATURE: Analytics Adapters API
assert(spyTestGlobal.calledOnce === true);
});

it('SHOULD allow us to edit the event as logged using the pipe option', () => {
const eventType = BID_RESPONSE;
const args = {cpm: .7};

adapter.enableAnalytics({
options: {
pipe: function(eventType, args) {
if(eventType === BID_RESPONSE) {
let bid = copy(args);
let cpm = bid.cpm;
if (cpm >= 0 && cpm < 0.5) {
bid.cpm = '0-0.5';
} else if (cpm >= 0.5 && cpm < 1) {
bid.cpm = '0.5-1';
} else if (cpm >= 1 && cpm < 1.5) {
bid.cpm = '1-1.5';
} else {
bid.cpm = '>1.5';
}
return bid;
}

return args;

function copy(obj) {
return JSON.parse(JSON.stringify(obj));
}
}
}
});
events.emit(eventType, args);

assert.ok(spyTestGlobal.args[0][1] === eventType, `with expected event type\n`);
assert.deepEqual(spyTestGlobal.args[0][2], {cpm: '0.5-1'}, `with expected event data\n`);
});


describe(`AND sampling is enabled\n`, () => {
const eventType = BID_WON;
const args = { more: 'info' };
Expand Down

0 comments on commit c377321

Please sign in to comment.