Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URO 95: Configure Google Analytics 4 #128

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-KXZCE6YQFZ"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-KXZCE6YQFZ', { send_page_view: false });
</script>
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="icon" href="https://www.kbase.us/wp-content/uploads/sites/6/2020/08/kbase-favicon-32-bgwhite_rounded_corners.png" sizes="192x192" />
<link rel="apple-touch-icon" href="https://www.kbase.us/wp-content/uploads/sites/6/2020/08/kbase-favicon-32-bgwhite_rounded_corners.png" />
Expand Down
7 changes: 6 additions & 1 deletion src/app/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ import {
detailPath,
detailDataProductPath,
} from '../features/collections/Collections';
import { useAppSelector, useFilteredParams } from '../common/hooks';
import {
useAppSelector,
useFilteredParams,
usePageTracking,
} from '../common/hooks';

export const LOGIN_ROUTE = '/legacy/login';
export const ROOT_REDIRECT_ROUTE = '/narratives';

const Routes: FC = () => {
useFilteredParams();
usePageTracking();
return (
<RRRoutes>
<Route path={`${LEGACY_BASE_ROUTE}/*`} element={<Legacy />} />
Expand Down
23 changes: 23 additions & 0 deletions src/common/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import { useCallback, useEffect, useRef, useState } from 'react';
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import { useLocation, useNavigate } from 'react-router-dom';
import type { RootState, AppDispatch } from '../app/store';
import { authMe } from '../features/auth/authSlice';
import {
generatePathWithSearchParams,
isValidParam,
setParams,
} from '../features/params/paramsSlice';

declare global {
interface Window {
gtag: Function;
}
}
// Use throughout the app instead of plain `useDispatch` and `useSelector`
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
Expand Down Expand Up @@ -88,3 +94,20 @@ export const useFilteredParams = () => {
}, [check.length, dispatch, loc.pathname, navigate, paramsFiltered]);
return paramsFiltered;
};

export const usePageTracking = () => {
const location = useLocation();
const me = useAppSelector(authMe);

useEffect(() => {
const pageView: Record<string, string> = {
page_path: location.pathname + location.search + location.hash,
page_search: location.search,
page_hash: location.hash,
};
if (me && me.anonid) {
pageView.user_id = me.anonid;
}
window.gtag('event', 'page_view', pageView);
}, [location, me]);
};
1 change: 1 addition & 0 deletions src/features/navigator/Navigator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const logError = (error: Error, info: { componentStack: string }) => {
describe('The <Navigator /> component...', () => {
beforeAll(() => {
enableFetchMocks();
window.gtag = jest.fn();
});

afterAll(() => {
Expand Down
1 change: 1 addition & 0 deletions src/features/profile/Profile.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ consoleError.mockImplementation(() => {});
describe('Profile related components', () => {
beforeAll(() => {
enableFetchMocks();
window.gtag = jest.fn();
});
afterAll(() => {
consoleError.mockRestore();
Expand Down
Loading