forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.ts
More file actions
65 lines (59 loc) · 1.6 KB
/
telemetry.ts
File metadata and controls
65 lines (59 loc) · 1.6 KB
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
import { createResource } from "frappe-ui";
import "../../../frappe/frappe/public/js/lib/posthog.js";
declare global {
interface Window {
posthog: {
init: (projectToken: string, options: any) => void;
identify: (userId: string) => void;
startSessionRecording: () => void;
capture: (eventName: string, data?: any) => void;
};
}
}
type PosthogSettings = {
posthog_project_id: string;
posthog_host: string;
enable_telemetry: boolean;
telemetry_site_age: number;
record_session: boolean;
posthog_identify: string;
};
type Posthog = {
init: (projectToken: string, options: any) => void;
identify: (userId: string) => void;
startSessionRecording: () => void;
capture: (eventName: string, data?: any) => void;
};
let posthog: Posthog = {
init: () => {},
identify: () => {},
startSessionRecording: () => {},
capture: () => {},
};
createResource({
url: "builder.api.get_posthog_settings",
method: "GET",
auto: true,
onSuccess: (posthogSettings: PosthogSettings) => {
if (!posthogSettings.enable_telemetry || !posthogSettings.posthog_project_id) {
return;
}
window.posthog.init(posthogSettings.posthog_project_id, {
api_host: posthogSettings.posthog_host,
person_profiles: "identified_only",
autocapture: false,
capture_pageview: false,
capture_pageleave: false,
enable_heatmaps: false,
disable_session_recording: true,
loaded: (ph: typeof posthog) => {
ph.identify(posthogSettings?.posthog_identify || window.location.host);
posthog = ph;
if (posthogSettings.record_session) {
ph.startSessionRecording();
}
},
});
},
});
export { posthog };