Skip to content

Open source alternative to Semrush and Ahrefs

Stop caching lost Google grants on the dashboard and fix the Testing-mode OAuth docs (#1030)

Ben Senescu779b3f5f5aaf
diff --git a/docs/SELF_HOSTING_GOOGLE_ANALYTICS.md b/docs/SELF_HOSTING_GOOGLE_ANALYTICS.md
index 5ee83918..85909a80 100644
--- a/docs/SELF_HOSTING_GOOGLE_ANALYTICS.md
+++ b/docs/SELF_HOSTING_GOOGLE_ANALYTICS.md
@@ -28,6 +28,12 @@ read-only reports added in later GA4 milestones.
Under **APIs & Services → OAuth consent screen**, configure the app. While the
app is in Testing, add every Google account that will connect as a test user.
+Then click **Publish app** to move it from **Testing** to **In production**:
+Google expires refresh tokens for External apps in Testing after 7 days, so a
+Testing app asks you to reconnect every week. See the
+[Search Console guide](./SELF_HOSTING_GOOGLE_SEARCH_CONSOLE.md#2-configure-the-oauth-consent-screen)
+for details.
+
## 3) Register the callback URL
Open **APIs & Services → Credentials**, edit the Web application OAuth client,
@@ -75,5 +81,6 @@ scheme, host, port, and `/api/ga4/oauth/callback` path used by the deployment.
**No properties appear** — confirm that the Analytics Admin API is enabled and
the connected Google account has access to the property.
-**Connection expired** — reconnect the Google account. OAuth apps left in
-Google's Testing status can receive short-lived refresh grants.
+**Analytics disconnects every week** — your OAuth app is still in Testing,
+where Google expires refresh tokens after 7 days. Publish it under **OAuth
+consent screen → Publish app**, then reconnect once.
diff --git a/src/server/features/dashboard/services/DashboardCacheService.test.ts b/src/server/features/dashboard/services/DashboardCacheService.test.ts
index 7794ba12..8a20825a 100644
--- a/src/server/features/dashboard/services/DashboardCacheService.test.ts
+++ b/src/server/features/dashboard/services/DashboardCacheService.test.ts
@@ -99,6 +99,23 @@ it("caches empty results rather than billing again on every visit", async () =>
expect(load).toHaveBeenCalledTimes(1);
});
+it("does not cache a lost Google grant, so a reconnect shows data on the next visit", async () => {
+ const load = vi
+ .fn()
+ .mockResolvedValueOnce({ connected: false })
+ .mockResolvedValueOnce({ connected: true });
+ const read = () =>
+ DashboardCacheService.readThrough({
+ ...input(),
+ schema: z.object({ connected: z.boolean() }),
+ shouldCache: (value) => value.connected,
+ load,
+ });
+ expect((await read()).value).toEqual({ connected: false });
+ expect((await read()).value).toEqual({ connected: true });
+ expect(load).toHaveBeenCalledTimes(2);
+});
+
it("keeps stale results and backs off after a failed paid refresh", async () => {
const load = vi.fn(async () => ["saved"]);
await DashboardCacheService.readThrough(input(load));
diff --git a/src/server/features/dashboard/services/DashboardCacheService.ts b/src/server/features/dashboard/services/DashboardCacheService.ts
index 9e4f8dda..6a02823c 100644
--- a/src/server/features/dashboard/services/DashboardCacheService.ts
+++ b/src/server/features/dashboard/services/DashboardCacheService.ts
@@ -19,6 +19,9 @@ async function readThrough<T>(input: {
schema: z.ZodType<T>;
maxAgeMs: number;
load: () => Promise<T>;
+ // Results that should not outlive the request, such as a dead Google grant:
+ // a reconnect only rewrites the account row, so the cache key would not change.
+ shouldCache?: (value: T) => boolean;
}) {
const key = await buildCacheKey(input.namespace, {
...input.params,
@@ -59,7 +62,8 @@ async function readThrough<T>(input: {
}
const value = input.schema.parse(await input.load());
const result = { value, fetchedAt: new Date().toISOString() };
- await setCached(key, result, 90 * 86400);
+ if (input.shouldCache?.(value) ?? true)
+ await setCached(key, result, 90 * 86400);
await DataRefreshClaimRepository.finish(key, token, 0);
return { ...result, stale: false, refreshError: null };
} catch (error) {
diff --git a/src/server/features/dashboard/services/DashboardSiteService.ts b/src/server/features/dashboard/services/DashboardSiteService.ts
index f5eade2d..5c1402ba 100644
--- a/src/server/features/dashboard/services/DashboardSiteService.ts
+++ b/src/server/features/dashboard/services/DashboardSiteService.ts
@@ -105,6 +105,7 @@ async function getKeywords(projectId: string, organizationId: string) {
endDate: z.string(),
}),
maxAgeMs: 86400_000,
+ shouldCache: (value) => value.connected,
load: async () => {
try {
const result = await GscService.getPerformance({
@@ -179,6 +180,7 @@ async function getEmergingQueries(projectId: string, organizationId: string) {
},
schema: queriesSchema,
maxAgeMs: 86400_000,
+ shouldCache: (value) => value.connected,
load: async () => {
try {
const [recent, baseline] = await Promise.all([