Skip to content

Commit

Permalink
feat: google analytics (#68)
Browse files Browse the repository at this point in the history
* feat(wip): gtag

* chore: rm tool-versions
  • Loading branch information
markphelps authored Sep 15, 2023
1 parent 541fba4 commit 4377909
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/site/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.next/
node_modules/
.envrc
15 changes: 15 additions & 0 deletions docs/site/lib/gtag.js
Original file line number Diff line number Diff line change
@@ -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,
});
};
44 changes: 44 additions & 0 deletions docs/site/pages/_app.mdx
Original file line number Diff line number Diff line change
@@ -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 (

<>
<Head>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gtag.GA_TRACKING_ID}', {
page_path: window.location.pathname,
});
`,
}}
/>
</Head>
{/* Global Site Tag (gtag.js) - Google Analytics */}
<Script
strategy="afterInteractive"
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`}
/>
<Component {...pageProps} />
</>
);
}

0 comments on commit 4377909

Please sign in to comment.