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 21a6db9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOOGLE_ANALYTICS_ID=""
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
generated
.env
7 changes: 5 additions & 2 deletions codegen/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copySync } from "@std/fs";
import { copySync, expandGlobSync } from "@std/fs";
import { generateTubes } from "./tubes.ts";
import { generateHTML } from "./html.tsx";
import { generateFeed } from "./feed.ts";
Expand All @@ -11,8 +11,11 @@ if (import.meta.main) {
function copyFiles() {
Deno.mkdirSync("generated", { recursive: true });
copySync("deno.json", "generated/deno.json", { overwrite: true });
copySync("main.ts", "generated/main.ts", { overwrite: true });
copySync("static", "generated", { overwrite: true });

for (const file of expandGlobSync("*.[ts|tsx]")) {
copySync(file.path, `generated/${file.name}`, { overwrite: true });
}
}

function generateFiles() {
Expand Down
5 changes: 3 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"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/[email protected]/mod.ts"
},
"tasks": {
"generate": "deno run --allow-read --allow-write codegen/codegen.ts",
"start": "deno run --allow-net --allow-read main.ts generated",
"start": "deno run --allow-net --allow-read --allow-env --env main.ts generated",
"molt": "deno run -A jsr:@molt/cli -w"
},
"fmt": {
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] ?? "." }),
));

0 comments on commit 21a6db9

Please sign in to comment.