mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2025-08-26 09:55:52 +00:00
* Fix Multiple webanalytics events send per single page load #8396 * Add comments * Do not throw error for collect API
This commit is contained in:
parent
537499fa61
commit
c74d5a9b18
@ -102,9 +102,17 @@ const AppRouter = () => {
|
|||||||
) : null;
|
) : null;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// track page view on route change
|
const { pathname } = location;
|
||||||
analytics.page();
|
|
||||||
}, [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 ? (
|
return loading ? (
|
||||||
<Loader />
|
<Loader />
|
||||||
|
@ -12,7 +12,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import Analytics, { AnalyticsInstance } from 'analytics';
|
import Analytics, { AnalyticsInstance } from 'analytics';
|
||||||
import { AxiosError } from 'axios';
|
|
||||||
import { postPageView } from '../axiosAPIs/WebAnalyticsAPI';
|
import { postPageView } from '../axiosAPIs/WebAnalyticsAPI';
|
||||||
import { WebPageData } from '../components/WebAnalytics/WebAnalytics.interface';
|
import { WebPageData } from '../components/WebAnalytics/WebAnalytics.interface';
|
||||||
import { PageViewEvent } from '../generated/analytics/pageViewEvent';
|
import { PageViewEvent } from '../generated/analytics/pageViewEvent';
|
||||||
@ -20,7 +19,6 @@ import {
|
|||||||
WebAnalyticEventData,
|
WebAnalyticEventData,
|
||||||
WebAnalyticEventType,
|
WebAnalyticEventType,
|
||||||
} from '../generated/analytics/webAnalyticEventData';
|
} from '../generated/analytics/webAnalyticEventData';
|
||||||
import { showErrorToast } from './ToastUtils';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if url is valid or not and return the pathname
|
* 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 { payload } = pageData;
|
||||||
|
|
||||||
const { location, navigator, performance, document } = window;
|
const { location, navigator, performance, document } = window;
|
||||||
const { hostname, pathname } = location;
|
const { hostname } = location;
|
||||||
|
|
||||||
// store the current path reference
|
|
||||||
let currentPathRef = pathname;
|
|
||||||
|
|
||||||
const pageLoadTime = getPageLoadTime(performance);
|
const pageLoadTime = getPageLoadTime(performance);
|
||||||
|
|
||||||
@ -75,43 +70,33 @@ export const trackPageView = async (pageData: WebPageData, userId: string) => {
|
|||||||
|
|
||||||
const referrer = properties.referrer ?? document.referrer;
|
const referrer = properties.referrer ?? document.referrer;
|
||||||
|
|
||||||
const previousPathRef = getReferrerPath(referrer);
|
|
||||||
|
|
||||||
// timestamp for the current event
|
// timestamp for the current event
|
||||||
const timestamp = meta.ts;
|
const timestamp = meta.ts;
|
||||||
|
|
||||||
if (userId) {
|
if (userId) {
|
||||||
/**
|
const pageViewEvent: PageViewEvent = {
|
||||||
* Check if the previous path and current path is not matching
|
fullUrl: properties.url,
|
||||||
* then only collect the data
|
url: properties.path,
|
||||||
*/
|
hostname,
|
||||||
if (currentPathRef !== previousPathRef) {
|
language: navigator.language,
|
||||||
currentPathRef = previousPathRef;
|
screenSize: `${properties.width}x${properties.height}`,
|
||||||
|
userId,
|
||||||
|
sessionId: anonymousId,
|
||||||
|
referrer,
|
||||||
|
pageLoadTime,
|
||||||
|
};
|
||||||
|
|
||||||
const pageViewEvent: PageViewEvent = {
|
const webAnalyticEventData: WebAnalyticEventData = {
|
||||||
fullUrl: properties.url,
|
eventType: WebAnalyticEventType.PageView,
|
||||||
url: properties.path,
|
eventData: pageViewEvent,
|
||||||
hostname,
|
timestamp,
|
||||||
language: navigator.language,
|
};
|
||||||
screenSize: `${properties.width}x${properties.height}`,
|
|
||||||
userId,
|
|
||||||
sessionId: anonymousId,
|
|
||||||
referrer,
|
|
||||||
pageLoadTime,
|
|
||||||
};
|
|
||||||
|
|
||||||
const webAnalyticEventData: WebAnalyticEventData = {
|
try {
|
||||||
eventType: WebAnalyticEventType.PageView,
|
// collect the page event
|
||||||
eventData: pageViewEvent,
|
await postPageView(webAnalyticEventData);
|
||||||
timestamp,
|
} catch (_error) {
|
||||||
};
|
// handle page view error
|
||||||
|
|
||||||
try {
|
|
||||||
// collect the page event
|
|
||||||
await postPageView(webAnalyticEventData);
|
|
||||||
} catch (error) {
|
|
||||||
showErrorToast(error as AxiosError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user