🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor Realtime components and update event fetching logic
Bill Yangb23d20d0c182
diff --git a/client/src/api/analytics/users.ts b/client/src/api/analytics/users.tsindex 4ee48de42..076968483 100644--- a/client/src/api/analytics/users.ts+++ b/client/src/api/analytics/users.ts@@ -76,6 +76,6 @@ export function useGetUsers(options: GetUsersOptions) {// Enable refetching when the window regains focusrefetchOnWindowFocus: true,// Add a background refetch interval (every 30 seconds)- refetchInterval: 30000,+ // refetchInterval: 30000,});}diff --git a/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsx b/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsxindex ebefd4580..f65d8179b 100644--- a/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsx+++ b/client/src/app/[site]/realtime/RealtimeChart/RealtimeChart.tsx@@ -4,7 +4,6 @@ import { useMemo } from "react";import { useGetOverviewBucketedPastMinutes } from "../../../../api/analytics/useGetOverviewBucketed";import { nivoTheme } from "../../../../lib/nivo";import { useStore } from "../../../../lib/store";-import { formatter } from "../../../../lib/utils";export function RealtimeChart() {const { site } = useStore();@@ -13,7 +12,7 @@ export function RealtimeChart() {pastMinutes: 30,site,bucket: "minute",- refetchInterval: 3000,+ refetchInterval: 5000,});const chartData = useMemo(() => {@@ -30,7 +29,7 @@ export function RealtimeChart() {}, [data]);if (isLoading) {- return <div>Loading...</div>;+ return null;}if (!chartData || chartData.length === 0) {@@ -49,46 +48,24 @@ export function RealtimeChart() {}return (- <div style={{ height: 300 }}>+ <div style={{ height: 70 }}><ResponsiveBardata={chartData}keys={["users"]}indexBy="time"- margin={{ top: 20, right: 0, bottom: 30, left: 35 }}+ margin={{ top: 0, right: 0, bottom: 12, left: 0 }}padding={0.3}valueScale={{ type: "linear" }}indexScale={{ type: "band", round: true }}- // colors={["hsla(210, 40%, 50%, 0.7)"]}- colors={["hsl(var(--accent-500))"]}+ colors={["hsl(var(--amber-200))"]}theme={nivoTheme}axisTop={null}axisRight={null}- axisBottom={{- tickSize: 5,- tickPadding: 5,- legend: "",- legendPosition: "middle",- legendOffset: 32,- format: (value: string) => {- const dt = DateTime.fromFormat(value, "yyyy-MM-dd HH:mm:ss", {- zone: "utc",- }).toLocal();- return dt.isValid ? dt.toFormat("h:mm") : "";- },- }}- axisLeft={{- tickSize: 5,- tickPadding: 5,- tickRotation: 0,- legend: "",- legendPosition: "middle",- legendOffset: -30,- format: formatter,- tickValues: 3,- }}+ axisBottom={null}+ axisLeft={null}enableLabel={false}enableGridX={false}- enableGridY={true}+ enableGridY={false}tooltip={({id,value,@@ -109,7 +86,7 @@ export function RealtimeChart() {<div className="bg-neutral-950 p-2 rounded-md text-xs"><div className="font-semibold mb-1">{currentTime.isValid- ? currentTime.toFormat("HH:mm")+ ? currentTime.toFormat("h:mm"): "Invalid Time"}</div><div className="flex justify-between items-center">@@ -121,6 +98,7 @@ export function RealtimeChart() {}}animate={true}motionConfig="gentle"+ borderRadius={2}/></div>);diff --git a/client/src/app/[site]/realtime/RealtimeEvents/RealtimeEvents.tsx b/client/src/app/[site]/realtime/RealtimeEvents/RealtimeEvents.tsxindex 649cd7f40..b8dc387d4 100644--- a/client/src/app/[site]/realtime/RealtimeEvents/RealtimeEvents.tsx+++ b/client/src/app/[site]/realtime/RealtimeEvents/RealtimeEvents.tsx@@ -47,86 +47,91 @@ function EventCard({ event }: { event: Event }) {}`;return (- <div className="mb-3 rounded-lg bg-neutral-850/50 border border-neutral-800 overflow-hidden p-3 flex flex-col filter backdrop-blur-sm">- <div className="flex items-center gap-2 text-sm text-neutral-100 mb-2">- <div className="flex items-center gap-2">- {isPageview ? (- <FileText className="w-4 h-4 text-blue-500" />+ <Link+ href={`user/${event.user_id}`}+ target="_blank"+ rel="noopener noreferrer"+ >+ <div className="mb-3 rounded-lg bg-neutral-850/50 border border-neutral-800 overflow-hidden p-3 flex flex-col filter backdrop-blur-sm hover:bg-neutral-800/70 transition-all duration-200">+ <div className="flex items-center gap-2 text-sm text-neutral-100 mb-2">+ <div className="flex items-center gap-2">+ {isPageview ? (+ <FileText className="w-4 h-4 text-blue-500" />+ ) : (+ <MousePointerClick className="w-4 h-4 text-amber-500" />+ )}+ </div>++ {event.type === "pageview" ? (+ <div>+ <Link href={fullPath} target="_blank" rel="noopener noreferrer">+ <div+ className="text-sm truncate hover:underline "+ title={event.pathname}+ style={{+ maxWidth: "calc(min(100vw, 1150px) - 250px)",+ }}+ >+ {truncatePath(+ `${event.pathname}${+ event.querystring ? `${event.querystring}` : ""+ }`+ )}+ </div>+ </Link>+ </div>) : (- <MousePointerClick className="w-4 h-4 text-amber-500" />+ <div>{event.event_name}</div>)}</div>-- {event.type === "pageview" ? (- <div>- <Link href={fullPath} target="_blank" rel="noopener noreferrer">- <div- className="text-sm truncate hover:underline "- title={event.pathname}- style={{- maxWidth: "calc(min(100vw, 1150px) - 250px)",- }}- >- {truncatePath(- `${event.pathname}${- event.querystring ? `${event.querystring}` : ""- }`- )}- </div>- </Link>- </div>- ) : (- <div>{event.event_name}</div>- )}- </div>- <div className="flex items-center gap-2">- <div className="flex space-x-2 items-center ml-6">- {event.country && (+ <div className="flex items-center gap-2">+ <div className="flex space-x-2 items-center ml-6">+ {event.country && (+ <Tooltip>+ <TooltipTrigger asChild>+ <div className="flex items-center">+ <CountryFlag country={event.country} />+ </div>+ </TooltipTrigger>+ <TooltipContent>+ <p>{getCountryName(event.country)}</p>+ </TooltipContent>+ </Tooltip>+ )}<Tooltip><TooltipTrigger asChild>- <div className="flex items-center">- <CountryFlag country={event.country} />+ <div>+ <Browser browser={event.browser || "Unknown"} /></div></TooltipTrigger><TooltipContent>- <p>{getCountryName(event.country)}</p>+ <p>{event.browser || "Unknown browser"}</p></TooltipContent></Tooltip>- )}- <Tooltip>- <TooltipTrigger asChild>- <div>- <Browser browser={event.browser || "Unknown"} />- </div>- </TooltipTrigger>- <TooltipContent>- <p>{event.browser || "Unknown browser"}</p>- </TooltipContent>- </Tooltip>- <Tooltip>- <TooltipTrigger asChild>- <div>- <OperatingSystem os={event.operating_system || ""} />- </div>- </TooltipTrigger>- <TooltipContent>- <p>{event.operating_system || "Unknown OS"}</p>- </TooltipContent>- </Tooltip>- <Tooltip>- <TooltipTrigger asChild>- <div>- <DeviceIcon deviceType={event.device_type || ""} />- </div>- </TooltipTrigger>- <TooltipContent>- <p>{event.device_type || "Unknown device"}</p>- </TooltipContent>- </Tooltip>- </div>+ <Tooltip>+ <TooltipTrigger asChild>+ <div>+ <OperatingSystem os={event.operating_system || ""} />+ </div>+ </TooltipTrigger>+ <TooltipContent>+ <p>{event.operating_system || "Unknown OS"}</p>+ </TooltipContent>+ </Tooltip>+ <Tooltip>+ <TooltipTrigger asChild>+ <div>+ <DeviceIcon deviceType={event.device_type || ""} />+ </div>+ </TooltipTrigger>+ <TooltipContent>+ <p>{event.device_type || "Unknown device"}</p>+ </TooltipContent>+ </Tooltip>+ </div>- {/* Path with tooltip for long paths */}- {/* <div className="flex items-center ml-3 flex-1 min-w-0">+ {/* Path with tooltip for long paths */}+ {/* <div className="flex items-center ml-3 flex-1 min-w-0"><Tooltip><TooltipTrigger asChild><span className="text-xs text-gray-400 truncate max-w-[200px] inline-block">@@ -147,12 +152,13 @@ function EventCard({ event }: { event: Event }) {</Tooltip></div> */}- {/* Time information */}- <div className="flex items-center text-xs text-gray-300">- <span className="text-gray-400">{eventTime.toRelative()}</span>+ {/* Time information */}+ <div className="flex items-center text-xs text-gray-300">+ <span className="text-gray-400">{eventTime.toRelative()}</span>+ </div></div></div>- </div>+ </Link>);}@@ -186,49 +192,46 @@ export function RealtimeEvents() {function EventCardSkeleton() {// Function to get a random width class for skeletonsconst getRandomWidth = () => {- const widths = ["w-16", "w-20", "w-24", "w-28", "w-32"];+ const widths = ["w-16", "w-20", "w-24", "w-28", "w-32", "w-36", "w-40"];return widths[Math.floor(Math.random() * widths.length)];};// Create multiple skeletons for a realistic loading stateconst skeletons = Array.from({ length: 5 }).map((_, index) => (<div- className="mb-3 rounded-lg bg-neutral-900 border border-neutral-800 overflow-hidden"+ className="mb-3 rounded-lg bg-neutral-850/50 border border-neutral-800 overflow-hidden p-3 flex flex-col filter backdrop-blur-sm"key={index}>- <div className="p-3">+ {/* Title row */}+ <div className="flex items-center gap-2 text-sm text-neutral-100 mb-2"><div className="flex items-center gap-2">- {/* Event badge and User ID */}- <div className="flex items-center gap-2">- <Skeleton className="h-6 w-20 rounded-md" />- <Skeleton className="h-3 w-14" />- </div>-- {/* Icons */}- <div className="flex space-x-2 items-center ml-3">- <Skeleton className="h-4 w-4 rounded-sm" />- <Skeleton className="h-4 w-4 rounded-sm" />- <Skeleton className="h-4 w-4 rounded-sm" />- <Skeleton className="h-4 w-4 rounded-sm" />- </div>+ <Skeleton className="h-4 w-4 rounded-sm" />+ </div>+ <Skeleton className={`h-4 ${getRandomWidth()}`} />+ </div>- {/* Path with randomized width */}- <div className="flex items-center ml-3 flex-1 min-w-0">- <Skeleton className={`h-3 max-w-[200px] ${getRandomWidth()}`} />- </div>+ {/* Details row */}+ <div className="flex items-center gap-2">+ <div className="flex space-x-2 items-center ml-6">+ <Skeleton className="h-4 w-4 rounded-sm" />+ <Skeleton className="h-4 w-4 rounded-sm" />+ <Skeleton className="h-4 w-4 rounded-sm" />+ <Skeleton className="h-4 w-4 rounded-sm" />+ </div>- {/* Time */}- <div className="flex items-center ml-3">- <Skeleton className="h-3 w-24" />- </div>+ {/* Time */}+ <div className="flex items-center text-xs">+ <Skeleton className="h-3 w-16" /></div></div></div>));return (- <div className="max-h-[400px] overflow-y-auto p-2">- <div className="mb-2 text-sm font-medium">Recent Events</div>+ <div+ className="overflow-y-auto p-2"+ style={{ height: "calc(100vh - 50px)" }}+ >{skeletons}</div>);diff --git a/client/src/app/[site]/realtime/page.tsx b/client/src/app/[site]/realtime/page.tsxindex 4f118cbad..bc71df632 100644--- a/client/src/app/[site]/realtime/page.tsx+++ b/client/src/app/[site]/realtime/page.tsx@@ -31,46 +31,47 @@ export default function RealtimePage() {<div style={{ position: "relative", overflow: "hidden" }} ref={ref}><World width={width ?? 0} />- <div className="absolute top-4 left-4 flex items-center w-full">- {/* <div style={{ width: (size.height ?? 0) + 100 }}> */}- <div className="flex flex-col p-4 pt-3 bg-neutral-900 rounded-lg shadow-lg border border-neutral-750 w-fit">- <div className="text-sm text-gray-400 flex gap-2 text-nowrap items-center">- <div className="flex justify-center">- <span className="relative flex h-4 w-4">- <span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-green-400 opacity-75"></span>- <span className="relative inline-flex h-4 w-4 rounded-full bg-green-500"></span>- </span>+ <div className="absolute top-4 left-4 flex items-center w-full">+ <div className="flex flex-col p-3 bg-neutral-900 rounded-lg shadow-lg border border-neutral-750 w-[400px]">+ <div className="p-2">+ <div className="text-sm text-gray-400 flex gap-2 text-nowrap items-center">+ <div className="flex justify-center">+ <span className="relative flex h-3 w-3">+ <span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-green-400 opacity-75"></span>+ <span className="relative inline-flex h-3 w-3 rounded-full bg-green-500"></span>+ </span>+ </div>+ Visitors in the last{" "}+ <Select+ value={minutes}+ onValueChange={(value) => setMinutes(value as MinutesType)}+ >+ <SelectTrigger>+ <SelectValue placeholder="Select a time period" />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="5">5 minutes</SelectItem>+ <SelectItem value="15">15 minutes</SelectItem>+ <SelectItem value="30">30 minutes</SelectItem>+ <SelectItem value="60">1 hour</SelectItem>+ <SelectItem value="120">2 hours</SelectItem>+ <SelectItem value="240">4 hours</SelectItem>+ <SelectItem value="480">8 hours</SelectItem>+ <SelectItem value="720">12 hours</SelectItem>+ <SelectItem value="1440">24 hours</SelectItem>+ </SelectContent>+ </Select>+ </div>+ <div className="text-5xl font-bold flex items-center gap-2">+ {+ <NumberFlow+ respectMotionPreference={false}+ value={data?.count ?? 0}+ />+ }</div>- Visitors in the last{" "}- <Select- value={minutes}- onValueChange={(value) => setMinutes(value as MinutesType)}- >- <SelectTrigger>- <SelectValue placeholder="Select a time period" />- </SelectTrigger>- <SelectContent>- <SelectItem value="5">5 minutes</SelectItem>- <SelectItem value="15">15 minutes</SelectItem>- <SelectItem value="30">30 minutes</SelectItem>- <SelectItem value="60">1 hour</SelectItem>- <SelectItem value="120">2 hours</SelectItem>- <SelectItem value="240">4 hours</SelectItem>- <SelectItem value="480">8 hours</SelectItem>- <SelectItem value="720">12 hours</SelectItem>- <SelectItem value="1440">24 hours</SelectItem>- </SelectContent>- </Select>- </div>- <div className="text-5xl font-bold flex items-center gap-2">- {- <NumberFlow- respectMotionPreference={false}- value={data?.count ?? 0}- />- }</div>- {/* </div> */}+ <RealtimeChart /></div></div>diff --git a/client/src/app/[site]/realtime/realtimeStore.ts b/client/src/app/[site]/realtime/realtimeStore.tsindex 43aa69fac..c816fcae9 100644--- a/client/src/app/[site]/realtime/realtimeStore.ts+++ b/client/src/app/[site]/realtime/realtimeStore.ts@@ -11,4 +11,4 @@ export type MinutesType =| "720"| "1440";-export const minutesAtom = atom<MinutesType>("5");+export const minutesAtom = atom<MinutesType>("30");diff --git a/server/src/api/analytics/getEvents.ts b/server/src/api/analytics/getEvents.tsindex 95daaa602..5eb7357b0 100644--- a/server/src/api/analytics/getEvents.ts+++ b/server/src/api/analytics/getEvents.ts@@ -65,6 +65,7 @@ export async function getEvents(WHEREsite_id = {siteId:Int32}AND (type = 'custom_event' OR type = 'pageview')+ AND timestamp > now() - INTERVAL 30 MINUTEORDER BY timestamp DESCLIMIT {limit:Int32}`,