Skip to content

Commit ac10801

Browse files
authored
Fixed data inconsistency in Post Analytics > Overview tab (#24558)
ref https://linear.app/ghost/issue/PROD-2243/inconsistency-in-the-post-analytics-overview-data-compared-to-post On the Post Analytics > Overview tab, the top level metric shown for unique visitors and the sum of the actual data in the chart can show different values. The metric and the chart are using separate requests to the same API endpoint with different `date_from` parameters, resulting in slightly different numbers, i.e. it's possible to see "1 unique visitor" but with 0 for every data point on the chart: <img width="2306" height="1388" alt="image" src="https://github.com/user-attachments/assets/110ede5c-8ea6-4600-bdb1-f1d7950fd59e" /> This fixes that by removing the duplicate API call, so the data for the metric and the chart use the same exact underlying data.
1 parent 2be1eed commit ac10801

File tree

1 file changed

+6
-33
lines changed

1 file changed

+6
-33
lines changed

apps/posts/src/views/PostAnalytics/Overview/Overview.tsx

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const Overview: React.FC = () => {
1818
const navigate = useNavigate();
1919
const {statsConfig, isLoading: isConfigLoading, post, isPostLoading, postId} = useGlobalData();
2020
const {totals, isLoading: isTotalsLoading, currencySymbol} = usePostReferrers(postId);
21-
const {startDate, endDate, timezone} = getRangeDates(STATS_RANGES.ALL_TIME.value);
2221
const {appSettings} = useAppContext();
2322
const {emailTrackClicks: emailTrackClicksEnabled, emailTrackOpens: emailTrackOpensEnabled} = appSettings?.analytics || {};
2423

@@ -33,28 +32,8 @@ const Overview: React.FC = () => {
3332

3433
const {startDate: chartStartDate, endDate: chartEndDate, timezone: chartTimezone} = getRangeDates(chartRange);
3534

36-
// KPI params for overview data
35+
// Params for KPI data (both chart and totals)
3736
const params = useMemo(() => {
38-
const baseParams = {
39-
site_uuid: statsConfig?.id || '',
40-
date_from: formatQueryDate(startDate),
41-
date_to: formatQueryDate(endDate),
42-
timezone: timezone,
43-
post_uuid: ''
44-
};
45-
46-
if (!isPostLoading && post?.uuid) {
47-
return {
48-
...baseParams,
49-
post_uuid: post.uuid
50-
};
51-
}
52-
53-
return baseParams;
54-
}, [isPostLoading, post, statsConfig?.id, startDate, endDate, timezone]);
55-
56-
// Chart params for chart data
57-
const chartParams = useMemo(() => {
5837
const baseParams = {
5938
site_uuid: statsConfig?.id || '',
6039
date_from: formatQueryDate(chartStartDate),
@@ -73,28 +52,22 @@ const Overview: React.FC = () => {
7352
return baseParams;
7453
}, [isPostLoading, post, statsConfig?.id, chartStartDate, chartEndDate, chartTimezone]);
7554

76-
const {data, loading: tbLoading} = useTinybirdQuery({
77-
endpoint: 'api_kpis',
78-
statsConfig: statsConfig || {id: ''},
79-
params: params
80-
});
81-
8255
const {data: chartData, loading: chartLoading} = useTinybirdQuery({
8356
endpoint: 'api_kpis',
8457
statsConfig: statsConfig || {id: ''},
85-
params: chartParams
58+
params: params
8659
});
8760

8861
// Calculate total visitors as a number for WebOverview component
8962
const totalVisitors = useMemo(() => {
90-
if (!data?.length) {
63+
if (!chartData?.length) {
9164
return 0;
9265
}
93-
return data.reduce((sum, item) => {
66+
return chartData.reduce((sum, item) => {
9467
const visits = Number(item.visits);
9568
return sum + (isNaN(visits) ? 0 : visits);
9669
}, 0);
97-
}, [data]);
70+
}, [chartData]);
9871

9972
// Process chart data for WebOverview
10073
const currentMetric = KPI_METRICS.visits;
@@ -115,7 +88,7 @@ const Overview: React.FC = () => {
11588
params: params
11689
});
11790

118-
const kpiIsLoading = isConfigLoading || isTotalsLoading || isPostLoading || tbLoading;
91+
const kpiIsLoading = isConfigLoading || isTotalsLoading || isPostLoading || chartLoading;
11992
const chartIsLoading = isPostLoading || isConfigLoading || chartLoading;
12093

12194
// Use the utility function from admin-x-framework

0 commit comments

Comments
 (0)