forked from keen/keen-tracking.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeen-redux-middleware.js
52 lines (45 loc) · 1.33 KB
/
keen-redux-middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import KeenTracking from 'keen-tracking';
// Record all actions to a single event stream
const EVENT_STREAM_NAME = 'app-actions';
// Omit noisy actions if necessary
const OMITTED_ACTIONS = [
// '@@router/LOCATION_CHANGE'
];
// Define a client instance
const client = new KeenTracking({
projectId: 'YOUR_PROJECT_ID',
writeKey: 'YOUR_WRITE_KEY'
});
if (process.env.NODE_ENV !== 'production') {
// Optionally prevent recording in dev mode
Keen.enabled = false;
// Display events in the browser console
client.on('recordEvent', Keen.log);
}
// Track a 'pageview' event and initialize auto-tracking data model
client.initAutoTracking({
recordClicks: false,
recordFormSubmits: false,
recordPageViews: true
});
const reduxMiddleware = function({ getState }) {
return (next) => (action) => {
const returnValue = next(action);
const eventBody = {
'action': action,
'state': getState()
/*
Include additional properties here, or
refine the state data that is recorded
by cherry-picking specific properties
*/
};
// Filter omitted actions by action.type
// ...or whatever you name this property
if (OMITTED_ACTIONS.indexOf(action.type) < 0) {
client.recordEvent(EVENT_STREAM_NAME, eventBody);
}
return returnValue;
};
}
export default reduxMiddleware;