Telegram-bot + Google Analytics

Greetings to the Habravites. I want to talk about how we screwed Google Analytics to the telegram bot, what problems we encountered, and what ultimately failed to be configured.

In the old fashion, 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 were no examples, so we decided to do it ourselves. In our case, there was no website or mobile application, but there was a desire to track events in the Telegram bot. And partly it turned out to be 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 the root files, to set the counter code, and configure events.
The frontend in this case is Telegram itself, to which we do not have access.

Our bot works with the Binance API and Telegram API, and accordingly there is a backend through which it has become possible to maneuver with analytics settings.

In Google Analytics, they 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. 

By standard Google methods, it was not possible to place targets in the bot, there was no access to the front.

The library came to the rescue, which allowed to transfer and process event parameters from the backend.

After setting up, we began to test the working capacity of the method, and got the first success. Completed events began to be displayed in Google Analytics, in the “real time” mode it was visible how many people are online in the bot, and other data.

imageimage

After independent tests, and users from 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 being carried out. 

But there were still some mistakes. For example, the type of device was not correctly detected, “Desktop computer” was displayed, even if you use Telegram from a mobile device. The location indicated only Greece, because the server with the bot was in this country. Demographics, interests, browsers and OSs were displayed as "(not set)".

Problem number 2


We created promotional links that, when activated, gave access to paid features for 7 days. And clicks on these links had to be linked with Google Analytics in order to analyze advertising campaigns.

By default, Google Analytics itself generates a user id or leaves it without this parameter. However, you can create your own custom id, and send it to Google. 

We used this method. We created user accounting through the encrypted userId of our system.

Telegram transmits meta-data when the bot starts. This works the same way as familiar utm tags.
In the link, for an advertising campaign, a label is inserted after start. 

For instance:

http://t.me/benice_tools_bot?start=habrahabr

When the bot starts, we parse the parameters for the start, and take them for a specific campaign.
On the backend, in the user’s profile, there will be information that he came for a particular campaign_habrahabr campaign.

It remained to specify our 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.

image

image

PS


All calls to Google Analytics ended in bewilderment from the support side. At the first calls, when nothing else worked, no tips could be obtained. And when the events were already transferred to analytics, the support took a 15-minute break to “clarify the details” of the question, and when they returned, they themselves began to ask questions: “How did you do this?”

Also popular now: