forked from geekuillaume/soundsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposthog.ts
62 lines (55 loc) · 1.75 KB
/
posthog.ts
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
62
import _ from 'lodash';
import { getConfigField } from '../../coordinator/config';
import { BUILD_VERSION } from '../version';
import { isBrowser } from '../environment/isBrowser';
export const AUDIO_SOURCE_EVENT_INTERVAL = 10 * 60 * 1000; // every 10 minutes of source audio, send an event to posthog
export const AUDIO_SINK_EVENT_INTERVAL = 10 * 60 * 1000; // every 10 minutes of sink audio, send an event to posthog
const POSTHOG_TOKEN = 'FYxoT1fEXnvGa6T-ynUJxnquK5ie-g_M78wNEyiOHw0';
const POSTHOG_HOST = 'https://posthog.apps.besson.co';
export const getClient = _.memoize(async () => {
if (isBrowser) {
const posthog = (await import('posthog-js')).default;
posthog.init(POSTHOG_TOKEN, {
api_host: POSTHOG_HOST,
autocapture: false,
});
posthog.identify(getConfigField('uuid'));
return posthog;
}
const Posthog = (await import('posthog-node')).default;
const client = new Posthog(POSTHOG_TOKEN, {
host: POSTHOG_HOST,
});
return client;
});
type EVENT_NAME = 'First run' |
'Audio source 10 minutes' |
'Audio sink 10 minutes';
export const captureEvent = async (eventName: EVENT_NAME, properties?: any) => {
try {
if (getConfigField('disableTelemetry') === true) {
return;
}
if (process.env.DEV_MODE) {
return;
}
const client = await getClient();
if (isBrowser) {
client.capture(eventName, properties);
} else {
client.capture({
distinctId: getConfigField('uuid'),
event: eventName,
properties,
});
}
} catch (e) {
// never let Posthog crash anything, if this doesn't work, just do nothing
}
};
export const trackInstall = () => {
captureEvent('First run', {
platform: process.platform,
version: BUILD_VERSION,
});
};