diff --git a/deno.json b/deno.json index 00dbab1..c1afb17 100644 --- a/deno.json +++ b/deno.json @@ -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", @@ -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", diff --git a/google-analytics.ts b/google-analytics.ts index 13c8763..c59a7fa 100644 --- a/google-analytics.ts +++ b/google-analytics.ts @@ -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, @@ -12,19 +10,21 @@ export function useGoogleAnalytics( ) { return async function ( request: Request, - info: Deno.ServeHandlerInfo, + conn: Deno.ServeHandlerInfo, ) { - 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; }; }