diff --git a/components/pages/realtime/RealtimeLog.js b/components/pages/realtime/RealtimeLog.js index 0ad579c2..cec9d9c6 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.js index 8f8b8312..06e81580 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,15 +52,13 @@ 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 }) => { + const row = arr.find(({ x }) => x === urlPath); - if (!row) { - arr.push({ x: url, y: 1 }); - } else { - row.y += 1; - } + if (!row) { + arr.push({ x: urlPath, y: 1 }); + } else { + row.y += 1; } return arr; }, []) diff --git a/queries/analytics/event/getEvents.ts b/queries/analytics/event/getEvents.ts index a5b1be8f..de66731c 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 eventName from 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.ts deleted file mode 100644 index 4e7730be..00000000 --- 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.ts index 989194bf..8c215215 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.js index 30e63086..1275e173 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';