umami/src/queries/analytics/getRealtimeData.ts

28 lines
914 B
TypeScript
Raw Normal View History

import { md5 } from 'next-basics';
2023-08-08 22:29:59 +00:00
import { getSessions, getEvents } from 'queries/index';
2023-04-07 01:31:16 +00:00
import { EVENT_TYPE } from 'lib/constants';
2022-07-12 21:14:36 +00:00
2023-12-01 05:58:11 +00:00
export async function getRealtimeData(websiteId: string, startDate: Date) {
2022-07-12 21:14:36 +00:00
const [pageviews, sessions, events] = await Promise.all([
2023-12-01 05:58:11 +00:00
getEvents(websiteId, startDate, EVENT_TYPE.pageView),
getSessions(websiteId, startDate),
getEvents(websiteId, startDate, EVENT_TYPE.customEvent),
2022-07-12 21:14:36 +00:00
]);
2023-12-01 05:58:11 +00:00
const decorate = (id: string, data: any[]) => {
return data.map((props: { [key: string]: any }) => ({
2022-07-12 21:14:36 +00:00
...props,
2023-02-18 05:42:42 +00:00
__id: md5(id, ...Object.values(props)),
2023-02-23 04:59:59 +00:00
__type: id,
timestamp: props.timestamp ? props.timestamp * 1000 : new Date(props.createdAt).getTime(),
2023-02-18 05:42:42 +00:00
}));
};
return {
2023-07-29 05:40:57 +00:00
pageviews: decorate('pageview', pageviews),
2023-02-23 04:59:59 +00:00
sessions: decorate('session', sessions),
events: decorate('event', events),
2022-07-12 21:14:36 +00:00
timestamp: Date.now(),
};
}