forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_middleware.ts
More file actions
116 lines (104 loc) · 2.88 KB
/
_middleware.ts
File metadata and controls
116 lines (104 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import type { FreshContext } from "fresh";
import type { Event } from "$ga4";
import { GA4Report, isDocument, isServerError } from "$ga4";
const GA4_MEASUREMENT_ID = Deno.env.get("GA4_MEASUREMENT_ID");
let showedMissingEnvWarning = false;
function ga4(
request: Request,
conn: FreshContext,
response: Response,
_start: number,
error?: unknown,
) {
if (GA4_MEASUREMENT_ID === undefined) {
if (!showedMissingEnvWarning) {
showedMissingEnvWarning = true;
// deno-lint-ignore no-console
console.warn(
"GA4_MEASUREMENT_ID environment variable not set. Google Analytics reporting disabled.",
);
}
return;
}
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;
// @ts-ignore GA4Report doesn't even use the localAddress parameter
const report = new GA4Report({
measurementId,
request,
response,
// Doesn't use localAddr
// deno-lint-ignore no-explicit-any
conn: conn.info as any,
});
// 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) => {
// deno-lint-ignore no-console
console.error(err);
});
}
export async function handler(
ctx: FreshContext,
): Promise<Response> {
let err;
let res: Response;
const start = performance.now();
try {
const resp = await ctx.next();
const headers = new Headers(resp.headers);
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(
ctx.request,
ctx,
res!,
start,
err,
);
}
}