diff --git a/frontend/src/AppRoutes/index.tsx b/frontend/src/AppRoutes/index.tsx index 2727ce88ea..15312fb66a 100644 --- a/frontend/src/AppRoutes/index.tsx +++ b/frontend/src/AppRoutes/index.tsx @@ -1,13 +1,13 @@ import { ConfigProvider } from 'antd'; import getLocalStorageApi from 'api/browser/localstorage/get'; import setLocalStorageApi from 'api/browser/localstorage/set'; +import logEvent from 'api/common/logEvent'; import NotFound from 'components/NotFound'; import Spinner from 'components/Spinner'; import { FeatureKeys } from 'constants/features'; import { LOCALSTORAGE } from 'constants/localStorage'; import ROUTES from 'constants/routes'; import AppLayout from 'container/AppLayout'; -import useAnalytics from 'hooks/analytics/useAnalytics'; import { KeyboardHotkeysProvider } from 'hooks/hotkeys/useKeyboardHotkeys'; import { useThemeConfig } from 'hooks/useDarkMode'; import { useGetTenantLicense } from 'hooks/useGetTenantLicense'; @@ -15,7 +15,6 @@ import { LICENSE_PLAN_KEY } from 'hooks/useLicense'; import { NotificationProvider } from 'hooks/useNotifications'; import { ResourceProvider } from 'hooks/useResourceAttribute'; import history from 'lib/history'; -import { identity, pickBy } from 'lodash-es'; import posthog from 'posthog-js'; import AlertRuleProvider from 'providers/Alert'; import { useAppContext } from 'providers/App/App'; @@ -51,8 +50,6 @@ function App(): JSX.Element { } = useAppContext(); const [routes, setRoutes] = useState(defaultRoutes); - const { trackPageView } = useAnalytics(); - const { hostname, pathname } = window.location; const { @@ -69,18 +66,21 @@ function App(): JSX.Element { const { name, email, role } = user; + const domain = extractDomain(email); + const hostNameParts = hostname.split('.'); + const identifyPayload = { email, name, company_name: orgName, - role, + tenant_id: hostNameParts[0], + data_region: hostNameParts[1], + tenant_url: hostname, + company_domain: domain, source: 'signoz-ui', + role, }; - const sanitizedIdentifyPayload = pickBy(identifyPayload, identity); - const domain = extractDomain(email); - const hostNameParts = hostname.split('.'); - const groupTraits = { name: orgName, tenant_id: hostNameParts[0], @@ -90,8 +90,13 @@ function App(): JSX.Element { source: 'signoz-ui', }; - window.analytics.identify(email, sanitizedIdentifyPayload); - window.analytics.group(domain, groupTraits); + if (email) { + logEvent('Email Identified', identifyPayload, 'identify'); + } + + if (domain) { + logEvent('Domain Identified', groupTraits, 'group'); + } posthog?.identify(email, { email, @@ -192,9 +197,7 @@ function App(): JSX.Element { hide_default_launcher: false, }); } - - trackPageView(pathname); - }, [pathname, trackPageView]); + }, [pathname]); useEffect(() => { // feature flag shouldn't be loading and featureFlags or fetchError any one of this should be true indicating that req is complete diff --git a/frontend/src/api/common/logEvent.ts b/frontend/src/api/common/logEvent.ts index a1bf3dba7c..26c7807bd9 100644 --- a/frontend/src/api/common/logEvent.ts +++ b/frontend/src/api/common/logEvent.ts @@ -7,11 +7,15 @@ import { EventSuccessPayloadProps } from 'types/api/events/types'; const logEvent = async ( eventName: string, attributes: Record, + eventType?: 'track' | 'group' | 'identify', + rateLimited?: boolean, ): Promise | ErrorResponse> => { try { const response = await axios.post('/event', { eventName, attributes, + eventType: eventType || 'track', + rateLimited: rateLimited || false, // TODO: Update this once we have a proper way to handle rate limiting }); return { diff --git a/frontend/src/container/AppLayout/index.tsx b/frontend/src/container/AppLayout/index.tsx index 1736d43b41..6594abaffa 100644 --- a/frontend/src/container/AppLayout/index.tsx +++ b/frontend/src/container/AppLayout/index.tsx @@ -392,11 +392,16 @@ function AppLayout(props: AppLayoutProps): JSX.Element { LOCALSTORAGE.DONT_SHOW_SLOW_API_WARNING, ); - logEvent(`Slow API Warning`, { - duration: `${data.duration}ms`, - url: data.url, - threshold: data.threshold, - }); + logEvent( + `Slow API Warning`, + { + durationMs: data.duration, + url: data.url, + thresholdMs: data.threshold, + }, + 'track', + true, // rate limited - controlled by Backend + ); const isDontShowSlowApiWarning = dontShowSlowApiWarning === 'true'; diff --git a/frontend/src/container/OnboardingContainer/__test__/InviteUserFlow.test.tsx b/frontend/src/container/OnboardingContainer/__test__/InviteUserFlow.test.tsx index 91d370e9ed..5764714d94 100644 --- a/frontend/src/container/OnboardingContainer/__test__/InviteUserFlow.test.tsx +++ b/frontend/src/container/OnboardingContainer/__test__/InviteUserFlow.test.tsx @@ -19,10 +19,6 @@ jest.mock('hooks/useNotifications', () => ({ })), })); -window.analytics = { - track: jest.fn(), -}; - describe('Onboarding invite team member flow', () => { it('initial render and get started page', async () => { const { findByText } = render( diff --git a/frontend/src/hooks/analytics/useAnalytics.tsx b/frontend/src/hooks/analytics/useAnalytics.tsx deleted file mode 100644 index 14a17ec8ad..0000000000 --- a/frontend/src/hooks/analytics/useAnalytics.tsx +++ /dev/null @@ -1,40 +0,0 @@ -import { useAppContext } from 'providers/App/App'; -import { useCallback } from 'react'; -import { extractDomain } from 'utils/app'; - -const useAnalytics = (): any => { - const { user } = useAppContext(); - - // Segment Page View - analytics.page([category], [name], [properties], [options], [callback]); - const trackPageView = useCallback( - (pageName: string): void => { - if (user && user.email) { - window.analytics.page(null, pageName, { - userId: user.email, - }); - } - }, - [user], - ); - - const trackEvent = ( - eventName: string, - properties?: Record, - ): void => { - if (user && user.email) { - const context = { - context: { - groupId: extractDomain(user?.email), - }, - }; - - const updatedProperties = { ...properties }; - updatedProperties.userId = user.email; - window.analytics.track(eventName, properties, context); - } - }; - - return { trackPageView, trackEvent }; -}; - -export default useAnalytics; diff --git a/frontend/src/index.html.ejs b/frontend/src/index.html.ejs index 81b820fdc2..e0643f936d 100644 --- a/frontend/src/index.html.ejs +++ b/frontend/src/index.html.ejs @@ -49,12 +49,10 @@ /> - + - - @@ -100,32 +98,16 @@ -