Skip to content

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

Enhance data handling in StandardSection components and update SQL queries for improved sorting

Bill Yang12712cb8aa16
diff --git a/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx b/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx
index c1a16b68b..7d23a0494 100644
--- a/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx
+++ b/client/src/app/[site]/components/shared/StandardSection/StandardSection.tsx
@@ -77,7 +77,21 @@ export function StandardSection({
rootMargin: "0px 0px 200px 0px",
});
- const itemsForDisplay = useMemo(() => data?.pages.flatMap(page => page.data), [data]);
+ // Dedupe by key across pages: offset pagination can return the same row on
+ // two pages when the underlying data shifts between fetches, which would
+ // render duplicate React keys.
+ const itemsForDisplay = useMemo(() => {
+ if (!data) return undefined;
+ const seen = new Set<string>();
+ return data.pages
+ .flatMap(page => page.data)
+ .filter(item => {
+ const key = getKey(item);
+ if (seen.has(key)) return false;
+ seen.add(key);
+ return true;
+ });
+ }, [data, getKey]);
useEffect(() => {
if (entry?.isIntersecting && hasNextPage && !isFetchingNextPage && !isLoading) {
diff --git a/client/src/app/[site]/components/shared/StandardSection/StandardSectionDialog.tsx b/client/src/app/[site]/components/shared/StandardSection/StandardSectionDialog.tsx
index 5801a37d5..bff33a921 100644
--- a/client/src/app/[site]/components/shared/StandardSection/StandardSectionDialog.tsx
+++ b/client/src/app/[site]/components/shared/StandardSection/StandardSectionDialog.tsx
@@ -50,6 +50,7 @@ const columnHelper = createColumnHelper<MetricResponse>();
export function StandardSectionDialogBody({
title,
+ getKey,
getLabel,
getValue,
getFilterLabel,
@@ -81,11 +82,20 @@ export function StandardSectionDialogBody({
rootMargin: "0px 0px 200px 0px", // Load more when user is 200px from the bottom
});
- // Flatten the pages data
+ // Flatten the pages data, deduping by key: offset pagination can return the
+ // same row on two pages when the underlying data shifts between fetches.
const allItems = useMemo(() => {
if (!data) return [];
- return data.pages.flatMap(page => page.data);
- }, [data]);
+ const seen = new Set<string>();
+ return data.pages
+ .flatMap(page => page.data)
+ .filter(item => {
+ const key = getKey(item);
+ if (seen.has(key)) return false;
+ seen.add(key);
+ return true;
+ });
+ }, [data, getKey]);
// Filter data based on search term
const filteredData = useMemo(() => {
diff --git a/server/src/api/analytics/getMetric.ts b/server/src/api/analytics/getMetric.ts
index 6e26ec4c1..6f11db71c 100644
--- a/server/src/api/analytics/getMetric.ts
+++ b/server/src/api/analytics/getMetric.ts
@@ -107,7 +107,7 @@ const getQuery = (request: FastifyRequest<GetMetricRequest>, isCountQuery: boole
${filterStatement}
${timeStatement}
AND type = 'custom_event'
- GROUP BY event_name ORDER BY count desc
+ GROUP BY event_name ORDER BY count desc, event_name asc
${limitStatement}
${offsetStatement};
`;
@@ -175,7 +175,7 @@ const getQuery = (request: FastifyRequest<GetMetricRequest>, isCountQuery: boole
) as bounce_rate
FROM TitleStatsWithSessions
GROUP BY value
- ORDER BY count DESC
+ ORDER BY count DESC, value ASC
${limitStatement}
${offsetStatement};
`;
@@ -270,7 +270,7 @@ const getQuery = (request: FastifyRequest<GetMetricRequest>, isCountQuery: boole
avg_time_on_page_seconds as time_on_page_seconds,
round((bounced_sessions / nullIf(unique_sessions, 0)) * 100, 2) as bounce_rate
FROM PathStats
- ORDER BY unique_sessions DESC
+ ORDER BY unique_sessions DESC, pathname ASC
${limitStatement}
${offsetStatement};`;
}
@@ -345,7 +345,7 @@ const getQuery = (request: FastifyRequest<GetMetricRequest>, isCountQuery: boole
avg_time_on_page_seconds as time_on_page_seconds,
round((bounced_sessions / nullIf(unique_sessions, 0)) * 100, 2) as bounce_rate
FROM PathStats
- ORDER BY unique_sessions DESC
+ ORDER BY unique_sessions DESC, pathname ASC
${limitStatement}
${offsetStatement};
`;
@@ -412,7 +412,7 @@ const getQuery = (request: FastifyRequest<GetMetricRequest>, isCountQuery: boole
round((countIf(DISTINCT session_id, pageviews_in_session = 1) / nullIf(COUNT(DISTINCT session_id), 0)) * 100, 2) as bounce_rate
FROM SessionData
GROUP BY value
- ORDER BY count desc
+ ORDER BY count desc, value asc
${limitStatement}
${offsetStatement};
`;
diff --git a/server/src/api/analytics/lite/getMetricLite.ts b/server/src/api/analytics/lite/getMetricLite.ts
index 70e199b69..24018753a 100644
--- a/server/src/api/analytics/lite/getMetricLite.ts
+++ b/server/src/api/analytics/lite/getMetricLite.ts
@@ -87,7 +87,7 @@ export async function getMetricLite(
)
GROUP BY ${parameter}
)
- ORDER BY count DESC
+ ORDER BY count DESC, value ASC
LIMIT ${limit}
${offsetStatement}
`;
@@ -133,7 +133,7 @@ export async function getMetricLite(
${timeStatement}
GROUP BY pathname
)
- ORDER BY count DESC
+ ORDER BY count DESC, value ASC
LIMIT ${limit}
${offsetStatement}
`;
@@ -156,7 +156,7 @@ export async function getMetricLite(
${timeStatement}
GROUP BY country
)
- ORDER BY count DESC
+ ORDER BY count DESC, value ASC
LIMIT ${limit}
${offsetStatement}
`;
@@ -180,7 +180,7 @@ export async function getMetricLite(
${timeStatement}
GROUP BY device_type
)
- ORDER BY count DESC
+ ORDER BY count DESC, value ASC
LIMIT ${limit}
${offsetStatement}
`;