Skip to content

Commit 0308aa1

Browse files
authored
593 google analytics not displaying page specific hits (#806)
* feat: update gtags * feat: updated tags with mapped routes
1 parent a19dd3f commit 0308aa1

File tree

7 files changed

+162
-4
lines changed

7 files changed

+162
-4
lines changed

compose/neurosynth-frontend/package-lock.json

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compose/neurosynth-frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"@testing-library/dom": "^8.3.0",
9393
"@testing-library/jest-dom": "^5.14.1",
9494
"@testing-library/react": "^11.2.7",
95+
"@testing-library/react-hooks": "^8.0.1",
9596
"@testing-library/user-event": "^12.8.3",
9697
"@types/node": "^18.6.1",
9798
"cypress": "^13.2.0",

compose/neurosynth-frontend/public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap"
2525
crossorigin="anonymous"
2626
/>
27+
2728
<!-- Google tag (gtag.js) -->
2829
<script async src="https://www.googletagmanager.com/gtag/js?id=G-0L9Y0HT9NR"></script>
2930
<script type="text/javascript">

compose/neurosynth-frontend/src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Close from '@mui/icons-material/Close';
22
import { IconButton } from '@mui/material';
33
import { AxiosError } from 'axios';
4+
import useGoogleAnalytics from 'hooks/useGoogleAnalytics';
45
import { SnackbarKey, SnackbarProvider } from 'notistack';
56
import { useEffect, useRef } from 'react';
67
import { QueryCache, QueryClient, QueryClientProvider } from 'react-query';
@@ -40,6 +41,7 @@ declare global {
4041
function App() {
4142
const notistackRef = useRef<SnackbarProvider>(null);
4243
useGetToken();
44+
useGoogleAnalytics();
4345

4446
const location = useLocation();
4547
useEffect(() => {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { routeMapping } from './useGoogleAnalytics';
2+
3+
describe('useGoogleAnalytics', () => {
4+
test.each`
5+
path | expected
6+
${'/projects/g7VRaCLw3iZJ/curation'} | ${'curation page'}
7+
${'/projects/g7VRaCLw3iZJ/curation/import'} | ${'curation import page'}
8+
${'/projects/g7VRaCLw3iZJ/curation/import?pageOfResults=2'} | ${'curation import page'}
9+
${'/projects/g7VRaCLw3iZJ/extraction'} | ${'extraction page'}
10+
${'/projects/g7VRaCLw3iZJ/extraction/annotations'} | ${'annotations page'}
11+
${'/projects'} | ${'projects page'}
12+
${'/projects/g7VRaCLw3iZJ/project'} | ${'project page'}
13+
${'/projects/6zW6TJtzpFjr/meta-analyses'} | ${'project meta-analyses page'}
14+
${'/projects/98ZZj2vbpySw/meta-analyses/8K6KhGEfTy6H'} | ${'project meta-analysis page'}
15+
${'/projects/new/sleuth'} | ${'sleuth import page'}
16+
${'/base-studies'} | ${'base-studies page'}
17+
${'/meta-analyses'} | ${'meta-analyses page'}
18+
${'/meta-analyses/BdCib6uM3QDX'} | ${'meta-analysis page'}
19+
${'/base-studies/FSfE96JVaPuU/5neCyoMwEvrM'} | ${'base-study page'}
20+
${'/base-studies/FSfE96JVaPuU'} | ${'base-study page'}
21+
${'/projects/g7VRaCLw3iZJ/extraction/studies/v7RBrDFEbAC7/edit'} | ${'edit project study page'}
22+
${'/projects/g7VRaCLw3iZJ/extraction/studies/v7RBrDFEbAC7'} | ${'project study page'}
23+
${'/user-profile'} | ${'user profile page'}
24+
${'/forbidden'} | ${'forbidden page'}
25+
${'/termsandconditions'} | ${'terms and conditions page'}
26+
${'/not-found'} | ${'not found page'}
27+
`('transforms $path to $expected', ({ path, expected }) => {
28+
expect(routeMapping(path)).toEqual(expected);
29+
});
30+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { useEffect } from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
4+
export const routeMapping = (path: string) => {
5+
if (/^\/projects\/.*\/curation\/import.*$/g.test(path)) {
6+
return 'curation import page';
7+
} else if (/^\/projects\/.*\/curation$/g.test(path)) {
8+
return 'curation page';
9+
} else if (/^\/projects\/.*\/project$/g.test(path)) {
10+
return 'project page';
11+
} else if (/^\/projects$/g.test(path)) {
12+
return 'projects page';
13+
} else if (/^\/projects\/.*\/meta-analyses\/.*/g.test(path)) {
14+
return 'project meta-analysis page';
15+
} else if (/^\/projects\/.*\/meta-analyses$/g.test(path)) {
16+
return 'project meta-analyses page';
17+
} else if (/^\/projects\/new\/sleuth$/g.test(path)) {
18+
return 'sleuth import page';
19+
} else if (/^\/base-studies$/g.test(path)) {
20+
return 'base-studies page';
21+
} else if (/^\/base-studies\/.*$/g.test(path)) {
22+
return 'base-study page';
23+
} else if (/^\/meta-analyses\/.*$/g.test(path)) {
24+
return 'meta-analysis page';
25+
} else if (/^\/meta-analyses$/g.test(path)) {
26+
return 'meta-analyses page';
27+
} else if (/^\/projects\/.*\/extraction\/studies\/.*\/edit$/g.test(path)) {
28+
return 'edit project study page';
29+
} else if (/^\/projects\/.*\/extraction\/studies\/.*$/g.test(path)) {
30+
return 'project study page';
31+
} else if (/^\/projects\/.*\/extraction\/annotations$/g.test(path)) {
32+
return 'annotations page';
33+
} else if (/^\/projects\/.*\/extraction$/g.test(path)) {
34+
return 'extraction page';
35+
} else if (/^\/user-profile$/g.test(path)) {
36+
return 'user profile page';
37+
} else if (/^\/forbidden$/g.test(path)) {
38+
return 'forbidden page';
39+
} else if (/^\/termsandconditions$/g.test(path)) {
40+
return 'terms and conditions page';
41+
} else {
42+
return 'not found page';
43+
}
44+
};
45+
46+
const useGoogleAnalytics = () => {
47+
const location = useLocation();
48+
49+
useEffect(() => {
50+
if (window.gtag) {
51+
window.gtag('event', 'page_view', {
52+
page_path: routeMapping(location.pathname + location.search),
53+
});
54+
}
55+
}, [location]);
56+
};
57+
58+
export default useGoogleAnalytics;

compose/neurosynth-frontend/src/pages/Project/ProjectPage.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ const ProjectPage: React.FC = (props) => {
5656
!getProjectIsLoading && getProjectIsError
5757
);
5858

59-
const tab = useMemo(
60-
() => (location.pathname.includes('meta-analyses') ? 1 : 0),
61-
[location.pathname]
62-
);
59+
const tab = useMemo(() => {
60+
if (!metaAnalysesTabEnabled) return 0;
61+
return location.pathname.includes('meta-analyses') ? 1 : 0;
62+
}, [location.pathname, metaAnalysesTabEnabled]);
6363

6464
return (
6565
<StateHandlerComponent isLoading={getProjectIsLoading} isError={getProjectIsError}>

0 commit comments

Comments
 (0)