Skip to content

Commit 0590562

Browse files
committed
Handle Google analytics
1 parent 0b81d4f commit 0590562

File tree

4 files changed

+44
-68
lines changed

4 files changed

+44
-68
lines changed

client/modules/User/components/CookieConsent.jsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
22
import { useSelector, useDispatch } from 'react-redux';
33
import Cookies from 'js-cookie';
44
import styled from 'styled-components';
5+
import ReactGA from 'react-ga';
56
import getConfig from '../../../utils/getConfig';
67
import { setUserCookieConsent } from '../actions';
78
import { remSize, prop } from '../../../theme';
@@ -61,7 +62,6 @@ function CookieConsent() {
6162
function acceptAllCookies() {
6263
if (user.authenticated) {
6364
dispatch(setUserCookieConsent('all'));
64-
return;
6565
}
6666
setBrowserCookieConsent('all');
6767
Cookies.set('p5-cookie-consent', 'all', { expires: 365 });
@@ -70,10 +70,12 @@ function CookieConsent() {
7070
function acceptEssentialCookies() {
7171
if (user.authenticated) {
7272
dispatch(setUserCookieConsent('essential'));
73-
return;
7473
}
7574
setBrowserCookieConsent('essential');
7675
Cookies.set('p5-cookie-consent', 'essential', { expires: 365 });
76+
Cookies.remove('_ga');
77+
Cookies.remove('_gat');
78+
Cookies.remove('_gid');
7779
}
7880

7981
function mergeCookieConsent() {
@@ -94,18 +96,30 @@ function CookieConsent() {
9496
} else {
9597
initializeCookieConsent();
9698
}
99+
100+
if (p5CookieConsent === 'essential') {
101+
ReactGA.initialize(getConfig('GA_MEASUREMENT_ID'), {
102+
gaOptions: {
103+
storage: 'none'
104+
}
105+
});
106+
} else {
107+
ReactGA.initialize(getConfig('GA_MEASUREMENT_ID'));
108+
}
109+
ReactGA.pageview(window.location.pathname + window.location.search);
97110
}, []);
98111

99112
useEffect(() => {
100113
mergeCookieConsent();
101114
}, [user.authenticated]);
102115

103116
// Turn off Google Analytics
104-
useEffect(() => {
105-
if (cookieConsent === 'essential' || user.cookieConsent === 'essential') {
106-
window[`ga-disable-${getConfig('GA_MEASUREMENT_ID')}`] = true;
107-
}
108-
}, [cookieConsent, user.cookieConsent]);
117+
// useEffect(() => {
118+
// if (cookieConsent === 'all' || user.cookieConsent === 'all') {
119+
// ReactGA.initialize(getConfig('GA_MEASUREMENT_ID'));
120+
// ReactGA.pageview(window.location.pathname + window.location.search);
121+
// }
122+
// }, [cookieConsent, user.cookieConsent]);
109123

110124
const showCookieConsent =
111125
(user.authenticated && user.cookieConsent === 'none') ||

package-lock.json

Lines changed: 22 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
"react": "^16.12.0",
207207
"react-dom": "^16.12.0",
208208
"react-final-form": "^6.5.2",
209+
"react-ga": "^3.3.0",
209210
"react-helmet": "^5.1.3",
210211
"react-hot-loader": "^4.12.19",
211212
"react-i18next": "^11.5.0",

server/views/index.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ export function renderIndex() {
4242
<div id="root" class="root-app">
4343
</div>
4444
<script src='${process.env.NODE_ENV === 'production' ? `${assetsManifest['/app.js']}` : '/app.js'}'></script>
45-
<script>
46-
${process.env.GA_MEASUREMENT_ID &&
47-
`(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
48-
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
49-
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
50-
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
51-
52-
ga('create', '${process.env.GA_MEASUREMENT_ID}', 'auto');
53-
ga('send', 'pageview');`
54-
}
55-
</script>
5645
</body>
5746
</html>
5847
`;

0 commit comments

Comments
 (0)