diff --git a/src/middleware.ts b/src/middleware.ts index d2a43fa291..c243db0547 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,8 +1,23 @@ import metrics from "../metrics"; +import {APIContext} from "astro"; -export async function onRequest({ request }: any, next: () => any) { +export async function onRequest({ request, clientAddress }: APIContext, next: () => any) { const path = new URL(request.url).pathname.slice(1); - + + await fetch("https://plausible.io/api/event", { + method: "POST", + headers: { + "Content-Type": "application/json", + "User-Agent": request.headers.get("User-Agent")!, + "X-Forwarded-For": clientAddress, + }, + body: JSON.stringify({ + domain: "sprig.hackclub.com", + url: request.url, + name: "pageview" + }) + }) + if (!path.includes("api")) return next(); const metricName = path.split("/").join("_");