Skip to content

Commit

Permalink
feat: GA cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
anuragxxd committed Sep 18, 2023
1 parent 3e7c21c commit b927422
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .sample.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# example .env.local file; replace values with your own
NOTION_TOKEN=secret_rrmXXXxxxXXXxxxXXXxxxXXXxxxXXXxxxXXXxxxXXXx
NOTION_TOKEN=secret_xxxx
GA_MEASUREMENT_ID=G-xxxx
25 changes: 21 additions & 4 deletions components/CookieConsent.js → components/CookieConsent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ const pluginConfig = {
current_lang: 'en',
autoclear_cookies: true,
page_scripts: true,
test: 'ciao',
force_consent: true,
gui_options: {
consent_modal: {
layout: 'bar',
position: 'bottom center',
transition: 'slide',
swap_buttons: false,
swap_buttons: true,
},
},
onAccept: function (cookie) {
// check for production environment
if (process.env.NODE_ENV === 'production') {
// handle analytics cookies
if (cookie.categories.includes('analytics')) {
window.GAConsentGranted();
}
}
},
languages: {
en: {
consent_modal: {
Expand Down Expand Up @@ -44,9 +52,9 @@ const pluginConfig = {
],
blocks: [
{
title: 'Cookie usage 📢',
title: 'Cookie usage',
description:
'We use cookies to ensure the basic functionalities of the website and to enhance your online experience. You can choose for each category to opt-in/out whenever you want. For more details relative to cookies and other sensitive data, please read the full <a href="/policy" class="cc-link">privacy policy</a>.',
'We use cookies to ensure the basic functionalities of the website and to enhance your online experience. You can choose for each category to opt-in/out whenever you want.',
},
{
title: 'Strictly necessary cookies',
Expand All @@ -58,6 +66,15 @@ const pluginConfig = {
readonly: true,
},
},
{
title: 'Anylatics cookies',
description:
'These cookies are used to collect information about how you use our website. The information collected includes the number of visitors, the source of traffic, and the pages visited anonymously.',
toggle: {
value: 'analytics',
enabled: true,
},
},
{
title: 'More information',
description:
Expand Down
42 changes: 42 additions & 0 deletions pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
import { useEffect } from 'react';
import Script from 'next/script';
import Layout from '../components/Layout';
import '../styles/globals.css';
import { DefaultSeo } from 'next-seo';
import SEO from '../next-seo.config';
import 'vanilla-cookieconsent/dist/cookieconsent.css';
import CookieConsent from '../components/CookieConsent';

function GAScript() {
return (
<>
<Script id="google-analytics-consent">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'denied'
});
`}
</Script>
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.GA_MEASUREMENT_ID}`}
/>
<Script id="google-analytics">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.GA_MEASUREMENT_ID}');
`}
</Script>
<Script id="google-analytics-consent-granted">
{`
function GAConsentGranted() {
gtag('consent', 'update', {
'ad_storage': 'granted',
'analytics_storage': 'granted'
});
}
`}
</Script>
</>
);
}

function MyApp({ Component, pageProps }) {
useEffect(() => {
if ('serviceWorker' in navigator) {
Expand All @@ -24,8 +64,10 @@ function MyApp({ Component, pageProps }) {
});
}
}, []);

return (
<>
{process.env.NODE_ENV === 'production' && <GAScript />}
<DefaultSeo {...SEO} />
<Layout>
<Component {...pageProps} />
Expand Down
6 changes: 6 additions & 0 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ ion-icon {
font-size: 24px;
pointer-events: none;
}

/* Cookie Consent Styles */

:root {
--cc-btn-primary-bg: #3da9f6 !important;
}

0 comments on commit b927422

Please sign in to comment.