If you run Telegram channel sooner or later the question arises: why do people subscribe when they unsubscribe and how to understand the effectiveness of publications and advertising. Telegram itself shows almost nothing: only the total number of participants. He does not keep any logs of "who came / who left".
But there is a way to track all this accurately to the user. And not through services, but with your own hands in PHP, using the MadelineProto library - a powerful tool for working with Telegram via the MTProto API.
Why do I need to keep track of subscribers and unsubscribers?
Having the data:
- who subscribed;
- who left;
- at what time;
- after which advertisement was the influx;
- which post caused the surge;
you can:
- understand the effectiveness of the content;
- evaluate the work of advertising;
- see the quality of the attracted audience;
- recognize outflow moments;
- predict the growth of the channel.
Why MadelineProto?
MadelineProto:
- free;
- works as a full-fledged Telegram client;
- allows you to get the list of channel participants directly;
- works without a webhook and without a bot.
Important: bot does not see subscriptions and unsubscriptions in channels - only the MTProto client.
Solution architecture
- We periodically receive a list of channel participants.
- Compare it with the previous saved list.
- We determine who came and who left.
- Saving the data to a file or database.
Example of implementation in PHP
1. Installing MadelineProto
composer require danog/madelineproto
2. Initializing and launching a session
require 'vendor/autoload.php';
$MadelineProto = new \danog\MadelineProto\API('session.madeline');
$MadelineProto->start();
3. Getting a list of Telegram channel participants
$participants = [];
$offset = 0;
do {
$result = $MadelineProto->channels->getParticipants([
'channel' => '@my_channel',
'filter' => ['_' => 'channelParticipantsRecent'],
'offset' => $offset,
'limit' => 200,
'hash' => 0
]);
foreach ($result['participants'] as $user) {
$participants[] = $user['user_id'];
}
$offset += 200;
} while (count($result['participants']) > 0);
4. Comparing the current list with the previous one
$previous = file_exists('members.json')
? json_decode(file_get_contents('members.json'), true)
: [];
$new = array_diff($participants, $previous);
$gone = array_diff($previous, $participants);
5. Displaying the results
if ($new) {
echo \"New subscribers: \" . implode(', ', $new) . PHP_EOL;
}
if ($gone) {
echo\"Unsubscribed: \" . implode(', ', $gone) . PHP_EOL;
}
6. Updating the list file
file_put_contents('members.json', json_encode($participants));
Automation via CRON
To start automatically every 10 minutes:
*/10 * * * * php /var/www/check.php
What can be improved?
- Statistics by day - how many came/left.
- Linking to advertising - who came after the advertising integrations.
- Growth charts - visualization of dynamics.
- Notifications if there is a sharp spike in subscriptions or unsubscriptions.
- Linking to posts increases after publication.
The result
Accounting for subscribers and unsubscribers of the Telegram channel through PHP MadelineProto is a simple and accurate way to get full-fledged analytics without third-party services. The solution is installed in a few minutes, is easily automated and works stably.
If you want to integrate this solution into your service, try write to me.
