Skip to content

Commit 2856d7c

Browse files
committed
Update cookie consent
1 parent 209f888 commit 2856d7c

File tree

3 files changed

+81
-23
lines changed

3 files changed

+81
-23
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import React, { useEffect, useState } from 'react';
2+
3+
import { Alert, Button, Box, Backdrop } from '@mui/material';
4+
import { styled } from '@mui/material/styles';
5+
6+
const Root = styled(Backdrop)(() => ({
7+
'&.backdrop': {
8+
zIndex: 10000,
9+
},
10+
'& .cookie-alert': {
11+
minHeight: '100px',
12+
bottom: 0,
13+
position: 'fixed',
14+
},
15+
}));
16+
17+
const CookieModal = () => {
18+
const [consent, setConsent] = useState();
19+
20+
useEffect(() => {
21+
setConsent(localStorage.getItem('data-driven-forms-cookie-consent'));
22+
}, []);
23+
24+
useEffect(() => {
25+
if (consent === 'google') {
26+
if (!window.location.origin.includes('localhost')) {
27+
let s = document.createElement('script');
28+
s.type = 'text/javascript';
29+
s.async = 'true';
30+
s.src = 'https://www.googletagmanager.com/gtag/js?id=UA-164334905-1';
31+
let x = document.getElementsByTagName('script')[0];
32+
x.parentNode.insertBefore(s, x);
33+
34+
window.dataLayer = window.dataLayer || [];
35+
36+
// eslint-disable-next-line no-inner-declarations
37+
function gtag() {
38+
window.dataLayer.push(arguments);
39+
}
40+
41+
window.gtag = gtag;
42+
global.gtag = gtag;
43+
44+
gtag('js', new Date());
45+
// eslint-disable-next-line camelcase
46+
gtag('config', 'UA-164334905-1', { anonymize_ip: true });
47+
}
48+
}
49+
}, [consent]);
50+
51+
const updateConsent = (type) => {
52+
setConsent(type);
53+
localStorage.setItem('data-driven-forms-cookie-consent', type);
54+
};
55+
56+
if (consent) {
57+
return null;
58+
}
59+
60+
return (
61+
<Root className="backdrop" open={true}>
62+
<Alert className="cookie-alert" severity="info">
63+
Hello, we would like to use cookies to track your anonymised information in Google Analytics. We are using this information to know what kind
64+
of users visits and uses our library. It also helps us to know what content is the the most read. We are also using neccessary cookies to know
65+
what notifications to load and to store information about this consent.
66+
<Box>
67+
<Button variant="outlined" sx={{ marginRight: 1 }} onClick={() => updateConsent('neccessary')}>
68+
Allow only neccessary cookies
69+
</Button>
70+
<Button variant="outlined" onClick={() => updateConsent('google')}>
71+
Allow Google analytics
72+
</Button>
73+
</Box>
74+
</Alert>
75+
</Root>
76+
);
77+
};
78+
79+
export default CookieModal;

packages/react-renderer-demo/src/pages/_app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import theme from '../theme';
88
import Layout from '@docs/components/layout';
99
import { MDXProvider } from '@mdx-js/react';
1010
import MdxComponents from '@docs/components/mdx/mdx-components';
11+
import CookieModal from '../components/cookie-modal';
1112

1213
export default class MyApp extends App {
1314
componentDidMount() {
@@ -35,6 +36,7 @@ export default class MyApp extends App {
3536
<Component {...pageProps} />
3637
</Layout>
3738
</MDXProvider>
39+
<CookieModal />
3840
</ThemeProvider>
3941
</StyledEngineProvider>
4042
</React.Fragment>

packages/react-renderer-demo/src/pages/_document.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class MyDocument extends Document {
1818
<link rel="preload" as="style" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
1919
<link rel="preload" as="style" href="https://fonts.googleapis.com/css?family=Montserrat:700&display=swap" />
2020
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css" />
21-
<link rel="stylesheet" type="text/css" href="https://wpcc.io/lib/1.0.2/cookieconsent.min.css" />
22-
<script src="https://wpcc.io/lib/1.0.2/cookieconsent.min.js"></script>
2321
<style
2422
dangerouslySetInnerHTML={{
2523
__html: `
@@ -50,27 +48,6 @@ class MyDocument extends Document {
5048
<body>
5149
<Main />
5250
<NextScript />
53-
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-164334905-1"></script>
54-
<script
55-
dangerouslySetInnerHTML={{
56-
__html: `
57-
(function() {
58-
if(!window.location.origin.includes("localhost")) {
59-
window.dataLayer = window.dataLayer || [];
60-
function gtag(){dataLayer.push(arguments);}
61-
gtag('js', new Date());
62-
63-
gtag('config', 'UA-164334905-1');
64-
}
65-
})()
66-
`,
67-
}}
68-
></script>
69-
<script
70-
dangerouslySetInnerHTML={{
71-
__html: `window.addEventListener("load", function(){window.wpcc.init({"border":"thin","corners":"small","colors":{"popup":{"background":"#f6f6f6","text":"#000000","border":"#555555"},"button":{"background":"#555555","text":"#ffffff"}}})});`,
72-
}}
73-
/>
7451
</body>
7552
</html>
7653
);

0 commit comments

Comments
 (0)