'use client'
import { TrendingUp, TrendingDown, Target, Zap, Award, Clock, CheckCircle2, XCircle, BarChart2 } from 'lucide-react'
import { cn, fmtNum } from '@/lib/utils'
import { cohorts, eventBreakdown, dailyEvents, roiData } from '@/lib/mock-data'
import { BarChart, Bar, LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts'
import { useState, useEffect } from 'react'
const Tip = ({ active, payload, label }: any) => {
if (!active || !payload?.length) return null
return (
{label}
{payload.map((p: any, i: number) =>
{p.name}: {typeof p.value==='number' && p.value>1000 ? fmtNum(p.value) : p.value}{p.unit||''}
)}
)
}
function getColor(v: number) {
if (v === 0) return 'bg-surface-elevated text-muted'
if (v >= 75) return 'bg-trading-up/20 text-trading-up'
if (v >= 50) return 'bg-brand-yellow/15 text-brand-yellow'
if (v >= 30) return 'bg-[#ff9500]/15 text-[#ff9500]'
return 'bg-trading-down/15 text-trading-down'
}
interface AttributionStats {
total: number; recovered: number; pending: number; successRate: number
avgDaysToRecover: number
weeklyTrend: { week: string; rate: number }[]
interventions: { wallet: string; eventFired: string; score: number; recovered: boolean; daysToRecover?: number }[]
}
export default function AnalyticsPage() {
const maxEvt = Math.max(...eventBreakdown.map(e => e.count))
const [attr, setAttr] = useState(null)
useEffect(() => {
fetch('/api/torque/attribution')
.then(r => r.ok ? r.json() : null)
.then(d => d && setAttr(d))
.catch(() => {})
}, [])
return (
Analytics
Deep retention insights, cohort analysis & ROI tracking
{[
{ l:'Avg Retention', v:'67.3%', c:'+9.6%', up:true, i:Target },
{ l:'Churn Rate', v:'4.2%', c:'-4.3%', up:false, i:TrendingDown },
{ l:'Campaign ROI', v:'847%', c:'+15.2%', up:true, i:TrendingUp },
{ l:'Saved Wallets', v:'15.2K', c:'+23.4%', up:true, i:Award },
{ l:'Agent Actions', v:'3,847', c:'+12.1%', up:true, i:Zap },
].map(k => { const I = k.i; return (
)})}
{/* Recovery Attribution Dashboard */}
{attr && (
Recovery Attribution
LIVE
Did interventions actually work?
Success Rate
{attr.successRate}%
{attr.recovered}/{attr.total} rescued
Avg Recovery
{attr.avgDaysToRecover}d
days to return
Pending
{attr.pending}
awaiting return
Recent interventions
{attr.interventions.map((item, i) => (
{item.recovered
?
: }
{item.wallet}
{item.eventFired}
score={item.score}
{item.recovered ? `✓ returned in ${item.daysToRecover}d` : 'pending'}
))}
)}
Daily Custom Events
}/>
Campaign ROI Over Time
}/>
Retention Cohorts
Weekly cohort heatmap — FlowState launched Mar 31
| Cohort |
Day 1 |
Day 7 |
Day 14 |
Day 30 |
Day 60 |
{cohorts.map(c => (
{c.week} |
{[c.d1,c.d7,c.d14,c.d30,c.d60].map((v,i) => (
{v===0?'—':v+'%'} |
))}
))}
📈 FlowState Impact: Cohorts after Mar 31 show +7pp higher day-7 retention and +11pp higher day-14 retention vs pre-launch.
Torque Custom Events Breakdown
{eventBreakdown.map(e => (
{e.event}{fmtNum(e.count)}
))}
)
}