import { createReporter } from "https://deno.land/x/blog@0.4.4/deps.ts";
Create and return a function which will dispatch messages to Google Analytics.
Examples
Using std/http
import { createReporter } from "https://deno.land/x/g_a/mod.ts";
import { serve } from "https://deno.land/std/http/server.ts";
import type { ConnInfo } from "https://deno.land/std/http/server.ts";
const ga = createReporter();
function handler(req: Request, conn: ConnInfo) {
let err;
let res: Response;
const start = performance.now();
try {
// processing of the request...
res = new Response();
} catch (e) {
err = e;
} finally {
ga(req, conn, res!, start, err);
}
return res!;
}
serve(handler);
Using low level APIs
import { createReporter } from "https://deno.land/x/g_a/mod.ts";
const ga = createReporter();
for await (const conn of Deno.listen({ port: 0 })) {
(async () => {
const httpConn = Deno.serveHttp(conn);
for await (const requestEvent of httpConn) {
let err;
const start = performance.now();
try {
// processing of the request...
const response = new Response();
await requestEvent.respondWith(response);
} catch (e) {
err = e;
} finally {
await ga(requestEvent.request, conn, response, start, err);
}
}
})();
}