Skip to content

Commit

Permalink
feat(website): add google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
stdavis committed Apr 29, 2024
1 parent 5eb91e7 commit 29d2de3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dataframe",
"dataframes",
"datetimepicker",
"DCLT",
"drivername",
"Dsplay",
"dtypes",
Expand All @@ -31,6 +32,7 @@
"googleapis",
"GRAMA",
"gserviceaccount",
"gtags",
"hemionus",
"IDFA",
"iloc",
Expand Down Expand Up @@ -64,6 +66,7 @@
"ondevice",
"openid",
"packagejson",
"pageview",
"palletjack",
"Podfile",
"postgis",
Expand Down
34 changes: 34 additions & 0 deletions src/website/app/root.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Links, LiveReload, Meta, Outlet, Scripts, ScrollRestoration } from '@remix-run/react';
import styles from './tailwind.css';
import * as gtag from "~/utils/gtags.client";
import { useEffect } from 'react';

export function meta() {
return [{ title: 'Utah Roadkill Reporter' }];
Expand All @@ -9,7 +11,16 @@ export function links() {
return [{ rel: 'stylesheet', href: styles }];
}

const gaTrackingId = 'G-P135E0DCLT';

export default function App() {

useEffect(() => {
if (gaTrackingId?.length) {
gtag.pageview(location.pathname, gaTrackingId);
}
}, []);

return (
<html lang="en">
<head>
Expand All @@ -18,6 +29,29 @@ export default function App() {
<Meta />
<Links />
</head>
{process.env.NODE_ENV === "development" || !gaTrackingId ? null : (
<>
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${gaTrackingId}`}
/>
<script
async
id="gtag-init"
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gaTrackingId}', {
page_path: window.location.pathname,
});
`,
}}
/>
</>
)}
<body className="p-10 font-sans text-gray-700">
<Outlet />
<ScrollRestoration />
Expand Down
40 changes: 40 additions & 0 deletions src/website/app/utils/gtags.client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ref: https://github.com/remix-run/examples/tree/main/google-analytics

/**
* @example
* https://developers.google.com/analytics/devguides/collection/gtagjs/pages
*/
export const pageview = (url, trackingId) => {
if (!window.gtag) {
console.warn(
"window.gtag is not defined. This could mean your google analytics script has not loaded on the page yet.",
);
return;
}
window.gtag("config", trackingId, {
page_path: url,
});
};

/**
* @example
* https://developers.google.com/analytics/devguides/collection/gtagjs/events
*/
export const event = ({
action,
category,
label,
value,
}) => {
if (!window.gtag) {
console.warn(
"window.gtag is not defined. This could mean your google analytics script has not loaded on the page yet.",
);
return;
}
window.gtag("event", action, {
event_category: category,
event_label: label,
value: value,
});
};

0 comments on commit 29d2de3

Please sign in to comment.