Skip to content

📈 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!

Fix SEO performance issues for larger time periods

Blue Mouse3f3b11174e63
diff --git a/backend/apps/cloud/src/project/gsc.service.ts b/backend/apps/cloud/src/project/gsc.service.ts
index ed388c712..5afdcdf42 100644
--- a/backend/apps/cloud/src/project/gsc.service.ts
+++ b/backend/apps/cloud/src/project/gsc.service.ts
@@ -41,7 +41,9 @@ const MAX_ROW_LIMIT = 25000
const MAX_BRANDED_PAGES = 10
const MAX_FILTER_EXPRESSION_LENGTH = 500
const MAX_FILTERS = 20
-const MAX_EXPENSIVE_ANALYTICS_RANGE_DAYS = 180
+const MAX_BRANDED_TRAFFIC_RANGE_DAYS = 31
+const MAX_POSITION_ANALYTICS_RANGE_DAYS = 31
+const OPTIONAL_ANALYTICS_TIMEOUT_MS = 12000
const IMPRESSION_POSITION_BUCKETS = [
{ key: 'pos1To3', label: '1-3' },
@@ -138,6 +140,31 @@ const getOrganicPositionBucketKey = (
return 'pos51Plus'
}
+const resolveOptionalAnalytics = async <T>(
+ promise: Promise<T>,
+ fallback: T,
+): Promise<T> => {
+ let timeout: ReturnType<typeof setTimeout> | undefined
+
+ try {
+ return await Promise.race([
+ promise,
+ new Promise<T>((resolve) => {
+ timeout = setTimeout(
+ () => resolve(fallback),
+ OPTIONAL_ANALYTICS_TIMEOUT_MS,
+ )
+ }),
+ ])
+ } catch {
+ return fallback
+ } finally {
+ if (timeout) {
+ clearTimeout(timeout)
+ }
+ }
+}
+
type GSCRow = {
keys: string[]
clicks?: number
@@ -767,6 +794,7 @@ export class GSCService {
const gsc = ctx || (await this.getGSCContext(pid))
const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)
const isHourly = timeBucket === 'hour'
+ const isMonthly = timeBucket === 'month'
try {
const rows = await this.queryGSC(gsc, from, to, {
@@ -776,6 +804,51 @@ export class GSCService {
dimensionFilterGroups,
})
+ if (isMonthly) {
+ const monthlySeries = new Map<
+ string,
+ { clicks: number; impressions: number; weightedPosition: number }
+ >()
+
+ for (const row of rows) {
+ const rawDate = row.keys?.[0]
+ if (!rawDate) continue
+
+ const date = dayjs.utc(rawDate).format('YYYY-MM-01')
+ const clicks = row.clicks || 0
+ const impressions = row.impressions || 0
+ const position = row.position || 0
+ const existing = monthlySeries.get(date) || {
+ clicks: 0,
+ impressions: 0,
+ weightedPosition: 0,
+ }
+
+ existing.clicks += clicks
+ existing.impressions += impressions
+ existing.weightedPosition += position * impressions
+ monthlySeries.set(date, existing)
+ }
+
+ return Array.from(monthlySeries.entries())
+ .sort(([dateA], [dateB]) => dateA.localeCompare(dateB))
+ .map(([date, entry]) => ({
+ date,
+ clicks: Math.round(entry.clicks),
+ impressions: Math.round(entry.impressions),
+ ctr:
+ entry.impressions > 0
+ ? Number(((entry.clicks / entry.impressions) * 100).toFixed(2))
+ : 0,
+ position:
+ entry.impressions > 0
+ ? Number(
+ (entry.weightedPosition / entry.impressions).toFixed(1),
+ )
+ : 0,
+ }))
+ }
+
return rows.map((row) => ({
date: row.keys?.[0]
? isHourly
@@ -992,7 +1065,12 @@ export class GSCService {
const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)
try {
- const rows: GSCRow[] = []
+ const impressionBuckets = initialImpressionPositionBuckets()
+ const organicPositionsByDate = new Map<
+ string,
+ OrganicPositionSeriesEntry
+ >()
+ let totalImpressions = 0
let startRow = 0
while (true) {
@@ -1003,43 +1081,35 @@ export class GSCService {
dimensionFilterGroups,
})
- rows.push(...page)
-
- if (page.length < MAX_ROW_LIMIT) break
-
- startRow += MAX_ROW_LIMIT
- }
+ for (const row of page) {
+ const position = Number(row.position || 0)
+ const impressions = Math.round(row.impressions || 0)
+ const date = row.keys?.[0]
+
+ if (
+ !date ||
+ !Number.isFinite(position) ||
+ position <= 0 ||
+ impressions <= 0
+ ) {
+ continue
+ }
- const impressionBuckets = initialImpressionPositionBuckets()
- const organicPositionsByDate = new Map<
- string,
- OrganicPositionSeriesEntry
- >()
- let totalImpressions = 0
+ const impressionBucket = getImpressionPositionBucketKey(position)
+ impressionBuckets[impressionBucket] += impressions
+ totalImpressions += impressions
- for (const row of rows) {
- const position = Number(row.position || 0)
- const impressions = Math.round(row.impressions || 0)
- const date = row.keys?.[0]
-
- if (
- !date ||
- !Number.isFinite(position) ||
- position <= 0 ||
- impressions <= 0
- ) {
- continue
+ const organicBucket = getOrganicPositionBucketKey(position)
+ const organicEntry =
+ organicPositionsByDate.get(date) ||
+ initialOrganicPositionEntry(date)
+ organicEntry[organicBucket] += 1
+ organicPositionsByDate.set(date, organicEntry)
}
- const impressionBucket = getImpressionPositionBucketKey(position)
- impressionBuckets[impressionBucket] += impressions
- totalImpressions += impressions
+ if (page.length < MAX_ROW_LIMIT) break
- const organicBucket = getOrganicPositionBucketKey(position)
- const organicEntry =
- organicPositionsByDate.get(date) || initialOrganicPositionEntry(date)
- organicEntry[organicBucket] += 1
- organicPositionsByDate.set(date, organicEntry)
+ startRow += MAX_ROW_LIMIT
}
const organicPositions: OrganicPositionSeriesEntry[] = []
@@ -1107,8 +1177,9 @@ export class GSCService {
const toDate = dayjs(to)
const durationMs = toDate.diff(fromDate)
const rangeDays = Math.abs(toDate.diff(fromDate, 'day'))
- const shouldLoadExpensiveAnalytics =
- rangeDays <= MAX_EXPENSIVE_ANALYTICS_RANGE_DAYS
+ const shouldLoadBrandedTraffic = rangeDays <= MAX_BRANDED_TRAFFIC_RANGE_DAYS
+ const shouldLoadPositionAnalytics =
+ rangeDays <= MAX_POSITION_ANALYTICS_RANGE_DAYS
const prevTo = fromDate
.subtract(1, 'millisecond')
.format('YYYY-MM-DD HH:mm:ss')
@@ -1134,14 +1205,20 @@ export class GSCService {
this.getKeywords(pid, from, to, 50, 0, filtersStr, undefined, ctx),
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(
- () => SKIPPED_BRANDED_ANALYTICS,
+ shouldLoadBrandedTraffic
+ ? resolveOptionalAnalytics<
+ BrandedTrafficAnalytics | SkippedBrandedAnalytics
+ >(
+ this.getBrandedTraffic(pid, from, to, filtersStr, ctx),
+ SKIPPED_BRANDED_ANALYTICS,
)
: Promise.resolve(SKIPPED_BRANDED_ANALYTICS),
- shouldLoadExpensiveAnalytics
- ? this.getPositionAnalytics(pid, from, to, filtersStr, ctx).catch(
- () => SKIPPED_POSITION_ANALYTICS,
+ shouldLoadPositionAnalytics
+ ? resolveOptionalAnalytics<
+ PositionAnalytics | SkippedPositionAnalytics
+ >(
+ this.getPositionAnalytics(pid, from, to, filtersStr, ctx),
+ SKIPPED_POSITION_ANALYTICS,
)
: Promise.resolve(SKIPPED_POSITION_ANALYTICS),
])
diff --git a/backend/apps/community/src/project/gsc.service.ts b/backend/apps/community/src/project/gsc.service.ts
index 5b1a7885c..2ef5b42ef 100644
--- a/backend/apps/community/src/project/gsc.service.ts
+++ b/backend/apps/community/src/project/gsc.service.ts
@@ -41,7 +41,9 @@ const MAX_ROW_LIMIT = 25000
const MAX_BRANDED_PAGES = 10
const MAX_FILTER_EXPRESSION_LENGTH = 500
const MAX_FILTERS = 20
-const MAX_EXPENSIVE_ANALYTICS_RANGE_DAYS = 180
+const MAX_BRANDED_TRAFFIC_RANGE_DAYS = 31
+const MAX_POSITION_ANALYTICS_RANGE_DAYS = 31
+const OPTIONAL_ANALYTICS_TIMEOUT_MS = 12000
const IMPRESSION_POSITION_BUCKETS = [
{ key: 'pos1To3', label: '1-3' },
@@ -138,6 +140,31 @@ const getOrganicPositionBucketKey = (
return 'pos51Plus'
}
+const resolveOptionalAnalytics = async <T>(
+ promise: Promise<T>,
+ fallback: T,
+): Promise<T> => {
+ let timeout: ReturnType<typeof setTimeout> | undefined
+
+ try {
+ return await Promise.race([
+ promise,
+ new Promise<T>((resolve) => {
+ timeout = setTimeout(
+ () => resolve(fallback),
+ OPTIONAL_ANALYTICS_TIMEOUT_MS,
+ )
+ }),
+ ])
+ } catch {
+ return fallback
+ } finally {
+ if (timeout) {
+ clearTimeout(timeout)
+ }
+ }
+}
+
type GSCRow = {
keys: string[]
clicks?: number
@@ -828,6 +855,7 @@ export class GSCService {
const gsc = ctx || (await this.getGSCContext(pid))
const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)
const isHourly = timeBucket === 'hour'
+ const isMonthly = timeBucket === 'month'
try {
const rows = await this.queryGSC(gsc, from, to, {
@@ -837,6 +865,51 @@ export class GSCService {
dimensionFilterGroups,
})
+ if (isMonthly) {
+ const monthlySeries = new Map<
+ string,
+ { clicks: number; impressions: number; weightedPosition: number }
+ >()
+
+ for (const row of rows) {
+ const rawDate = row.keys?.[0]
+ if (!rawDate) continue
+
+ const date = dayjs.utc(rawDate).format('YYYY-MM-01')
+ const clicks = row.clicks || 0
+ const impressions = row.impressions || 0
+ const position = row.position || 0
+ const existing = monthlySeries.get(date) || {
+ clicks: 0,
+ impressions: 0,
+ weightedPosition: 0,
+ }
+
+ existing.clicks += clicks
+ existing.impressions += impressions
+ existing.weightedPosition += position * impressions
+ monthlySeries.set(date, existing)
+ }
+
+ return Array.from(monthlySeries.entries())
+ .sort(([dateA], [dateB]) => dateA.localeCompare(dateB))
+ .map(([date, entry]) => ({
+ date,
+ clicks: Math.round(entry.clicks),
+ impressions: Math.round(entry.impressions),
+ ctr:
+ entry.impressions > 0
+ ? Number(((entry.clicks / entry.impressions) * 100).toFixed(2))
+ : 0,
+ position:
+ entry.impressions > 0
+ ? Number(
+ (entry.weightedPosition / entry.impressions).toFixed(1),
+ )
+ : 0,
+ }))
+ }
+
return rows.map((row) => ({
date: row.keys?.[0]
? isHourly
@@ -1053,7 +1126,12 @@ export class GSCService {
const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)
try {
- const rows: GSCRow[] = []
+ const impressionBuckets = initialImpressionPositionBuckets()
+ const organicPositionsByDate = new Map<
+ string,
+ OrganicPositionSeriesEntry
+ >()
+ let totalImpressions = 0
let startRow = 0
while (true) {
@@ -1064,43 +1142,35 @@ export class GSCService {
dimensionFilterGroups,
})
- rows.push(...page)
-
- if (page.length < MAX_ROW_LIMIT) break
-
- startRow += MAX_ROW_LIMIT
- }
+ for (const row of page) {
+ const position = Number(row.position || 0)
+ const impressions = Math.round(row.impressions || 0)
+ const date = row.keys?.[0]
+
+ if (
+ !date ||
+ !Number.isFinite(position) ||
+ position <= 0 ||
+ impressions <= 0
+ ) {
+ continue
+ }
- const impressionBuckets = initialImpressionPositionBuckets()
- const organicPositionsByDate = new Map<
- string,
- OrganicPositionSeriesEntry
- >()
- let totalImpressions = 0
+ const impressionBucket = getImpressionPositionBucketKey(position)
+ impressionBuckets[impressionBucket] += impressions
+ totalImpressions += impressions
- for (const row of rows) {
- const position = Number(row.position || 0)
- const impressions = Math.round(row.impressions || 0)
- const date = row.keys?.[0]
-
- if (
- !date ||
- !Number.isFinite(position) ||
- position <= 0 ||
- impressions <= 0
- ) {
- continue
+ const organicBucket = getOrganicPositionBucketKey(position)
+ const organicEntry =
+ organicPositionsByDate.get(date) ||
+ initialOrganicPositionEntry(date)
+ organicEntry[organicBucket] += 1
+ organicPositionsByDate.set(date, organicEntry)
}
- const impressionBucket = getImpressionPositionBucketKey(position)
- impressionBuckets[impressionBucket] += impressions
- totalImpressions += impressions
+ if (page.length < MAX_ROW_LIMIT) break
- const organicBucket = getOrganicPositionBucketKey(position)
- const organicEntry =
- organicPositionsByDate.get(date) || initialOrganicPositionEntry(date)
- organicEntry[organicBucket] += 1
- organicPositionsByDate.set(date, organicEntry)
+ startRow += MAX_ROW_LIMIT
}
const organicPositions: OrganicPositionSeriesEntry[] = []
@@ -1172,8 +1242,9 @@ export class GSCService {
const toDate = dayjs(to)
const durationMs = toDate.diff(fromDate)
const rangeDays = Math.abs(toDate.diff(fromDate, 'day'))
- const shouldLoadExpensiveAnalytics =
- rangeDays <= MAX_EXPENSIVE_ANALYTICS_RANGE_DAYS
+ const shouldLoadBrandedTraffic = rangeDays <= MAX_BRANDED_TRAFFIC_RANGE_DAYS
+ const shouldLoadPositionAnalytics =
+ rangeDays <= MAX_POSITION_ANALYTICS_RANGE_DAYS
const prevTo = fromDate
.subtract(1, 'millisecond')
.format('YYYY-MM-DD HH:mm:ss')
@@ -1199,14 +1270,20 @@ export class GSCService {
this.getKeywords(pid, from, to, 50, 0, filtersStr, undefined, ctx),
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(
- () => SKIPPED_BRANDED_ANALYTICS,
+ shouldLoadBrandedTraffic
+ ? resolveOptionalAnalytics<
+ BrandedTrafficAnalytics | SkippedBrandedAnalytics
+ >(
+ this.getBrandedTraffic(pid, from, to, filtersStr, ctx),
+ SKIPPED_BRANDED_ANALYTICS,
)
: Promise.resolve(SKIPPED_BRANDED_ANALYTICS),
- shouldLoadExpensiveAnalytics
- ? this.getPositionAnalytics(pid, from, to, filtersStr, ctx).catch(
- () => SKIPPED_POSITION_ANALYTICS,
+ shouldLoadPositionAnalytics
+ ? resolveOptionalAnalytics<
+ PositionAnalytics | SkippedPositionAnalytics
+ >(
+ this.getPositionAnalytics(pid, from, to, filtersStr, ctx),
+ SKIPPED_POSITION_ANALYTICS,
)
: Promise.resolve(SKIPPED_POSITION_ANALYTICS),
])