📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
fix: distinguish skipped GSC analytics and honor stream timeouts
Blue Mouse50ef1d2c551c
diff --git a/backend/apps/cloud/src/project/gsc.service.ts b/backend/apps/cloud/src/project/gsc.service.tsindex b174775cd..ed388c712 100644--- a/backend/apps/cloud/src/project/gsc.service.ts+++ b/backend/apps/cloud/src/project/gsc.service.ts@@ -65,6 +65,38 @@ type OrganicPositionSeriesEntry = { date: string } & Record<OrganicPositionBucketKey,number>+type BrandedTrafficAnalytics = { branded: number; nonBranded: number }+type SkippedBrandedAnalytics = {+ skipped: true+ branded: null+ nonBranded: null+}+type PositionAnalytics = {+ impressionsByPosition: {+ key: ImpressionPositionBucketKey+ label: string+ impressions: number+ percentage: number+ }[]+ organicPositions: OrganicPositionSeriesEntry[]+}+type SkippedPositionAnalytics = {+ skipped: true+ impressionsByPosition: null+ organicPositions: null+}++const SKIPPED_BRANDED_ANALYTICS: SkippedBrandedAnalytics = {+ skipped: true,+ branded: null,+ nonBranded: null,+}++const SKIPPED_POSITION_ANALYTICS: SkippedPositionAnalytics = {+ skipped: true,+ impressionsByPosition: null,+ organicPositions: null,+}const initialImpressionPositionBuckets = (): Record<ImpressionPositionBucketKey,@@ -87,16 +119,6 @@ const initialOrganicPositionEntry = (pos51Plus: 0,})-const emptyPositionAnalytics = () => ({- impressionsByPosition: IMPRESSION_POSITION_BUCKETS.map(({ key, label }) => ({- key,- label,- impressions: 0,- percentage: 0,- })),- organicPositions: [] as OrganicPositionSeriesEntry[],-})-const getImpressionPositionBucketKey = (position: number,): ImpressionPositionBucketKey => {@@ -908,7 +930,7 @@ export class GSCService {to: string,filtersStr?: string,ctx?: GSCContext,- ): Promise<{ branded: number; nonBranded: number }> {+ ): Promise<BrandedTrafficAnalytics> {const brandKeywords = await this.getProjectBrandKeywords(pid)if (_isEmpty(brandKeywords)) {return { branded: 0, nonBranded: 0 }@@ -965,15 +987,7 @@ export class GSCService {to: string,filtersStr?: string,ctx?: GSCContext,- ): Promise<{- impressionsByPosition: {- key: ImpressionPositionBucketKey- label: string- impressions: number- percentage: number- }[]- organicPositions: OrganicPositionSeriesEntry[]- }> {+ ): Promise<PositionAnalytics> {const gsc = ctx || (await this.getGSCContext(pid))const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)@@ -1121,16 +1135,15 @@ export class GSCService {this.getTopCountries(pid, from, to, 50, 0, filtersStr, ctx),this.getTopDevices(pid, from, to, 50, 0, filtersStr, ctx),shouldLoadExpensiveAnalytics- ? this.getBrandedTraffic(pid, from, to, filtersStr, ctx).catch(() => ({- branded: 0,- nonBranded: 0,- }))- : Promise.resolve({ branded: 0, nonBranded: 0 }),+ ? this.getBrandedTraffic(pid, from, to, filtersStr, ctx).catch(+ () => SKIPPED_BRANDED_ANALYTICS,+ )+ : Promise.resolve(SKIPPED_BRANDED_ANALYTICS),shouldLoadExpensiveAnalytics- ? this.getPositionAnalytics(pid, from, to, filtersStr, ctx).catch(() =>- emptyPositionAnalytics(),+ ? this.getPositionAnalytics(pid, from, to, filtersStr, ctx).catch(+ () => SKIPPED_POSITION_ANALYTICS,)- : Promise.resolve(emptyPositionAnalytics()),+ : Promise.resolve(SKIPPED_POSITION_ANALYTICS),])return {@@ -1143,6 +1156,7 @@ export class GSCService {topCountries,topDevices,brandedTraffic,+ positionAnalyticsSkipped: 'skipped' in positionAnalytics,impressionsByPosition: positionAnalytics.impressionsByPosition,organicPositions: positionAnalytics.organicPositions,}diff --git a/backend/apps/community/src/project/gsc.service.ts b/backend/apps/community/src/project/gsc.service.tsindex 9ad84452e..5b1a7885c 100644--- a/backend/apps/community/src/project/gsc.service.ts+++ b/backend/apps/community/src/project/gsc.service.ts@@ -65,6 +65,38 @@ type OrganicPositionSeriesEntry = { date: string } & Record<OrganicPositionBucketKey,number>+type BrandedTrafficAnalytics = { branded: number; nonBranded: number }+type SkippedBrandedAnalytics = {+ skipped: true+ branded: null+ nonBranded: null+}+type PositionAnalytics = {+ impressionsByPosition: {+ key: ImpressionPositionBucketKey+ label: string+ impressions: number+ percentage: number+ }[]+ organicPositions: OrganicPositionSeriesEntry[]+}+type SkippedPositionAnalytics = {+ skipped: true+ impressionsByPosition: null+ organicPositions: null+}++const SKIPPED_BRANDED_ANALYTICS: SkippedBrandedAnalytics = {+ skipped: true,+ branded: null,+ nonBranded: null,+}++const SKIPPED_POSITION_ANALYTICS: SkippedPositionAnalytics = {+ skipped: true,+ impressionsByPosition: null,+ organicPositions: null,+}const initialImpressionPositionBuckets = (): Record<ImpressionPositionBucketKey,@@ -87,16 +119,6 @@ const initialOrganicPositionEntry = (pos51Plus: 0,})-const emptyPositionAnalytics = () => ({- impressionsByPosition: IMPRESSION_POSITION_BUCKETS.map(({ key, label }) => ({- key,- label,- impressions: 0,- percentage: 0,- })),- organicPositions: [] as OrganicPositionSeriesEntry[],-})-const getImpressionPositionBucketKey = (position: number,): ImpressionPositionBucketKey => {@@ -969,7 +991,7 @@ export class GSCService {to: string,filtersStr?: string,ctx?: GSCContext,- ): Promise<{ branded: number; nonBranded: number }> {+ ): Promise<BrandedTrafficAnalytics> {const brandKeywords = await this.getProjectBrandKeywords(pid)if (_isEmpty(brandKeywords)) {return { branded: 0, nonBranded: 0 }@@ -1026,15 +1048,7 @@ export class GSCService {to: string,filtersStr?: string,ctx?: GSCContext,- ): Promise<{- impressionsByPosition: {- key: ImpressionPositionBucketKey- label: string- impressions: number- percentage: number- }[]- organicPositions: OrganicPositionSeriesEntry[]- }> {+ ): Promise<PositionAnalytics> {const gsc = ctx || (await this.getGSCContext(pid))const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)@@ -1186,16 +1200,15 @@ export class GSCService {this.getTopCountries(pid, from, to, 50, 0, filtersStr, ctx),this.getTopDevices(pid, from, to, 50, 0, filtersStr, ctx),shouldLoadExpensiveAnalytics- ? this.getBrandedTraffic(pid, from, to, filtersStr, ctx).catch(() => ({- branded: 0,- nonBranded: 0,- }))- : Promise.resolve({ branded: 0, nonBranded: 0 }),+ ? this.getBrandedTraffic(pid, from, to, filtersStr, ctx).catch(+ () => SKIPPED_BRANDED_ANALYTICS,+ )+ : Promise.resolve(SKIPPED_BRANDED_ANALYTICS),shouldLoadExpensiveAnalytics- ? this.getPositionAnalytics(pid, from, to, filtersStr, ctx).catch(() =>- emptyPositionAnalytics(),+ ? this.getPositionAnalytics(pid, from, to, filtersStr, ctx).catch(+ () => SKIPPED_POSITION_ANALYTICS,)- : Promise.resolve(emptyPositionAnalytics()),+ : Promise.resolve(SKIPPED_POSITION_ANALYTICS),])return {@@ -1208,6 +1221,7 @@ export class GSCService {topCountries,topDevices,brandedTraffic,+ positionAnalyticsSkipped: 'skipped' in positionAnalytics,impressionsByPosition: positionAnalytics.impressionsByPosition,organicPositions: positionAnalytics.organicPositions,}diff --git a/web/app/api/api.server.ts b/web/app/api/api.server.tsindex 58bed3dd1..f52b01b96 100644--- a/web/app/api/api.server.ts+++ b/web/app/api/api.server.ts@@ -34,8 +34,12 @@ export function getClientIP(request: Request): string | null {async function fetchWithTimeout(input: string | URL,init: Parameters<typeof fetch>[1] = {},- timeoutMs = 15000,+ timeoutMs?: number,): Promise<Response> {+ if (timeoutMs === undefined) {+ return fetch(input, init)+ }+const controller = new AbortController()const id = setTimeout(() => controller.abort(), timeoutMs)return fetch(input, { ...init, signal: controller.signal }).finally(() =>@@ -219,6 +223,7 @@ export async function streamingServerFetch(body,headers: customHeaders = {},skipAuth = false,+ timeoutMs,} = optionsconst cleanEndpoint = endpoint.startsWith('/') ? endpoint.slice(1) : endpoint@@ -246,11 +251,17 @@ export async function streamingServerFetch(}}- const response = await fetch(url, {- method,- headers: fetchHeaders,- body: body ? JSON.stringify(body) : undefined,- })+ const serializedBody = body ? JSON.stringify(body) : undefined++ const response = await fetchWithTimeout(+ url,+ {+ method,+ headers: fetchHeaders,+ body: serializedBody,+ },+ timeoutMs,+ )// If 401/403, try to refresh the token and retryif ((response.status === 401 || response.status === 403) && !skipAuth) {@@ -260,11 +271,15 @@ export async function streamingServerFetch(fetchHeaders['Authorization'] =`Bearer ${refreshResult.tokens.accessToken}`- const retryResponse = await fetch(url, {- method,- headers: fetchHeaders,- body: body ? JSON.stringify(body) : undefined,- })+ const retryResponse = await fetchWithTimeout(+ url,+ {+ method,+ headers: fetchHeaders,+ body: serializedBody,+ },+ timeoutMs,+ )// If the response is not ok, we still want to propagate the refreshed cookiesconst finalResponse = new Response(retryResponse.body, retryResponse)@@ -2750,6 +2765,18 @@ type GSCOrganicPositionEntry = { date: string } & Record<number>+interface GSCBrandedTraffic {+ branded: number+ nonBranded: number+ skipped?: false+}++interface GSCSkippedBrandedTraffic {+ skipped: true+ branded: null+ nonBranded: null+}+export interface GSCDashboardResponse {notConnected?: booleannoProperty?: boolean@@ -2770,12 +2797,10 @@ export interface GSCDashboardResponse {topQueries?: GSCTopQueryEntry[]topCountries?: GSCTopCountryEntry[]topDevices?: GSCTopDeviceEntry[]- brandedTraffic?: {- branded: number- nonBranded: number- }- impressionsByPosition?: GSCImpressionsByPositionEntry[]- organicPositions?: GSCOrganicPositionEntry[]+ brandedTraffic?: GSCBrandedTraffic | GSCSkippedBrandedTraffic+ positionAnalyticsSkipped?: boolean+ impressionsByPosition?: GSCImpressionsByPositionEntry[] | null+ organicPositions?: GSCOrganicPositionEntry[] | null}export async function getGSCDashboardServer(diff --git a/web/app/pages/Project/tabs/SEO/SEOView.tsx b/web/app/pages/Project/tabs/SEO/SEOView.tsxindex ea08fab3a..8ecba1dcc 100644--- a/web/app/pages/Project/tabs/SEO/SEOView.tsx+++ b/web/app/pages/Project/tabs/SEO/SEOView.tsx@@ -411,10 +411,15 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {[data?.topDevices],)- const brandedTraffic = useMemo(- () => data?.brandedTraffic || { branded: 0, nonBranded: 0 },- [data?.brandedTraffic],- )+ const isBrandedTrafficSkipped = data?.brandedTraffic?.skipped === true++ const brandedTraffic = useMemo(() => {+ if (!data?.brandedTraffic || data.brandedTraffic.skipped) {+ return { branded: 0, nonBranded: 0 }+ }++ return data.brandedTraffic+ }, [data?.brandedTraffic])const donutChartOptions = useMemo(() =>@@ -437,6 +442,11 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {[data?.organicPositions],)+ const isPositionAnalyticsSkipped =+ data?.positionAnalyticsSkipped === true ||+ data?.impressionsByPosition === null ||+ data?.organicPositions === null+const hasImpressionsByPositionData = useMemo(() => impressionsByPosition.some((bucket) => bucket.impressions > 0),[impressionsByPosition],@@ -903,7 +913,9 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {{t('project.seo.brandedTraffic')}</Text></div>- {brandedTraffic.branded + brandedTraffic.nonBranded > 0 ? (+ {isBrandedTrafficSkipped ? (+ <PanelEmptyState message={t('project.seo.analyticsSkipped')} />+ ) : brandedTraffic.branded + brandedTraffic.nonBranded > 0 ? (<BillboardChartoptions={donutChartOptions}className='[&_.bb-chart-arc]:text-xs [&_.bb-chart-arcs-title]:fill-gray-900 [&_.bb-chart-arcs-title]:text-lg [&_.bb-chart-arcs-title]:font-semibold dark:[&_.bb-chart-arcs-title]:fill-gray-100'@@ -922,7 +934,9 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {type='impressionsByPosition'contentClassName='relative flex min-h-[21rem] flex-col overflow-hidden'>- {hasImpressionsByPositionData ? (+ {isPositionAnalyticsSkipped ? (+ <PanelEmptyState message={t('project.seo.analyticsSkipped')} />+ ) : hasImpressionsByPositionData ? (<BillboardChartoptions={impressionsByPositionOptions}className='seo-impressions-position-chart min-h-72 flex-1 [&_svg]:overflow-visible!'@@ -939,7 +953,9 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {type='organicPositions'contentClassName='relative flex min-h-[21rem] flex-col overflow-hidden'>- {hasOrganicPositions ? (+ {isPositionAnalyticsSkipped ? (+ <PanelEmptyState message={t('project.seo.analyticsSkipped')} />+ ) : hasOrganicPositions ? (<BillboardChartoptions={organicPositionsOptions}className='min-h-80 flex-1 [&_svg]:overflow-visible!'diff --git a/web/public/locales/en.json b/web/public/locales/en.jsonindex 0adb57844..5deac2f75 100644--- a/web/public/locales/en.json+++ b/web/public/locales/en.json@@ -1783,6 +1783,7 @@"brandedTraffic": "Branded traffic","others": "Others","noBrandData": "No query data",+ "analyticsSkipped": "Skipped for this date range","impressionsByPosition": "Impressions by position","organicPositions": "Organic positions","allPositions": "All positions",