快轉到主要內容

Cookie Consent

查看原始碼編輯此頁 (外部連結)
首次發表:

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.

重要
  1. Google Ads related products are not compatible (外部連結) with vanilla-cookieconsent.
  2. 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.
  3. 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.

hugo.yaml
params:
  cookieConsent:
    enable: true
    categories:
      - necessary
      - functional
      - analytics
      - advertising

Preferences 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:

  1. Script Management: manage inline scripts.
  2. 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:

layouts/_partials/extend-head.html
<!-- Hotjar, always executed -->
<script>
  // ...
</script>

After:

layouts/_partials/extend-head.html
<script type="text/plain" data-category="analytics">
  // ...
</script>

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:

  1. Append foo to the categories field in hugo.yaml.

    hugo.yaml
    params:
      cookieConsent:
        enable: true
        categories:
          - necessary
          - functional
          - analytics
          - foo
  2. Override the theme's assets/cookies/translations/[LANG].json and add a new entry to the sections array.

    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"
          }
        ]
      }
      // ...
    }
  3. 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.

ParameterDescription
nameCategory identifier.
categoryConfigSee the category config reference (外部連結) for all available fields (enabled, readOnly, autoClear, services).
sectionAccepts title and description. See the section reference (外部連結). If omitted, the entry must exist in the translation JSON instead.

Example: Register a custom category

assets/js/cookies/config.js
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.',
    },
  });
}

  1. Triggered when cookieConsent.categories includes analytics and analytics.gtag is enabled. ↩︎

文章關聯

無障礙設定