2024-10-17 20:56:10 +03:00
|
|
|
import httpx
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
from user_agents import parse as ua_parse
|
2024-10-21 19:45:53 +03:00
|
|
|
from http import HTTPStatus
|
2024-10-17 20:56:10 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-10-21 19:45:53 +03:00
|
|
|
if HTTPStatus(response.status_code).is_client_error:
|
|
|
|
return
|
|
|
|
|
2024-10-17 20:56:10 +03:00
|
|
|
event = {
|
|
|
|
"domain": config['analytics']['plausible_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']['plausible_api'],
|
|
|
|
json=event,
|
|
|
|
headers={
|
|
|
|
"Authorization": f"Bearer {config['analytics']['plausible_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
|