Skip to content

Commit 8d5218d

Browse files
committed
Setup google analytics
1 parent 51959a0 commit 8d5218d

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

public/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
work correctly both with client-side routing and a non-root public URL.
3030
Learn how to configure a non-root public URL by running `npm run build`.
3131
-->
32+
<!-- Global site tag (gtag.js) - Google Analytics -->
33+
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-125599395-1"></script>
34+
<script>
35+
window.dataLayer = window.dataLayer || [];
36+
function gtag(){dataLayer.push(arguments);}
37+
gtag('js', new Date());
38+
39+
gtag('config', 'UA-125599395-1');
40+
</script>
41+
3242
<title>React App</title>
3343
</head>
3444
<body>

src/containers/base/Core.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react';
1+
import React, { useEffect } from 'react';
22
import { connect } from 'react-redux';
33
import OpaqueLayer from '../../components/common/OpaqueLayer';
44
import AuthModalContainer from '../auth/AuthModalContainer';
@@ -8,6 +8,8 @@ import useUserLoader from './hooks/useUserLoader';
88
import GlobalStyles from '../../GlobalStyles';
99
import { ToastContainer, Flip } from 'react-toastify';
1010
import 'react-toastify/dist/ReactToastify.css';
11+
import { useHistory } from 'react-router-dom';
12+
import gtag from '../../lib/gtag';
1113

1214
interface OwnProps {}
1315
interface StateProps {
@@ -19,6 +21,24 @@ type CoreProps = OwnProps & StateProps & DispatchProps;
1921
const Core: React.FC<CoreProps> = ({ layer }) => {
2022
useUserLoader();
2123

24+
const history = useHistory();
25+
26+
useEffect(() => {
27+
const unregister = history.listen(location => {
28+
// adds setTimeout for page title sync
29+
// is there any better solution?
30+
setTimeout(() => {
31+
gtag('config', 'UA-125599395-1', {
32+
page_path: location.pathname + location.search,
33+
});
34+
}, 1000);
35+
});
36+
37+
return () => {
38+
unregister();
39+
};
40+
}, [history]);
41+
2242
return (
2343
<>
2444
<OpaqueLayer visible={layer} />

src/lib/gtag.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function gtag(...params: any[]) {
2+
if (typeof window === 'undefined') return;
3+
const { gtag } = window as any;
4+
if (!gtag) return;
5+
gtag(...params);
6+
}

src/server/Html.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ function Html({
6565
/>
6666
))}
6767
<meta name="viewport" content="width=device-width, initial-scale=1" />
68+
<script
69+
async
70+
src="https://www.googletagmanager.com/gtag/js?id=UA-125599395-1"
71+
></script>
72+
<script
73+
dangerouslySetInnerHTML={{
74+
__html: `window.dataLayer = window.dataLayer || [];
75+
function gtag(){dataLayer.push(arguments);}
76+
gtag('js', new Date());
77+
78+
gtag('config', 'UA-125599395-1');`,
79+
}}
80+
></script>
6881
</head>
6982
<body>
7083
<div id="root" dangerouslySetInnerHTML={{ __html: content }}></div>

0 commit comments

Comments
 (0)