diff --git a/package.json b/package.json index e9bbbaa2..ded99fe7 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "qs": "6.11.0", "react": "18.2.0", "react-dom": "18.2.0", + "react-ga4": "1.4.1", "react-i18next": "11.16.9", "react-router": "6.3.0", "react-router-dom": "6.3.0", diff --git a/src/config/constants.js b/src/config/constants.js index aff4f1e5..8ca4253d 100644 --- a/src/config/constants.js +++ b/src/config/constants.js @@ -7,6 +7,9 @@ const { REACT_APP_DOMAIN: ENV_DOMAIN, } = env; +export const { REACT_APP_GA_MEASUREMENT_ID: GA_MEASUREMENT_ID, NODE_ENV } = + process.env; + export const DOMAIN = ENV_DOMAIN || process.env.REACT_APP_DOMAIN; export const APP_NAME = 'Graasp Authentication'; diff --git a/src/index.js b/src/index.js index 8093aba1..e7f4cdc4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,19 @@ import React from 'react'; import { createRoot } from 'react-dom/client'; +import ReactGA from 'react-ga4'; + +import { hasAcceptedCookies } from '@graasp/sdk'; import Root from './components/Root'; +import { GA_MEASUREMENT_ID, NODE_ENV } from './config/constants'; import './index.css'; import * as serviceWorker from './serviceWorker'; +if (GA_MEASUREMENT_ID && hasAcceptedCookies() && NODE_ENV !== 'test') { + ReactGA.initialize(GA_MEASUREMENT_ID); + ReactGA.send('pageview'); +} + const container = document.getElementById('root'); const root = createRoot(container); // createRoot(container!) if you use TypeScript root.render(); diff --git a/yarn.lock b/yarn.lock index 3a4cfe59..bf353ea3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9208,6 +9208,7 @@ __metadata: qs: 6.11.0 react: 18.2.0 react-dom: 18.2.0 + react-ga4: 1.4.1 react-i18next: 11.16.9 react-router: 6.3.0 react-router-dom: 6.3.0 @@ -14412,6 +14413,13 @@ __metadata: languageName: node linkType: hard +"react-ga4@npm:1.4.1": + version: 1.4.1 + resolution: "react-ga4@npm:1.4.1" + checksum: 4c33ecb47dfc26c241a6af8e2b08b29b6c059f40e456e93d1c2b65c995edce18f32d9777295774ec992d968d7c5efdd105eb62398972d85ff2e2b6dad8646a26 + languageName: node + linkType: hard + "react-i18next@npm:11.16.9": version: 11.16.9 resolution: "react-i18next@npm:11.16.9"