Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add plausible server side analytics #2308

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we exclude /api/ endpoints in these analytics?

name: "pageview"
})
})

if (!path.includes("api")) return next();

const metricName = path.split("/").join("_");
Expand Down
Loading