1
- ---
2
- const isProd = import .meta .env .PROD ;
3
- ---
4
- { isProd ? (
5
- <>
6
- <!-- Global site tag (gtag.js) - Google Analytics -->
7
- <script async src = " https://www.googletagmanager.com/gtag/js?id=G-1Y0GZ34XC7" ></script >
8
- <script >
9
- window.dataLayer = window.dataLayer || [];
10
- function gtag(){ dataLayer .push (arguments );}
11
- gtag('js', new Date());
12
-
13
- gtag('config', 'G-1Y0GZ34XC7');
14
- </script >
15
- </>
16
- ) : null }
1
+ <script >
2
+ const STORAGE_KEY = 'analytics_consent';
3
+ const confirmed = !!window.localStorage.getItem(STORAGE_KEY);
4
+ const allowed = (window.localStorage.getItem(STORAGE_KEY) ?? "") === "true";
5
+
6
+ const injectGtag = () => {
7
+ const s = document.createElement('script');
8
+ s.setAttribute('async', '');
9
+ s.setAttribute('src', 'https://www.googletagmanager.com/gtag/js?id=G-1Y0GZ34XC7');
10
+ window.dataLayer = window.dataLayer || [];
11
+ function gtag(){dataLayer.push(arguments);}
12
+ gtag('js', new Date());
13
+ gtag('config', 'G-1Y0GZ34XC7');
14
+ document.documentElement.querySelector('head')?.appendChild(s);
15
+ }
16
+
17
+ const showConfirm = () => {
18
+ const layer = document.createElement('div');
19
+ const okButton = document.createElement('button');
20
+ const rejectButton = document.createElement('button');
21
+ okButton.setAttribute('type', 'button');
22
+ okButton.classList.add('allow');
23
+ okButton.innerHTML = 'Allow';
24
+ okButton.addEventListener('click', () => {
25
+ window.localStorage.setItem(STORAGE_KEY, "true");
26
+ injectGtag();
27
+ layer.remove();
28
+ });
29
+ rejectButton.setAttribute('type', 'button');
30
+ rejectButton.classList.add('reject');
31
+ rejectButton.addEventListener('click', () => {
32
+ window.localStorage.setItem(STORAGE_KEY, "false");
33
+ layer.remove();
34
+ });
35
+ rejectButton.innerHTML = 'Reject';
36
+ layer.classList.add('analytics-consent-layer');
37
+ layer.appendChild(document.createElement('p')).innerHTML = 'Do you allow us to collect data for website statistics through Google Analytics?';
38
+ layer.appendChild(okButton);
39
+ layer.appendChild(rejectButton);
40
+ document.body.appendChild(layer);
41
+ }
42
+
43
+ if (allowed) {
44
+ injectGtag();
45
+ }
46
+
47
+ if (!confirmed) {
48
+ showConfirm();
49
+ }
50
+ </script >
51
+
52
+ <style is:global >
53
+ .analytics-consent-layer {
54
+ align-items: center;
55
+ background-color: #000;
56
+ box-sizing: border-box;
57
+ bottom: 0;
58
+ color: #fff;
59
+ display: flex;
60
+ justify-content: center;
61
+ left: 0;
62
+ padding: 20px;
63
+ position: fixed;
64
+ right: 0;
65
+ }
66
+
67
+ .analytics-consent-layer p {
68
+ margin: 0 10px 0 0;
69
+ }
70
+
71
+ .analytics-consent-layer button {
72
+ border: 0;
73
+ color: #fff;
74
+ cursor: pointer;
75
+ margin: 0 5px;
76
+ padding: 10px 20px;
77
+ }
78
+
79
+ .analytics-consent-layer button.allow {
80
+ background-color: darkgreen;
81
+ }
82
+
83
+ .analytics-consent-layer button.reject {
84
+ background-color: darkred;
85
+ }
86
+ </style >
0 commit comments