Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing analytics #1694

Merged
merged 6 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/web/lib/hooks/useGoogleAnalytics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CONFIG } from 'config';
import React from 'react';

declare global {
interface Window {
dataLayer: any[];
}
}

const { gaId, appEnv } = CONFIG;

export const Analytics: React.FC = () => {

React.useEffect(() => {
if (!gaId || appEnv !== 'production') {
return; // Explicitly return here
}
// Load Google Analytics script dynamically
const script = document.createElement('script');
script.src = `https://www.googletagmanager.com/gtag/js?id=${gaId}`;
script.async = true;

script.onload = () => {
// Initialize Google Analytics
window.dataLayer = window.dataLayer || [];
function gtag(...args: any) {
window.dataLayer.push(args);
}
gtag('js', new Date());
gtag('config', gaId, {
page_path: window.location.pathname,
});
};
// Append the script to the document body
document.body.appendChild(script);
}, []);

return <></>
};
34 changes: 4 additions & 30 deletions packages/web/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,22 @@ import { ComposeDBContextProvider } from 'contexts/ComposeDBContext';
import { Web3ContextProvider } from 'contexts/Web3Context';
import { wrapUrqlClient } from 'graphql/client';
import { useMounted } from 'lib/hooks';
import { Analytics } from 'lib/hooks/useGoogleAnalytics';
import Head from 'next/head';
import Image from 'next/image';
import PlausibleProvider from 'next-plausible';
import { WithUrqlProps } from 'next-urql';
import React from 'react';

const { userbackToken, honeybadgerAPIKey, gaId, appEnv } = CONFIG;

const Analytics: React.FC = () => {
if (!gaId || appEnv !== 'production') {
return null;
}
return (
<>
<script
async
defer
src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`}
/>
<script
type="text/javascript"
async
defer
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments) }
gtag('js', new Date());
gtag('config', '${gaId}');
`,
}}
/>
</>
);
};
const { userbackToken, honeybadgerAPIKey } = CONFIG;

const App: React.FC<WithUrqlProps> = ({
pageProps,
resetUrqlClient,
Component,
}) => {
const isMounted = useMounted();

if (!isMounted) {
return (
<div
Expand Down Expand Up @@ -92,12 +66,12 @@ const App: React.FC<WithUrqlProps> = ({
<Head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MetaGame</title>
<Analytics />
</Head>
<Web3ContextProvider {...{ resetUrqlClient }}>
<ComposeDBContextProvider>
<MegaMenu hide={pageProps.hideTopMenu}>
<Component {...pageProps} />
<Analytics />
</MegaMenu>
</ComposeDBContextProvider>
</Web3ContextProvider>
Expand Down
Loading