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!

Minor fixes & improvements

Blue Mousecf18500830ef
diff --git a/backend/apps/cloud/src/project/gsc.service.ts b/backend/apps/cloud/src/project/gsc.service.ts
index 10afa470a..e4307a466 100644
--- a/backend/apps/cloud/src/project/gsc.service.ts
+++ b/backend/apps/cloud/src/project/gsc.service.ts
@@ -282,8 +282,8 @@ export class GSCService {
let dimension
if (column === 'pg') dimension = 'page'
else if (column === 'keywords') dimension = 'query'
- else if (column === 'country') dimension = 'country'
- else if (column === 'device') dimension = 'device'
+ else if (column === 'country' || column === 'cc') dimension = 'country'
+ else if (column === 'device' || column === 'dv') dimension = 'device'
else continue
let operator = 'equals'
@@ -310,6 +310,88 @@ export class GSCService {
return undefined
}
+ private getDimensionFilterGroups(
+ filtersStr?: string,
+ extraFilters: Array<{ dimension: string; expression: string }> = [],
+ ) {
+ const baseGroups = this.parseFilters(filtersStr) || []
+
+ if (_isEmpty(extraFilters)) {
+ return baseGroups
+ }
+
+ if (_isEmpty(baseGroups)) {
+ return [{ groupType: 'and', filters: extraFilters }]
+ }
+
+ return baseGroups.map((group, index) =>
+ index === 0
+ ? {
+ ...group,
+ filters: [...(group.filters || []), ...extraFilters],
+ }
+ : group,
+ )
+ }
+
+ private async getProjectBrandKeywords(pid: string): Promise<string[]> {
+ const project = await this.projectService.findOne({
+ where: { id: pid },
+ select: ['name', 'websiteUrl', 'brandKeywords'],
+ })
+
+ if (!project) return []
+
+ if (project.brandKeywords) {
+ try {
+ const parsed = JSON.parse(project.brandKeywords)
+
+ if (Array.isArray(parsed)) {
+ return Array.from(
+ new Set(
+ parsed
+ .map((keyword) =>
+ typeof keyword === 'string'
+ ? keyword.toLowerCase().trim()
+ : '',
+ )
+ .filter(Boolean),
+ ),
+ )
+ }
+ } catch {
+ //
+ }
+ }
+
+ const keywords = new Set<string>()
+
+ if (project.websiteUrl) {
+ try {
+ const url = new URL(project.websiteUrl)
+ const domain = url.hostname.replace(/^www\./, '')
+ const parts = domain.split('.')
+ if (parts.length >= 2) {
+ const main = parts[parts.length - 2]
+ if (main && main.length >= 3) {
+ keywords.add(main.toLowerCase())
+ }
+ }
+ } catch {
+ //
+ }
+ }
+
+ if (project.name) {
+ const name = project.name.toLowerCase().trim()
+ if (name.length >= 3) {
+ keywords.add(name)
+ }
+ }
+
+ return Array.from(keywords)
+ }
+
async getKeywords(
pid: string,
from: string,
@@ -340,14 +422,10 @@ export class GSCService {
const startDate = dayjs(from).format('YYYY-MM-DD')
const endDate = dayjs(to).format('YYYY-MM-DD')
- const dimensionFilterGroups = this.parseFilters(filtersStr) || []
-
- if (page) {
- dimensionFilterGroups.push({
- groupType: 'and',
- filters: [{ dimension: 'page', expression: page }],
- })
- }
+ const dimensionFilterGroups = this.getDimensionFilterGroups(
+ filtersStr,
+ page ? [{ dimension: 'page', expression: page }] : [],
+ )
try {
const { data } = await sc.searchanalytics.query({
@@ -416,7 +494,7 @@ export class GSCService {
const startDate = dayjs(from).format('YYYY-MM-DD')
const endDate = dayjs(to).format('YYYY-MM-DD')
- const dimensionFilterGroups = this.parseFilters(filtersStr) || []
+ const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)
try {
const { data } = await sc.searchanalytics.query({
@@ -475,7 +553,7 @@ export class GSCService {
const startDate = dayjs(from).format('YYYY-MM-DD')
const endDate = dayjs(to).format('YYYY-MM-DD')
- const dimensionFilterGroups = this.parseFilters(filtersStr) || []
+ const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)
const isHourly = timeBucket === 'hour'
try {
@@ -543,14 +621,10 @@ export class GSCService {
const startDate = dayjs(from).format('YYYY-MM-DD')
const endDate = dayjs(to).format('YYYY-MM-DD')
- const dimensionFilterGroups = this.parseFilters(filtersStr) || []
-
- if (query) {
- dimensionFilterGroups.push({
- groupType: 'and',
- filters: [{ dimension: 'query', expression: query }],
- })
- }
+ const dimensionFilterGroups = this.getDimensionFilterGroups(
+ filtersStr,
+ query ? [{ dimension: 'query', expression: query }] : [],
+ )
try {
const { data } = await sc.searchanalytics.query({
@@ -613,7 +687,7 @@ export class GSCService {
const startDate = dayjs(from).format('YYYY-MM-DD')
const endDate = dayjs(to).format('YYYY-MM-DD')
- const dimensionFilterGroups = this.parseFilters(filtersStr) || []
+ const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)
try {
const { data } = await sc.searchanalytics.query({
@@ -676,7 +750,7 @@ export class GSCService {
const startDate = dayjs(from).format('YYYY-MM-DD')
const endDate = dayjs(to).format('YYYY-MM-DD')
- const dimensionFilterGroups = this.parseFilters(filtersStr) || []
+ const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)
try {
const { data } = await sc.searchanalytics.query({
@@ -716,6 +790,80 @@ export class GSCService {
}
}
+ async getBrandedTraffic(
+ pid: string,
+ from: string,
+ to: string,
+ filtersStr?: string,
+ ): Promise<{ branded: number; nonBranded: number }> {
+ const brandKeywords = await this.getProjectBrandKeywords(pid)
+ if (_isEmpty(brandKeywords)) {
+ return { branded: 0, nonBranded: 0 }
+ }
+
+ const tokens = await this.ensurePropertyLinked(pid)
+ const auth = await this.getAuthedClientForPid(pid)
+ const sc = searchconsole({ version: 'v1', auth })
+
+ const startDate = dayjs(from).format('YYYY-MM-DD')
+ const endDate = dayjs(to).format('YYYY-MM-DD')
+ const dimensionFilterGroups = this.getDimensionFilterGroups(filtersStr)
+ const rowLimit = 25000
+ let startRow = 0
+ let branded = 0
+ let nonBranded = 0
+
+ try {
+ while (true) {
+ const { data } = await sc.searchanalytics.query({
+ siteUrl: tokens.property_uri as string,
+ requestBody: {
+ startDate,
+ endDate,
+ dimensions: ['query'],
+ rowLimit,
+ startRow,
+ dataState: 'all',
+ ...(dimensionFilterGroups.length > 0
+ ? { dimensionFilterGroups }
+ : {}),
+ },
+ })
+
+ const rows = (data.rows || []) as Array<{
+ keys: string[]
+ clicks?: number
+ }>
+
+ for (const row of rows) {
+ const query = (row.keys?.[0] || '').toLowerCase()
+ const clicks = row.clicks || 0
+
+ if (brandKeywords.some((keyword) => query.includes(keyword))) {
+ branded += clicks
+ } else {
+ nonBranded += clicks
+ }
+ }
+
+ if (rows.length < rowLimit) {
+ break
+ }
+
+ startRow += rowLimit
+ }
+
+ return {
+ branded: Math.round(branded),
+ nonBranded: Math.round(nonBranded),
+ }
+ } catch {
+ throw new InternalServerErrorException(
+ 'Failed to fetch branded traffic from Search Console',
+ )
+ }
+ }
+
async getDashboard(
pid: string,
from: string,
@@ -749,6 +897,7 @@ export class GSCService {
topQueries,
topCountries,
topDevices,
+ brandedTraffic,
] = await Promise.all([
this.getSummary(pid, from, to, filtersStr),
this.getSummary(pid, prevFrom, prevTo, filtersStr).catch(() => null),
@@ -757,6 +906,7 @@ export class GSCService {
this.getKeywords(pid, from, to, 50, 0, filtersStr),
this.getTopCountries(pid, from, to, 50, 0, filtersStr),
this.getTopDevices(pid, from, to, 50, 0, filtersStr),
+ this.getBrandedTraffic(pid, from, to, filtersStr),
])
return {
@@ -768,6 +918,7 @@ export class GSCService {
topQueries,
topCountries,
topDevices,
+ brandedTraffic,
}
}
}
diff --git a/web/app/api/api.server.ts b/web/app/api/api.server.ts
index 380351e0e..9ab3bac82 100644
--- a/web/app/api/api.server.ts
+++ b/web/app/api/api.server.ts
@@ -2470,6 +2470,10 @@ export interface GSCDashboardResponse {
topQueries?: GSCTopQueryEntry[]
topCountries?: GSCTopCountryEntry[]
topDevices?: GSCTopDeviceEntry[]
+ brandedTraffic?: {
+ branded: number
+ nonBranded: number
+ }
}
export async function getGSCDashboardServer(
diff --git a/web/app/pages/Project/View/ViewProject.tsx b/web/app/pages/Project/View/ViewProject.tsx
index d5a6ac0b5..70ded937b 100644
--- a/web/app/pages/Project/View/ViewProject.tsx
+++ b/web/app/pages/Project/View/ViewProject.tsx
@@ -363,6 +363,7 @@ const ViewProjectContent = () => {
const tnMapping = typeNameMapping(t)
const refCalendar = useRef(null)
const refCalendarCompare = useRef(null)
+ const seoTabId = !isSelfhosted ? PROJECT_TABS.seo : null
const activeTab = useMemo(() => {
const tab = searchParams.get('tab') as keyof typeof PROJECT_TABS
@@ -723,11 +724,15 @@ const ViewProjectContent = () => {
label: t('dashboard.performance'),
icon: GaugeIcon,
},
- {
- id: PROJECT_TABS.seo,
- label: t('project.seo.title'),
- icon: MagnifyingGlassIcon,
- },
+ ...(seoTabId
+ ? [
+ {
+ id: seoTabId,
+ label: t('project.seo.title'),
+ icon: MagnifyingGlassIcon,
+ },
+ ]
+ : []),
{
id: PROJECT_TABS.profiles,
label: t('dashboard.profiles'),
@@ -804,7 +809,7 @@ const ViewProjectContent = () => {
}
return newTabs
- }, [t, projectQueryTabs, allowedToManage])
+ }, [t, projectQueryTabs, allowedToManage, seoTabId])
const activeTabLabel = useMemo(
() => _find(tabs, (tab) => tab.id === activeTab)?.label,
@@ -930,13 +935,13 @@ const ViewProjectContent = () => {
return
}
- if (activeTab === PROJECT_TABS.seo) {
+ if (seoTabId && activeTab === seoTabId) {
setSeoRefreshTrigger((prev) => prev + 1)
return
}
}
},
- [authLoading, dataLoading, activeTab],
+ [authLoading, dataLoading, activeTab, seoTabId],
)
useEffect(() => {
@@ -1549,7 +1554,7 @@ const ViewProjectContent = () => {
{activeTab === PROJECT_TABS.performance ? (
<PerformanceView tnMapping={tnMapping} />
) : null}
- {activeTab === PROJECT_TABS.seo ? (
+ {seoTabId && activeTab === seoTabId ? (
<SEOView projectId={id} tnMapping={tnMapping} />
) : null}
{activeTab === PROJECT_TABS.funnels ? (
diff --git a/web/app/pages/Project/tabs/SEO/SEOView.tsx b/web/app/pages/Project/tabs/SEO/SEOView.tsx
index aab9f32ab..bef52806c 100644
--- a/web/app/pages/Project/tabs/SEO/SEOView.tsx
+++ b/web/app/pages/Project/tabs/SEO/SEOView.tsx
@@ -34,11 +34,11 @@ dayjs.extend(quarterOfYear)
import { useGSCDashboardProxy } from '~/hooks/useAnalyticsProxy'
import type { TimeBucket } from '~/lib/constants'
import type { Entry } from '~/lib/models/Entry'
-import type { Project } from '~/lib/models/Project'
import BillboardChart from '~/ui/BillboardChart'
import DashboardHeader from '~/pages/Project/View/components/DashboardHeader'
import { MainChart } from '~/pages/Project/View/components/MainChart'
import Filters from '~/pages/Project/View/components/Filters'
+import type { Filter } from '~/pages/Project/View/interfaces/traffic'
import { Panel, PanelContainer } from '~/pages/Project/View/Panels'
import {
useViewProjectContext,
@@ -116,9 +116,7 @@ const aggregateDateSeries = (
{
clicks: number
impressions: number
- ctr: number
- position: number
- count: number
+ weightedPosition: number
}
>()
const orderedKeys: string[] = []
@@ -129,17 +127,13 @@ const aggregateDateSeries = (
if (existing) {
existing.clicks += entry.clicks
existing.impressions += entry.impressions
- existing.ctr += entry.ctr
- existing.position += entry.position
- existing.count += 1
+ existing.weightedPosition += entry.position * entry.impressions
} else {
orderedKeys.push(key)
buckets.set(key, {
clicks: entry.clicks,
impressions: entry.impressions,
- ctr: entry.ctr,
- position: entry.position,
- count: 1,
+ weightedPosition: entry.position * entry.impressions,
})
}
}
@@ -150,61 +144,58 @@ const aggregateDateSeries = (
date: key,
clicks: b.clicks,
impressions: b.impressions,
- ctr: Number((b.ctr / b.count).toFixed(2)),
- position: Number((b.position / b.count).toFixed(1)),
+ ctr:
+ b.impressions > 0
+ ? Number(((b.clicks / b.impressions) * 100).toFixed(2))
+ : 0,
+ position:
+ b.impressions > 0
+ ? Number((b.weightedPosition / b.impressions).toFixed(1))
+ : 0,
}
})
}
-const deriveBrandKeywords = (project: Project): string[] => {
- if (project.brandKeywords?.length) {
- return project.brandKeywords.map((k) => k.toLowerCase().trim())
- }
-
- const keywords: string[] = []
-
- if (project.websiteUrl) {
- try {
- const url = new URL(project.websiteUrl)
- const domain = url.hostname.replace(/^www\./, '')
- const parts = domain.split('.')
- if (parts.length >= 2) {
- const main = parts[parts.length - 2]
- if (main && main.length >= 3) keywords.push(main.toLowerCase())
- }
- } catch {
- // invalid URL, skip
+const getGSCCompatibleFilters = (filters: Filter[]): Filter[] => {
+ return filters.flatMap((filter) => {
+ if (filter.column === 'cc') {
+ const alpha3 = countries.alpha2ToAlpha3(filter.filter.toUpperCase())
+ if (!alpha3) return []
+
+ return [
+ {
+ ...filter,
+ column: 'country',
+ filter: alpha3.toLowerCase(),
+ },
+ ]
}
- }
- if (project.name) {
- const name = project.name.toLowerCase().trim()
- if (name.length >= 3 && !keywords.includes(name)) {
- keywords.push(name)
+ if (filter.column === 'dv') {
+ return [
+ {
+ ...filter,
+ column: 'device',
+ filter: filter.filter.toLowerCase(),
+ },
+ ]
}
- }
- return keywords
-}
+ if (filter.column === 'country' || filter.column === 'device') {
+ return [
+ {
+ ...filter,
+ filter: filter.filter.toLowerCase(),
+ },
+ ]
+ }
-const classifyBrandedTraffic = (
- queries: Array<{ name: string; count: number; impressions?: number }>,
- brandKeywords: string[],
-): { branded: number; nonBranded: number } => {
- let branded = 0
- let nonBranded = 0
-
- for (const q of queries) {
- const lower = q.name.toLowerCase()
- const isBranded = brandKeywords.some((kw) => lower.includes(kw))
- if (isBranded) {
- branded += q.count
- } else {
- nonBranded += q.count
+ if (filter.column === 'pg' || filter.column === 'keywords') {
+ return [filter]
}
- }
- return { branded, nonBranded }
+ return []
+ })
}
const COMPACT_MAX_ENTRIES = 10
@@ -343,7 +334,7 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {
i18n: { language },
} = useTranslation('common')
const { theme } = useTheme()
- const { id, project } = useCurrentProject()
+ const { id } = useCurrentProject()
const {
period,
timezone,
@@ -443,6 +434,7 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {
)
const noopFilterLink = useCallback(() => '#', [])
+ const gscFilters = useMemo(() => getGSCCompatibleFilters(filters), [filters])
const [isManualRefreshing, setIsManualRefreshing] = useState(false)
@@ -458,7 +450,7 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {
to,
timezone,
timeBucket,
- filters,
+ filters: gscFilters,
})
}, [
fetchDashboard,
@@ -468,7 +460,7 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {
to,
timezone,
timeBucket,
- filters,
+ gscFilters,
])
useEffect(() => {
@@ -576,14 +568,9 @@ const SEOViewInner = ({ projectId, tnMapping }: SEOViewProps) => {
[data?.topDevices],
)
- const brandKeywords = useMemo(
- () => (project ? deriveBrandKeywords(project) : []),
- [project],
- )
-
const brandedTraffic = useMemo(
- () => classifyBrandedTraffic(data?.topQueries || [], brandKeywords),
- [data?.topQueries, brandKeywords],
+ () => data?.brandedTraffic || { branded: 0, nonBranded: 0 },
+ [data?.brandedTraffic],
)
const donutChartOptions: ChartOptions = useMemo(() => {
diff --git a/web/app/routes/projects.$id.tsx b/web/app/routes/projects.$id.tsx
index b32fe45ec..3a4e1cbd5 100644
--- a/web/app/routes/projects.$id.tsx
+++ b/web/app/routes/projects.$id.tsx
@@ -48,6 +48,7 @@ import {
PROJECT_TABS,
getProjectOgImageUrl,
getValidTimeBucket,
+ isSelfhosted,
type Period,
} from '~/lib/constants'
import { Project } from '~/lib/models/Project'
@@ -164,10 +165,11 @@ export const meta: MetaFunction<typeof loader> = ({ data, location }) => {
const tab = (searchParams.get('tab') || PROJECT_TABS.traffic) as
| keyof typeof PROJECT_TABS
| 'settings'
- const tabNames: Record<keyof typeof PROJECT_TABS | 'settings', string> = {
+ const seoTabId = !isSelfhosted ? PROJECT_TABS.seo : null
+ const tabNames: Record<string, string> = {
[PROJECT_TABS.traffic]: t('dashboard.traffic'),
[PROJECT_TABS.performance]: t('dashboard.performance'),
- [PROJECT_TABS.seo]: t('project.seo.title'),
+ ...(seoTabId ? { [seoTabId]: t('project.seo.title') } : {}),
[PROJECT_TABS.profiles]: t('dashboard.profiles'),
[PROJECT_TABS.sessions]: t('dashboard.sessions'),
[PROJECT_TABS.errors]: t('dashboard.errors'),
@@ -175,9 +177,13 @@ export const meta: MetaFunction<typeof loader> = ({ data, location }) => {
[PROJECT_TABS.goals]: t('dashboard.goals'),
[PROJECT_TABS.featureFlags]: t('dashboard.featureFlags'),
[PROJECT_TABS.captcha]: t('common.captcha'),
- [PROJECT_TABS.ai]: t('dashboard.askAi'),
- [PROJECT_TABS.alerts]: t('dashboard.alerts'),
- [PROJECT_TABS.experiments]: t('dashboard.experiments'),
+ ...(PROJECT_TABS.ai ? { [PROJECT_TABS.ai]: t('dashboard.askAi') } : {}),
+ ...(PROJECT_TABS.alerts
+ ? { [PROJECT_TABS.alerts]: t('dashboard.alerts') }
+ : {}),
+ ...(PROJECT_TABS.experiments
+ ? { [PROJECT_TABS.experiments]: t('dashboard.experiments') }
+ : {}),
settings: t('common.settings'),
}
const tabName = tabNames[tab] || tabNames[PROJECT_TABS.traffic]
@@ -322,6 +328,7 @@ export interface ProjectLoaderData {
export async function loader({ request, params }: LoaderFunctionArgs) {
const { id: projectId } = params
const url = new URL(request.url)
+ const seoTabId = !isSelfhosted ? PROJECT_TABS.seo : null
const passwordFromQuery = url.searchParams.get('password') || ''
const passwordFromCookie = getProjectPasswordCookie(request, projectId || '')
@@ -479,7 +486,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
customEvents,
).then((res) => res.data)
}
- } else if (tab === PROJECT_TABS.seo) {
+ } else if (seoTabId && tab === seoTabId) {
// Fetch traffic data for referral breakdowns (search engines / AI referrals)
trafficData = getProjectDataServer(
request,