Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 734bce4

Browse files
authored
feat(website): add Google Analytics (#390)
1 parent 5667240 commit 734bce4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

packages/website/src/pages/_app.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'swiper/css';
33
import type { CSSProperties } from 'react';
44
import type { AppProps } from 'next/app';
55
import Head from 'next/head';
6+
import Script from 'next/script';
67

78
import { ThemeProvider } from '@coderscamp/ui/theme';
89

@@ -16,13 +17,33 @@ const globalStyles: Record<string, CSSProperties> = {
1617
'.swiper-slide': { height: 'unset' },
1718
};
1819

20+
const googleAnalyticsId = process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS;
21+
22+
const GoogleAnalytics = ({ id }: { id: string }) => (
23+
<>
24+
<Script strategy="lazyOnload" src={`https://www.googletagmanager.com/gtag/js?id=${id}`} />
25+
26+
<Script strategy="lazyOnload">
27+
{`
28+
window.dataLayer = window.dataLayer || [];
29+
function gtag(){dataLayer.push(arguments);}
30+
gtag('js', new Date());
31+
gtag('config', '${id}', {
32+
page_path: window.location.pathname,
33+
});
34+
`}
35+
</Script>
36+
</>
37+
);
38+
1939
const userPrefersDark =
2040
typeof window !== 'undefined' && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
2141

2242
// eslint-disable-next-line @typescript-eslint/naming-convention
2343
const MyApp = ({ Component, pageProps }: AppProps) => {
2444
return (
2545
<>
46+
{googleAnalyticsId && <GoogleAnalytics id={googleAnalyticsId} />}
2647
<Head>
2748
<title>CodersCamp - największy otwarty kurs programowania webowego w Polsce</title>
2849
<meta

0 commit comments

Comments
 (0)