-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuseGA.js
More file actions
34 lines (28 loc) · 940 Bytes
/
useGA.js
File metadata and controls
34 lines (28 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { useLayoutEffect } from 'react';
const useGA = () => {
useLayoutEffect(() => {
if (process.env.NODE_ENV === 'production') {
window.dataLayer = window.dataLayer || [];
function gtag() {
window.dataLayer.push(arguments);
}
gtag('consent', 'default', {
ad_storage: 'denied',
analytics_storage: 'denied'
});
const script = document.createElement('script');
script.src = `https://www.googletagmanager.com/gtag/js?id=${process.env.REACT_APP_GA_MEASUREMENT_ID}`;
script.async = true;
document.head.appendChild(script);
function GAConsentGranted() {
gtag('consent', 'update', {
ad_storage: 'granted',
analytics_storage: 'granted'
});
}
// Make the GAConsentGranted function accessible in the global scope
window.GAConsentGranted = GAConsentGranted;
}
}, []);
};
export default useGA;