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!

Simplify code

Blue Mousedfe162bbfe2b
diff --git a/web/app/pages/Project/View/ViewProject.tsx b/web/app/pages/Project/View/ViewProject.tsx
index 7ab5a9eb4..35707229e 100644
--- a/web/app/pages/Project/View/ViewProject.tsx
+++ b/web/app/pages/Project/View/ViewProject.tsx
@@ -3933,6 +3933,7 @@ const ViewProjectContent = () => {
}
if (type === 'traffic-sources') {
+ const hasRefNameFilter = filters.some((f) => f.column === 'refn')
const trafficSourcesTabs = [
{ id: 'ref', label: t('project.mapping.ref') },
!isSelfhosted && { id: 'keywords', label: t('project.mapping.keywords') },
@@ -3961,7 +3962,6 @@ const ViewProjectContent = () => {
getFilterLink={(column: string, value: string) => {
if (panelsActiveTabs.source === 'ref') {
// If grouped by name/domain (no refn filter active) -> filter by refn
- const hasRefNameFilter = filters.some((f) => f.column === 'refn')
return getFilterLink(hasRefNameFilter ? 'ref' : 'refn', value)
}
return getFilterLink(column, value)
@@ -3980,7 +3980,6 @@ const ViewProjectContent = () => {
? keywords
: panelsActiveTabs.source === 'ref'
? (() => {
- const hasRefNameFilter = filters.some((f) => f.column === 'refn')
const raw = panelsData.data?.ref || []
return hasRefNameFilter
? (raw as unknown as Entry[])
diff --git a/web/app/pages/Project/View/components/RefRow.tsx b/web/app/pages/Project/View/components/RefRow.tsx
index d81b6627a..2811ae408 100644
--- a/web/app/pages/Project/View/components/RefRow.tsx
+++ b/web/app/pages/Project/View/components/RefRow.tsx
@@ -2,31 +2,24 @@ import { LinkIcon } from 'lucide-react'
import { useMemo } from 'react'
import { useTranslation } from 'react-i18next'
-import { REFERRER_MAP, extractHostname } from '~/utils/referrers'
+import { getFaviconHost } from '~/utils/referrers'
const RefRow = ({ rowName }: { rowName: string | null }) => {
const { t } = useTranslation('common')
const { isUrl, faviconHost } = useMemo(() => {
if (!rowName) return { isUrl: false, faviconHost: null as string | null }
-
+ let isUrl = false
try {
- const urlObj = new URL(rowName)
- return { isUrl: true, faviconHost: urlObj.hostname }
+ // If it parses, we treat it as a URL for linking
+ // Favicon host will be derived by the util anyway
+ // from either the URL, a hostname string or a mapped group name
+ new URL(rowName)
+ isUrl = true
} catch {
- const hostFromValue = extractHostname(rowName)
- if (hostFromValue) {
- return { isUrl: false, faviconHost: hostFromValue }
- }
-
- // Find first literal domain pattern
- const mapping = REFERRER_MAP.find((m) => (rowName || '').toLowerCase() === m.name.toLowerCase())
- if (mapping) {
- return { isUrl: false, faviconHost: mapping.patterns[0] }
- }
-
- return { isUrl: false, faviconHost: null as string | null }
+ isUrl = false
}
+ return { isUrl, faviconHost: getFaviconHost(rowName) }
}, [rowName])
const linkClassName = 'text-blue-600 hover:underline dark:text-blue-500'
diff --git a/web/app/utils/referrers.ts b/web/app/utils/referrers.ts
index 0f8134f61..a0698b22d 100644
--- a/web/app/utils/referrers.ts
+++ b/web/app/utils/referrers.ts
@@ -47,7 +47,7 @@ const matchByMap = (host: string): string | null => {
return null
}
-const getCanonicalRefGroup = (refValue: string | null | undefined): { key: string; name: string } | null => {
+export const getCanonicalRefGroup = (refValue: string | null | undefined): { key: string; name: string } | null => {
const value = (refValue || '').trim()
// If a non-http(s) scheme is present, keep as-is (do not group)
const scheme = value.match(/^([a-z][a-z0-9+.-]*):/i)?.[1]
@@ -89,3 +89,23 @@ export const groupRefEntries = (entries: Entry[]): Entry[] => {
// Sort desc by count
return list.sort((a, b) => b.count - a.count)
}
+
+// Best-effort favicon host derivation for a ref row value (URL, host or mapped group name)
+export const getFaviconHost = (value: string | null | undefined): string | null => {
+ if (!value) return null
+ // 1) Direct URL -> hostname
+ try {
+ const urlObj = new URL(value)
+ return urlObj.hostname
+ } catch {
+ // not a URL
+ }
+ // 2) Try to extract hostname from non-URL strings
+ const hostFromValue = extractHostname(value)
+ if (hostFromValue) return hostFromValue
+ // 3) If value is a mapped referrer group name, use the first domain-like pattern
+ const mapping = REFERRER_MAP.find((m) => value.toLowerCase() === m.name.toLowerCase())
+ if (!mapping) return null
+ const domainLike = mapping.patterns.find((p) => !p.includes('://') && p.includes('.'))
+ return domainLike || null
+}