From 4377909e106d20fcf8372d5dc8d6d4ea6451b0e2 Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Fri, 15 Sep 2023 13:26:24 -0400 Subject: [PATCH] feat: google analytics (#68) * feat(wip): gtag * chore: rm tool-versions --- docs/site/.gitignore | 1 + docs/site/lib/gtag.js | 15 ++++++++++++++ docs/site/pages/_app.mdx | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 docs/site/lib/gtag.js create mode 100644 docs/site/pages/_app.mdx diff --git a/docs/site/.gitignore b/docs/site/.gitignore index 5b3ad33..5690274 100644 --- a/docs/site/.gitignore +++ b/docs/site/.gitignore @@ -1,2 +1,3 @@ .next/ node_modules/ +.envrc diff --git a/docs/site/lib/gtag.js b/docs/site/lib/gtag.js new file mode 100644 index 0000000..7b63539 --- /dev/null +++ b/docs/site/lib/gtag.js @@ -0,0 +1,15 @@ +export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID; + +export const pageview = (url) => { + window.gtag("config", GA_TRACKING_ID, { + page_path: url, + }); +}; + +export const event = ({ action, category, label, value }) => { + window.gtag("event", action, { + event_category: category, + event_label: label, + value: value, + }); +}; diff --git a/docs/site/pages/_app.mdx b/docs/site/pages/_app.mdx new file mode 100644 index 0000000..62720df --- /dev/null +++ b/docs/site/pages/_app.mdx @@ -0,0 +1,44 @@ +import Head from "next/head"; +import Script from 'next/script' +import { useEffect } from "react"; +import { useRouter } from "next/router"; +import * as gtag from "../lib/gtag"; + +export default function App({ Component, pageProps }) { + const router = useRouter(); + useEffect(() => { + const handleRouteChange = (url) => { + gtag.pageview(url); + }; + router.events.on("routeChangeComplete", handleRouteChange); + return () => { + router.events.off("routeChangeComplete", handleRouteChange); + }; + }, [router.events]); + +return ( + +<> + +