Skip to content

Commit 6cd648f

Browse files
authored
1731 add analytics tag for judge (#1662)
Closes SoftUni-Internal/exam-systems-issues#1731 **Summary of the changes made**: 1. {Describe your changes in the list} 2.
1 parent 94e898d commit 6cd648f

File tree

7 files changed

+61
-4
lines changed

7 files changed

+61
-4
lines changed

Servers/UI/OJS.Servers.Ui/ClientApp/.env.production

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ VITE_UI_SERVER_URL = 'https://alpha.judge.softuni.org'
22
VITE_ADMINISTRATION_URL = 'https://admin.alpha.judge.softuni.org'
33
VITE_PLATFORM_URL = 'https://platform.softuni.bg'
44
VITE_YOUTUBE_VIDEO_ID = 'EjQlqgTbvsE'
5+
VITE_GA_ID=G-19F5E0RPNK

Servers/UI/OJS.Servers.Ui/ClientApp/src/components/portals/client/ClientPortal.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useCallback, useEffect } from 'react';
22
import { Outlet, ScrollRestoration } from 'react-router-dom';
33
import { LocalizationProvider } from '@mui/x-date-pickers';
44
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
5+
import usePageViewTracking from 'src/hooks/use-page-view-tracking';
56

67
import MuiUiThemeProvider from '../../../hooks/use-mui-ui-theme';
78
import useTheme from '../../../hooks/use-theme';
@@ -47,6 +48,8 @@ const ClientPortal = () => {
4748
};
4849
}, [ isDarkMode ]);
4950

51+
usePageViewTracking();
52+
5053
useEffect(() => {
5154
toggleGlobalStyles();
5255
}, [ isDarkMode, toggleGlobalStyles ]);

Servers/UI/OJS.Servers.Ui/ClientApp/src/env.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
2-
2+
33
/// <reference types="vite/client" />
44

55
interface ImportMetaEnv {
66
readonly VITE_UI_SERVER_URL: string;
77
readonly VITE_ADMINISTRATION_URL: string;
88
readonly VITE_PLATFORM_URL: string;
99
readonly VITE_YOUTUBE_VIDEO_ID: string;
10+
readonly VITE_GA_ID: string;
1011
}
1112

1213
declare module '*.css';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
3+
declare global {
4+
interface Window {
5+
gtag?: (...args: any[]) => void;
6+
}
7+
}
8+
9+
export {};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useEffect } from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
4+
const usePageViewTracking = () => {
5+
const location = useLocation();
6+
const GA_ID = import.meta.env.VITE_GA_ID;
7+
8+
useEffect(() => {
9+
if (!GA_ID || import.meta.env.DEV) {
10+
return;
11+
}
12+
13+
if (window.gtag) {
14+
window.gtag(
15+
'config', GA_ID,
16+
{ page_path: location.pathname + location.search },
17+
);
18+
}
19+
}, [ GA_ID, location ]);
20+
};
21+
22+
export default usePageViewTracking;

Servers/UI/OJS.Servers.Ui/ClientApp/src/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
2-
31
import { Suspense } from 'react';
42
import { createRoot } from 'react-dom/client';
3+
import initAnalytics from 'src/utils/initAnalytics';
54

65
import SpinningLoader from './components/guidelines/spinning-loader/SpinningLoader';
76
import { flexCenterObjectStyles } from './utils/object-utils';
87
import App from './App';
98

109
import './styles/global.scss';
1110

11+
initAnalytics();
12+
1213
const container = document.getElementById('root');
1314
const root = createRoot(container!);
1415

15-
const comp =
16+
const comp =
1617
<Suspense fallback={<div style={{ ...flexCenterObjectStyles }}><SpinningLoader /></div>}>
1718
<App />
1819
</Suspense>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const initAnalytics = () => {
2+
const GA_ID = import.meta.env.VITE_GA_ID;
3+
if (!GA_ID || import.meta.env.DEV) {return;}
4+
5+
const script = document.createElement('script');
6+
script.setAttribute('async', '');
7+
script.src = `https://www.googletagmanager.com/gtag/js?id=${GA_ID}`;
8+
document.head.appendChild(script);
9+
10+
const inlineScript = document.createElement('script');
11+
inlineScript.innerHTML = `
12+
window.dataLayer = window.dataLayer || [];
13+
function gtag(){dataLayer.push(arguments);}
14+
gtag('js', new Date());
15+
gtag('config', '${GA_ID}');
16+
`;
17+
document.head.appendChild(inlineScript);
18+
};
19+
20+
export default initAnalytics;

0 commit comments

Comments
 (0)