mirror of
https://github.com/LukeHagar/pypistats.dev.git
synced 2025-12-09 12:47:48 +00:00
Add analytics tracking to API endpoints for improved event monitoring. Updated GET handlers across various routes to include tracking of successful and failed requests, enhancing data collection for package usage insights.
This commit is contained in:
56
src/lib/analytics.ts
Normal file
56
src/lib/analytics.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { PLAUSIBLE_DOMAIN as PUBLIC_DOMAIN, PLAUSIBLE_EVENT_ENDPOINT } from '$lib/plausible-config.js';
|
||||
|
||||
|
||||
type TrackProps = Record<string, string | number | boolean | null | undefined>;
|
||||
|
||||
function getEnv(name: string, fallback: string = ''): string {
|
||||
const v = process.env[name];
|
||||
return typeof v === 'string' && v.trim() !== '' ? v : fallback;
|
||||
}
|
||||
|
||||
function buildEventUrl(pathname: string): string {
|
||||
const siteBase = 'https://pypistats.dev';
|
||||
return `${siteBase}${pathname}`;
|
||||
}
|
||||
|
||||
export function trackApiEvent(
|
||||
name: string,
|
||||
pathname: string,
|
||||
props: TrackProps = {},
|
||||
requestHeaders?: Headers
|
||||
): void {
|
||||
const endpoint = PLAUSIBLE_EVENT_ENDPOINT;
|
||||
const domain = PUBLIC_DOMAIN;
|
||||
if (!domain) return;
|
||||
|
||||
const url = endpoint;
|
||||
const body = {
|
||||
name,
|
||||
url: buildEventUrl(pathname),
|
||||
domain,
|
||||
props
|
||||
} as any;
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
const apiKey = getEnv('PLAUSIBLE_API_KEY');
|
||||
if (apiKey) headers['Authorization'] = `Bearer ${apiKey}`;
|
||||
|
||||
const ua = requestHeaders?.get('user-agent');
|
||||
if (ua) headers['User-Agent'] = ua;
|
||||
|
||||
const ip =
|
||||
requestHeaders?.get('x-forwarded-for') ||
|
||||
requestHeaders?.get('x-real-ip') ||
|
||||
'';
|
||||
if (ip) headers['X-Forwarded-For'] = ip;
|
||||
|
||||
// Fire-and-forget; never throw or await
|
||||
void fetch(url, { method: 'POST', headers, body: JSON.stringify(body) })
|
||||
.then(() => {})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user