LEYUN · SITE SPEC & COMPONENT LIBRARY独立站技术规范手册

Cookie Consent Banner Components and Consent Management Rules

Section:COMPLIANCE BANNER SPEC · Reviewed:2026-11-20 · Template asset

Key takeawayThis section defines the structure template, field dictionary, integration points and compliance boundaries for 「Cookie Consent Banner Components and Consent Management Rules」 in COMPLIANCE BANNER SPEC. Templates are for reference only; verify local regulations and platform rules before launch.

Industries and use cases

  • Sites serving regions where cookie rules apply, such as the EU and the UK
  • Sites that need consent because they use analytics, advertising or third-party tools
  • Sellers that have received complaints or penalties for loading trackers without consent
  • Sites that must load no tracking code at all before consent
Template / code snippethtml
<!-- 同意前不加载任何追踪脚本:脚本在同意后才注入 -->
<div class="cc-bar" id="ccBar" role="region"
     aria-label="Cookie 使用说明" hidden>
  <p class="cc-text">
    本站使用必要 Cookie 以保证页面功能正常;
    经你同意后,我们才会加载统计与广告相关的 Cookie。
    详情见<a href="/privacy/">隐私政策</a>。
  </p>
  <div class="cc-actions">
    <button type="button" class="cc-btn cc-accept" data-cc="all">全部接受</button>
    <button type="button" class="cc-btn cc-reject" data-cc="necessary">仅必要项</button>
    <button type="button" class="cc-btn cc-settings" data-cc="settings">偏好设置</button>
  </div>
</div>

<!-- 偏好设置面板 -->
<div class="cc-modal" id="ccSettings" hidden>
  <div class="cc-modal-in">
    <h2>Cookie 偏好设置</h2>
    <fieldset class="cc-group">
      <legend>必要项(始终启用)</legend>
      <label><input type="checkbox" checked disabled>
        会话保持、购物车、页面安全</label>
    </fieldset>
    <fieldset class="cc-group">
      <legend>统计类</legend>
      <label><input type="checkbox" data-cc-cat="analytics">
        访问统计(${analyticsProvider})</label>
    </fieldset>
    <fieldset class="cc-group">
      <legend>广告类</legend>
      <label><input type="checkbox" data-cc-cat="ads">
        广告投放与再营销(${adsProvider})</label>
    </fieldset>
    <button type="button" class="cc-btn cc-save">保存偏好</button>
  </div>
</div>

<script>
/* 关键:默认不注入任何追踪脚本。
   同意状态由 localStorage 记录,撤回后需再次注入/移除。 */
(function () {
  var CONSENT_KEY = 'cc_consent_v1';
  var bar = document.getElementById('ccBar');
  var saved = null;
  try { saved = JSON.parse(localStorage.getItem(CONSENT_KEY)); } catch (e) {}

  if (saved && saved.necessary) { bar.hidden = true; loadTrackers(saved); return; }
  bar.hidden = false;

  function loadTrackers(c) {
    if (c.analytics && !window.__ga) {
      var s = document.createElement('script');
      s.async = true; s.src = '${analyticsSrc}';
      document.head.appendChild(s); window.__ga = 1;
    }
    if (c.ads && !window.__ads) {
      var a = document.createElement('script');
      a.async = true; a.src = '${adsSrc}';
      document.head.appendChild(a); window.__ads = 1;
    }
  }

  document.querySelectorAll('[data-cc]').forEach(function (btn) {
    btn.addEventListener('click', function () {
      var mode = btn.getAttribute('data-cc');
      var c = mode === 'all'
        ? { necessary: true, analytics: true, ads: true }
        : { necessary: true, analytics: false, ads: false };
      localStorage.setItem(CONSENT_KEY, JSON.stringify(c));
      bar.hidden = true;
      loadTrackers(c);
    });
  });
})();
</script>
Fields and parameters
Field / parameterTypeDescription and values
Consent state keyTextlocalStorage key, versioned for later updates
Necessary cookiesFixedAlways enabled, no switch offered
AnalyticsToggleCan be refused separately
AdvertisingToggleCan be refused separately
Choice granularityEnumAccept all / necessary only / preferences
Withdrawal entryLink"Cookie settings" link in the footer
Dialog roleARIArole="region" or role="dialog"
CopyTextStates the purpose, no intimidating wording

SEO and structured-data notes

SEO and structured-data checkpoints
Load no trackers before consentThis is the decisive test: loading first and showing a banner afterwards is not accepted as valid consent under most interpretations
A refuse option is mandatoryA banner with only an accept button does not meet the requirement, and the refuse button should not be visually de-emphasised
Allow category-level settingsAn all-or-nothing choice is not recognised under some interpretations
Consent must be withdrawableThe footer needs an entry point, and withdrawal must actually stop tracking
List necessary and optional categories separatelyNecessary items carry no switch; optional items are listed one category at a time
The banner must not cover the main contentCovering the main content on first visit affects how search engines assess the page

Compliance risk notes

  • Refusal must not require several clicks to take effect
  • Tracking pixels must not be preloaded before consent
  • The banner must not be impossible to close with no alternative entry point
  • Intimidating wording must not be used to push consent
  • Consent records must be retained for inspection

Do not reuse this template verbatim in regulated contexts; verify local regulations and platform rules before going live.

Modification example

<!-- Modification example: replace "accept + close X" with a three-option compliant form -->
<!-- Original: accept and close only -->
<div class="cc-bar">
  <p>We use cookies to improve your experience.</p>
  <button onclick="acceptAll()">Accept</button>
  <button onclick="closeBar()">Close</button>
</div>

<!-- Modified: accept all / necessary only / preferences -->
<div class="cc-bar" id="ccBar" role="region"
     aria-label="Cookie notice" hidden>
  <p class="cc-text">
    This site uses necessary cookies to keep pages working;
    analytics and advertising cookies are loaded only after you consent.
  </p>
  <div class="cc-actions">
    <button type="button" class="cc-btn cc-accept" data-cc="all">
      Accept all</button>
    <button type="button" class="cc-btn cc-reject" data-cc="necessary">
      Necessary only</button>
    <button type="button" class="cc-btn cc-settings" data-cc="settings">
      Preferences</button>
  </div>
</div>

<!-- Two details that go wrong easily:
     1. each button states its consequence —
        "Close" does not tell the user what will happen, "Necessary only" does
     2. visual treatment: the refuse button is the same size as the accept button,
        not a small grey text link
        (some regulatory interpretations focus on how reachable the refusal is) -->

FAQ

Does the banner need three options?
At minimum accept and refuse; under most interpretations a category-level settings entry is also expected.
Should the banner appear immediately on page load?
It must not cover the main content. It may appear after load or after some scrolling, but not only after the user has visited several pages.
How long should consent be stored?
Usually six months. Most regimes require periodic re-asking, and re-asking when the banner version changes is good practice.
We have a banner but still receive complaints. Why?
The usual causes are trackers loading before the banner appears, or a refusal option that is not reachable.
Version notes2026-11-20: first release, compiled from official Google documentation and public technical sources; updated as engine rules change. Engine behaviour details follow the latest official Google documentation.

Note: this page is a summary of publicly documented search-engine rules and is for self-check reference only. Engine behaviour is subject to the latest official Google documentation; verify against your own Search Console data.