Skip to content

Commit

Permalink
fix: analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Feb 7, 2025
1 parent 13cdec2 commit 4cd58c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/js/components/CookieBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as React from 'react';

import { hasCookiePreferences, acceptAllCookies } from '../utils/cookies';
import {
hasCookiePreferences,
acceptAllCookies,
isAnalyticsCookiesConsentGiven,
} from '../utils/cookies';
import { Route } from '../utils/routes';
import Container from './reusable/Container';
import Heading from './reusable/Heading';
Expand Down Expand Up @@ -28,8 +32,8 @@ const CookieBar = () => {

// init GA consent
React.useEffect(() => {
initGa4();
setTimeout(() => {
initGa4(isAnalyticsCookiesConsentGiven());
setHasCookieBar(!hasCookiePreferences());
}, 1000);
}, []);
Expand Down
27 changes: 19 additions & 8 deletions src/js/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
export const initGa4 = () => {
export const initGa4 = (analyticsConsentGiven: boolean) => {
window.dataLayer = window.dataLayer || [];

gtag('js', new Date());
gtag('consent', 'default', {
ad_storage: 'denied',
analytics_storage: 'denied',
personalization_storage: 'denied',
functionality_storage: 'granted', // Functional cookies are OK
security_storage: 'granted',
});
if (analyticsConsentGiven) {
gtag('config', 'G-3SYTZ509HY');
gtag('consent', 'default', {
ad_storage: 'granted',
analytics_storage: 'granted',
personalization_storage: 'granted',
functionality_storage: 'granted',
security_storage: 'granted',
});
} else {
gtag('consent', 'default', {
ad_storage: 'denied',
analytics_storage: 'denied',
personalization_storage: 'denied',
functionality_storage: 'granted', // Functional cookies are OK
security_storage: 'granted',
});
}
};

export const acceptGa4 = () => {
Expand Down

0 comments on commit 4cd58c4

Please sign in to comment.