diff --git a/.changeset/admin-root-visit-counts.md b/.changeset/admin-root-visit-counts.md new file mode 100644 index 0000000..d74143e --- /dev/null +++ b/.changeset/admin-root-visit-counts.md @@ -0,0 +1,5 @@ +--- +'@goto/web': patch +--- + +Report root visits separately from valid links and invalid clicks in admin summaries and Analytics, including historical traffic. Show root traffic over time in the Root report. diff --git a/apps/web/e2e/auth.e2e.ts b/apps/web/e2e/auth.e2e.ts index 56a461c..c601f92 100644 --- a/apps/web/e2e/auth.e2e.ts +++ b/apps/web/e2e/auth.e2e.ts @@ -45,10 +45,16 @@ test('shared credentials protect admin pages and sign-out removes access', async await expect(summary.locator('.admin-stat-label')).toHaveText([ 'Domains', 'Links', - 'Total Visits' + 'Total Visits', + 'Invalid clicks', + 'Root visits' ]); const totalVisits = summary.getByRole('region', { name: 'Total Visits', exact: true }); - await expect(totalVisits.getByText('5', { exact: true })).toBeVisible(); + const invalidClicks = page.getByRole('region', { name: 'Invalid clicks', exact: true }); + const rootVisits = page.getByRole('region', { name: 'Root visits', exact: true }); + await expect(totalVisits.getByText('4', { exact: true })).toBeVisible(); + await expect(invalidClicks.locator('.admin-stat-value')).toHaveText('1'); + await expect(rootVisits.locator('.admin-stat-value')).toHaveText('1'); await expect(summary.getByRole('link')).toHaveCount(0); await expect(page.getByRole('region', { name: 'Visits over time', exact: true })).toHaveCount(0); await expect(page.getByRole('region', { name: 'Accepted languages', exact: true })).toHaveCount( @@ -60,10 +66,10 @@ test('shared credentials protect admin pages and sign-out removes access', async await expect(timeframe).toHaveCount(0); const domainLink = page.getByRole('link', { name: 'go.example.org', exact: true }); const domainRow = page.getByRole('listitem').filter({ has: domainLink }); - await expect(domainRow.locator('.admin-inventory-number')).toHaveText(['6', '3']); + await expect(domainRow.locator('.admin-inventory-number')).toHaveText(['6', '2']); const overviewResponse = await page.goto(`${appServer.origin}/admin?period=7d`); expect(overviewResponse?.headers()['cache-control']).toBe('private, no-store'); - await expect(totalVisits.getByText('5', { exact: true })).toBeVisible(); + await expect(totalVisits.getByText('4', { exact: true })).toBeVisible(); const adminNav = page.getByRole('navigation', { name: 'Administration', exact: true }); const overviewLink = adminNav.getByRole('link', { name: 'GoTo', exact: true }); await expect(adminNav.getByText('GoTo', { exact: true })).toBeVisible(); @@ -86,12 +92,12 @@ test('shared credentials protect admin pages and sign-out removes access', async await expect(summary.locator('.admin-stat-label')).toHaveText([ 'Links', 'Total Visits', - 'Invalid clicks' + 'Invalid clicks', + 'Root visits' ]); - await expect(totalVisits.getByText('3', { exact: true })).toBeVisible(); - await expect( - page.getByRole('region', { name: 'Invalid clicks', exact: true }).locator('.admin-stat-value') - ).toHaveText('1'); + await expect(totalVisits.getByText('2', { exact: true })).toBeVisible(); + await expect(invalidClicks.locator('.admin-stat-value')).toHaveText('1'); + await expect(rootVisits.locator('.admin-stat-value')).toHaveText('1'); const linksInventory = page.getByRole('region', { name: 'Links on go.example.org', exact: true }); const domainDetails = page.getByRole('region', { name: 'Details', exact: true }); const rootRow = linksInventory.getByRole('listitem').filter({ @@ -100,7 +106,7 @@ test('shared credentials protect admin pages and sign-out removes access', async const rootLink = rootRow.getByRole('link', { name: '(root)', exact: true }); await expect(rootRow.locator('.admin-inventory-secondary')).toHaveText('[not found]'); await expect(rootRow.locator('.admin-inventory-state')).toHaveText('Active'); - const columns = ['Path', 'Destination', 'State', 'Total Visits']; + const columns = ['Path', 'Destination', 'State', 'Visits']; await expect(linksInventory.locator('.admin-inventory-heading > span')).toHaveText(columns); const percentLinkRow = linksInventory .getByRole('listitem') @@ -120,6 +126,7 @@ test('shared credentials protect admin pages and sign-out removes access', async '404 Not found', domainReportPath ]); + await expect(rootRow.getByText('Root visits', { exact: true })).toBeVisible(); await expect(rootRow.locator('.admin-inventory-number')).toHaveText('1'); await expect(domainDetails.getByRole('link', { name: 'Root', exact: true })).toHaveCount(0); await expect(domainDetails.getByRole('link', { name: 'Fallback', exact: true })).toHaveCount(0); @@ -314,7 +321,7 @@ test('shared credentials protect admin pages and sign-out removes access', async ]); await expect(timeframe).toHaveCount(0); await expect(trend).toHaveCount(0); - await expect(summaryVisitCount).toHaveText('1'); + await expect(rootVisits.locator('.admin-stat-value')).toHaveText('1'); await expect(page.getByRole('region', { name: 'Status', exact: true })).toHaveCount(0); const rootDetails = page.getByRole('region', { name: 'Root details', exact: true }); await expect(rootDetails.locator('dt')).toHaveText([ @@ -323,19 +330,20 @@ test('shared credentials protect admin pages and sign-out removes access', async 'Destination', 'Analytics' ]); + const rootReportPath = `${domainReportPath}&path=%2F`; await expect(rootDetails.locator('dd')).toHaveText([ 'go.example.org', 'go.example.org/', 'Uses fallback', - domainReportPath + rootReportPath ]); await expect(page.getByRole('region', { name: 'Fallback', exact: true })).toContainText( '404 Not found' ); - const rootAnalyticsLink = rootDetails.getByRole('link', { name: domainReportPath, exact: true }); - await expect(rootAnalyticsLink).toHaveAttribute('href', domainReportPath); + const rootAnalyticsLink = rootDetails.getByRole('link', { name: rootReportPath, exact: true }); + await expect(rootAnalyticsLink).toHaveAttribute('href', rootReportPath); await rootDetails.getByRole('link', { name: 'go.example.org', exact: true }).click(); - await expect(totalVisits.getByText('3', { exact: true })).toBeVisible(); + await expect(totalVisits.getByText('2', { exact: true })).toBeVisible(); await page .getByRole('navigation', { name: 'Administration', exact: true }) @@ -367,8 +375,10 @@ test('shared credentials protect admin pages and sign-out removes access', async await expect(page.getByRole('heading', { name: 'Analytics', exact: true })).toBeVisible(); const analyticsVisits = page.getByRole('region', { name: 'Visits', exact: true }); const analyticsChart = page.getByRole('region', { name: 'Visits over time', exact: true }); - await expect(analyticsVisits).toContainText('3'); - await expect(analyticsChart.getByRole('row', { name: 'Total 3', exact: true })).toBeVisible(); + await expect(analyticsVisits).toContainText('2'); + await expect(analyticsChart.getByRole('row', { name: 'Total 2', exact: true })).toBeVisible(); + await expect(invalidClicks.locator('.admin-stat-value')).toHaveText('1'); + await expect(rootVisits.locator('.admin-stat-value')).toHaveText('1'); const reportFilters = page.getByRole('group', { name: 'Report filters', exact: true }); const scopeInput = reportFilters.getByRole('combobox', { name: 'Report scope', exact: true }); const scopeOptions = page.getByRole('listbox', { name: 'Report scopes', exact: true }); @@ -383,7 +393,7 @@ test('shared credentials protect admin pages and sign-out removes access', async await expect(page).toHaveURL(`${analyticsUrl}?period=7d`); await chooseReportScope('go.example.org'); await expect(page).toHaveURL(goReportUrl); - await expect(analyticsChart.getByRole('row', { name: 'Total 1', exact: true })).toBeVisible(); + await expect(analyticsChart.getByRole('row', { name: 'Total 0', exact: true })).toBeVisible(); const isHeldRedirectRequest = (url: URL) => url.pathname === '/admin/analytics/__data.json' && @@ -414,8 +424,8 @@ test('shared credentials protect admin pages and sign-out removes access', async await expect(reportFilters).toHaveAttribute('aria-busy', 'false'); await expect(page).toHaveURL(`${analyticsUrl}?domain=go.example.org`); await expect(scopeInput).toHaveAttribute('placeholder', 'Domain: go.example.org'); - await expect(analyticsVisits).toContainText('2'); - await expect(analyticsChart.getByRole('row', { name: 'Total 2', exact: true })).toBeVisible(); + await expect(analyticsVisits).toContainText('1'); + await expect(analyticsChart.getByRole('row', { name: 'Total 1', exact: true })).toBeVisible(); } finally { releaseRedirectReport.resolve(); } @@ -423,7 +433,7 @@ test('shared credentials protect admin pages and sign-out removes access', async await page.goBack(); await expect(page).toHaveURL(`${analyticsUrl}?period=7d`); await expect(scopeInput).toHaveAttribute('placeholder', 'All traffic'); - await expect(analyticsChart.getByRole('row', { name: 'Total 2', exact: true })).toBeVisible(); + await expect(analyticsChart.getByRole('row', { name: 'Total 1', exact: true })).toBeVisible(); const searchRequests: string[] = []; function recordSearchRequest(request: Request) { @@ -440,7 +450,7 @@ test('shared credentials protect admin pages and sign-out removes access', async 'tiny.example.org/docs' ]); await expect(page).toHaveURL(`${analyticsUrl}?period=7d`); - await expect(analyticsChart.getByRole('row', { name: 'Total 2', exact: true })).toBeVisible(); + await expect(analyticsChart.getByRole('row', { name: 'Total 1', exact: true })).toBeVisible(); expect(searchRequests).toEqual([]); } finally { page.off('request', recordSearchRequest); @@ -450,7 +460,10 @@ test('shared credentials protect admin pages and sign-out removes access', async await expect(analyticsChart.getByRole('row', { name: 'Total 0', exact: true })).toBeVisible(); await chooseReportScope('go.example.org/ (Root)'); await expect(page).toHaveURL(`${goReportUrl}&path=%2F`); - await expect(analyticsChart.getByRole('row', { name: 'Total 1', exact: true })).toBeVisible(); + const rootChart = page.getByRole('region', { name: 'Root visits over time', exact: true }); + await expect(rootChart.getByRole('row', { name: 'Total 1', exact: true })).toBeVisible(); + await expect(rootVisits.locator('.admin-stat-value')).toHaveText('1'); + await expect(invalidClicks).toHaveCount(0); await page.goBack(); await expect(scopeInput).toHaveAttribute('placeholder', 'go.example.org/docs'); await expect(analyticsChart.getByRole('row', { name: 'Total 0', exact: true })).toBeVisible(); @@ -508,7 +521,7 @@ test('shared credentials protect admin pages and sign-out removes access', async await expect(analyticsChart).toHaveCount(0); await chooseReportScope('All traffic'); await expect(page).toHaveURL(`${analyticsUrl}?period=7d`); - await expect(analyticsChart.getByRole('row', { name: 'Total 2', exact: true })).toBeVisible(); + await expect(analyticsChart.getByRole('row', { name: 'Total 1', exact: true })).toBeVisible(); await chooseReportScope('go.example.org/docs'); await expect(page).toHaveURL(`${goReportUrl}&path=%2Fdocs`); @@ -580,7 +593,9 @@ test.describe('analytics disabled', () => { await expect(page.locator('.admin-cards .admin-stat-label')).toHaveText([ 'Domains', 'Links', - 'Total Visits' + 'Total Visits', + 'Invalid clicks', + 'Root visits' ]); await expect( page @@ -607,7 +622,8 @@ test.describe('analytics disabled', () => { await expect(page.locator('.admin-cards .admin-stat-label')).toHaveText([ 'Links', 'Total Visits', - 'Invalid clicks' + 'Invalid clicks', + 'Root visits' ]); await expect(page.getByRole('group', { name: 'Timeframe', exact: true })).toHaveCount(0); await expect(page.getByRole('region', { name: 'Accepted languages', exact: true })).toHaveCount( @@ -620,7 +636,7 @@ test.describe('analytics disabled', () => { page .getByRole('region', { name: 'Links on go.example.org', exact: true }) .locator('.admin-inventory-heading > span') - ).toHaveText(['Path', 'Destination', 'State', 'Total Visits']); + ).toHaveText(['Path', 'Destination', 'State', 'Visits']); const details = page.getByRole('region', { name: 'Details', exact: true }); const reportLinks = 'a[href^="/admin/analytics"]'; await expect(details.locator('dt')).toHaveText(['Domain', 'Root', 'Fallback']); diff --git a/apps/web/src/lib/components/admin/LinkInventory.svelte b/apps/web/src/lib/components/admin/LinkInventory.svelte index d71c9bf..db182e9 100644 --- a/apps/web/src/lib/components/admin/LinkInventory.svelte +++ b/apps/web/src/lib/components/admin/LinkInventory.svelte @@ -40,7 +40,8 @@ + + diff --git a/apps/web/src/lib/components/admin/VisitsChart.svelte b/apps/web/src/lib/components/admin/VisitsChart.svelte index 3d9971e..f7b3ec7 100644 --- a/apps/web/src/lib/components/admin/VisitsChart.svelte +++ b/apps/web/src/lib/components/admin/VisitsChart.svelte @@ -8,11 +8,13 @@ let { daily, window: viewWindow, - granularity + granularity, + visitLabel = 'Visits' }: { daily: VisitDetails['daily']; window: VisitWindow; granularity: VisitGranularity; + visitLabel?: string; } = $props(); let ready = $state(false); let unavailable = $state(false); @@ -39,7 +41,7 @@ let buckets = $derived(groupVisitDays(daily, viewWindow, granularity)); let total = $derived(buckets.reduce((sum, bucket) => sum + bucket.validVisits, 0)); let summary = $derived( - `Visits by ${granularity}. Total: ${formatCount(total)}. ${windowLabel(viewWindow)}.` + `${visitLabel} by ${granularity}. Total: ${formatCount(total)}. ${windowLabel(viewWindow)}.` ); function windowLabel(window: VisitWindow): string { @@ -100,7 +102,7 @@ const bucket = points[items[0]?.dataIndex ?? -1]; return bucket ? `${bucketPeriod(bucket)} UTC` : ''; }, - label: (item) => `Visits: ${formatCount(item.parsed.y)}`, + label: (item) => `${visitLabel}: ${formatCount(item.parsed.y)}`, footer: (items) => (points[items[0]?.dataIndex ?? -1]?.partial ? 'Partial period' : '') } } @@ -114,7 +116,7 @@ chart.data.labels = points.map((point) => bucketLabel(point.start, grouping)); chart.data.datasets = [ { - label: 'Visits', + label: visitLabel, data: points.map((point) => point.validVisits), borderColor: color, borderWidth: 2, @@ -192,9 +194,9 @@ }); -
+
-

Visits

+

{visitLabel}

{groupingLabels[granularity]}
@@ -207,9 +209,9 @@ {/if}
- +
- + {#each buckets as bucket (bucket.start)} diff --git a/apps/web/src/lib/server/visits/reports.test.ts b/apps/web/src/lib/server/visits/reports.test.ts index 73b0218..f75a0e7 100644 --- a/apps/web/src/lib/server/visits/reports.test.ts +++ b/apps/web/src/lib/server/visits/reports.test.ts @@ -9,7 +9,7 @@ import { type VisitDetails } from '@goto/analytics'; import type { GotoConfiguration } from '@goto/config'; -import { openDb, runMigrations } from '@goto/db'; +import { addVisitTotals, openDb, runMigrations } from '@goto/db'; import { addDailyVisitStatistics } from '@goto/db/visit-reports'; import { afterEach, beforeEach, expect, test, vi } from 'vitest'; import { readLinkDetails, readVisitDetails, readLastActivity } from './reports.js'; @@ -61,6 +61,9 @@ test('reads daily output and derives zero scopes in a window from current time a const linkScope = { kind: 'link', domain, path: '/docs' } as const; const linkDetails: VisitDetails = { ...details, scope: linkScope }; const otherLink = { kind: 'link', domain: 'other.example.test', path: '/docs' } as const; + const formerDomain = { kind: 'domain', domain: 'old.example.test' } as const; + const formerRoot = { kind: 'link', domain: formerDomain.domain, path: '/' } as const; + const rootCounts = { validVisits: 1, invalidVisits: 0 }; const otherLinkActivityAt = '2026-06-15T11:00:00.000Z'; const emptyDetails: VisitDetails = { ...details, @@ -82,22 +85,35 @@ test('reads daily output and derives zero scopes in a window from current time a available: true, lastActivityAt: null }); - await db - .transaction() - .execute((transaction) => - addDailyVisitStatistics(transaction, [ - daily, - { ...daily, scope: domainScope }, - { ...daily, scope: linkScope }, - { ...daily, scope: otherLink, lastVisitAt: Date.parse(otherLinkActivityAt) } - ]) - ); + await db.transaction().execute(async (transaction) => { + await addVisitTotals(transaction, [{ scope: formerDomain, totals: rootCounts }]); + await addDailyVisitStatistics(transaction, [ + daily, + { ...daily, scope: domainScope }, + { ...daily, scope: linkScope }, + { ...daily, scope: otherLink, lastVisitAt: Date.parse(otherLinkActivityAt) }, + { + ...daily, + scope: formerRoot, + ...rootCounts, + browsers: [{ name: 'Other', ...rootCounts }], + operatingSystems: [{ name: 'Other', ...rootCounts }], + acceptedLanguages: [{ name: 'Other', ...rootCounts }] + } + ]); + }); } finally { await db.destroy(); } const close = vi.spyOn(DatabaseSync.prototype, 'close'); try { - expect(await readVisitDetails(configuration, details.scope)).toEqual(details); + const overview = await readVisitDetails(configuration, details.scope); + expect(overview?.daily.at(-1)).toMatchObject({ + validVisits: 1, + invalidVisits: 1, + rootVisits: 1 + }); + expect(overview?.lastActivityAt).toBe(lastActivityAt); expect(close).toHaveBeenCalledTimes(1); expect(await readVisitDetails(configuration, domainScope)).toEqual(domainDetails); expect(close).toHaveBeenCalledTimes(2); diff --git a/apps/web/src/lib/server/visits/reports.ts b/apps/web/src/lib/server/visits/reports.ts index 0eae556..b49e81d 100644 --- a/apps/web/src/lib/server/visits/reports.ts +++ b/apps/web/src/lib/server/visits/reports.ts @@ -1,10 +1,11 @@ -import { createVisitPeriod, type ReportScope, type VisitDetails } from '@goto/analytics'; +import { createVisitPeriod, type ReportScope } from '@goto/analytics'; import type { GotoConfiguration } from '@goto/config'; import { openDb } from '@goto/db'; import { readLinkVisitDetails, - readVisitDetails as readStoredDetails, + readAnalyticsVisitDetails, readLastVisitActivity, + type AnalyticsVisitDetails, type LinkVisitDetails } from '@goto/db/visit-reports'; import type { Immutable } from '../snapshot.js'; @@ -33,14 +34,14 @@ export async function readLinkDetails( export async function readVisitDetails( configuration: ReportConfiguration, scope: ReportScope -): Promise { +): Promise { const months = configuration.analytics.retention_months; if (months === 0) return null; let db: Awaited> | undefined; try { db = await openDb(configuration.database.path, { readOnly: true }); const period = createVisitPeriod(months, Date.now()); - return await readStoredDetails(db, scope, period); + return await readAnalyticsVisitDetails(db, scope, period); } catch { warnUnavailable(); return null; diff --git a/apps/web/src/routes/(admin)/(auth)/admin/+page.server.ts b/apps/web/src/routes/(admin)/(auth)/admin/+page.server.ts index 715cfb7..2a3a7b4 100644 --- a/apps/web/src/routes/(admin)/(auth)/admin/+page.server.ts +++ b/apps/web/src/routes/(admin)/(auth)/admin/+page.server.ts @@ -23,6 +23,8 @@ export const load = (async ({ locals }) => { domainCount: domains.length, linkCount: domains.reduce((sum, row) => sum + row.linkCount, 0), totalVisits: totals?.overview.validVisits ?? null, + invalidClicks: totals?.overview.invalidVisits ?? null, + rootVisits: totals?.overview.rootVisits ?? null, domains }; }) satisfies PageServerLoad; diff --git a/apps/web/src/routes/(admin)/(auth)/admin/+page.svelte b/apps/web/src/routes/(admin)/(auth)/admin/+page.svelte index 467fd44..44a48b1 100644 --- a/apps/web/src/routes/(admin)/(auth)/admin/+page.svelte +++ b/apps/web/src/routes/(admin)/(auth)/admin/+page.svelte @@ -17,10 +17,12 @@ {#if data.totalVisits === null}

Visit totals unavailable.

{/if} -
+
+ +
@@ -64,6 +66,12 @@
Period (UTC)Visits
Period (UTC){visitLabel}