Gathering detailed insights and metrics for @porscheofficial/cookie-consent-banner
Gathering detailed insights and metrics for @porscheofficial/cookie-consent-banner
Gathering detailed insights and metrics for @porscheofficial/cookie-consent-banner
Gathering detailed insights and metrics for @porscheofficial/cookie-consent-banner
The lightweight and flexible Cookie Consent Banner
npm install @porscheofficial/cookie-consent-banner
Typescript
Module System
Node Version
NPM Version
TypeScript (63.3%)
CSS (21.74%)
HTML (14.96%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
167 Stars
142 Commits
12 Forks
7 Watchers
5 Branches
5 Contributors
Updated on Jun 30, 2025
Latest Version
4.0.3
Package Id
@porscheofficial/cookie-consent-banner@4.0.3
Unpacked Size
1.36 MB
Size
218.46 kB
File Count
43
NPM Version
9.8.1
Node Version
18.18.1
Published on
Apr 25, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
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.
Either install via a package manager, or include the component through one of the various npm delivery networks (skypack, unpkg,..).
1// yarn 2yarn add @porscheofficial/cookie-consent-banner 3// or npm 4npm i @porscheofficial/cookie-consent-banner --S
1<script 2 type="module" 3 src="https://unpkg.com/@porscheofficial/cookie-consent-banner@1.0.0/dist/cookie-consent-banner/cookie-consent-banner.esm.js" 4></script>
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 |
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. |
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. |
In order to allow the user to always update its preferences it's possible to trigger the banner by sending an event:
1<a href="javascript:document.dispatchEvent(new Event('cookie_consent_show'))"> 2 Show Cookie Settings 3</a>
Styling can be done in two different ways: Either via the availabe CSS Properties or via the CSS ::part
pseudo-element.
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.
1<style> 2 :root { 3 /* COLORS */ 4 --cookie-consent-banner-colors-primary: #81c784; 5 --cookie-consent-banner-colors-primary-border: #81c784; 6 --cookie-consent-banner-colors-primary-content: #fff; 7 --cookie-consent-banner-colors-secondary: transparent; 8 --cookie-consent-banner-colors-secondary-border: #fff; 9 --cookie-consent-banner-colors-secondary-content: #fff; 10 --cookie-consent-banner-colors-background-body: rgba(25, 31, 34, 0.92); 11 --cookie-consent-banner-colors-text: #fff; 12 13 /* BORDER-RADIUS */ 14 --cookie-consent-banner-border-radius-buttons: 1rem; 15 --cookie-consent-banner-border-radius-body: 0; 16 17 /* BOX-SHADOW */ 18 --cookie-consent-banner-box-shadow: 0px -3px 13px 0px rgba(57, 57, 57, 0.38); 19 20 /* SPACINGS */ 21 --cookie-consent-banner-spacings-container-padding-top: 1rem; 22 --cookie-consent-banner-spacings-container-padding-left: 1rem; 23 --cookie-consent-banner-spacings-container-padding-bottom: 1rem; 24 --cookie-consent-banner-spacings-container-padding-right: 1rem; 25 26 --cookie-consent-banner-spacings-body-padding-top: 0; 27 --cookie-consent-banner-spacings-body-padding-left: 2rem; 28 --cookie-consent-banner-spacings-body-padding-bottom: 0; 29 --cookie-consent-banner-spacings-body-padding-right: 2rem; 30 31 /* Z-INDEX */ 32 --cookie-consent-banner-z-index-container: 99; 33 34 /* FONTS */ 35 --cookie-consent-banner-font-family-headline: inherit; 36 --cookie-consent-banner-font-size-headline: 1.5rem; 37 --cookie-consent-banner-font-family-body: inherit; 38 --cookie-consent-banner-font-size-body: 0.875rem; 39 } 40</style>
::part
selectorsFor 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 body1<style> 2 cookie-consent-banner::part(button-accept-all) { 3 text-transform: uppercase; 4 } 5 6 cookie-consent-banner::part(button-persist-selection), 7 cookie-consent-banner::part(button-essential-only) { 8 text-transform: uppercase; 9 box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26); 10 } 11 12 cookie-consent-banner::part(checkbox) { 13 accent-color: rgb(24, 251, 107); 14 } 15 16 cookie-consent-banner::part(description), 17 cookie-consent-banner::part(headline) { 18 font-family: "Arial", sans-serif; 19 } 20 21 cookie-consent-banner::part(body) { 22 max-width: 80ch; 23 } 24</style>
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.
1<!-- Import Web Component --> 2<script 3 type="module" 4 src="https://unpkg.com/@porscheofficial/cookie-consent-banner@2.1.0/dist/cookie-consent-banner/cookie-consent-banner.esm.js" 5></script> 6<!-- Update Styles--> 7<style> 8 :root { 9 --cookie-consent-banner-z-index-container: 101; 10 --cookie-consent-banner-colors-primary: rgba(0, 255, 255, 0.82); 11 --cookie-consent-banner-colors-primary-border: var( 12 --cookie-consent-banner-colors-primary 13 ); 14 --cookie-consent-banner-colors-primary-content: #fff; 15 16 --cookie-consent-banner-border-radius-buttons: 100px; 17 --cookie-consent-banner-border-radius-body: 0; 18 19 --cookie-consent-banner-spacings-container-padding-top: 0; 20 --cookie-consent-banner-spacings-container-padding-left: 0; 21 --cookie-consent-banner-spacings-container-padding-bottom: 0; 22 --cookie-consent-banner-spacings-container-padding-right: 0; 23 } 24</style> 25 26<!-- Init Web Component --> 27<cookie-consent-banner 28 btn-label-accept-and-continue="Agree and continue" 29 btn-label-only-essential-and-continue="Continue with technically required cookies only" 30 btn-label-persist-selection-and-continue="Save selection and continue" 31 btn-label-select-all-and-continue="Select all and continue" 32 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." 33> 34 We use cookies and similar technologies to provide certain features, enhance 35 the user experience and deliver content that is relevant to your interests. 36 Depending on their purpose, analysis and marketing cookies may be used in 37 addition to technically necessary cookies. By clicking on "Agree and 38 continue", you declare your consent to the use of the aforementioned cookies. 39 <a 40 href="javascript:document.dispatchEvent(new Event('cookie_consent_details_show'))" 41 > 42 Here 43 </a> 44 you can make detailed settings or revoke your consent (in part if necessary) 45 with effect for the future. For further information, please refer to our 46 <a href="/privacy-policy">Privacy Policy</a> 47 . 48</cookie-consent-banner> 49 50<!-- Open Banner again --> 51<a href="javascript:document.dispatchEvent(new Event('cookie_consent_show'))"> 52 Show Cookie Consent Settings 53</a> 54 55<!-- Scripts --> 56<script> 57 /* Update available Cookie Categories */ 58 const cookieConsentBannerElement = document.querySelector( 59 "cookie-consent-banner", 60 ); 61 cookieConsentBannerElement.availableCategories = [ 62 { 63 description: 64 "Enable you to navigate and use the basic functions and to store preferences.", 65 key: "technically_required", 66 label: "Technically necessary cookies", 67 isMandatory: true, 68 }, 69 { 70 description: 71 "Enable us to determine how visitors interact with our service in order to improve the user experience.", 72 key: "analytics", 73 label: "Analysis cookies", 74 }, 75 { 76 description: 77 "Enable us to offer and evaluate relevant content and interest-based advertising.", 78 key: "marketing", 79 label: "Marketing cookies", 80 }, 81 ]; 82 83 /* Init Tag Manager */ 84 function loadTagManager() { 85 if (typeof google_tag_manager !== "undefined") return; // load only once 86 const gTags = function (w, d, s, l, i) { 87 w[l] = w[l] || []; 88 w[l].push({ 89 "gtm.start": new Date().getTime(), 90 event: "gtm.js", 91 }); 92 let f = d.getElementsByTagName(s)[0], 93 j = d.createElement(s), 94 dl = l != "dataLayer" ? "&l=" + l : ""; 95 j.async = true; 96 j.src = "https://www.googletagmanager.com/gtm.js?id=" + i + dl; 97 f.parentNode.insertBefore(j, f); 98 }; 99 100 gTags(window, document, "script", "dataLayer", "GTM-XXX"); 101 } 102 window.addEventListener( 103 "cookie_consent_preferences_restored", 104 loadTagManager, 105 ); 106 window.addEventListener("cookie_consent_preferences_updated", loadTagManager); 107</script>
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.
1<!-- Import Web Component --> 2<script 3 type="module" 4 src="https://unpkg.com/@porscheofficial/cookie-consent-banner@2.1.0/dist/cookie-consent-banner/cookie-consent-banner.esm.js" 5></script> 6<!-- Update Styles--> 7<style> 8 :root { 9 --cookie-consent-banner-z-index-container: 101; 10 --cookie-consent-banner-colors-primary: rgba(0, 255, 255, 0.82); 11 --cookie-consent-banner-colors-primary-border: var( 12 --cookie-consent-banner-colors-primary 13 ); 14 --cookie-consent-banner-colors-primary-content: #fff; 15 16 --cookie-consent-banner-border-radius-buttons: 100px; 17 --cookie-consent-banner-border-radius-body: 0; 18 19 --cookie-consent-banner-spacings-container-padding-top: 0; 20 --cookie-consent-banner-spacings-container-padding-left: 0; 21 --cookie-consent-banner-spacings-container-padding-bottom: 0; 22 --cookie-consent-banner-spacings-container-padding-right: 0; 23 } 24</style> 25 26<!-- Init Web Component --> 27<cookie-consent-banner 28 btn-label-accept-and-continue="Agree and continue" 29 btn-label-only-essential-and-continue="Continue with technically required cookies only" 30 btn-label-persist-selection-and-continue="Save selection and continue" 31 btn-label-select-all-and-continue="Select all and continue" 32 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." 33> 34 We use cookies and similar technologies to provide certain features, enhance 35 the user experience and deliver content that is relevant to your interests. 36 Depending on their purpose, analysis and marketing cookies may be used in 37 addition to technically necessary cookies. By clicking on "Agree and 38 continue", you declare your consent to the use of the aforementioned cookies. 39 <a 40 href="javascript:document.dispatchEvent(new Event('cookie_consent_details_show'))" 41 > 42 Here 43 </a> 44 you can make detailed settings or revoke your consent (in part if necessary) 45 with effect for the future. For further information, please refer to our 46 <a href="/privacy-policy">Privacy Policy</a> 47 . 48</cookie-consent-banner> 49 50<!-- Open Banner again --> 51<a href="javascript:document.dispatchEvent(new Event('cookie_consent_show'))"> 52 Show Cookie Consent Settings 53</a> 54 55<script> 56 /* Update available Cookie Categories */ 57 const cookieConsentBannerElement = document.querySelector( 58 "cookie-consent-banner", 59 ); 60 cookieConsentBannerElement.availableCategories = [ 61 { 62 description: 63 "Enable you to navigate and use the basic functions and to store preferences.", 64 key: "technically_required", 65 label: "Technically necessary cookies", 66 isMandatory: true, 67 }, 68 { 69 description: 70 "Enable us to determine how visitors interact with our service in order to improve the user experience.", 71 key: "analytics", 72 label: "Analysis cookies", 73 }, 74 { 75 description: 76 "Enable us to offer and evaluate relevant content and interest-based advertising.", 77 key: "marketing", 78 label: "Marketing cookies", 79 }, 80 ]; 81</script> 82 83<script> 84 // ========================================================================= 85 // EXAMPLE 86 // ANALYTICS w/o TAG MANAGER 87 // ========================================================================= 88 window.dataLayer = window.dataLayer || []; 89 function gtag() { 90 dataLayer.push(arguments); 91 } 92 gtag("js", new Date()); 93 94 gtag("config", "ENTERID"); 95 96 function loadAnalyticsScript() { 97 // Add Script only once 98 const scriptElementExists = document.querySelector("[data-scriptid='ga']"); 99 if (scriptElementExists || window?.ga) return; 100 101 const firstScriptElement = document.getElementsByTagName("script")[0]; 102 103 const scriptElement = document.createElement("script"); 104 scriptElement.type = "text/javascript"; 105 scriptElement.setAttribute("async", "true"); 106 scriptElement.setAttribute( 107 "src", 108 "https://www.googletagmanager.com/gtag/js?id=ENTERID", 109 ); 110 scriptElement.setAttribute("data-scriptid", "ga"); 111 112 firstScriptElement.parentNode.insertBefore( 113 scriptElement, 114 firstScriptElement, 115 ); 116 } 117 // ========================================================================= 118 // COOKIE CONSENT: LOAD SCRIPTS AFTER USER INTERACTION 119 // ========================================================================= 120 function loadScripts(event) { 121 const acceptedCategories = event?.detail?.acceptedCategories; 122 123 if (acceptedCategories.includes("analytics")) { 124 console.log("Analytics accepted."); 125 126 loadAnalyticsScript(); 127 } 128 if (acceptedCategories.includes("marketing")) { 129 console.log("Marketing accepted."); 130 } 131 } 132 133 window.addEventListener("cookie_consent_preferences_restored", loadScripts); 134 window.addEventListener("cookie_consent_preferences_updated", loadScripts); 135</script>
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.
See LICENSE.
No vulnerabilities found.
No security vulnerabilities found.