How to set up a daily summary of Yandex.Direct work in Telegram?
The article is intended for marketers and business owners who run advertising campaigns in Yandex Direct (or any other advertising system) and want to keep a daily finger on the pulse of how their advertising works. We'll look at how you can automate data collection from an advertising system, process it, and regularly send notifications about the results to Telegram using Python.
Main goal quickly analyze advertising data for the current period, at first glance at the message, understand how things are going in advertising without going into advertising accounts. Evaluating this task, we came to the conclusion that numbers with statistics “for yesterday”, divorced from the context of the month / half a year, do not make any sense. For contextual ad analytics, we used a 7-day, 30-day, and 90-day average approach.
This way we can quickly draw a conclusion about how advertising is working now and how it has worked on average recently. If the indicators over the last 7 days show an increase relative to 30, 90 days, then things are going in the right direction; if there is a decline, then you need to start figuring out what’s going on.
Content
Choosing a method for storing and retrieving source data
Step 1: Selecting a method for storing and retrieving source data
Before sending out anything in notifications, we need to prepare the data. There are two main approaches here: using a ready-made data warehouse or directly connecting to the advertising system API.
The ideal (for my taste) option is with storage
This option is considered more stable and preferable. You configure in advance the daily download of data from advertising systems into a single storage using, for example Python (the example is not ideal but the logic is correct) or language R.
For example, data from platforms such as Yandex Direct, VK Advertising, TG Ads can be automatically downloaded and updated in the database. This allows you to work with data that has already been processed and eliminate the risks associated with loss of connection or incomplete receipt of information.
Advantages:
Stability: The data is already loaded into the database, which eliminates the risks associated with the unpredictability of the API. This is especially important if you need to receive large amounts of data on a regular basis.
Availability: Data is always at hand and can be accessed at any time without the need to make requests to external services.
Historical data: Data can be stored and processed over an extended period, which is useful for analysis and long-term trends.
Flaws:
Infrastructure required: The need to organize and maintain a data warehouse, which can be time-consuming and resource-intensive.
Initial setup: Requires setting up processes to automatically load data, which can take a significant amount of time.
Difficulty updating data: If data is updated frequently, there may be delays in obtaining the most current information.
Direct connection to API
This approach is less reliable, since it directly depends on the stability of the connection to the advertising system API, but requests to it can also be sent using any programming language, including Python or R.
If requests for data are too large, it is possible that the connection will be interrupted and the data may arrive incomplete or not arrive at all. Each time you request data, you connect to the API, receive the necessary information for the selected period and process it before sending it to Telegram.
Advantages:
Latest data: The data is always relevant to the “now”, since queries occur in real time. This is especially useful if you need the latest data for quick decision-making, but it is not very relevant if you want to evaluate advertising in the “generalized analytics” mode for the past period.
No need for storage: You can avoid setting up and maintaining a database by simply connecting to the API as needed. This is probably the key advantage of this approach.
Flaws:
Instability: Direct connection to the advertising systems API may result in connection interruptions, especially with large volumes of data, which can lead to data loss or delays.
API restrictions: Many advertising systems place limits on the number of requests or the amount of data that can be retrieved at one time, making it difficult to obtain a complete set of data.
Error Handling: It is necessary to implement additional logic to handle failed requests, repetitions and partial data, which increases the complexity of implementation specifically within the framework of building a notification system.
Step 2: Connect to Telegram
Once you have decided on the data source, the next step is to set up integration with Telegram to send notifications.
Creating a bot
To work with Telegram we need a bot. It can be easily created using the official @BotFather bot on Telegram:
Open a conversation with @BotFather and run the command
/newbot
.Follow the instructions to create a bot and get a token that will be used in Python to send messages. Received token be sure to copythen we will need it to work with sending messages.
Add the created bot to a chat or channel where notifications will be sent. For this step, it is necessary that the bot be added to a Telegram channel or chat and receive administrator rights there with the rights to send messages.
Rights are added in the channel or chat administration section when going to resource administrators.
Step 3: Connect to the data store
Now that the Telegram bot is configured, you need to connect to the data storage where the results of advertising campaigns are stored. To do this, you can use Python libraries such as pymysql
or sqlalchemy
.
Linked Libraries
from sqlalchemy import create_engine
import pandas as pd
from datetime import datetime
import requests
Here we include four necessary packages:
sqlalchemy
: This package is used to create a connection to the database. It allows you to interact with various DBMS (database management systems) through a single interface.pandas
: This is a popular library for working with data in Python. We usepandas
to load data from the database in the form of a DataFrame – a data structure convenient for further analysis.datetime:
This A module from the Python standard library provides functionality for working with dates and times.requests
: This library is designed to perform HTTP requests. It is used to interact with external web services or APIs.
Connecting to the database
db_url = f"mysql+mysqlconnector://{user}:{password}@{host}:{port}/{dbname}?auth_plugin=mysql_native_password"
engine = create_engine(db_url)
This is where you configure and create a connection to the database:
db_url
: A connection string is generated that includes:user
: Database user name.password
: Password to access the database.host
: Database server address.port
: Port on which the connection is made (usually 3306 for MySQL).dbname
: The name of the database that will be used to work with.auth_plugin
: A parameter indicating which plugin is used for authentication (in this casemysql_native_password
).
The connection string is passed to SQLAlchemy to create a secure connection to the database.
engine
: We use the functioncreate_engine
from SQLAlchemy to create a database connection object. This object allows you to execute SQL queries and manage the connection.
SQL query data for last 7 days
Here we are creating an SQL query to retrieve data for the last 7 days. The request performs the following operations:
SELECT
: Select the required metrics:CabinetName
: field with data about the name of the advertising accountTotalCost
: Total expenses for the last 7 days. We useROUND
to round the value to a whole number.TotalClicks
: Total number of ad clicks.TotalConversions
: The total number of conversions received from advertising campaigns. We summarize multiple goals (goal_1 – goal_6) usingCOALESCE
to account for cases where target values may be null.TotalImpressions
: Total number of ad impressions (reach).AverageCPC
: Average cost per click (CPC), calculated as total costs divided by the number of clicks.CPA
: Cost per conversion (CPA), calculated as total cost divided by number of conversions.
FROM
: Specify the tabletf_direct
which contains data from Yandex Direct (your table may be called differently).WHERE
: Filter data by date. Function usedDATE_SUB
to select data for the last 7 days from the current date (CURDATE()
).
SQL queries for last 30 and 90 days
query_last_30_days = query.replace("7 DAY", "30 DAY")
query_last_90_days = query.replace("7 DAY", "90 DAY")
These lines modify the original SQL query to fetch data for the last 30 and 90 days, respectively:
query_last_30_days
: A copy of the request is created, where the date filter is replaced with “last 30 days”.query_last_90_days
: A copy of the query is created to retrieve data for the last 90 days.
This allows you to reuse the original request, replacing only the time slots.
Executing SQL Queries and Retrieving Data
last_7_days_data = pd.read_sql(query, con=engine)
last_30_days_data = pd.read_sql(query_last_30_days, con=engine)
last_90_days_data = pd.read_sql(query_last_90_days, con=engine)
Here the queries are executed and the results are loaded into a DataFrame using pandas
:
pd.read_sql
: The function loads the data received from the SQL query into an objectDataFrame
which is convenient for subsequent work and data analysis.last_7_days_data
,last_30_days_data
,last_90_days_data
: Three different variables for data for the last 7, 30 and 90 days.
Returning results
return last_7_days_data, last_30_days_data, last_90_days_data
As a result, the function returns three DataFrame objects containing data for the last 7, 30 and 90 days. This data can be used to analyze advertising campaign performance metrics – General request code.
Step 4: Generating reports and sending data to Telegram
After receiving summary data from the database, it is important to process it and generate understandable reports. Use the Pandas library to work with data.
Since we had the task of generating average values for 7, 30, 90 days, we will write functions that can access the data collected on Step 3 and form them into average values for all key indicators – Averaging functions.
After we have received the averaged data, we can form a message from it to send to Telegram – Function for generating a message to be sent to the TG
Sending data to Telegram:
def sent_msg(msg):
TOKEN = f'777********43:AAHOR***********cEj4lbJo' # Указываем Токен, который получили на Шаге 2 от BotFather
CHATS_ID = [f'-35*****8'] # Указываем ID чата в который планируем отправлять сообщения ботом
for CHAT_ID in CHATS_ID:
url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={CHAT_ID}&text={msg}&parse_mode=HTML"
response = requests.get(url)
if response.status_code == 200:
print(f"Message sent successfully to {CHAT_ID}")
else:
print(f"Failed to send message to {CHAT_ID}: {response.text}")
To get CHATS_ID you can use the Telegram bot –@getmyid_bot
adding him to your group/chat and he will send you your chat ID in response.
Already at this stage, by executing the request:
message = build_msg(merged_data)
sent_msg(message)
Your message with all the advertising data should go to the chat for which we received CHATS_ID above.
Step 5: Automate notification sending
The last stage is process automation. One way is to set up a task to send reports daily using Task Scheduler on Windows.
Setting up a task in Task Scheduler
Open Task Scheduler.
Create a new task by selecting New Task.
Set the trigger to run at a specified time every day.
In the “Actions” section, specify the launch of the Python script:
Save the task.
Now your script will automatically run every day at the specified time and send reports to Telegram.
Alternative solutions
For Linux users, you can use cron to configure the script to run automatically. For example:
0 9 * * * /usr/bin/python3 /path/to/script.py
This job will run the script daily at 9:00 am.
Conclusion
We looked at how we can automate the process of generating reports on advertising campaigns and sending them to Telegram. Now, with the help of Python and a small script, you can receive up-to-date dynamic data on a daily basis and monitor the performance of your advertising campaigns. Don't miss important changes, respond to fluctuations in time and optimize your advertising strategies to achieve better results.
P.S. The article does not cover in detail the topic of how data is collected and loaded into the database; I plan to talk about this later if the topic is relevant.
Thank you for reading, I wish you successful implementation =)