www: add ga4 middleware (#958)
This commit is contained in:
+16
-14
@@ -5,13 +5,14 @@
|
||||
import config from "./deno.json" assert { type: "json" };
|
||||
import * as $0 from "./routes/_404.tsx";
|
||||
import * as $1 from "./routes/_500.tsx";
|
||||
import * as $2 from "./routes/components.tsx";
|
||||
import * as $3 from "./routes/docs/[...slug].tsx";
|
||||
import * as $4 from "./routes/gfm.css.ts";
|
||||
import * as $5 from "./routes/index.tsx";
|
||||
import * as $6 from "./routes/raw.ts";
|
||||
import * as $7 from "./routes/showcase.tsx";
|
||||
import * as $8 from "./routes/update.tsx";
|
||||
import * as $2 from "./routes/_middleware.ts";
|
||||
import * as $3 from "./routes/components.tsx";
|
||||
import * as $4 from "./routes/docs/[...slug].tsx";
|
||||
import * as $5 from "./routes/gfm.css.ts";
|
||||
import * as $6 from "./routes/index.tsx";
|
||||
import * as $7 from "./routes/raw.ts";
|
||||
import * as $8 from "./routes/showcase.tsx";
|
||||
import * as $9 from "./routes/update.tsx";
|
||||
import * as $$0 from "./islands/ComponentGallery.tsx";
|
||||
import * as $$1 from "./islands/CopyArea.tsx";
|
||||
import * as $$2 from "./islands/Counter.tsx";
|
||||
@@ -22,13 +23,14 @@ const manifest = {
|
||||
routes: {
|
||||
"./routes/_404.tsx": $0,
|
||||
"./routes/_500.tsx": $1,
|
||||
"./routes/components.tsx": $2,
|
||||
"./routes/docs/[...slug].tsx": $3,
|
||||
"./routes/gfm.css.ts": $4,
|
||||
"./routes/index.tsx": $5,
|
||||
"./routes/raw.ts": $6,
|
||||
"./routes/showcase.tsx": $7,
|
||||
"./routes/update.tsx": $8,
|
||||
"./routes/_middleware.ts": $2,
|
||||
"./routes/components.tsx": $3,
|
||||
"./routes/docs/[...slug].tsx": $4,
|
||||
"./routes/gfm.css.ts": $5,
|
||||
"./routes/index.tsx": $6,
|
||||
"./routes/raw.ts": $7,
|
||||
"./routes/showcase.tsx": $8,
|
||||
"./routes/update.tsx": $9,
|
||||
},
|
||||
islands: {
|
||||
"./islands/ComponentGallery.tsx": $$0,
|
||||
|
||||
+2
-1
@@ -11,6 +11,7 @@
|
||||
"@preact/signals": "https://esm.sh/*@preact/signals@1.0.3",
|
||||
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.0.1",
|
||||
|
||||
"$std/": "https://deno.land/std@0.150.0/"
|
||||
"$std/": "https://deno.land/std@0.150.0/",
|
||||
"$ga4": "https://raw.githubusercontent.com/denoland/ga4/main/mod.ts"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
import type { Event } from "$ga4";
|
||||
import { GA4Report, isDocument, isServerError } from "$ga4";
|
||||
|
||||
const GA4_MEASUREMENT_ID = Deno.env.get("GA4_MEASUREMENT_ID");
|
||||
|
||||
function ga4(
|
||||
request: Request,
|
||||
conn: MiddlewareHandlerContext,
|
||||
response: Response,
|
||||
_start: number,
|
||||
error?: unknown,
|
||||
) {
|
||||
Promise.resolve().then(async () => {
|
||||
// We're tracking page views and file downloads. These are the only two
|
||||
// HTTP methods that _might_ be used.
|
||||
if (!/^(GET|POST)$/.test(request.method)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the visitor is using a web browser, only create events when we serve
|
||||
// a top level documents or download; skip assets like css, images, fonts.
|
||||
if (!isDocument(request, response) && error == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
let event: Event | null = null;
|
||||
const contentType = response.headers.get("content-type");
|
||||
if (/text\/html/.test(contentType!)) {
|
||||
event = { name: "page_view", params: {} }; // Probably an old browser.
|
||||
}
|
||||
|
||||
if (event == null && error == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If an exception was thrown, build a separate event to report it.
|
||||
let exceptionEvent;
|
||||
if (error != null) {
|
||||
exceptionEvent = {
|
||||
name: "exception",
|
||||
params: {
|
||||
description: String(error),
|
||||
fatal: isServerError(response),
|
||||
},
|
||||
};
|
||||
} else {
|
||||
exceptionEvent = undefined;
|
||||
}
|
||||
|
||||
// Create basic report.
|
||||
const measurementId = GA4_MEASUREMENT_ID;
|
||||
const report = new GA4Report({ measurementId, request, response, conn });
|
||||
|
||||
// Override the default (page_view) event.
|
||||
report.event = event;
|
||||
|
||||
// Add the exception event, if any.
|
||||
if (exceptionEvent != null) {
|
||||
report.events.push(exceptionEvent);
|
||||
}
|
||||
|
||||
await report.send();
|
||||
}).catch((err) => {
|
||||
console.error(`Internal error: ${err}`);
|
||||
});
|
||||
}
|
||||
|
||||
export async function handler(
|
||||
req: Request,
|
||||
ctx: MiddlewareHandlerContext,
|
||||
): Promise<Response> {
|
||||
let err;
|
||||
let res: Response;
|
||||
const start = performance.now();
|
||||
try {
|
||||
const resp = await ctx.next();
|
||||
const headers = new Headers(resp.headers);
|
||||
headers.set(
|
||||
"Strict-Transport-Security",
|
||||
"max-age=63072000; includeSubDomains; preload",
|
||||
);
|
||||
headers.set("X-Content-Type-Options", "nosniff");
|
||||
headers.set("X-Frame-Options", "DENY");
|
||||
headers.set("Cross-Origin-Resource-Policy", "same-origin");
|
||||
headers.set("Cross-Origin-Opener-Policy", "same-origin");
|
||||
headers.set("Cross-Origin-Embedder-Policy", "same-origin");
|
||||
res = new Response(resp.body, { status: resp.status, headers });
|
||||
return res;
|
||||
} catch (e) {
|
||||
res = new Response("Internal Server Error", {
|
||||
status: 500,
|
||||
});
|
||||
err = e;
|
||||
throw e;
|
||||
} finally {
|
||||
ga4(
|
||||
req,
|
||||
ctx,
|
||||
res!,
|
||||
start,
|
||||
err,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user