Skip to content

Commit

Permalink
adding GA to marketing (#417)
Browse files Browse the repository at this point in the history
* adding GA to marketing

* adding the right code

* trying with root url

* fixing sandbox

---------

Co-authored-by: Nick Grato <[email protected]>
  • Loading branch information
nickgrato and Nick Grato authored Oct 3, 2023
1 parent 9e05364 commit 85efea4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 17 deletions.
37 changes: 29 additions & 8 deletions marketing/layouts/LayoutWrapper/LayoutWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ThemeContext } from 'contexts/ThemeProvider';
import Nav from '@Navigation/Nav/Nav';
import Footer from '@Navigation/Footer/Footer';
import { NavigationT } from 'types';
import Head from 'next/head';
import { GA_TRACKING_ID } from 'services/analytics.service';

type LayoutWrapperPropsT = {
navData?: NavigationT;
Expand All @@ -18,14 +20,33 @@ const LayoutWrapper = ({ navData, children }: LayoutWrapperPropsT) => {
const themeContext = useContext(ThemeContext);

return (
// Hard coding light while dark theme is being designed
// <main data-theme={themeContext.theme}>
<main data-theme="light">
<div id="LP_modal_portal" />
<Nav navData={navData} />
{children}
<Footer />
</main>
<>
<Head>
{/* Google Analytics script */}
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`}
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', '${GA_TRACKING_ID}');
`,
}}
/>
</Head>
<main data-theme="light">
<div id="LP_modal_portal" />
<Nav navData={navData} />
{children}
<Footer />
</main>
</>
);
};

Expand Down
23 changes: 23 additions & 0 deletions marketing/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,31 @@ import NotificationProvider from 'contexts/NotificationProvider';
import favicon32 from '../public/favicon-32x32.png';
import favicon16 from '../public/favicon-16x16.png';
import appleTouch from '../public/apple-touch-icon.png';
import { pageview } from 'services/analytics.service';
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import getEnvVariable from 'config';

function MyApp({ Component, pageProps }: AppProps) {
const router = useRouter();

useEffect(() => {
const root = getEnvVariable('DASH_ROOT_DOMAIN');
pageview(root + router.route);

const handleRouteChange = (url: string) => {
pageview(root + url);
};

// Listen for route changes and call the handler
router.events.on('routeChangeComplete', handleRouteChange);

// Remove the event listener on unmount
return () => {
router.events.off('routeChangeComplete', handleRouteChange);
};
}, [router.events]);

return (
<ThemeProvider>
<NotificationProvider>
Expand Down
20 changes: 11 additions & 9 deletions marketing/pages/sandbox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Head from 'next/head';

import LayoutWrapper from 'layouts/LayoutWrapper/LayoutWrapper';
type SandboxPropsT = {};

/**
Expand All @@ -8,14 +8,16 @@ type SandboxPropsT = {};
*/
const Sandbox = ({}: SandboxPropsT) => {
return (
<div className="page_wrapper">
<Head>
<title>Sandbox</title>
</Head>
<main>
<h1>Sandbox</h1>
</main>
</div>
<LayoutWrapper>
<div className="page_wrapper">
<Head>
<title>Sandbox</title>
</Head>
<main>
<h1>Sandbox</h1>
</main>
</div>
</LayoutWrapper>
);
};

Expand Down
14 changes: 14 additions & 0 deletions marketing/services/analytics.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare global {
interface Window {
gtag: (...args: any[]) => void;
}
}

export const GA_TRACKING_ID = 'G-4W4Q68ZFMX';

// Function to track page views
export const pageview = (url: string) => {
window.gtag('config', GA_TRACKING_ID, {
page_path: url,
});
};

0 comments on commit 85efea4

Please sign in to comment.