feat: add Bot & Spam settings tab with session review UI
This commit is contained in:
56
lib/api/bot-filter.ts
Normal file
56
lib/api/bot-filter.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import apiRequest from './client'
|
||||
|
||||
export interface SessionSummary {
|
||||
session_id: string
|
||||
pageviews: number
|
||||
duration: number | null
|
||||
first_page: string
|
||||
referrer: string | null
|
||||
country: string | null
|
||||
city: string | null
|
||||
region: string | null
|
||||
browser: string | null
|
||||
os: string | null
|
||||
screen_resolution: string | null
|
||||
first_seen: string
|
||||
bot_filtered: boolean
|
||||
suspicion_score: number
|
||||
}
|
||||
|
||||
export interface BotFilterStats {
|
||||
filtered_sessions: number
|
||||
filtered_events: number
|
||||
auto_blocked_this_month: number
|
||||
}
|
||||
|
||||
function buildQuery(opts: { startDate?: string; endDate?: string; suspicious?: boolean; limit?: number }): string {
|
||||
const params = new URLSearchParams()
|
||||
if (opts.startDate) params.append('start_date', opts.startDate)
|
||||
if (opts.endDate) params.append('end_date', opts.endDate)
|
||||
if (opts.suspicious) params.append('suspicious', 'true')
|
||||
if (opts.limit != null) params.append('limit', opts.limit.toString())
|
||||
const q = params.toString()
|
||||
return q ? `?${q}` : ''
|
||||
}
|
||||
|
||||
export function listSessions(siteId: string, startDate: string, endDate: string, suspiciousOnly?: boolean, limit?: number): Promise<{ sessions: SessionSummary[] }> {
|
||||
return apiRequest<{ sessions: SessionSummary[] }>(`/sites/${siteId}/sessions${buildQuery({ startDate, endDate, suspicious: suspiciousOnly, limit })}`)
|
||||
}
|
||||
|
||||
export function botFilterSessions(siteId: string, sessionIds: string[]): Promise<{ updated: number }> {
|
||||
return apiRequest<{ updated: number }>(`/sites/${siteId}/bot-filter`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ session_ids: sessionIds }),
|
||||
})
|
||||
}
|
||||
|
||||
export function botUnfilterSessions(siteId: string, sessionIds: string[]): Promise<{ updated: number }> {
|
||||
return apiRequest<{ updated: number }>(`/sites/${siteId}/bot-filter`, {
|
||||
method: 'DELETE',
|
||||
body: JSON.stringify({ session_ids: sessionIds }),
|
||||
})
|
||||
}
|
||||
|
||||
export function getBotFilterStats(siteId: string): Promise<BotFilterStats> {
|
||||
return apiRequest<BotFilterStats>(`/sites/${siteId}/bot-filter/stats`)
|
||||
}
|
||||
@@ -33,6 +33,7 @@ import { listFunnels, type Funnel } from '@/lib/api/funnels'
|
||||
import { getUptimeStatus, type UptimeStatusResponse } from '@/lib/api/uptime'
|
||||
import { listGoals, type Goal } from '@/lib/api/goals'
|
||||
import { listReportSchedules, type ReportSchedule } from '@/lib/api/report-schedules'
|
||||
import { listSessions, getBotFilterStats, type SessionSummary, type BotFilterStats } from '@/lib/api/bot-filter'
|
||||
import { getGSCStatus, getGSCOverview, getGSCTopQueries, getGSCTopPages, getGSCDailyTotals, getGSCNewQueries } from '@/lib/api/gsc'
|
||||
import type { GSCStatus, GSCOverview, GSCQueryResponse, GSCPageResponse, GSCDailyTotal, GSCNewQueries } from '@/lib/api/gsc'
|
||||
import { getBunnyStatus, getBunnyOverview, getBunnyDailyStats, getBunnyTopCountries } from '@/lib/api/bunny'
|
||||
@@ -517,5 +518,23 @@ export function useSubscription() {
|
||||
)
|
||||
}
|
||||
|
||||
// * Hook for session list (bot review)
|
||||
export function useSessions(siteId: string, startDate: string, endDate: string, suspiciousOnly: boolean) {
|
||||
return useSWR<{ sessions: SessionSummary[] }>(
|
||||
siteId && startDate && endDate ? ['sessions', siteId, startDate, endDate, suspiciousOnly] : null,
|
||||
() => listSessions(siteId, startDate, endDate, suspiciousOnly),
|
||||
{ ...dashboardSWRConfig, refreshInterval: 0, dedupingInterval: 10 * 1000 }
|
||||
)
|
||||
}
|
||||
|
||||
// * Hook for bot filter stats
|
||||
export function useBotFilterStats(siteId: string) {
|
||||
return useSWR<BotFilterStats>(
|
||||
siteId ? ['botFilterStats', siteId] : null,
|
||||
() => getBotFilterStats(siteId),
|
||||
{ ...dashboardSWRConfig, refreshInterval: 60 * 1000, dedupingInterval: 10 * 1000 }
|
||||
)
|
||||
}
|
||||
|
||||
// * Re-export for convenience
|
||||
export { fetchers }
|
||||
|
||||
Reference in New Issue
Block a user