Integrate Amplitude with Telegram bot

Integrate Amplitude with Telegram bot

Want to know how your bots are used? Want to measure DAU, retention and build funnels? I'll show you how, with as low code as possible.

The problem

Websites have Google Analytics, mobile apps have Firebase. Both are really useful to understand how your service is being used and to calculate general product metrics (DAU/MAU, retentions, onboarding funnels, etc).

If you create a Facebook bot, you will have basic usage metrics in your admin dashboard. Unfortunately, Telegram doesn't provide any bot usage stats, even the number of users who activated your bot.

That's why you have to track all users and their activity by yourself. Recently I built django-telegram-bot template which benefits from a built-in Django ORM & Admin panel.

The Solution

🚫 Store events on the backend

Since there is no official way to integrate Google Analytics or Firebase with chatbots (because all events would be executed not from the client-side but from the backend-side), the obvious idea is to store all events on your side.

Since Django is already integrated with Postgres, the majority of young devs will store activity events in a simple Postgres table. Later, after the queries become really slow, devs open themselves to DBA (indexes, partitions). Later they might switch from Postgres to Clickhouse. And then become Big Data Analyst or DevOps engineer. Instead of solving users' problems.

Even if you do everything above, you need to know SQL well to perform queries to your data. Open-source BI like Metabase can help you to generate basic SQL queries but you are still an Analyst to calculate funnels and retentions.

I don't think this is a scalable solution for your Telegram bot (probably it is your pet project). Ideally, you need to focus on users, not on tech supremacy. Ideally, you don't need to write code at all because more code leads to more bugs.

👩‍🔧 Why Amplitude?

Well, because

  1. You can fire events from the backend.
  2. It is a very-very popular platform for product analysis. The experience with this platform will be useful for future projects and it would be easy to find someone who is already familiar with it to join your team.
  3. It has built-in generators for funnel and retention graphs - no more HUGE SQLs.
  4. It has a free tier that allows you to send up to 10 MILLION events per month. It is more than enough to start.
  5. You can export all events if you need. But you probably would not need it.

Integrate Amplitude with Python

You basically can use any language which can send POST requests.

So first of all you need to register on Amplitude. Then basically you need to perform a POST request every time you need to track an event.

Small ready-to-be-copy-pasted code snippet (read comments there):

More info can be found on Amplitude official docs.

Amplitude has special event properties which can be useful for e.g. profit calculation. Read official docs on this.

If you have a question: how to name events properly and what should be inside events_properties then read these cool docs.


So basically I collected all the useful links for you. All you need to do now is to go through all of them and copy-paste proper requests.post every time you want to track something.

Thanks for reading 👋