Skip to content

Commit

Permalink
fix: analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
apalchys committed Nov 19, 2023
1 parent a48fbab commit 9eb399c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@types/cookie": "0.5.4",
"@types/jest": "29.5.8",
"@types/lodash": "4.14.201",
"@types/node": "^18.18.10",
"@types/node": "20.9.2",
"@types/react": "18.2.37",
"@types/react-dom": "18.2.7",
"@typescript-eslint/eslint-plugin": "6.6.0",
Expand Down
4 changes: 3 additions & 1 deletion client/src/components/Timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function Timer({ onElapsed, seconds }: { onElapsed: () => void; seconds:
onElapsed();
}
return () => {
timeout && clearTimeout(timeout);
if (timeout) {
clearTimeout(timeout as NodeJS.Timeout);
}
};
}, [leftSeconds]);

Expand Down
2 changes: 2 additions & 0 deletions client/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import Head from 'next/head';
import 'antd/dist/reset.css';
import '../styles/main.css';
import { initializeFeatures } from 'services/features';
import { Analytics } from './analytics';

class RsSchoolApp extends App {
render() {
const { Component, pageProps, router } = this.props;
initializeFeatures(router.query);
return (
<>
<Analytics />
<Head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>App / The Rolling Scopes School</title>
Expand Down
21 changes: 0 additions & 21 deletions client/src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import * as React from 'react';
import Document, { Head, Html, Main, NextScript } from 'next/document';
import Script from 'next/script';

const enableAnalytics = process.env.NODE_ENV === 'production';

export default class extends Document {
render() {
return (
<Html lang="en">
<Head>
<link rel="shortcut icon" href="https://rs.school/favicon.ico" />

{enableAnalytics && <Script async src="https://www.googletagmanager.com/gtag/js?id=G-WJLHZ9CCXJ" />}
{enableAnalytics && <script dangerouslySetInnerHTML={{ __html: gaJsCode }} />}
{enableAnalytics && (
<script
defer
src="https://static.cloudflareinsights.com/beacon.min.js"
data-cf-beacon={'{"token": "e607238d732c4713b01b851ed3df61c2"}'}
/>
)}
</Head>
<body>
<Main />
Expand All @@ -29,11 +16,3 @@ export default class extends Document {
);
}
}

const gaJsCode = `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-WJLHZ9CCXJ');
`;
30 changes: 30 additions & 0 deletions client/src/pages/analytics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Script from 'next/script';

const enableAnalytics = process.env.NODE_ENV === 'production';

export function Analytics() {
if (!enableAnalytics) {
return null;
}

return (
<>
<Script defer src="https://www.googletagmanager.com/gtag/js?id=G-WJLHZ9CCXJ" />
<Script id="google-analytics">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-WJLHZ9CCXJ');
`}
</Script>

<Script
defer
src="https://static.cloudflareinsights.com/beacon.min.js"
data-cf-beacon={'{"token": "e607238d732c4713b01b851ed3df61c2"}'}
/>
</>
);
}
22 changes: 20 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9eb399c

Please sign in to comment.