Skip to content

Commit

Permalink
implement ga4
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Oct 30, 2024
1 parent 4ddb9ee commit 7c261de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"imports": {
"#/": "./",
"@fartlabs/htx": "jsr:@fartlabs/htx@^0.0.5",
"@kitsonk/ga4": "jsr:@kitsonk/ga4@^0.1.0",
"@std/front-matter": "jsr:@std/front-matter@^1.0.0",
"@std/fs": "jsr:@std/fs@^1.0.0",
"@std/http": "jsr:@std/http@^1.0.0",
Expand All @@ -15,8 +16,7 @@
"highlight.js": "npm:highlight.js@^11.9.0",
"markdown-it": "npm:markdown-it@^14.1.0",
"markdown-it-anchor": "npm:markdown-it-anchor@^9.0.0",
"simplex-noise": "npm:simplex-noise@^4.0.1",
"ga": "https://deno.land/x/g_a@0.1.2/mod.ts"
"simplex-noise": "npm:simplex-noise@^4.0.1"
},
"tasks": {
"generate": "deno run --allow-read --allow-write codegen/codegen.ts",
Expand Down
22 changes: 11 additions & 11 deletions google-analytics.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { createReporter } from "ga";
import { GA4Report } from "@kitsonk/ga4";

const GOOGLE_ANALYTICS_ID = Deno.env.get("GOOGLE_ANALYTICS_ID")!;

const ga = createReporter({ id: GOOGLE_ANALYTICS_ID });

export function useGoogleAnalytics(
fn: (
request: Request,
Expand All @@ -12,19 +10,21 @@ export function useGoogleAnalytics(
) {
return async function (
request: Request,
info: Deno.ServeHandlerInfo<Deno.NetAddr>,
conn: Deno.ServeHandlerInfo<Deno.NetAddr>,
) {
let error;
let response: Response;
const start = performance.now();
try {
response = await fn(request, info);
} catch (e) {
error = e;
response = await fn(request, conn);
} finally {
ga(request, info, response!, start, error);
const report = new GA4Report({
measurementId: GOOGLE_ANALYTICS_ID,
conn,
request,
response: response!,
});
await report.send();
}

return response!;
return response;
};
}

0 comments on commit 7c261de

Please sign in to comment.