если 4хх ошибка, то аналитика не будет обрабатываться

This commit is contained in:
Данил 2024-10-20 11:06:16 +03:00
parent 7aa8a15cbd
commit 544a15f612

View file

@ -1,9 +1,10 @@
import httpx import httpx
import yaml import yaml
from http import HTTPStatus
from user_agents import parse as ua_parse from user_agents import parse as ua_parse
config = yaml.safe_load(open('../config.yaml')) config = yaml.safe_load(open('./config.yaml'))
class PlausibleAnalytics: class PlausibleAnalytics:
async def __call__(self, request, call_next): async def __call__(self, request, call_next):
@ -12,6 +13,9 @@ class PlausibleAnalytics:
user_agent = request.headers.get('user-agent', 'unknown') user_agent = request.headers.get('user-agent', 'unknown')
user_agent_parsed = ua_parse(user_agent) user_agent_parsed = ua_parse(user_agent)
if HTTPStatus(response.status_code).is_client_error:
return
event = { event = {
"domain": config['analytics']['domain'], "domain": config['analytics']['domain'],
"name": request.url.path or '404 - Not Found', "name": request.url.path or '404 - Not Found',