cookie-consent.js (外部連結) config.js (外部連結) translations (外部連結) cookie-consent.html (外部連結)
Yore integrates vanilla-cookieconsent (外部連結) for GDPR-compliant cookie management. Built-in categories are necessary, functional, analytics, and advertising.
- Google Ads related products are not compatible (外部連結) with vanilla-cookieconsent.
- Most libraries exclude functional cookies from consent handling and do not use the consent modal (外部連結). Yore follows the same behavior and does not manage them.
- Provide a cookie settings entry accessible from all pages to meet GDPR requirements.
Configuration 此標題的連結
Set enable: true and list the categories you want to expose to visitors.
params:
cookieConsent:
enable: true
categories:
- necessary
- functional
- analytics
- advertisingPreferences Button 此標題的連結
The cookie-settings shortcode renders a button that opens the preferences modal.
{{< cookie-settings >}}
{{< cookie-settings title="Cookie Settings" >}}
Yore Integration 此標題的連結
Yore automatically wires cookie consent into gtag.js, so no source changes are needed on your end1.
Other analytics tools that don't require a consent banner, such as Plausible or Umami, are not managed by the cookie consent banner. You are still responsible for disclosure obligations, for example, linking to your privacy policy in the footer and documenting which analytics you use there.
Examples 此標題的連結
Once your categories are configured, you need to put your scripts under consent management. There are two ways to do this:
- Script Management: manage inline scripts.
- Event listeners: listen for consent events to run or tear down code programmatically.
These examples are simplified from How to manage scripts (外部連結).
Scripts Management 此標題的連結
Add type="text/plain" and data-category to any <script> tag to gate its execution behind consent.
Before:
<!-- Hotjar, always executed -->
<script>
// ...
</script>After:
<script type="text/plain" data-category="analytics">
// ...
</script>Execute code on consent 此標題的連結
Use event listeners when you need more control. cc:onConsent fires on every page load once consent has been given. cc:onChange fires when the user updates their preferences after consent was already expressed.
window.addEventListener('cc:onConsent', () => {
if (window.CookieConsent.acceptedCategory('analytics')) {
// initialize analytics
}
});See Callbacks and Events (外部連結) for all available events.
Advanced 此標題的連結
Category creation falls into three types: a simple category defined in configuration, a category registered with detailed control using Yore's addCategory API, or a full JS override for complete control.
hugo.yaml determines which categories are enabled and in what order; the JSON translation files define the display text. Both are required. JS configuration files are optional and intended for advanced use.
Create a Simple Category 此標題的連結
To create a new category called foo, follow these steps:
Append
footo thecategoriesfield inhugo.yaml.hugo.yamlparams: cookieConsent: enable: true categories: - necessary - functional - analytics - fooOverride the theme's
assets/cookies/translations/[LANG].jsonand add a new entry to thesectionsarray.assets/cookies/translations/[LANG].json{ // ... "preferencesModal": { "sections": [ // new "foo" category { "title": "Category Foo", "description": "A custom category. Provides a minimal entry point for adding categories beyond the defaults.", "linkedCategory": "foo" } ] } // ... }Use the category in your JS, for example:
if (window.CookieConsent.acceptedCategory('foo')) { // ... }Or use an inline script:
<script type="text/plain" data-category="foo"> // ... </script>
API 此標題的連結
This API is provided by Yore for advanced control. You still need to set up hugo.yaml and [LANG].json to create a new category.
addCategory 此標題的連結
vanilla-cookieconsent's CookieConsent.run() accepts a single config object. addCategory is a helper provided by Yore that registers a category and its corresponding preferences modal section into that config object before CookieConsent.run() is called.
| Parameter | Description |
|---|---|
name | Category identifier. |
categoryConfig | See the category config reference (外部連結) for all available fields (enabled, readOnly, autoClear, services). |
section | Accepts title and description. See the section reference (外部連結). If omitted, the entry must exist in the translation JSON instead. |
Example: Register a custom category
export default function ({ addCategory }) {
addCategory({
name: 'analytics',
categoryConfig: {
autoClear: {
cookies: [
{ name: /^_ga/ }, // matches _ga, _ga_XXXXXXX, etc.
{ name: '_gid' },
],
},
},
section: {
title: 'Analytics Cookies',
description: 'Help us understand how visitors interact with the site. All data is anonymous.',
},
});
}Triggered when
cookieConsent.categoriesincludesanalyticsandanalytics.gtagis enabled. ↩︎