lbry-desktop/src/renderer/analytics.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-02-16 09:47:52 +01:00
// @flow
import mixpanel from 'mixpanel-browser';
import Lbryio from 'lbryio';
2018-03-08 06:44:03 +01:00
import isDev from 'electron-is-dev';
2018-03-08 06:44:03 +01:00
if (isDev) {
mixpanel.init('691723e855cabb9d27a7a79002216967');
} else {
mixpanel.init('af5c6b8110068fa4f5c4600c81f05e60');
}
2018-02-16 09:47:52 +01:00
type Analytics = {
track: (string, ?Object) => void,
2018-02-24 01:24:00 +01:00
setUser: Object => void,
toggle: (boolean, ?boolean) => void,
apiLogView: (string, string, string) => void,
2018-02-24 01:24:00 +01:00
};
2018-02-16 09:47:52 +01:00
let analyticsEnabled: boolean = false;
2018-02-16 09:47:52 +01:00
const analytics: Analytics = {
track: (name: string, payload: ?Object): void => {
2018-02-24 01:24:00 +01:00
if (analyticsEnabled) {
if (payload) {
mixpanel.track(name, payload);
} else {
mixpanel.track(name);
}
2018-02-16 09:47:52 +01:00
}
},
setUser: (user: Object): void => {
2018-02-24 01:24:00 +01:00
if (user.id) {
2018-02-16 09:47:52 +01:00
mixpanel.identify(user.id);
}
2018-02-24 01:24:00 +01:00
if (user.primary_email) {
2018-02-16 09:47:52 +01:00
mixpanel.people.set({
2018-02-24 01:24:00 +01:00
$email: user.primary_email,
2018-02-16 09:47:52 +01:00
});
}
},
toggle: (enabled: boolean, logDisabled: ?boolean): void => {
2018-02-24 01:24:00 +01:00
if (!enabled && logDisabled) {
mixpanel.track('DISABLED');
}
analyticsEnabled = enabled;
2018-02-24 01:24:00 +01:00
},
apiLogView: (uri: string, outpoint: string, claimId: string): void => {
2018-03-08 06:07:42 +01:00
if (analyticsEnabled) {
Lbryio.call('file', 'view', {
uri,
2018-03-08 06:07:42 +01:00
outpoint,
claim_id: claimId,
}).catch(() => {});
}
2018-03-08 06:07:42 +01:00
},
2018-02-24 01:24:00 +01:00
};
2018-02-16 09:47:52 +01:00
export default analytics;