Lightweight, privacy-first analytics for Telegram (and other) chatbots. Track users, messages, events, funnels, and ratings. Get beautiful Markdown reports and an optional Flask web dashboard.
From day-one analytics to privacy-compliant data retention. Built for production Telegram bots.
All track_update, event, rating calls enqueue instantly. A background writer thread batches and commits. Never block your bot's event loop.
Usernames off, raw JSON off, text truncated, optional HMAC-SHA256 user ID hashing with per-DB salt. Configurable at every level.
DAU/WAU/MAU/YAU, new users, accumulation, hourly activity heatmap, message analysis, funnels, ratings, reviews, and more.
Every query uses bound parameters. Read-only mode=ro connections for all reads. Custom sql_query only allows SELECT/WITH.
Optional web dashboard with a card-grid UI, client-side Chart.js graphs, hashed-password auth, and a read-only JSON API.
Automatic data purging with configurable retention_days. Cross-surface consistency: metrics, reports, and dashboard all see the same trimmed dataset.
Multiple Analytics instances with different bot_id write to one DB. Each metric is scoped per-bot. Dashboard auto-detects or pins via CLI.
Five report categories (total, users, messages, events, ratings) deliver rich Markdown directly in chat. Admin-gated allowlist access.
The core library uses only Python stdlib. Install with pip install . and you're done. Dashboard is an optional Flask extra.
Works with telebot, aiogram, python-telegram-bot, or any framework that gives you a dict.
# pip install analytics-for-telegram-bot
from analytics_for_telegram_bot import Analytics, Config
config = Config(
db_path="analytics.db",
bot_id="my_bot",
admin_ids=[123456789],
)
ana = Analytics(config)
# In your /start handler
ana.track_update(update)
# Track custom events
ana.event("purchase", event_type="shop", user_id=user_id)
ana.rating(5, user_id)
ana.review("Great bot!", user_id)
ana.close() # flushes all pending writes
Every metric opens a read-only connection, works after close(), and returns safe empty values on DB errors.
analyze_totalTotals + averagesanalyze_dauDaily activeanalyze_wauWeekly activeanalyze_mauMonthly activeanalyze_yauYearly activeanalyze_new_userNew users/dayanalyze_user_accumulationCumulativeanalyze_hour_activity24×7 heatmapanalyze_messages_numberMsgs/dayanalyze_messages_typeType distanalyze_update_typeUpdate distanalyze_chat_typeChat distanalyze_languageLanguagesanalyze_eventsEvent countsanalyze_events_typeEvent typesanalyze_events_funnelEvent funnelanalyze_funnelUser funnelanalyze_assessmentRatings 1-5analyze_reviewText reviewsanalyze_messagesTop messagesanalyze_bots_usersMulti-botsql_queryCustom SQLEvery privacy setting defaults to the most restrictive option. Opt in only to what you need.
| Setting | Default | Description |
|---|---|---|
| collect_username | False | Telegram usernames are not collected |
| collect_raw | False | Full update JSON is not stored |
| hash_user_id | False | When True: HMAC-SHA256 with per-DB random salt |
| collect_text | True | Message text stored, truncated to text_max_len |
| text_max_len | 200 | Max characters of message text retained |
| collect_language | True | language_code from Telegram user object |
A single writer thread owns the only write connection. All reads use independent read-only connections.
┌──────────────────────────────────────────────────────────────┐
│ Your Telegram Bot │
│ (telebot / aiogram / python-telegram-bot) │
└──────────┬───────────────────┬────────────────────┬──────────┘
│ │ │
track_update() event() rating()
funnel_step() review()
│ │ │
▼ ▼ ▼
┌──────────────────────────────────────────────────────────────┐
│ In-Memory Queue │
│ (non-blocking, thread-safe enqueue) │
└──────────────────────────┬───────────────────────────────────┘
│ batch + flush_interval
▼
┌──────────────────────────────────────────────────────────────┐
│ Background Writer Thread │
│ (single daemon thread, sole write connection) │
│ INSERT OR IGNORE dedup · error rollback · no data loss │
└──────────────────────────┬───────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────┐
│ SQLite Database │
│ ┌─────────┐ ┌────────┐ ┌─────────┐ ┌────────────┐ │
│ │ updates │ │ events │ │ ratings │ │ funnel_step│ + users │
│ └─────────┘ └────────┘ └─────────┘ └────────────┘ │
└──────┬───────────────────────────────────────────┬──────────┘
│ mode=ro (read-only) │ mode=ro
▼ ▼
┌──────────────┐ ┌───────────────────┐ ┌─────────────────┐
│ Metrics API │ │ Markdown Reports │ │ Flask Dashboard │
│ (22 funcs) │ │ (5 categories) │ │ (Chart.js + API)│
└──────────────┘ └───────────────────┘ └─────────────────┘
Install in seconds. Zero dependencies. Production-ready.