Intermediate

Analytics Dashboard

Build analytics tracking for resolution rate, customer satisfaction (CSAT), common question topics, and cost savings compared to human-only support.

Analytics Tracker

# app/analytics/tracker.py
import json
from datetime import datetime, timedelta
from collections import Counter
from pathlib import Path


class AnalyticsTracker:
    def __init__(self, log_file="data/analytics.jsonl"):
        self.log_file = Path(log_file)
        self.log_file.parent.mkdir(parents=True, exist_ok=True)

    def log_conversation(self, session_id, turns, resolved_by_ai,
                         topics, response_times, csat=None):
        entry = {
            "timestamp": datetime.utcnow().isoformat(),
            "session_id": session_id,
            "turns": turns,
            "resolved_by_ai": resolved_by_ai,
            "topics": topics,
            "avg_response_ms": sum(response_times) / max(len(response_times), 1),
            "csat": csat,
        }
        with open(self.log_file, "a") as f:
            f.write(json.dumps(entry) + "\n")

    def get_summary(self, hours=24):
        entries = self._load_recent(hours)
        if not entries:
            return {"total": 0, "message": "No data yet"}

        total = len(entries)
        ai_resolved = sum(1 for e in entries if e["resolved_by_ai"])
        csat_scores = [e["csat"] for e in entries if e["csat"] is not None]
        all_topics = []
        for e in entries:
            all_topics.extend(e.get("topics", []))

        # Cost savings: assume $5 per human-handled conversation
        human_cost = 5.0
        ai_cost = 0.05  # per conversation
        savings = ai_resolved * (human_cost - ai_cost)

        return {
            "total_conversations": total,
            "ai_resolution_rate": round(ai_resolved / total * 100, 1),
            "avg_csat": round(sum(csat_scores) / max(len(csat_scores), 1), 2),
            "top_topics": Counter(all_topics).most_common(10),
            "avg_turns": round(sum(e["turns"] for e in entries) / total, 1),
            "estimated_savings": round(savings, 2),
        }

    def _load_recent(self, hours):
        if not self.log_file.exists(): return []
        cutoff = datetime.utcnow() - timedelta(hours=hours)
        entries = []
        with open(self.log_file) as f:
            for line in f:
                e = json.loads(line)
                if e["timestamp"] > cutoff.isoformat():
                    entries.append(e)
        return entries

Dashboard API

# Add to app/main.py
from app.analytics.tracker import AnalyticsTracker
tracker = AnalyticsTracker()

@app.get("/api/analytics")
async def get_analytics(hours: int = 24):
    return tracker.get_summary(hours)
💡
Key metric: AI resolution rate is the most important metric. Start by targeting 60-70% AI resolution and iterate on the knowledge base to improve. Each percentage point increase directly reduces support costs.

Key Takeaways

  • AI resolution rate is the primary metric: higher means more cost savings.
  • Topic analysis reveals knowledge base gaps that need more documentation.
  • CSAT tracking ensures AI quality does not degrade customer satisfaction.
  • Cost savings calculation demonstrates ROI to stakeholders.