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!

respect hostname-navigation period

Blue Mouse355257022174
diff --git a/backend/apps/cloud/src/project/project.controller.ts b/backend/apps/cloud/src/project/project.controller.ts
index 919d6ed50..0009bb965 100644
--- a/backend/apps/cloud/src/project/project.controller.ts
+++ b/backend/apps/cloud/src/project/project.controller.ts
@@ -194,7 +194,7 @@ export class ProjectController {
if (isHostnameNavigationEnabled) {
paginated = await this.projectService.paginateHostnameNavigation(
- { take, skip },
+ { take, skip, period },
userId,
search,
)
diff --git a/backend/apps/cloud/src/project/project.service.ts b/backend/apps/cloud/src/project/project.service.ts
index 3acbd7152..2f5010017 100644
--- a/backend/apps/cloud/src/project/project.service.ts
+++ b/backend/apps/cloud/src/project/project.service.ts
@@ -306,8 +306,8 @@ const getCompiledHTML = (title: string, desc: string, styles: string) => {
}
interface TrafficPaginationOptions extends PaginationOptionsInterface {
- mode: 'high-traffic' | 'low-traffic' | 'performance' | 'lost-traffic'
- period: '1h' | '1d' | '7d' | '4w' | '3M' | '12M' | '24M' | 'all'
+ mode?: 'high-traffic' | 'low-traffic' | 'performance' | 'lost-traffic'
+ period?: '1h' | '1d' | '7d' | '4w' | '3M' | '12M' | '24M' | 'all'
}
@Injectable()
@@ -456,7 +456,7 @@ export class ProjectService {
}
async paginateHostnameNavigation(
- options: PaginationOptionsInterface,
+ options: TrafficPaginationOptions,
userId: string,
search?: string,
): Promise<Pagination<Project>> {
@@ -466,6 +466,8 @@ export class ProjectService {
return new Pagination<Project>({ results: [], total: 0 })
}
+ const timeFrameClause = this.getTimeFrameClause(options.period)
+
// Process project IDs in chunks to avoid overwhelming Clickhouse
const CHUNK_SIZE = 1000
const chunks = []
@@ -484,7 +486,7 @@ export class ProjectService {
WHERE pid IN {pids:Array(String)}
AND host IS NOT NULL
AND host != ''
- AND created >= now() - INTERVAL 30 DAY
+ AND created BETWEEN ${timeFrameClause.currentStart} AND ${timeFrameClause.currentEnd}
GROUP BY host, pid
ORDER BY visits DESC
)
@@ -515,17 +517,27 @@ export class ProjectService {
allResults = allResults.concat(chunkResults)
// Get total count for this chunk
- const countQuery = query
- .replace(/SELECT .* FROM/, 'SELECT count() as total FROM')
- .replace(/ORDER BY.*$/, '')
+ const countQuery = `
+ WITH hostStats AS (
+ SELECT
+ host,
+ pid,
+ count() as visits
+ FROM analytics
+ WHERE pid IN {pids:Array(String)}
+ AND host IS NOT NULL
+ AND host != ''
+ AND created BETWEEN ${timeFrameClause.currentStart} AND ${timeFrameClause.currentEnd}
+ GROUP BY host, pid
+ )
+ SELECT count() as total FROM hostStats
+ `
// eslint-disable-next-line no-await-in-loop
const countResult = await clickhouse.query({
query: countQuery,
query_params: {
pids: chunk,
- limit: 100000000,
- offset: 0,
},
format: 'JSONEachRow',
})
@@ -2120,17 +2132,27 @@ export class ProjectService {
allResults = allResults.concat(chunkResults)
// Get total count for this chunk
- const countQuery = query
- .replace(/SELECT .* FROM/, 'SELECT count() as total FROM')
- .replace(/ORDER BY.*$/, '')
+ const countQuery = `
+ WITH hostStats AS (
+ SELECT
+ host,
+ pid,
+ count() as visits
+ FROM analytics
+ WHERE pid IN {pids:Array(String)}
+ AND host IS NOT NULL
+ AND host != ''
+ AND created BETWEEN ${timeFrameClause.currentStart} AND ${timeFrameClause.currentEnd}
+ GROUP BY host, pid
+ )
+ SELECT count() as total FROM hostStats
+ `
// eslint-disable-next-line no-await-in-loop
const countResult = await clickhouse.query({
query: countQuery,
query_params: {
pids: chunk,
- limit: 100000000,
- offset: 0,
},
format: 'JSONEachRow',
})
diff --git a/web/app/pages/Dashboard/Dashboard.tsx b/web/app/pages/Dashboard/Dashboard.tsx
index d1efc9603..363a4f401 100644
--- a/web/app/pages/Dashboard/Dashboard.tsx
+++ b/web/app/pages/Dashboard/Dashboard.tsx
@@ -118,7 +118,7 @@ const Dashboard = () => {
}
const updateOverallStats = async (projectIds: string[]) => {
- if (!projectIds.length) return
+ if (!projectIds.length || isHostnameNavigationEnabled) return
try {
const stats = await getOverallStats(projectIds, activePeriod)
@@ -152,7 +152,7 @@ const Dashboard = () => {
const interval = setInterval(updateLiveVisitors, LIVE_VISITORS_UPDATE_INTERVAL)
return () => clearInterval(interval)
- }, [projects, activePeriod]) // Reset interval when projects change
+ }, [projects, activePeriod, isHostnameNavigationEnabled]) // Reset interval when projects change
if (error && isLoading === false) {
return (
diff --git a/web/app/pages/Dashboard/ProjectCard.tsx b/web/app/pages/Dashboard/ProjectCard.tsx
index 68f9a6345..4712874ce 100644
--- a/web/app/pages/Dashboard/ProjectCard.tsx
+++ b/web/app/pages/Dashboard/ProjectCard.tsx
@@ -210,15 +210,23 @@ export const ProjectCard = ({ live = null, project, overallStats }: ProjectCardP
)}
</div>
<div className='mt-4 flex flex-shrink-0 gap-5'>
- <MiniCard
- labelTKey={project.isCaptchaProject ? 'dashboard.captchaEvents' : 'dashboard.pageviews'}
- total={live === 'N/A' ? 'N/A' : (overallStats?.current.all ?? null)}
- percChange={
- live === 'N/A'
- ? 0
- : calculateRelativePercentage(overallStats?.previous.all ?? 0, overallStats?.current.all ?? 0)
- }
- />
+ {isHostnameNavigationEnabled ? (
+ <MiniCard
+ labelTKey={project.isCaptchaProject ? 'dashboard.captchaEvents' : 'dashboard.pageviews'}
+ // @ts-expect-error
+ total={project?.trafficStats?.visits}
+ />
+ ) : (
+ <MiniCard
+ labelTKey={project.isCaptchaProject ? 'dashboard.captchaEvents' : 'dashboard.pageviews'}
+ total={live === 'N/A' ? 'N/A' : (overallStats?.current.all ?? null)}
+ percChange={
+ live === 'N/A'
+ ? 0
+ : calculateRelativePercentage(overallStats?.previous.all ?? 0, overallStats?.current.all ?? 0)
+ }
+ />
+ )}
{project.isAnalyticsProject && <MiniCard labelTKey='dashboard.liveVisitors' total={live} />}
</div>
</div>