Mahdi Naser Moghadasi commited on
Commit ·
acc74af
1
Parent(s): 7b87cd9
Add date filtering to geographic stats (country and US state maps)
Browse files- Update readAnalyticsFromDB to accept filterDate parameter
- Filter countryStats and usStateStats by date when filterDate is provided
- Use filtered stats from readAnalyticsFromDB instead of separate queries
- Maps will now show aggregated data for selected day or all time based on filter
- app/api/analytics/route.ts +3 -1
- lib/analytics-db.ts +53 -39
app/api/analytics/route.ts
CHANGED
|
@@ -154,10 +154,12 @@ export async function POST(request: NextRequest) {
|
|
| 154 |
|
| 155 |
export async function GET(request: NextRequest) {
|
| 156 |
try {
|
| 157 |
-
const analytics = await readAnalyticsFromDB()
|
| 158 |
const searchParams = request.nextUrl.searchParams
|
| 159 |
const includeNonEngagedBreakdown = searchParams.get('breakdown') === 'true'
|
| 160 |
const filterDate = searchParams.get('date') // YYYY-MM-DD format
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
// If date filter is provided, filter all data to that date only
|
| 163 |
let filteredAnalytics = analytics
|
|
|
|
| 154 |
|
| 155 |
export async function GET(request: NextRequest) {
|
| 156 |
try {
|
|
|
|
| 157 |
const searchParams = request.nextUrl.searchParams
|
| 158 |
const includeNonEngagedBreakdown = searchParams.get('breakdown') === 'true'
|
| 159 |
const filterDate = searchParams.get('date') // YYYY-MM-DD format
|
| 160 |
+
|
| 161 |
+
// Pass filterDate to readAnalyticsFromDB so geographic stats are filtered
|
| 162 |
+
const analytics = await readAnalyticsFromDB(filterDate || null)
|
| 163 |
|
| 164 |
// If date filter is provided, filter all data to that date only
|
| 165 |
let filteredAnalytics = analytics
|
lib/analytics-db.ts
CHANGED
|
@@ -112,7 +112,7 @@ interface AnalyticsData {
|
|
| 112 |
usStateStats: Record<string, number>
|
| 113 |
}
|
| 114 |
|
| 115 |
-
export async function readAnalyticsFromDB(): Promise<AnalyticsData> {
|
| 116 |
const db = getDatabase()
|
| 117 |
const excludedIds = getExcludedVisitorIds()
|
| 118 |
const excludedIdsArray = Array.from(excludedIds)
|
|
@@ -535,49 +535,63 @@ export async function readAnalyticsFromDB(): Promise<AnalyticsData> {
|
|
| 535 |
// Merge unique visitor counts into engagement object
|
| 536 |
Object.assign(engagement, engagementUnique)
|
| 537 |
|
| 538 |
-
// Get geographic stats (excluding test visits)
|
| 539 |
-
const
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
const
|
| 555 |
-
?
|
| 556 |
-
:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 557 |
const countryStats: Record<string, number> = {}
|
| 558 |
countryStatsRows.forEach(row => {
|
| 559 |
countryStats[row.country || 'Unknown'] = row.count
|
| 560 |
})
|
| 561 |
|
| 562 |
-
// Get US state stats (excluding test visits)
|
| 563 |
-
const
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
const
|
| 579 |
-
|
| 580 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
const usStateStats: Record<string, number> = {}
|
| 582 |
usStateStatsRows.forEach(row => {
|
| 583 |
usStateStats[row.region || 'Unknown'] = row.count
|
|
|
|
| 112 |
usStateStats: Record<string, number>
|
| 113 |
}
|
| 114 |
|
| 115 |
+
export async function readAnalyticsFromDB(filterDate?: string | null): Promise<AnalyticsData> {
|
| 116 |
const db = getDatabase()
|
| 117 |
const excludedIds = getExcludedVisitorIds()
|
| 118 |
const excludedIdsArray = Array.from(excludedIds)
|
|
|
|
| 535 |
// Merge unique visitor counts into engagement object
|
| 536 |
Object.assign(engagement, engagementUnique)
|
| 537 |
|
| 538 |
+
// Get geographic stats (excluding test visits, optionally filtered by date)
|
| 539 |
+
const countryStatsWhereClause = []
|
| 540 |
+
const countryStatsParams: any[] = []
|
| 541 |
+
|
| 542 |
+
if (filterDate) {
|
| 543 |
+
countryStatsWhereClause.push('visit_date = date(?)')
|
| 544 |
+
countryStatsParams.push(filterDate)
|
| 545 |
+
}
|
| 546 |
+
|
| 547 |
+
if (excludedIdsArray.length > 0) {
|
| 548 |
+
countryStatsWhereClause.push(`visitor_id NOT IN (${excludedIdsArray.map(() => '?').join(',')})`)
|
| 549 |
+
countryStatsParams.push(...excludedIdsArray)
|
| 550 |
+
}
|
| 551 |
+
|
| 552 |
+
countryStatsWhereClause.push('country IS NOT NULL')
|
| 553 |
+
|
| 554 |
+
const countryStatsWhere = countryStatsWhereClause.length > 0
|
| 555 |
+
? `WHERE ${countryStatsWhereClause.join(' AND ')}`
|
| 556 |
+
: ''
|
| 557 |
+
|
| 558 |
+
const countryStatsQuery = db.prepare(`
|
| 559 |
+
SELECT country, country_code, COUNT(*) as count
|
| 560 |
+
FROM analytics_visits
|
| 561 |
+
${countryStatsWhere}
|
| 562 |
+
GROUP BY country, country_code
|
| 563 |
+
ORDER BY count DESC
|
| 564 |
+
`)
|
| 565 |
+
const countryStatsRows = countryStatsQuery.all(...countryStatsParams) as Array<{ country: string; country_code: string; count: number }>
|
| 566 |
const countryStats: Record<string, number> = {}
|
| 567 |
countryStatsRows.forEach(row => {
|
| 568 |
countryStats[row.country || 'Unknown'] = row.count
|
| 569 |
})
|
| 570 |
|
| 571 |
+
// Get US state stats (excluding test visits, optionally filtered by date)
|
| 572 |
+
const usStateStatsWhereClause = ["country_code = 'US'", 'region IS NOT NULL']
|
| 573 |
+
const usStateStatsParams: any[] = []
|
| 574 |
+
|
| 575 |
+
if (filterDate) {
|
| 576 |
+
usStateStatsWhereClause.push('visit_date = date(?)')
|
| 577 |
+
usStateStatsParams.push(filterDate)
|
| 578 |
+
}
|
| 579 |
+
|
| 580 |
+
if (excludedIdsArray.length > 0) {
|
| 581 |
+
usStateStatsWhereClause.push(`visitor_id NOT IN (${excludedIdsArray.map(() => '?').join(',')})`)
|
| 582 |
+
usStateStatsParams.push(...excludedIdsArray)
|
| 583 |
+
}
|
| 584 |
+
|
| 585 |
+
const usStateStatsWhere = `WHERE ${usStateStatsWhereClause.join(' AND ')}`
|
| 586 |
+
|
| 587 |
+
const usStateStatsQuery = db.prepare(`
|
| 588 |
+
SELECT region, COUNT(*) as count
|
| 589 |
+
FROM analytics_visits
|
| 590 |
+
${usStateStatsWhere}
|
| 591 |
+
GROUP BY region
|
| 592 |
+
ORDER BY count DESC
|
| 593 |
+
`)
|
| 594 |
+
const usStateStatsRows = usStateStatsQuery.all(...usStateStatsParams) as Array<{ region: string; count: number }>
|
| 595 |
const usStateStats: Record<string, number> = {}
|
| 596 |
usStateStatsRows.forEach(row => {
|
| 597 |
usStateStats[row.region || 'Unknown'] = row.count
|