Skip to content

Commit

Permalink
feat: SKFP-889 add google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
celinepelletier committed Dec 12, 2023
1 parent 66a12dc commit 9257ba1
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .env.schema
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ REACT_APP_NAMESPACE=dev
# App lifecycle
REACT_APP_MAINTENANCE_MODE=false

# Google Analytics
REACT_APP_MEASUREMENT_ID=

# APIS
REACT_APP_PERSONA_API=
REACT_APP_USERS_API_URL=
Expand All @@ -20,7 +23,6 @@ REACT_APP_ARRANGER_PROJECT_ID=
REACT_APP_CAVATICA_USER_BASE_URL=
REACT_APP_VARIANT_CLUSTER_API=

jkjdkljksjd;w
# FENCES
REACT_APP_KEY_MANAGER_API_URL=
REACT_APP_FENCE_API_URL=
Expand Down
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"react-app-polyfill": "^3.0.0",
"react-dev-utils": "^12.0.1",
"react-dom": "^17.0.2",
"react-ga4": "^2.1.0",
"react-highlight-words": "^0.18.0",
"react-intl-universal": "^2.4.8",
"react-redux": "^7.2.6",
Expand Down
3 changes: 3 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import Spinner from 'components/uiKit/Spinner';
import NotificationContextHolder from 'components/utils/NotificationContextHolder';
import { useLang } from 'store/global';
import { DYNAMIC_ROUTES, STATIC_ROUTES } from 'utils/routes';
import { initGa } from 'services/analytics';

const loadableProps = { fallback: <Spinner size="large" /> };
const Dashboard = loadable(() => import('views/Dashboard'), loadableProps);
Expand All @@ -53,6 +54,8 @@ const VariantEntity2 = loadable(() => import('views/VariantEntity'), loadablePro
const FileEntity = loadable(() => import('views/FileEntity'), loadableProps);
const ProfileSettings = loadable(() => import('views/Profile/Settings'), loadableProps);

initGa();

const App = () => {
const lang = useLang();
const { keycloak, initialized } = useKeycloak();
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/EnvVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default class EnvironmentVariables {
REPORTS_API_URL: process.env.REACT_APP_REPORTS_API_URL,
// MAINTENANCE
MAINTENANCE_MODE: process.env.REACT_APP_MAINTENANCE_MODE,
// GA
MEASUREMENT_ID: process.env.REACT_APP_MEASUREMENT_ID,
};

static configFor(key: string): string {
Expand Down
5 changes: 5 additions & 0 deletions src/provider/KeycloakProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import EnvVariables from "helpers/EnvVariables";
import { ReactKeycloakProvider as KeycloakProvider } from "@react-keycloak/web";
import keycloak from "auth/keycloak-api/keycloak";
import { trackAuthSuccess } from "services/analytics";

export interface IProvider {
children: React.ReactNode;
Expand All @@ -16,6 +17,10 @@ const eventLogger = (eventType: AuthClientEvent, error?: AuthClientError) => {
console.error("eventLogger ", "eventType ", eventType);
console.error("eventLogger ", error);
}

if (eventType === 'onAuthSuccess') {
trackAuthSuccess();
}
};

const Keycloak = ({ children }: IProvider): ReactElement => (
Expand Down
21 changes: 21 additions & 0 deletions src/services/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import EnvironmentVariables from 'helpers/EnvVariables';
import ReactGA from 'react-ga4';

const measurementId = EnvironmentVariables.configFor('MEASUREMENT_ID');
const isDev = EnvironmentVariables.configFor('ENV') === 'development';
const isGaActive = measurementId && !isDev;

export const initGa = () => {
if (isGaActive) {
ReactGA.initialize(measurementId);
}
};

export const trackAuthSuccess = () => {
if (isGaActive) {
ReactGA.event({
category: 'Authentication',
action: 'login success',
});
}
};

0 comments on commit 9257ba1

Please sign in to comment.