Skip to content

Commit

Permalink
Merge pull request #32 from sunnyfeijen/v4.0.0-alpha.4
Browse files Browse the repository at this point in the history
v4.0 | Update docs
  • Loading branch information
sunnyfeijen authored Oct 13, 2023
2 parents 66691ec + 7a5ec70 commit a283af1
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 34 deletions.
74 changes: 40 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The cookie settings are stored as follows:

`'["functional","analytics","marketing"]'`

## Setup with @freshheads/analytics-essentials

- [Gtag & analytics-essentials setup](doc/gtag_setup.md)
- [Tag Manager & analytics-essentials setup](doc/tagmanager_setup.md)

## Usage

You can use the build in cookieguard to get up and running quickly or use the provided hooks to make your own.
Expand All @@ -14,13 +19,13 @@ The cookiebanner makes use of Context to share the cookie state throughout the a

- Wrap your app with the Provider:

```jsx
import { CookieGuardContextProvider } from '@freshheads/cookie-guard';
```jsx
import { CookieGuardContextProvider } from '@freshheads/cookie-guard';

<CookieGuardContextProvider>
<App />
</CookieGuardContextProvider>;
```
<CookieGuardContextProvider>
<App />
</CookieGuardContextProvider>;
```

- The provider optionally accepts:

Expand All @@ -32,37 +37,38 @@ The cookiebanner makes use of Context to share the cookie state throughout the a

- Add the premade Cookie Banner to your app

```jsx
import {
CookieGuard,
CookieGuardContextProvider,
CookieGuardProps
} from '@freshheads/cookie-guard';
import '@freshheads/cookie-guard/dist/style.css';
const CookieGuardProps = {
title: 'Onze site maakt gebruik van cookies.',
description:
'Wij gebruiken cookies voor de werking van de website, analyse en verbetering en marketingdoeleinden.',
acceptAllLabel: 'Alle cookies accepteren',
saveLabel: 'Opslaan',
functionalLabel: 'Noodzakelijke cookies',
analyticsLabel: 'Analytische cookies',
marketingLabel: 'Marketing cookies',
}
<CookieGuardContextProvider>
<App/>
<CookieGuard {...CookieGuardProps}>
</CookieGuardContextProvider>
```
```jsx
import {
CookieBanner,
CookieCategorySettings,
CookieGuardProvider
} from '@freshheads/cookie-guard';
import '@freshheads/cookie-guard/dist/style.css';

const CookieGuardProps = {
title: 'Onze site maakt gebruik van cookies.',
description:
'Wij gebruiken cookies voor de werking van de website, analyse en verbetering en marketingdoeleinden.',
acceptAllLabel: 'Alle cookies accepteren',
saveLabel: 'Opslaan',
requiredLabel: 'Noodzakelijke cookies'.
functionalLabel: 'Noodzakelijke cookies',
analyticsLabel: 'Analytische cookies',
marketingLabel: 'Marketing cookies',
}

return (
<CookieGuardProvider>
{/** your app components **/}
<CookieBanner {...CookieGuardProps} />
</CookieGuardProvider>
);
```

- To style look at the source code `/src/popupstyles.css` and import your own css.

- You can also use the hooks to make your own custom cookiebanner

TODO: explain hooks with chakraui example

```jsx
## Examples

```
- [Chakra-UI](examples/ChakraUI) custom cookie banner
95 changes: 95 additions & 0 deletions doc/gtag_setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Gtag setup

Setup with use of Gtag & [@freshheads/analytics-essentials](https://github.com/freshheads/analytics-essentials)

#### **`components/GA4Loader.tsx`**

Add a component to load GA4 within the `<CookieGuardProvider />` so we have access to the analytics cookies.

```jsx
import {
CookieConsentOptions,
CookieConsentValues,
LoadGA4,
} from '@freshheads/analytics-essentials';
import { CookieCategorySettings, useCookies } from '@freshheads/cookie-guard';

export const formatCookiesForGA = (
cookieSettings: CookieCategorySettings
): CookieConsentOptions | undefined => {
// formats cookies from Cookie Guard
if (!cookieSettings) return undefined;

return {
ad_storage:
cookieSettings.marketing === true
? CookieConsentValues.GRANTED
: CookieConsentValues.DENIED,
analytics_storage:
cookieSettings.analytics === true
? CookieConsentValues.GRANTED
: CookieConsentValues.DENIED,
functionality_storage:
cookieSettings.functional === true
? CookieConsentValues.GRANTED
: CookieConsentValues.DENIED,
personalization_storage:
cookieSettings.marketing === true
? CookieConsentValues.GRANTED
: CookieConsentValues.DENIED,
};
};

const GA4Loader = () => {
const { cookieSettings } = useCookies();

return (
<>
{process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID && (
<LoadGA4
measurementID={process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID}
defaultConsent={formatCookiesForGA(cookieSettings)} // set default consent based on cookies that are already set
/>
)}
</>
);
};
```

#### **`app.tsx`**

```jsx
import { pushGaConsent } from '@freshheads/analytics-essentials';
import {
CookieBanner,
CookieCategorySettings,
CookieGuardProvider,
} from '@freshheads/cookie-guard';
import GA4Loader, { formatCookiesForGA } from '@/components/GA4Loader';

const App = () => {
const onCookiesChanged = (cookieSettings: CookieCategorySettings) => {
if (!cookieSettings) return;
const formattedCookies = formatCookiesForGA(cookieSettings);
if (!formattedCookies) return;
pushGaConsent(formattedCookies);
};

return (
<CookieGuardProvider onCookieSettingsChange={onCookiesChanged}>
<GA4Loader />
{/** your app components **/}
<CookieBanner
title="Onze site maakt gebruik van cookies."
description="Wij gebruiken cookies voor de werking van de website, analyse en verbetering en marketingdoeleinden."
acceptAllLabel="Alle cookies accepteren"
saveLabel="Opslaan"
requiredLabel="Noodzakelijke cookies"
functionalLabel="Functionele cookies"
analyticsLabel="Analytische cookies"
marketingLabel="Marketing cookies"
/>
</CookieGuardProvider>
);
};
```
52 changes: 52 additions & 0 deletions doc/tagmanager_setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Tag Manager setup

Setup with use of Tag Manager & [@freshheads/analytics-essentials](https://github.com/freshheads/analytics-essentials)

#### **`app.tsx`**

```jsx
import {
CookieBanner,
CookieCategorySettings,
CookieGuardProvider,
} from '@freshheads/cookie-guard';
import {
EventTypes,
LoadTagManager,
pushDataLayerEvent,
} from '@freshheads/analytics-essentials';

const App = () => {
const onCookiesChanged = (cookieSettings: CookieCategorySettings) => {
if (!cookieSettings) return;

pushDataLayerEvent({
type: EventTypes.CUSTOM,
name: 'cookie_settings_changed',
});
};

return (
<>
{process.env.NEXT_PUBLIC_GTM_ID && (
<LoadTagManager
measurementID={process.env.NEXT_PUBLIC_GTM_ID}
/>
)}
<CookieGuardProvider onCookieSettingsChange={onCookiesChanged}>
{/** your app components **/}
<CookieBanner
title="Onze site maakt gebruik van cookies."
description="Wij gebruiken cookies voor de werking van de website, analyse en verbetering en marketingdoeleinden."
acceptAllLabel="Alle cookies accepteren"
saveLabel="Opslaan"
requiredLabel="Noodzakelijke cookies"
functionalLabel="Functionele cookies"
analyticsLabel="Analytische cookies"
marketingLabel="Marketing cookies"
/>
</CookieGuardProvider>
</>
);
};
```
3 changes: 3 additions & 0 deletions examples/ChakraUI/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules

package-lock.json
142 changes: 142 additions & 0 deletions examples/ChakraUI/CookieBanner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import {
Button,
Checkbox,
HStack,
Modal,
ModalBody,
ModalContent,
ModalHeader,
ModalOverlay,
Text,
VStack,
} from '@chakra-ui/react';
import { useCookies } from '@freshheads/cookie-guard';
import React from 'react';
import { FC, useEffect, useState } from 'react';

const CookieBannerPrimitive: FC = () => {
const {
cookieSettings,
setCookieSettings,
cookieBannerIsOpen,
setCookieBannerIsOpen,
} = useCookies();
/*
To prevent the cookie banner changing the settings without pressing save,
we need to keep track of the options in the banner itself.
*/
const [cookieOptions, setCookieOptions] = useState<{
required: boolean;
functional: boolean;
analytics: boolean;
marketing: boolean;
}>({
required: cookieSettings?.required ?? true,
functional: cookieSettings?.functional ?? false,
analytics: cookieSettings?.analytics ?? false,
marketing: cookieSettings?.marketing ?? false,
});

useEffect(() => {
setCookieOptions({
required: cookieSettings?.required ?? true,
functional: cookieSettings?.functional ?? false,
analytics: cookieSettings?.analytics ?? false,
marketing: cookieSettings?.marketing ?? false,
});
}, [cookieSettings]);

const onAcceptAll = () => {
setCookieSettings({
functional: true,
analytics: true,
marketing: true,
});
setCookieBannerIsOpen(false);
};

return (
<Modal
isOpen={cookieBannerIsOpen}
onClose={() => {}}
isCentered
size={'2xl'}
closeOnEsc={false}
closeOnOverlayClick={false}
>
<ModalOverlay />
<ModalContent>
<ModalHeader>Onze site maakt gebruik van cookies</ModalHeader>
<ModalBody>
<Text mb={4}>
Wij gebruiken cookies voor de werking van de website,
analyse en verbetering en marketingdoeleinden.
</Text>

<VStack alignItems={'flex-start'} mb={4}>
<Checkbox
isChecked={cookieOptions.required}
isDisabled={true}
>
Noodzakelijke cookies
</Checkbox>
<Checkbox
isChecked={cookieOptions.functional}
onChange={() =>
setCookieOptions({
...cookieOptions,
functional: !cookieOptions.functional,
})
}
>
Functionele cookies
</Checkbox>
<Checkbox
isChecked={cookieOptions.analytics}
onChange={() =>
setCookieOptions({
...cookieOptions,
analytics: !cookieOptions.analytics,
})
}
>
Analytische cookies
</Checkbox>
<Checkbox
isChecked={cookieOptions.marketing}
onChange={() =>
setCookieOptions({
...cookieOptions,
marketing: !cookieOptions.marketing,
})
}
>
Marketing cookies
</Checkbox>
</VStack>

<HStack alignItems={'center'}>
<Button
flex={1}
onClick={() => {
setCookieSettings(cookieOptions);
setCookieBannerIsOpen(false);
}}
>
Opslaan
</Button>
<Button
flex={1}
colorScheme="blue"
onClick={onAcceptAll}
>
Alles cookies accepteren
</Button>
</HStack>
</ModalBody>
</ModalContent>
</Modal>
);
};

export default CookieBannerPrimitive;
Loading

0 comments on commit a283af1

Please sign in to comment.