This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
analytics.service.js
61 lines (50 loc) · 1.57 KB
/
analytics.service.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
53
54
55
56
57
58
59
60
61
// @flow
/**
* We currently use Mixpanel for our analytics.
*
* The goal of this file is to abstract that API, so that it can easily be
* swapped in the future for a different provider.
*/
import mixpanel from 'mixpanel-browser';
import uuid from 'uuid/v1';
import electronStore from './electron-store.service';
export const MIXPANEL_KEY = '5840a1a4b9bdcdb518471f0e0830baa2';
const DISTINCT_ID_KEY = 'distinct-id';
export type EventType =
| 'load-application'
| 'create-project'
| 'import-project'
| 'select-project'
| 'launch-dev-server'
| 'run-task'
| 'clear-console'
| 'add-dependency'
| 'update-dependency'
| 'delete-dependency'
| 'delete-project';
export const createLogger = (environment?: ?string = process.env.NODE_ENV) => {
mixpanel.init(MIXPANEL_KEY);
// Every user is given a distinct ID so that we can track return visits.
// Because electron doesn't persist cookies, we have to do this ourselves.
let distinctId = electronStore.get(DISTINCT_ID_KEY);
if (!distinctId) {
distinctId = uuid();
electronStore.set(DISTINCT_ID_KEY, distinctId);
}
mixpanel.identify(distinctId);
return {
logEvent: (event: EventType, data: any) => {
if (environment !== 'production') {
console.info('Event tracked', event, data);
return;
}
if (mixpanel.has_opted_out_tracking()) {
return;
}
mixpanel.track(event, data);
},
};
};
// Export a singleton so that multiple modules can use the same instance.
// We'll only ever want to create alternatives in tests.
export default createLogger();