Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/hooks/use-analytics-tracking.ts
2500 views
1
/**
2
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { useEffect } from "react";
8
import { useHistory } from "react-router";
9
import { trackButtonOrAnchor, trackPathChange } from "../Analytics";
10
import { useTheme } from "../theme-context";
11
import { useOrbital } from "./use-orbital";
12
import { useCurrentUser } from "../user-context";
13
14
export const useAnalyticsTracking = () => {
15
const history = useHistory();
16
const user = useCurrentUser();
17
const { isDark } = useTheme();
18
19
const { orbital, isLoaded: isOrbitalLoaded, discoveryIds } = useOrbital("4aErj3uvRbye");
20
21
// listen and notify Segment of client-side path updates
22
useEffect(() => {
23
//store current path to have access to previous when path changes
24
const w = window as any;
25
const _gp = w._gp || (w._gp = {});
26
_gp.path = window.location.pathname;
27
28
return history.listen(() => {
29
const path = window.location.pathname;
30
// Make sure that we don't accidentally track the same path twice. This can happen if just the hash or search params change.
31
if (path === _gp.path) {
32
return;
33
}
34
35
trackPathChange({
36
prev: (window as any)._gp.path,
37
path: path,
38
});
39
(window as any)._gp.path = path;
40
});
41
}, [history]);
42
43
// Track button/anchor clicks
44
useEffect(() => {
45
const handleButtonOrAnchorTracking = (props: MouseEvent) => {
46
var curr = props.target as HTMLElement;
47
48
// TODO: Look at using curr.closest('a,button') instead - determine if divs w/ onClick are being used
49
//check if current target or any ancestor up to document is button or anchor
50
while (!(curr instanceof Document)) {
51
if (
52
curr instanceof HTMLButtonElement ||
53
curr instanceof HTMLAnchorElement ||
54
(curr instanceof HTMLDivElement && curr.onclick)
55
) {
56
trackButtonOrAnchor(curr);
57
break; //finding first ancestor is sufficient
58
}
59
curr = curr.parentNode as HTMLElement;
60
}
61
};
62
window.addEventListener("click", handleButtonOrAnchorTracking, true);
63
return () => window.removeEventListener("click", handleButtonOrAnchorTracking, true);
64
}, []);
65
66
useEffect(() => {
67
if (!user || !user.profile?.onboardedTimestamp || !isOrbitalLoaded) {
68
return;
69
}
70
71
orbital("identify", user.id);
72
73
orbital("customConfig", {
74
theme: {
75
colorScheme: isDark ? "dark" : "light",
76
colorsLight: {
77
primary: "#292524",
78
},
79
colorsDark: {
80
primary: "#fff",
81
},
82
},
83
});
84
85
// Initiate every discovery we have inside of Configcat. This does not show the modals right away, but rather lets Orbital's cleverness take over and decide when to show them.
86
for (const id of discoveryIds) {
87
orbital("trigger", id, { position: "bottom_left" });
88
}
89
90
return () => orbital("reset");
91
}, [discoveryIds, isDark, isOrbitalLoaded, orbital, user]);
92
};
93
94