Cookie Consent Banner – Web Component
The Cookie Consent Banner is implemented as Web Component and additionally exported as React Component.
This guide explains how to use the Web Component.
Click here to have a look on the documentation of the React Component.
Make sure to also have a look on the main repository.
:spiral_notepad: Usage
Installation
Either install via a package manager, or include the component through one of the various npm delivery networks (skypack, unpkg,..).
// yarn
yarn add @porscheofficial/cookie-consent-banner
// or npm
npm i @porscheofficial/cookie-consent-banner --S
<script
type="module"
src="https://unpkg.com/@porscheofficial/cookie-consent-banner@1.0.0/dist/cookie-consent-banner/cookie-consent-banner.esm.js"
></script>
Attributes
Attribute (Web Component) | Default | Type | Description |
---|
available-categories | [] | CategoryItem[] | Provide the available Categories. See Real World example |
cookie-attributes | {path: "/",expires: 7,domain: document.location.hostname} | CookieAttributes | Customization of the cookie attributes for the consent cookie (expires can either be a number of days or a Date object). |
cookie-name | "cookies_accepted_categories" | string | |
disable-reset-site-cookies-on-consent-withdrawn | false | boolean | Prevent cookies from being deleted automatically if consent of the user changed. |
disable-slide-in-animation | false | boolean | Disable slide-in animation of banner (See #7) |
headline | undefined | string | |
btn-label-accept-and-continue | undefined | string | |
btn-label-select-all-and-continue | undefined | string | |
btn-label-only-essential-and-continue | undefined | string | |
btn-label-persist-selection-and-continue | undefined | string | |
content-settings-description | undefined | string | |
Events Dispatched by the Component
Event | Description |
---|
cookie_consent_preferences_restored | Consent Settings have been saved previously and are now restored on Page Load. |
cookie_consent_preferences_updated | Consent Settings have been updated. Either initially set or changed. |
Events Receivable by the Component
It's possible to send JavaScript Events to control the banner.
Event | Description |
---|
cookie_consent_show | Show the the Consent Banner. |
cookie_consent_details_show | Show the detailed settings (categories). Also the Consent Banner is shown if not done already. |
Q: How to show the cookie consent banner again?
In order to allow the user to always update its preferences it's possible to trigger the banner by sending an event:
<a href="javascript:document.dispatchEvent(new Event('cookie_consent_show'))">
Show Cookie Settings
</a>
Styling
Styling can be done in two different ways: Either via the availabe CSS Properties or via the CSS ::part
pseudo-element.
Styling with CSS Properties
The appearance of the component can be influenced by updating the availabe CSS Properties. In most cases this is enough customization flexibility but since not every aspect is exposed, you might want to use the ::part
selectors if you need more flexibility.
<style>
:root {
--cookie-consent-banner-colors-primary: #81c784;
--cookie-consent-banner-colors-primary-border: #81c784;
--cookie-consent-banner-colors-primary-content: #fff;
--cookie-consent-banner-colors-secondary: transparent;
--cookie-consent-banner-colors-secondary-border: #fff;
--cookie-consent-banner-colors-secondary-content: #fff;
--cookie-consent-banner-colors-background-body: rgba(25, 31, 34, 0.92);
--cookie-consent-banner-colors-text: #fff;
--cookie-consent-banner-border-radius-buttons: 1rem;
--cookie-consent-banner-border-radius-body: 0;
--cookie-consent-banner-box-shadow: 0px -3px 13px 0px rgba(57, 57, 57, 0.38);
--cookie-consent-banner-spacings-container-padding-top: 1rem;
--cookie-consent-banner-spacings-container-padding-left: 1rem;
--cookie-consent-banner-spacings-container-padding-bottom: 1rem;
--cookie-consent-banner-spacings-container-padding-right: 1rem;
--cookie-consent-banner-spacings-body-padding-top: 0;
--cookie-consent-banner-spacings-body-padding-left: 2rem;
--cookie-consent-banner-spacings-body-padding-bottom: 0;
--cookie-consent-banner-spacings-body-padding-right: 2rem;
--cookie-consent-banner-z-index-container: 99;
--cookie-consent-banner-font-family-headline: inherit;
--cookie-consent-banner-font-size-headline: 1.5rem;
--cookie-consent-banner-font-family-body: inherit;
--cookie-consent-banner-font-size-body: 0.875rem;
}
</style>
Styling with the ::part
selectors
For full control over the styles we provide you these CSS parts to customize completely by your own:
cookie-consent-banner::part(button-accept-all)
for styling the primary button which triggers "Accept All Cookies"cookie-consent-banner::part(button-persist-selection)
for styling the secondary button which triggers "Save Selection"cookie-consent-banner::part(button-essential-only)
for styling the secondary button which triggers "Only required Cookies"cookie-consent-banner::part(checkbox)
for styling the checkboxescookie-consent-banner::part(description-main)
for styling the main description textcookie-consent-banner::part(description-settings)
for styling the description text on the expanded settingscookie-consent-banner::part(headline)
for styling the headlinecookie-consent-banner::part(body)
for styling the banner body
<style>
cookie-consent-banner::part(button-accept-all) {
text-transform: uppercase;
}
cookie-consent-banner::part(button-persist-selection),
cookie-consent-banner::part(button-essential-only) {
text-transform: uppercase;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26);
}
cookie-consent-banner::part(checkbox) {
accent-color: rgb(24, 251, 107);
}
cookie-consent-banner::part(description),
cookie-consent-banner::part(headline) {
font-family: "Arial", sans-serif;
}
cookie-consent-banner::part(body) {
max-width: 80ch;
}
</style>
:rocket: Real World Examples
...with Tag Manager
This example shows how the component could be integrated into your web application by leveraging the features of a tag manager.
The tag manager is not loaded as long as there is no consent given.
Once the visitor stores the consent settings, two things happen: The consent data is stored as a cookie cookies_accepted_categories
and the tag manager is initialized.
Have a look on the main repository for an example consent flow.
<script
type="module"
src="https://unpkg.com/@porscheofficial/cookie-consent-banner@2.1.0/dist/cookie-consent-banner/cookie-consent-banner.esm.js"
></script>
<style>
:root {
--cookie-consent-banner-z-index-container: 101;
--cookie-consent-banner-colors-primary: rgba(0, 255, 255, 0.82);
--cookie-consent-banner-colors-primary-border: var(
--cookie-consent-banner-colors-primary
);
--cookie-consent-banner-colors-primary-content: #fff;
--cookie-consent-banner-border-radius-buttons: 100px;
--cookie-consent-banner-border-radius-body: 0;
--cookie-consent-banner-spacings-container-padding-top: 0;
--cookie-consent-banner-spacings-container-padding-left: 0;
--cookie-consent-banner-spacings-container-padding-bottom: 0;
--cookie-consent-banner-spacings-container-padding-right: 0;
}
</style>
<cookie-consent-banner
btn-label-accept-and-continue="Agree and continue"
btn-label-only-essential-and-continue="Continue with technically required cookies only"
btn-label-persist-selection-and-continue="Save selection and continue"
btn-label-select-all-and-continue="Select all and continue"
content-settings-description="You can decide which cookies are used by selecting the respective options below. Please note that your selection may impair in the functionality of the service."
>
We use cookies and similar technologies to provide certain features, enhance
the user experience and deliver content that is relevant to your interests.
Depending on their purpose, analysis and marketing cookies may be used in
addition to technically necessary cookies. By clicking on "Agree and
continue", you declare your consent to the use of the aforementioned cookies.
<a
href="javascript:document.dispatchEvent(new Event('cookie_consent_details_show'))"
>
Here
</a>
you can make detailed settings or revoke your consent (in part if necessary)
with effect for the future. For further information, please refer to our
<a href="/privacy-policy">Privacy Policy</a>
.
</cookie-consent-banner>
<a href="javascript:document.dispatchEvent(new Event('cookie_consent_show'))">
Show Cookie Consent Settings
</a>
<script>
const cookieConsentBannerElement = document.querySelector(
"cookie-consent-banner",
);
cookieConsentBannerElement.availableCategories = [
{
description:
"Enable you to navigate and use the basic functions and to store preferences.",
key: "technically_required",
label: "Technically necessary cookies",
isMandatory: true,
},
{
description:
"Enable us to determine how visitors interact with our service in order to improve the user experience.",
key: "analytics",
label: "Analysis cookies",
},
{
description:
"Enable us to offer and evaluate relevant content and interest-based advertising.",
key: "marketing",
label: "Marketing cookies",
},
];
function loadTagManager() {
if (typeof google_tag_manager !== "undefined") return;
const gTags = function (w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({
"gtm.start": new Date().getTime(),
event: "gtm.js",
});
let f = d.getElementsByTagName(s)[0],
j = d.createElement(s),
dl = l != "dataLayer" ? "&l=" + l : "";
j.async = true;
j.src = "https://www.googletagmanager.com/gtm.js?id=" + i + dl;
f.parentNode.insertBefore(j, f);
};
gTags(window, document, "script", "dataLayer", "GTM-XXX");
}
window.addEventListener(
"cookie_consent_preferences_restored",
loadTagManager,
);
window.addEventListener("cookie_consent_preferences_updated", loadTagManager);
</script>
...without Tag Manager
This example shows how the component could be integrated into your web application without leveraging the features of a tag manager.
The scripts are not loaded as long as there is no consent given.
Once the visitor stores the consent settings, two things happen: The consent data is stored as a cookie cookies_accepted_categories
and the script elements are added to the page.
<script
type="module"
src="https://unpkg.com/@porscheofficial/cookie-consent-banner@2.1.0/dist/cookie-consent-banner/cookie-consent-banner.esm.js"
></script>
<style>
:root {
--cookie-consent-banner-z-index-container: 101;
--cookie-consent-banner-colors-primary: rgba(0, 255, 255, 0.82);
--cookie-consent-banner-colors-primary-border: var(
--cookie-consent-banner-colors-primary
);
--cookie-consent-banner-colors-primary-content: #fff;
--cookie-consent-banner-border-radius-buttons: 100px;
--cookie-consent-banner-border-radius-body: 0;
--cookie-consent-banner-spacings-container-padding-top: 0;
--cookie-consent-banner-spacings-container-padding-left: 0;
--cookie-consent-banner-spacings-container-padding-bottom: 0;
--cookie-consent-banner-spacings-container-padding-right: 0;
}
</style>
<cookie-consent-banner
btn-label-accept-and-continue="Agree and continue"
btn-label-only-essential-and-continue="Continue with technically required cookies only"
btn-label-persist-selection-and-continue="Save selection and continue"
btn-label-select-all-and-continue="Select all and continue"
content-settings-description="You can decide which cookies are used by selecting the respective options below. Please note that your selection may impair in the functionality of the service."
>
We use cookies and similar technologies to provide certain features, enhance
the user experience and deliver content that is relevant to your interests.
Depending on their purpose, analysis and marketing cookies may be used in
addition to technically necessary cookies. By clicking on "Agree and
continue", you declare your consent to the use of the aforementioned cookies.
<a
href="javascript:document.dispatchEvent(new Event('cookie_consent_details_show'))"
>
Here
</a>
you can make detailed settings or revoke your consent (in part if necessary)
with effect for the future. For further information, please refer to our
<a href="/privacy-policy">Privacy Policy</a>
.
</cookie-consent-banner>
<a href="javascript:document.dispatchEvent(new Event('cookie_consent_show'))">
Show Cookie Consent Settings
</a>
<script>
const cookieConsentBannerElement = document.querySelector(
"cookie-consent-banner",
);
cookieConsentBannerElement.availableCategories = [
{
description:
"Enable you to navigate and use the basic functions and to store preferences.",
key: "technically_required",
label: "Technically necessary cookies",
isMandatory: true,
},
{
description:
"Enable us to determine how visitors interact with our service in order to improve the user experience.",
key: "analytics",
label: "Analysis cookies",
},
{
description:
"Enable us to offer and evaluate relevant content and interest-based advertising.",
key: "marketing",
label: "Marketing cookies",
},
];
</script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "ENTERID");
function loadAnalyticsScript() {
const scriptElementExists = document.querySelector("[data-scriptid='ga']");
if (scriptElementExists || window?.ga) return;
const firstScriptElement = document.getElementsByTagName("script")[0];
const scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.setAttribute("async", "true");
scriptElement.setAttribute(
"src",
"https://www.googletagmanager.com/gtag/js?id=ENTERID",
);
scriptElement.setAttribute("data-scriptid", "ga");
firstScriptElement.parentNode.insertBefore(
scriptElement,
firstScriptElement,
);
}
function loadScripts(event) {
const acceptedCategories = event?.detail?.acceptedCategories;
if (acceptedCategories.includes("analytics")) {
console.log("Analytics accepted.");
loadAnalyticsScript();
}
if (acceptedCategories.includes("marketing")) {
console.log("Marketing accepted.");
}
}
window.addEventListener("cookie_consent_preferences_restored", loadScripts);
window.addEventListener("cookie_consent_preferences_updated", loadScripts);
</script>
Disclaimer
Please note that you must individually assess the legal requirements regarding the implementation of the Cookie Consent Banner, in particular which choices to offer in which granularity and which information to provide in which detail and at which point of the user journey. The examples mentioned are not intended to provide any advice regarding legal requirements. All responsibility for the implementation of the Cookie Consent Banner and its compliance with legal requirements lies with the user.
License
See LICENSE.