<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alon Segal on Medium]]></title>
        <description><![CDATA[Stories by Alon Segal on Medium]]></description>
        <link>https://medium.com/@anialonsegal?source=rss-316ac4102102------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*TR7qajYFPhHrF2S3hp6NRA.jpeg</url>
            <title>Stories by Alon Segal on Medium</title>
            <link>https://medium.com/@anialonsegal?source=rss-316ac4102102------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:39:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anialonsegal/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Adding statistics and analytics to your telegram bot]]></title>
            <link>https://medium.com/@anialonsegal/adding-statistics-and-analytics-to-your-telegram-bot-fc941351e9d4?source=rss-316ac4102102------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc941351e9d4</guid>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[mixpanel]]></category>
            <category><![CDATA[telegram-bot]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Alon Segal]]></dc:creator>
            <pubDate>Mon, 15 Jul 2024 08:01:20 GMT</pubDate>
            <atom:updated>2024-07-15T08:01:20.341Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9X-SEgCgOqV3EXOeZ3FKeQ.png" /></figure><p>In the world of software development, understanding how users interact with your application is crucial. For telegram bot developers, collecting statistics and analytics can provide valuable insights into user behavior and help optimize the bot’s API, functionality and performance. Here’s a brief guide on how to<strong> integrate analytics into your telegram bot</strong>, based on my experience 😊.</p><h4>My Motivation for Collecting Statistics</h4><p>I spend quite a lot of time maintaining and improving my bot, but sometimes I suspect I might be working on areas that won’t provide the most value for my users….</p><p>Understanding user engagement and behavior is crucial for improving your bot. Analytics can help you <strong>monitor user interactions, identify popular features</strong>, and <strong>pinpoint areas that need improvement</strong>. By collecting and analyzing data, you can make informed decisions that enhance the user experience and boost the bot’s effectiveness.</p><h4>Exploring Solutions</h4><p>My bot setup is running a nodejs server, and using telegram as my “client side”, so all of the events should be reported from the server.</p><p>Initially, I searched for an out-of-the-box solution provided by telegram or a ready-made npm package. Surprisingly, I found limited options tailored specifically for telegram bots. To broaden my scope, I consulted ChatGPT for advice on the best available analytics tools.</p><h4>Reviewing Options with ChatGPT</h4><p>ChatGPT helped me review several options, from them I chose to focus on <strong>Google Analytics </strong>and <strong>Mixpanel</strong>. I needed information on <strong>usage, pricing, and the availability of a free tier </strong>to ensure that the chosen solution met my requirements without incurring excessive costs.</p><p><strong>Google Analytics</strong>:</p><ul><li><strong>Usage</strong>: Widely used for web and app analytics.</li><li><strong>Free tier</strong>: Google Analytics 4 offers a robust free tier ( supports up to 10 million events per month) that is suitable for most small to medium-sized applications. It includes event tracking, audience segmentation, and basic reporting capabilities.</li><li><strong>Paid Plans</strong>: For enterprises requiring more advanced features, Google Analytics 360 is available. This is a premium, paid version of Google Analytics that offers additional capabilities, including higher data limits, more advanced reporting, and dedicated support. Pricing for Google Analytics 360 is custom and requires contacting Google’s sales team.</li></ul><p><strong>Mixpanel</strong>:</p><ul><li><strong>Usage</strong>: Strong emphasis on event tracking and user engagement analytics.</li><li><strong>Free Tier</strong>: With Mixpanel, you can track up to 100,000 monthly active users and utilize core analytics features. The platform provides access to data history for up to 90 days and supports unlimited data exports and insights, allowing for flexible analysis and integration with other tools.</li><li><strong>Paid Plans: </strong>Mixpanel&#39;s “Growth Plan” starts at $25 per month and includes advanced features such as tracking up to 1 million monthly users, access to data history for up to 12 months, and enhanced analytics capabilities.<br>For larger organizations, Mixpanel&#39;s Enterprise Plan offers custom pricing and features like unlimited tracked users, unlimited data history, advanced user permissions and data governance, a dedicated customer success manager, and tailored solutions and support.</li></ul><p>After evaluating the options, I initially chose Google Analytics due to its large free tier and popularity.</p><h4>Setting Up Google Analytics</h4><p>I followed the instructions to set up a Google Analytics account and began integrating the code into my bot. The process involved:</p><ol><li>Creating a Google Analytics account.</li><li>Setting up a new property for the bot.</li><li>Adding the tracking code snippets to the bot’s codebase.</li></ol><h4>But…</h4><p>After deploying the changes, I eagerly awaited data to flow into my Google Analytics dashboard. Unfortunately, no data appeared, and after troubleshooting, it became clear that the basic integration process was more time-consuming than anticipated for my server only use case.</p><h4>Switching to Mixpanel</h4><p>Given the excess effort with Google Analytics, I decided to switch to Mixpanel, which promised quicker and easier integration. The steps included:</p><ol><li>Creating a Mixpanel account.</li><li>Setting up a project.</li><li>Adding the <a href="https://www.npmjs.com/package/mixpanel">Mixpanel SDK</a> to the bot and configuring event tracking.</li></ol><h4>Testing Mixpanel Integration</h4><p>Mixpanel offers their NPM package for reporting events, which makes the integration easier</p><pre>npm i mixpanel</pre><p>After that I’ve created my own wrapping reporting function</p><pre><br>import Mixpanel from &#39;mixpanel&#39;;<br><br>const mixpanel = Mixpanel.init(&#39;YOUR_TOKEN&#39;)<br><br>export async function trackEvent(eventName, userId, params = {}) {<br>    try {<br>        mixpanel.track(eventName, {<br>        distinct_id: userId,<br>        ...params<br>        });<br>    } catch (error) {<br>        console.error(`Error tracking event ${eventName}: ${error}`);<br>  }<br>}</pre><p>And used it in my telegram bot actions</p><pre>import { trackEvent } from &#39;../services/analytics.js&#39;;<br><br>...<br><br>onDeals = async (msg, match) =&gt; {<br>        try {<br>            trackEvent(&#39;Deals&#39;, msg.chat.id);<br>            // API logic<br>            ....<br>        } catch (error) {<br><br>        }<br>    };</pre><p>I tested the Mixpanel integration and, to my relief,<strong> it worked seamlessly</strong>. Events were being tracked, and data appeared in the Mixpanel dashboard almost immediately.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gXxBse6s9OERI0H1OzJP4g.png" /></figure><h4>Final Steps</h4><p>Satisfied with the Mixpanel integration, I updated my code accordingly and deployed the changes. Now, I can view comprehensive analytics and insights into how users interact with my telegram bot.</p><p>As these changes a fairly new, I’m waiting to collect enough data to answer questions such as</p><ul><li>How many <strong>distict users </strong>use my bot per day/month?</li><li>What are my <strong>top popular bot actions</strong>?</li><li>What’s my <strong>retention</strong> rate? how many users return each week/month</li></ul><h4>Conclusion</h4><p>Adding analytics to your telegram bot may require some trial and error to find the best solution for your needs. While Google Analytics is a robust tool, Mixpanel’s ease of integration and event-focused approach made it a better fit for my project. By leveraging these analytics tools, you can gain valuable insights and continuously improve your bot’s performance.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc941351e9d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>