Сделал аналитику (plausible.io)

This commit is contained in:
Данил 2024-10-18 19:57:31 +03:00
parent c92f7761b1
commit e57a9f3159
2 changed files with 46 additions and 1 deletions

View file

@ -1,4 +1,7 @@
fastapi[standard]~=0.111.0
requests~=2.32.0
starlette~=0.37.2
uvicorn~=0.31.0
uvicorn~=0.31.0
httpx~=0.27.0
PyYAML~=6.0.1
user-agents~=2.2.0

View file

@ -0,0 +1,42 @@
import httpx
import yaml
from user_agents import parse as ua_parse
config = yaml.safe_load(open('../config.yaml'))
class PlausibleAnalytics:
async def __call__(self, request, call_next):
response = await call_next(request)
user_agent = request.headers.get('user-agent', 'unknown')
user_agent_parsed = ua_parse(user_agent)
event = {
"domain": config['analytics']['domain'],
"name": request.url.path or '404 - Not Found',
"url": str(request.url),
"props": {
"method": request.method,
"statusCode": response.status_code,
"browser": f"{user_agent_parsed.browser.family} {user_agent_parsed.browser.version_string}",
"os": f"{user_agent_parsed.os.family} {user_agent_parsed.os.version_string}",
"source": request.headers.get('referer', 'direct'),
},
}
async with httpx.AsyncClient() as client:
try:
await client.post(
config['analytics']['endpoint'],
json=event,
headers={
"Authorization": f"Bearer {config['analytics']['token']}",
"Content-Type": "application/json",
"User-Agent": request.headers.get('user-agent', 'unknown'),
},
)
except Exception as e:
print(f"Error sending event to Plausible: {e}")
return response