Skip to content

Commit

Permalink
add plausible server side analytics (#2308)
Browse files Browse the repository at this point in the history
  • Loading branch information
recursiveforte authored Sep 18, 2024
1 parent 7f7d348 commit 3180988
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -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("_");
Expand Down

0 comments on commit 3180988

Please sign in to comment.