Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Enhance analytics data retrieval and error handling in StandardSection

Bill Yang39b878a360ea
diff --git a/client/src/api/analytics/useSingleCol.ts b/client/src/api/analytics/useSingleCol.ts
index 1cbf48124..033875471 100644
--- a/client/src/api/analytics/useSingleCol.ts
+++ b/client/src/api/analytics/useSingleCol.ts
@@ -10,6 +10,8 @@ export type SingleColResponse = {
value: string;
count: number;
percentage: number;
+ avg_session_duration?: number;
+ bounce_rate?: number;
};
export function useSingleCol({
@@ -17,20 +19,18 @@ export function useSingleCol({
limit = 10000,
periodTime,
useFilters = true,
- type = "events",
}: {
parameter: FilterParameter;
limit?: number;
periodTime?: PeriodTime;
useFilters?: boolean;
- type?: "events" | "sessions";
}): UseQueryResult<APIResponse<SingleColResponse[]>> {
const { time, previousTime, site, filters } = useStore();
const timeToUse = periodTime === "previous" ? previousTime : time;
const { startDate, endDate } = getStartAndEndDate(timeToUse);
return useQuery({
- queryKey: [parameter, timeToUse, site, filters, limit, type],
+ queryKey: [parameter, timeToUse, site, filters, limit],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return authedFetch(`${BACKEND_URL}/single-col`, {
@@ -41,7 +41,6 @@ export function useSingleCol({
parameter,
limit,
filters: useFilters ? filters : undefined,
- type,
}).then((res) => res.json());
},
staleTime: Infinity,
diff --git a/client/src/app/[site]/components/shared/StandardSection.tsx b/client/src/app/[site]/components/shared/StandardSection.tsx
index 8700cf0f5..6948df6a0 100644
--- a/client/src/app/[site]/components/shared/StandardSection.tsx
+++ b/client/src/app/[site]/components/shared/StandardSection.tsx
@@ -9,7 +9,7 @@ import {
DialogTrigger,
} from "@/components/ui/dialog";
import { round } from "lodash";
-import { SquareArrowOutUpRight } from "lucide-react";
+import { AlertCircle, SquareArrowOutUpRight, RefreshCcw } from "lucide-react";
import { memo, ReactNode, useMemo } from "react";
import {
SingleColResponse,
@@ -76,7 +76,6 @@ export function StandardSection({
getValue,
getLink,
filterParameter,
- type,
}: {
title: string;
isFetching?: boolean;
@@ -85,11 +84,9 @@ export function StandardSection({
getValue: (item: SingleColResponse) => string;
getLink?: (item: SingleColResponse) => string;
filterParameter: FilterParameter;
- type: "events" | "sessions";
}) {
- const { data, isFetching } = useSingleCol({
+ const { data, isFetching, error, refetch } = useSingleCol({
parameter: filterParameter,
- type,
});
const { data: previousData, isFetching: previousIsFetching } = useSingleCol({
@@ -111,10 +108,35 @@ export function StandardSection({
? 100 / data?.data?.[0]?.percentage
: 1;
+ // Check for errors from either query
+ const hasError = error || previousData?.error;
+ const errorMessage =
+ error?.message || "An error occurred while fetching data";
+
return (
<div className="flex flex-col gap-2">
{isLoading ? (
<Skeleton />
+ ) : hasError ? (
+ <div className="py-6 flex-1 flex flex-col items-center justify-center gap-3 transition-all">
+ <AlertCircle className="text-amber-400 w-8 h-8" />
+ <div className="text-center">
+ <div className="text-neutral-100 font-medium mb-1">
+ Failed to load data
+ </div>
+ <div className="text-sm text-neutral-400 max-w-md mx-auto mb-3">
+ {errorMessage}
+ </div>
+ </div>
+ <Button
+ variant="outline"
+ size="sm"
+ className="bg-transparent hover:bg-neutral-800 border-neutral-700 text-neutral-300 hover:text-neutral-100"
+ onClick={() => refetch()}
+ >
+ <RefreshCcw className="w-3 h-3" /> Try Again
+ </Button>
+ </div>
) : (
data?.data?.slice(0, 10).map((e) => (
<div
@@ -177,7 +199,7 @@ export function StandardSection({
<Button variant="outline" disabled className="opacity-50">
View All
</Button>
- ) : data?.data?.length && data?.data?.length > 10 ? (
+ ) : !hasError && data?.data?.length && data?.data?.length > 10 ? (
<Dialog>
<DialogTrigger asChild>
<Button variant="outline">View All</Button>
diff --git a/client/src/app/[site]/main/components/sections/Countries.tsx b/client/src/app/[site]/main/components/sections/Countries.tsx
index db0a0785c..db6da3823 100644
--- a/client/src/app/[site]/main/components/sections/Countries.tsx
+++ b/client/src/app/[site]/main/components/sections/Countries.tsx
@@ -37,7 +37,6 @@ export function Countries() {
</TabsList>
<TabsContent value="countries">
<StandardSection
- type="sessions"
filterParameter="country"
title="Countries"
getValue={(e) => e.value}
diff --git a/client/src/app/[site]/main/components/sections/Devices.tsx b/client/src/app/[site]/main/components/sections/Devices.tsx
index 4c5be61ec..188064e26 100644
--- a/client/src/app/[site]/main/components/sections/Devices.tsx
+++ b/client/src/app/[site]/main/components/sections/Devices.tsx
@@ -41,7 +41,6 @@ export function Devices() {
</TabsList>
<TabsContent value="devices">
<StandardSection
- type="sessions"
filterParameter="device_type"
title="Devices"
getValue={(e) => e.value}
@@ -58,7 +57,6 @@ export function Devices() {
</TabsContent>
<TabsContent value="browsers">
<StandardSection
- type="sessions"
filterParameter="browser"
title="Browsers"
getValue={(e) => e.value}
@@ -73,7 +71,6 @@ export function Devices() {
</TabsContent>
<TabsContent value="os">
<StandardSection
- type="sessions"
title="Operating Systems"
getValue={(e) => e.value}
getKey={(e) => e.value}
@@ -88,7 +85,6 @@ export function Devices() {
</TabsContent>
<TabsContent value="dimensions">
<StandardSection
- type="sessions"
title="Screen Dimensions"
getValue={(e) => e.value}
getKey={(e) => e.value}
diff --git a/client/src/app/[site]/main/components/sections/Pages.tsx b/client/src/app/[site]/main/components/sections/Pages.tsx
index 82fea3235..51c9ae2de 100644
--- a/client/src/app/[site]/main/components/sections/Pages.tsx
+++ b/client/src/app/[site]/main/components/sections/Pages.tsx
@@ -42,7 +42,6 @@ export function Pages({
</TabsList>
<TabsContent value="pages">
<StandardSection
- type="events"
filterParameter="pathname"
title="Pages"
getValue={(e) => e.value}
@@ -53,7 +52,6 @@ export function Pages({
</TabsContent>
<TabsContent value="entry_pages">
<StandardSection
- type="events"
filterParameter="entry_page"
title="Entry Pages"
getValue={(e) => e.value}
@@ -64,7 +62,6 @@ export function Pages({
</TabsContent>
<TabsContent value="exit_pages">
<StandardSection
- type="events"
filterParameter="exit_page"
title="Exit Pages"
getValue={(e) => e.value}
diff --git a/client/src/app/[site]/main/components/sections/Referrers.tsx b/client/src/app/[site]/main/components/sections/Referrers.tsx
index 0ad806312..cf71cc50a 100644
--- a/client/src/app/[site]/main/components/sections/Referrers.tsx
+++ b/client/src/app/[site]/main/components/sections/Referrers.tsx
@@ -34,7 +34,6 @@ export function Referrers() {
</TabsList>
<TabsContent value="referrers">
<StandardSection
- type="sessions"
filterParameter="referrer"
title="Referrers"
getValue={(e) => e.value}
diff --git a/server/src/api/getSingleCol.ts b/server/src/api/getSingleCol.ts
index 6c8933a95..d87fca05d 100644
--- a/server/src/api/getSingleCol.ts
+++ b/server/src/api/getSingleCol.ts
@@ -17,7 +17,6 @@ interface GenericRequest {
site: string;
filters: string;
parameter: FilterParameter;
- type: "events" | "sessions";
limit?: number;
};
}
@@ -26,91 +25,65 @@ type GetSingleColResponse = {
value: string;
count: number;
percentage: number;
+ avg_session_duration?: number;
+ bounce_rate?: number;
}[];
const getQuery = (request: GenericRequest["Querystring"]) => {
- const {
- startDate,
- endDate,
- timezone,
- site,
- filters,
- parameter,
- limit,
- type,
- } = request;
+ const { startDate, endDate, timezone, site, filters, parameter, limit } =
+ request;
const filterStatement = getFilterStatement(filters);
- if (parameter === "exit_page" || parameter === "entry_page") {
- return `
- SELECT
- ${parameter} as value,
- count() as count,
- ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
- FROM
- sessions_mv
- WHERE
- site_id = ${site}
- AND notEmpty(entry_page)
- ${getTimeStatement(startDate, endDate, timezone, "sessions")}
- GROUP BY
- ${parameter}
- ORDER BY
- COUNT() DESC
- ${limit ? `LIMIT ${limit}` : ""};
- `;
- }
-
if (parameter === "dimensions") {
return `
SELECT
concat(toString(screen_width), 'x', toString(screen_height)) AS value,
- ${
- type === "sessions"
- ? "COUNT(distinct(session_id)) as count"
- : "COUNT(*) as count"
- },
- ${
- type === "sessions"
- ? `ROUND(
- COUNT(distinct(session_id)) * 100.0 / SUM(COUNT(distinct(session_id))) OVER (),
- 2
- ) as percentage`
- : "ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage"
- }
- FROM pageviews
+ COUNT(*) as count,
+ ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
+ FROM sessions_mv
WHERE
site_id = ${site}
${filterStatement}
- ${getTimeStatement(startDate, endDate, timezone)}
+ ${getTimeStatement(startDate, endDate, timezone, "sessions")}
GROUP BY value ORDER BY count desc
${limit ? `LIMIT ${limit}` : ""};`;
}
+ if (["querystring", "page_title", "pathname"].includes(parameter)) {
+ return `
+ SELECT
+ ${geSqlParam(parameter)} as value,
+ COUNT(*) as count,
+ ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage
+ FROM pageviews
+ WHERE
+ site_id = ${site}
+ ${filterStatement}
+ ${getTimeStatement(startDate, endDate, timezone)}
+ AND type = 'pageview'
+ GROUP BY value ORDER BY count desc
+ ${limit ? `LIMIT ${limit}` : ""};
+ `;
+ }
+
return `
SELECT
- ${geSqlParam(parameter)} as value,
- ${
- type === "sessions"
- ? "COUNT(distinct(session_id)) as count"
- : "COUNT(*) as count"
- },
- ${
- type === "sessions"
- ? `ROUND(
- COUNT(distinct(session_id)) * 100.0 / SUM(COUNT(distinct(session_id))) OVER (),
- 2
- ) as percentage`
- : "ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage"
- }
- FROM pageviews
+ ${parameter} as value,
+ count() as count,
+ ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) as percentage,
+ ROUND(AVG(dateDiff('second', session_start, session_end))) as avg_session_duration,
+ ROUND(SUM(if(pageviews = 1, 1, 0)) * 100.0 / COUNT(), 2) as bounce_rate
+ FROM
+ sessions_mv
WHERE
site_id = ${site}
- ${filterStatement}
- ${getTimeStatement(startDate, endDate, timezone)}
- AND type = 'pageview'
- GROUP BY value ORDER BY count desc
+ AND notEmpty(${parameter})
+ ${getTimeStatement(startDate, endDate, timezone, "sessions")}
+ GROUP BY
+ ${parameter}
+ ORDER BY
+ COUNT() DESC
${limit ? `LIMIT ${limit}` : ""};
`;
};