Skip to content

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

refactor(gsc): request named dimensions in the client (#1271)

Goldflag32ccc73cb396
diff --git a/client/src/api/gsc/endpoints/data.test.ts b/client/src/api/gsc/endpoints/data.test.ts
new file mode 100644
index 000000000..d54047fa9
--- /dev/null
+++ b/client/src/api/gsc/endpoints/data.test.ts
@@ -0,0 +1,64 @@
+import axios, { AxiosRequestConfig } from "axios";
+import Papa from "papaparse";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { BACKEND_URL } from "../../../lib/const";
+import { generateCSV } from "../../../lib/export";
+import { fetchGSCData, GSCDimension } from "./data";
+
+vi.mock("axios", () => ({ default: vi.fn() }));
+
+const axiosMock = vi.mocked(axios);
+const lastRequest = () => axiosMock.mock.calls.at(-1)?.[0] as AxiosRequestConfig;
+
+afterEach(() => {
+ axiosMock.mockReset();
+});
+
+describe("fetchGSCData", () => {
+ it.each<[GSCDimension, string]>([
+ ["query", 'analytics, "privacy"'],
+ ["page", "https://example.test/docs"],
+ ["country", "US"],
+ ["device", "MOBILE"],
+ ])("requests %s through dimensions and keeps dashboard and CSV rows unchanged", async (dimension, name) => {
+ const metrics = { clicks: 12, impressions: 200, ctr: 0.06, position: 3.5 };
+ axiosMock.mockResolvedValue({ data: { data: [{ [dimension]: name, ...metrics }] } });
+
+ const rows = await fetchGSCData("site-42", {
+ dimension,
+ startDate: "2026-09-01",
+ endDate: "2026-09-28",
+ timeZone: "America/Los_Angeles",
+ });
+
+ expect(lastRequest()).toMatchObject({
+ url: `${BACKEND_URL}/sites/site-42/gsc/data`,
+ params: {
+ dimensions: dimension,
+ start_date: "2026-09-01",
+ end_date: "2026-09-28",
+ time_zone: "America/Los_Angeles",
+ },
+ });
+ expect(lastRequest().params).not.toHaveProperty("dimension");
+ expect(rows).toEqual([{ name, ...metrics }]);
+
+ const csv = Papa.parse<Record<string, string>>(generateCSV(rows), { header: true });
+ expect(csv.meta.fields).toEqual(["name", "clicks", "impressions", "ctr", "position"]);
+ expect(csv.data).toEqual([{ name, clicks: "12", impressions: "200", ctr: "0.06", position: "3.5" }]);
+ expect(csv.errors).toEqual([]);
+ });
+
+ it("returns an empty dashboard result when the API has no rows", async () => {
+ axiosMock.mockResolvedValue({ data: { data: [] } });
+
+ await expect(
+ fetchGSCData(42, {
+ dimension: "query",
+ startDate: "2026-09-01",
+ endDate: "2026-09-28",
+ timeZone: "UTC",
+ })
+ ).resolves.toEqual([]);
+ });
+});
diff --git a/client/src/api/gsc/endpoints/data.ts b/client/src/api/gsc/endpoints/data.ts
index 160b9f251..4a7c37b7a 100644
--- a/client/src/api/gsc/endpoints/data.ts
+++ b/client/src/api/gsc/endpoints/data.ts
@@ -21,18 +21,24 @@ export interface GSCDataParams {
* Fetch Google Search Console data for a specific dimension
* GET /api/sites/:site/gsc/data
*/
-export async function fetchGSCData(
+export async function fetchGSCData<Dimension extends GSCDimension>(
site: string | number,
- params: GSCDataParams
+ params: GSCDataParams & { dimension: Dimension }
): Promise<GSCData[]> {
- const response = await authedFetch<{ data: GSCData[] }>(
+ const response = await authedFetch<{ data: (Omit<GSCData, "name"> & Record<Dimension, string>)[] }>(
`/sites/${site}/gsc/data`,
{
- dimension: params.dimension,
+ dimensions: params.dimension,
start_date: params.startDate,
end_date: params.endDate,
time_zone: params.timeZone,
}
);
- return response.data;
+ return response.data.map(row => ({
+ name: row[params.dimension],
+ clicks: row.clicks,
+ impressions: row.impressions,
+ ctr: row.ctr,
+ position: row.position,
+ }));
}