From c74d5a9b18aed64ae4e3d2fbfb8be83adb3f7a0d Mon Sep 17 00:00:00 2001 From: Sachin Chaurasiya Date: Fri, 28 Oct 2022 14:45:35 +0530 Subject: [PATCH] Fix #8396 Multiple webanalytics events send per single page load (#8402) * Fix Multiple webanalytics events send per single page load #8396 * Add comments * Do not throw error for collect API --- .../resources/ui/src/router/AppRouter.tsx | 14 ++++- .../ui/src/utils/WebAnalyticsUtils.ts | 59 +++++++------------ 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/openmetadata-ui/src/main/resources/ui/src/router/AppRouter.tsx b/openmetadata-ui/src/main/resources/ui/src/router/AppRouter.tsx index a2a8d777a66..ada8f6be0f8 100644 --- a/openmetadata-ui/src/main/resources/ui/src/router/AppRouter.tsx +++ b/openmetadata-ui/src/main/resources/ui/src/router/AppRouter.tsx @@ -102,9 +102,17 @@ const AppRouter = () => { ) : null; useEffect(() => { - // track page view on route change - analytics.page(); - }, [location]); + const { pathname } = location; + + /** + * Ignore the slash path because we are treating my data as + * default path. + */ + if (pathname !== '/') { + // track page view on route change + analytics.page(); + } + }, [location.pathname]); return loading ? ( diff --git a/openmetadata-ui/src/main/resources/ui/src/utils/WebAnalyticsUtils.ts b/openmetadata-ui/src/main/resources/ui/src/utils/WebAnalyticsUtils.ts index 11bb582b968..a09dde64da7 100644 --- a/openmetadata-ui/src/main/resources/ui/src/utils/WebAnalyticsUtils.ts +++ b/openmetadata-ui/src/main/resources/ui/src/utils/WebAnalyticsUtils.ts @@ -12,7 +12,6 @@ */ import Analytics, { AnalyticsInstance } from 'analytics'; -import { AxiosError } from 'axios'; import { postPageView } from '../axiosAPIs/WebAnalyticsAPI'; import { WebPageData } from '../components/WebAnalytics/WebAnalytics.interface'; import { PageViewEvent } from '../generated/analytics/pageViewEvent'; @@ -20,7 +19,6 @@ import { WebAnalyticEventData, WebAnalyticEventType, } from '../generated/analytics/webAnalyticEventData'; -import { showErrorToast } from './ToastUtils'; /** * Check if url is valid or not and return the pathname @@ -64,10 +62,7 @@ export const trackPageView = async (pageData: WebPageData, userId: string) => { const { payload } = pageData; const { location, navigator, performance, document } = window; - const { hostname, pathname } = location; - - // store the current path reference - let currentPathRef = pathname; + const { hostname } = location; const pageLoadTime = getPageLoadTime(performance); @@ -75,43 +70,33 @@ export const trackPageView = async (pageData: WebPageData, userId: string) => { const referrer = properties.referrer ?? document.referrer; - const previousPathRef = getReferrerPath(referrer); - // timestamp for the current event const timestamp = meta.ts; if (userId) { - /** - * Check if the previous path and current path is not matching - * then only collect the data - */ - if (currentPathRef !== previousPathRef) { - currentPathRef = previousPathRef; + const pageViewEvent: PageViewEvent = { + fullUrl: properties.url, + url: properties.path, + hostname, + language: navigator.language, + screenSize: `${properties.width}x${properties.height}`, + userId, + sessionId: anonymousId, + referrer, + pageLoadTime, + }; - const pageViewEvent: PageViewEvent = { - fullUrl: properties.url, - url: properties.path, - hostname, - language: navigator.language, - screenSize: `${properties.width}x${properties.height}`, - userId, - sessionId: anonymousId, - referrer, - pageLoadTime, - }; + const webAnalyticEventData: WebAnalyticEventData = { + eventType: WebAnalyticEventType.PageView, + eventData: pageViewEvent, + timestamp, + }; - const webAnalyticEventData: WebAnalyticEventData = { - eventType: WebAnalyticEventType.PageView, - eventData: pageViewEvent, - timestamp, - }; - - try { - // collect the page event - await postPageView(webAnalyticEventData); - } catch (error) { - showErrorToast(error as AxiosError); - } + try { + // collect the page event + await postPageView(webAnalyticEventData); + } catch (_error) { + // handle page view error } } };