-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
593 google analytics not displaying page specific hits (#806)
* feat: update gtags * feat: updated tags with mapped routes
- Loading branch information
Showing
7 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
compose/neurosynth-frontend/src/hooks/useGoogleAnalytics.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { routeMapping } from './useGoogleAnalytics'; | ||
|
||
describe('useGoogleAnalytics', () => { | ||
test.each` | ||
path | expected | ||
${'/projects/g7VRaCLw3iZJ/curation'} | ${'curation page'} | ||
${'/projects/g7VRaCLw3iZJ/curation/import'} | ${'curation import page'} | ||
${'/projects/g7VRaCLw3iZJ/curation/import?pageOfResults=2'} | ${'curation import page'} | ||
${'/projects/g7VRaCLw3iZJ/extraction'} | ${'extraction page'} | ||
${'/projects/g7VRaCLw3iZJ/extraction/annotations'} | ${'annotations page'} | ||
${'/projects'} | ${'projects page'} | ||
${'/projects/g7VRaCLw3iZJ/project'} | ${'project page'} | ||
${'/projects/6zW6TJtzpFjr/meta-analyses'} | ${'project meta-analyses page'} | ||
${'/projects/98ZZj2vbpySw/meta-analyses/8K6KhGEfTy6H'} | ${'project meta-analysis page'} | ||
${'/projects/new/sleuth'} | ${'sleuth import page'} | ||
${'/base-studies'} | ${'base-studies page'} | ||
${'/meta-analyses'} | ${'meta-analyses page'} | ||
${'/meta-analyses/BdCib6uM3QDX'} | ${'meta-analysis page'} | ||
${'/base-studies/FSfE96JVaPuU/5neCyoMwEvrM'} | ${'base-study page'} | ||
${'/base-studies/FSfE96JVaPuU'} | ${'base-study page'} | ||
${'/projects/g7VRaCLw3iZJ/extraction/studies/v7RBrDFEbAC7/edit'} | ${'edit project study page'} | ||
${'/projects/g7VRaCLw3iZJ/extraction/studies/v7RBrDFEbAC7'} | ${'project study page'} | ||
${'/user-profile'} | ${'user profile page'} | ||
${'/forbidden'} | ${'forbidden page'} | ||
${'/termsandconditions'} | ${'terms and conditions page'} | ||
${'/not-found'} | ${'not found page'} | ||
`('transforms $path to $expected', ({ path, expected }) => { | ||
expect(routeMapping(path)).toEqual(expected); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
compose/neurosynth-frontend/src/hooks/useGoogleAnalytics.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useEffect } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
export const routeMapping = (path: string) => { | ||
if (/^\/projects\/.*\/curation\/import.*$/g.test(path)) { | ||
return 'curation import page'; | ||
} else if (/^\/projects\/.*\/curation$/g.test(path)) { | ||
return 'curation page'; | ||
} else if (/^\/projects\/.*\/project$/g.test(path)) { | ||
return 'project page'; | ||
} else if (/^\/projects$/g.test(path)) { | ||
return 'projects page'; | ||
} else if (/^\/projects\/.*\/meta-analyses\/.*/g.test(path)) { | ||
return 'project meta-analysis page'; | ||
} else if (/^\/projects\/.*\/meta-analyses$/g.test(path)) { | ||
return 'project meta-analyses page'; | ||
} else if (/^\/projects\/new\/sleuth$/g.test(path)) { | ||
return 'sleuth import page'; | ||
} else if (/^\/base-studies$/g.test(path)) { | ||
return 'base-studies page'; | ||
} else if (/^\/base-studies\/.*$/g.test(path)) { | ||
return 'base-study page'; | ||
} else if (/^\/meta-analyses\/.*$/g.test(path)) { | ||
return 'meta-analysis page'; | ||
} else if (/^\/meta-analyses$/g.test(path)) { | ||
return 'meta-analyses page'; | ||
} else if (/^\/projects\/.*\/extraction\/studies\/.*\/edit$/g.test(path)) { | ||
return 'edit project study page'; | ||
} else if (/^\/projects\/.*\/extraction\/studies\/.*$/g.test(path)) { | ||
return 'project study page'; | ||
} else if (/^\/projects\/.*\/extraction\/annotations$/g.test(path)) { | ||
return 'annotations page'; | ||
} else if (/^\/projects\/.*\/extraction$/g.test(path)) { | ||
return 'extraction page'; | ||
} else if (/^\/user-profile$/g.test(path)) { | ||
return 'user profile page'; | ||
} else if (/^\/forbidden$/g.test(path)) { | ||
return 'forbidden page'; | ||
} else if (/^\/termsandconditions$/g.test(path)) { | ||
return 'terms and conditions page'; | ||
} else { | ||
return 'not found page'; | ||
} | ||
}; | ||
|
||
const useGoogleAnalytics = () => { | ||
const location = useLocation(); | ||
|
||
useEffect(() => { | ||
if (window.gtag) { | ||
window.gtag('event', 'page_view', { | ||
page_path: routeMapping(location.pathname + location.search), | ||
}); | ||
} | ||
}, [location]); | ||
}; | ||
|
||
export default useGoogleAnalytics; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters