-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
useTelemetry.js
75 lines (61 loc) · 1.67 KB
/
useTelemetry.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Telemetry in bruno is just an anonymous visit counter (triggered once per day).
* The only details shared are:
* - OS (ex: mac, windows, linux)
* - Bruno Version (ex: 1.3.0)
* We don't track usage analytics / micro-interactions / crash logs / anything else.
*/
import { useEffect } from 'react';
import getConfig from 'next/config';
import { PostHog } from 'posthog-node';
import platformLib from 'platform';
import { uuid } from 'utils/common';
const { publicRuntimeConfig } = getConfig();
const posthogApiKey = 'phc_7gtqSrrdZRohiozPMLIacjzgHbUlhalW1Bu16uYijMR';
let posthogClient = null;
const isPlaywrightTestRunning = () => {
return publicRuntimeConfig.PLAYWRIGHT ? true : false;
};
const isDevEnv = () => {
return publicRuntimeConfig.ENV === 'dev';
};
const getPosthogClient = () => {
if (posthogClient) {
return posthogClient;
}
posthogClient = new PostHog(posthogApiKey);
return posthogClient;
};
const getAnonymousTrackingId = () => {
let id = localStorage.getItem('bruno.anonymousTrackingId');
if (!id || !id.length || id.length !== 21) {
id = uuid();
localStorage.setItem('bruno.anonymousTrackingId', id);
}
return id;
};
const trackStart = () => {
if (isPlaywrightTestRunning()) {
return;
}
if (isDevEnv()) {
return;
}
const trackingId = getAnonymousTrackingId();
const client = getPosthogClient();
client.capture({
distinctId: trackingId,
event: 'start',
properties: {
os: platformLib.os.family,
version: '1.34.2'
}
});
};
const useTelemetry = () => {
useEffect(() => {
trackStart();
setInterval(trackStart, 24 * 60 * 60 * 1000);
}, []);
};
export default useTelemetry;