This project provides a Fresh Plugin that embeds GA4 middleware into your project.
Much credit to the Fresh authors, specifically whoever worked on the www/routes/_middleware.ts file, since I just copied that here.
In your main.ts
(or wherever you invoke start
) add an import like the
following:
import { ga4Plugin } from "https://deno.land/x/fresh_ga4@0.0.1/mod.ts";
(Note: you probably want to use the latest version, which isn't 0.0.1, but don't do this:
https://deno.land/x/fresh_ga4/mod.ts
.)
Then change your start
invocation like so:
-await start(manifest);
+await start(manifest, { plugins: [ga4Plugin()] });
Make sure you have GA4_MEASUREMENT_ID
set as an environment variable.