Skip to content

Commit

Permalink
implement google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Oct 30, 2024
1 parent 05452f5 commit b8f5c59
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"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"
"simplex-noise": "npm:simplex-noise@^4.0.1",
"ga": "https://deno.land/x/g_a@0.1.2/mod.ts"
},
"tasks": {
"generate": "deno run --allow-read --allow-write codegen/codegen.ts",
Expand Down
30 changes: 30 additions & 0 deletions google-analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createReporter } from "ga";

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

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

export function useGoogleAnalytics(
fn: (
request: Request,
info: Deno.ServeHandlerInfo<Deno.NetAddr>,
) => Response | Promise<Response>,
) {
return async function (
request: Request,
info: Deno.ServeHandlerInfo<Deno.NetAddr>,
) {
let error;
let response: Response;
const start = performance.now();
try {
response = await fn(request, info);
} catch (e) {
error = e;
} finally {
ga(request, info, response!, start, error);
}

return response!;
};
}
5 changes: 4 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { serveDir } from "@std/http/file-server";
import { useGoogleAnalytics } from "./google-analytics.ts";

Deno.serve((request) => serveDir(request, { fsRoot: Deno.args[0] ?? "." }));
Deno.serve(useGoogleAnalytics(
(request) => serveDir(request, { fsRoot: Deno.args[0] ?? "." }),
));

1 comment on commit b8f5c59

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on b8f5c59 Oct 30, 2024

Choose a reason for hiding this comment

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

Failed to deploy:

Module not found "file:///src/google-analytics.ts".

Please sign in to comment.