Path: blob/main/components/dashboard/src/hooks/use-analytics-tracking.ts
2500 views
/**1* Copyright (c) 2022 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { useEffect } from "react";7import { useHistory } from "react-router";8import { trackButtonOrAnchor, trackPathChange } from "../Analytics";9import { useTheme } from "../theme-context";10import { useOrbital } from "./use-orbital";11import { useCurrentUser } from "../user-context";1213export const useAnalyticsTracking = () => {14const history = useHistory();15const user = useCurrentUser();16const { isDark } = useTheme();1718const { orbital, isLoaded: isOrbitalLoaded, discoveryIds } = useOrbital("4aErj3uvRbye");1920// listen and notify Segment of client-side path updates21useEffect(() => {22//store current path to have access to previous when path changes23const w = window as any;24const _gp = w._gp || (w._gp = {});25_gp.path = window.location.pathname;2627return history.listen(() => {28const path = window.location.pathname;29// Make sure that we don't accidentally track the same path twice. This can happen if just the hash or search params change.30if (path === _gp.path) {31return;32}3334trackPathChange({35prev: (window as any)._gp.path,36path: path,37});38(window as any)._gp.path = path;39});40}, [history]);4142// Track button/anchor clicks43useEffect(() => {44const handleButtonOrAnchorTracking = (props: MouseEvent) => {45var curr = props.target as HTMLElement;4647// TODO: Look at using curr.closest('a,button') instead - determine if divs w/ onClick are being used48//check if current target or any ancestor up to document is button or anchor49while (!(curr instanceof Document)) {50if (51curr instanceof HTMLButtonElement ||52curr instanceof HTMLAnchorElement ||53(curr instanceof HTMLDivElement && curr.onclick)54) {55trackButtonOrAnchor(curr);56break; //finding first ancestor is sufficient57}58curr = curr.parentNode as HTMLElement;59}60};61window.addEventListener("click", handleButtonOrAnchorTracking, true);62return () => window.removeEventListener("click", handleButtonOrAnchorTracking, true);63}, []);6465useEffect(() => {66if (!user || !user.profile?.onboardedTimestamp || !isOrbitalLoaded) {67return;68}6970orbital("identify", user.id);7172orbital("customConfig", {73theme: {74colorScheme: isDark ? "dark" : "light",75colorsLight: {76primary: "#292524",77},78colorsDark: {79primary: "#fff",80},81},82});8384// 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.85for (const id of discoveryIds) {86orbital("trigger", id, { position: "bottom_left" });87}8889return () => orbital("reset");90}, [discoveryIds, isDark, isOrbitalLoaded, orbital, user]);91};929394