Skip to content

Commit

Permalink
Merge abf4561 into 12fbd40
Browse files Browse the repository at this point in the history
  • Loading branch information
ipanasenko committed Oct 11, 2016
2 parents 12fbd40 + abf4561 commit c9c16ae
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -27,6 +27,28 @@ const action = {
}
}
};

const actionWithMultipleEvents = {
type: 'MY_ACTION',
meta: {
analytics: [
{
type: 'my-analytics-event',
payload: {
some: 'data',
more: 'stuff'
}
},
{
type: 'my-another-analytics-event',
payload: {
some: 'more-data',
more: 'other-stuff'
}
}
]
}
};
```

Note that the `analytics` metadata must also be a [Flux Standard Action](https://github.com/acdlite/flux-standard-action). If this isn't the case, an error will be printed to the console.
Expand Down
22 changes: 14 additions & 8 deletions src/index.js
Expand Up @@ -7,19 +7,25 @@ export default (track, select = ({ meta }) => meta.analytics) => store => next =
return returnValue;
}

const event = select(action);
const eventOrEvents = select(action);

if (!event) {
return returnValue;
}
let events = !Array.isArray(eventOrEvents) ? [eventOrEvents] : eventOrEvents;
events = events.filter(event => Boolean(event));

if (!isFSA(event)) {
const message = "The following event wasn't tracked because it isn't a Flux Standard Action (https://github.com/acdlite/flux-standard-action)";
console.error(message, event);
if (!events.length) {
return returnValue;
}

track(event, store.getState());
events.forEach(event => {
if (!isFSA(event)) {
const message = "The following event wasn't tracked because it isn't a Flux Standard Action (https://github.com/acdlite/flux-standard-action)";
console.error(message, event);
return;
}

track(event, store.getState());
});


return returnValue;
};
36 changes: 36 additions & 0 deletions test/index.js
Expand Up @@ -61,6 +61,42 @@ describe('Given: A Store with analytics middleware', () => {

});

describe('When: An action with multiple analytics meta is dispatched', () => {

beforeEach(() => store.dispatch({
type: 'ROUTE_CHANGE',
meta: {
analytics: [
{ type: 'page-unload' },
{ type: 'button-click' }
]
}
}));

it('Then: It should invoke the tracking callback with the meta as the first argument', () => {
const [ meta0 ] = eventCallbackSpy.getCall(0).args;
const [ meta1 ] = eventCallbackSpy.getCall(1).args;
assert.deepEqual(meta0, { type: 'page-unload' });
assert.deepEqual(meta1, { type: 'button-click' });
});

it('Then: It should invoke the tracking callback with the current state as the second argument', () => {
const [, analyticsState0 ] = eventCallbackSpy.getCall(0).args;
const [, analyticsState1 ] = eventCallbackSpy.getCall(1).args;
assert.deepEqual(analyticsState0, { name: 'jane smith', loggedIn: false });
assert.deepEqual(analyticsState1, { name: 'jane smith', loggedIn: false });
});

it('Then: It should invoke the tracking callback exactly times as analytics array length', () => {
assert.equal(eventCallbackSpy.callCount, 2);
});

it('Then: It should not provide an error to the console', () => {
assert.equal(global.console.error.callCount, 0);
});

});

describe('When: An action with analytics meta is dispatched that updates state', () => {

beforeEach(() => store.dispatch({
Expand Down

0 comments on commit c9c16ae

Please sign in to comment.