Greetings habravchane. I want to talk about how we screwed Google Analytics to the telegram-bot, what problems we encountered, and what we couldn’t manage in the end.
In the old manner, Google Analytics is installed on a site with a domain or in an application with an SDK. Googling the topic, it became clear that there are no examples, so we decided to do it ourselves. In our case there was no site or mobile application, but there was a desire to track events in the Telegram bot. And partly it was done.
Problem number 1
The bots are physically located at
t.me/benice_tools_bot . This is similar to a regular site, but without direct access to root files, for setting the counter code, and setting up events.
The frontend in this case is Telegram itself, to which we have no access.
')
Our bot works with the Binance API and the Telegram API, and accordingly there is a backend through which it became possible to maneuver with analytics settings.
In Google Analytics, we created a counter for the site, and registered events with the parameters “Action, Label, Category”. Detailed settings are described on dozens of sites. And after that the first problem appeared.
Standard Google methods could not be placed in the target bot, there was no access to the front.
A
library came to the rescue, which allowed transmitting and processing event parameters from the backend.
After setting up, we began to test the performance of the method, and got the first success. Completed events were displayed in Google Analytics, in “real time” it was clear how many people were online in the bot, and the rest of the data.

After independent tests, and users with advertising traffic, it became clear that the information we need is 100% reliable. It became clear how many people are online, and what specific events are performed.
But some mistakes were still. For example, the device type was not determined correctly, the “Desktop computer” was displayed, even if you use Telegrams from a mobile device. Geo-location only pointed to Greece, because the server with the bot was located in this country. Demographics, interests, browsers and OS, displayed as "(not set)".
Problem number 2
We created promotional links that, when activated, gave access to paid features for 7 days. And the transitions on these links needed to be linked to Google Analytics in order to analyze advertising campaigns.
By default, Google Analytics itself generates the user id or leaves it without this parameter. However, you can create your own custom id, and send it to Google.
This method we used. Created user accounts through our system’s encrypted userId.
Telegram transmits meta-data at the start of the bot. This works the same as the usual utm tags.
In the link, for an advertising campaign, the label is inserted after the start.
For example:
http://t.me/benice_tools_bot?start=habrahabr
At the start of the bot, we parse the parameters for the start, and take them for a specific campaign.
On the backend, in the user profile, there will be information that he came for a specific campaign campaign_habrahabr.
It remained to concise our visitor (visitor) in the code:
if (!code) code = "organic"; const visitor = ua(googleAnalyticsProjectId, `token:${code};CLIENTID:${saltUserId}`, { strictCidFormat: false }); visitor.event(category, action, label).send();
If the user came by himself, without an advertising campaign, then this source is recognized as “organic”.
Thus, it turned out to analyze the triggering of goals from a specific user who came from an advertising campaign.
PS
All calls to Google Analytics ended in confusion from the support side. At the first calls, when nothing else worked, no prompts could be obtained. And when events were already transmitted to analytics, the support took a 15-minute break to “clarify the details” of the question, and returning, they themselves began to ask questions: “How did you do this?”.