Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
fix some components in realtime data
Francis Cao05ece6a4d079
diff --git a/components/pages/realtime/RealtimeLog.js b/components/pages/realtime/RealtimeLog.jsindex 0ad579c23..cec9d9c61 100644--- a/components/pages/realtime/RealtimeLog.js+++ b/components/pages/realtime/RealtimeLog.js@@ -57,7 +57,7 @@ export default function RealtimeLog({ data, websiteDomain }) {const getIcon = ({ __type }) => icons[__type];const getDetail = log => {- const { __type, eventName, url, browser, os, country, device } = log;+ const { __type, eventName, urlPath: url, browser, os, country, device } = log;if (__type === TYPE_EVENT) {return (diff --git a/components/pages/realtime/RealtimeUrls.js b/components/pages/realtime/RealtimeUrls.jsindex 8f8b8312b..e2e52715a 100644--- a/components/pages/realtime/RealtimeUrls.js+++ b/components/pages/realtime/RealtimeUrls.js@@ -35,18 +35,14 @@ export default function RealtimeUrls({ websiteDomain, data = {} }) {if (pageviews) {const referrers = percentFilter(pageviews- .reduce((arr, { referrer }) => {- if (referrer?.startsWith('http')) {- const hostname = new URL(referrer).hostname.replace(/^www\./, '');+ .reduce((arr, { referrerDomain }) => {+ if (referrerDomain) {+ const row = arr.find(({ x }) => x === referrerDomain);- if (hostname) {- const row = arr.find(({ x }) => x === hostname);-- if (!row) {- arr.push({ x: hostname, y: 1 });- } else {- row.y += 1;- }+ if (!row) {+ arr.push({ x: referrerDomain, y: 1 });+ } else {+ row.y += 1;}}return arr;@@ -56,12 +52,12 @@ export default function RealtimeUrls({ websiteDomain, data = {} }) {const pages = percentFilter(pageviews- .reduce((arr, { url }) => {- if (url?.startsWith('/')) {- const row = arr.find(({ x }) => x === url);+ .reduce((arr, { urlPath }) => {+ if (urlPath?.startsWith('/')) {+ const row = arr.find(({ x }) => x === urlPath);if (!row) {- arr.push({ x: url, y: 1 });+ arr.push({ x: urlPath, y: 1 });} else {row.y += 1;}diff --git a/queries/analytics/event/getEvents.ts b/queries/analytics/event/getEvents.tsindex a5b1be8f7..de66731c0 100644--- a/queries/analytics/event/getEvents.ts+++ b/queries/analytics/event/getEvents.ts@@ -3,17 +3,18 @@ import clickhouse from 'lib/clickhouse';import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';import { EVENT_TYPE } from 'lib/constants';-export function getEvents(...args: [websiteId: string, startAt: Date]) {+export function getEvents(...args: [websiteId: string, startAt: Date, eventType: number]) {return runQuery({[PRISMA]: () => relationalQuery(...args),[CLICKHOUSE]: () => clickhouseQuery(...args),});}-function relationalQuery(websiteId: string, startAt: Date) {+function relationalQuery(websiteId: string, startAt: Date, eventType: number) {return prisma.client.websiteEvent.findMany({where: {websiteId,+ eventType,createdAt: {gte: startAt,},@@ -21,7 +22,7 @@ function relationalQuery(websiteId: string, startAt: Date) {});}-function clickhouseQuery(websiteId: string, startAt: Date) {+function clickhouseQuery(websiteId: string, startAt: Date, eventType: number) {const { rawQuery } = clickhouse;return rawQuery(@@ -34,12 +35,13 @@ function clickhouseQuery(websiteId: string, startAt: Date) {url_path,event_name as eventNamefrom website_event- where event_type = ${EVENT_TYPE.customEvent}+ where event_type = {eventType:Uint32}and website_id = {websiteId:UUID}and created_at >= {startAt:DateTime('UTC')}`,{websiteId,startAt,+ eventType,},);}diff --git a/queries/analytics/pageview/getPageviews.ts b/queries/analytics/pageview/getPageviews.tsdeleted file mode 100644index 4e7730be8..000000000--- a/queries/analytics/pageview/getPageviews.ts+++ /dev/null@@ -1,43 +0,0 @@-import prisma from 'lib/prisma';-import clickhouse from 'lib/clickhouse';-import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';-import { EVENT_TYPE } from 'lib/constants';--export async function getPageviews(...args: [websiteId: string, startAt: Date]) {- return runQuery({- [PRISMA]: () => relationalQuery(...args),- [CLICKHOUSE]: () => clickhouseQuery(...args),- });-}--async function relationalQuery(websiteId: string, startAt: Date) {- return prisma.client.websiteEvent.findMany({- where: {- websiteId,- createdAt: {- gte: startAt,- },- },- });-}--async function clickhouseQuery(websiteId: string, startAt: Date) {- const { rawQuery } = clickhouse;-- return rawQuery(- `select- website_id as websiteId,- session_id as sessionId,- created_at as createdAt,- toUnixTimestamp(created_at) as timestamp,- url_path- from website_event- where event_type = ${EVENT_TYPE.pageView}- and website_id = {websiteId:UUID}- and created_at >= {startAt:DateTime('UTC')}`,- {- websiteId,- startAt,- },- );-}diff --git a/queries/analytics/stats/getRealtimeData.ts b/queries/analytics/stats/getRealtimeData.tsindex 989194bf2..8c2152151 100644--- a/queries/analytics/stats/getRealtimeData.ts+++ b/queries/analytics/stats/getRealtimeData.ts@@ -1,13 +1,13 @@import { md5 } from 'lib/crypto';-import { getPageviews } from '../pageview/getPageviews';import { getSessions } from '../session/getSessions';import { getEvents } from '../event/getEvents';+import { EVENT_TYPE } from 'lib/constants';export async function getRealtimeData(websiteId, time) {const [pageviews, sessions, events] = await Promise.all([- getPageviews(websiteId, time),+ getEvents(websiteId, time, EVENT_TYPE.pageView),getSessions(websiteId, time),- getEvents(websiteId, time),+ getEvents(websiteId, time, EVENT_TYPE.customEvent),]);const decorate = (id, data) => {diff --git a/queries/index.js b/queries/index.jsindex 30e630863..1275e1738 100644--- a/queries/index.js+++ b/queries/index.js@@ -7,7 +7,6 @@ export * from './analytics/event/getEvents';export * from './analytics/eventData/getEventData';export * from './analytics/event/saveEvent';export * from './analytics/pageview/getPageviewMetrics';-export * from './analytics/pageview/getPageviews';export * from './analytics/pageview/getPageviewStats';export * from './analytics/session/createSession';export * from './analytics/session/getSession';