跳到正文
DEV Community· Rajneesh Aka Raj·· 2 小時前AI 評分62

如何確認 Cookie 橫幅的「拒絕」選項真的停止追蹤

Your cookie banner says Reject. Does your code listen?

AI 導讀

文章示範如何用 DevTools 檢查網站有否在用戶同意前載入追蹤項目,並介紹三種常見修正模式。在私人瀏覽視窗開啟網站,查看 Network 請求,再按「拒絕」重載並檢查 Cookies;修正方式包括延後執行指令碼、移開 iframe 和 pixel 的 src,以及待取得同意後才啟動 SDK。

正文

Most cookie banners look fine. You click Reject, the banner goes away, and everyone moves on. But the banner is only half the job. The other half is your code: every analytics tag, pixel and embed has to wait for that choice. Often it doesn't.

This isn't rare. A 2025 study found that about half of websites send a tracking cookie to a third party before the visitor has consented (PETS 2025). In 2025 Healthline agreed to a $1.55M settlement, partly because its banner didn't actually turn off tracking cookies (CMSWire).

This post covers how to check your own site in five minutes, the three patterns that fix most leaks, and how I now get an AI coding assistant to do the fix over MCP, with me approving every change.

Check your site in five minutes (no tools needed)

  1. Open a private window, so you start with no cookies and no saved choice.
  2. Open DevTools, go to Network, and tick Preserve log.
  3. Load your page and don't touch the banner.
  4. In the filter box, try collect, gtag, fbevents, pixel, clarity, hotjar, tiktok. Anything that shows up here loaded before consent.
  5. Click Reject, reload, and filter again.
  6. Check Application → Cookies too. _ga, _gid, _fbp or _clck showing up after Reject means something isn't waiting.

If step 4 or 5 shows requests, your banner is decoration. The good news: the fix is usually small.

Why it happens

Almost always it's one of these:

  • A tag pasted straight into <head>. The browser runs <script src="https://www.googletagmanager.com/gtag/js?id=G-XXXX"> the moment it parses it. Your banner hasn't even loaded yet.
  • An SDK started from your bundle. posthog.init() or analytics.load() in _app.tsx / main.ts runs on page load, whatever the visitor chose.
  • Embeds. A YouTube iframe or a map sets cookies as soon as it renders.
  • A tag manager with no consent rules. GTM loads, and every tag inside it fires on "All pages".

The three patterns that fix most leaks

These are the patterns most consent tools support in some form. The attribute names below are Kiescy's, but the idea is the same everywhere.

1. Scripts: make them inert until consent. A script with type="text/plain" is never run by the browser. The consent tool switches it on when the visitor allows that category.

<!-- before -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXX"></script>

<!-- after: tag the loader AND its inline config -->
<script type="text/plain" data-pira-category="analytics" async
        src="https://www.googletagmanager.com/gtag/js?id=G-XXXX"></script>
<script type="text/plain" data-pira-category="analytics">
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date()); gtag('config', 'G-XXXX');
</script>

In React or Next.js, check the rendered HTML: some components drop or rewrite the type attribute.

2. Iframes and pixels: move src out of the way. The browser can't contact a server it doesn't have an address for.

<iframe data-pira-category="social"
        data-pira-src="https://www.youtube.com/embed/VIDEO_ID"></iframe>

3. SDKs started in code: wrap the init.

window.pira.when("analytics", () => {
  posthog.init("KEY", { api_host: "https://eu.posthog.com" });
});

// If this can run before the consent script has loaded, queue it:
(window.piraQueue = window.piraQueue || []).push((pira) => {
  pira.when("advertising", startAds);
});

And if a visitor withdraws consent later, a script that already ran can't be unloaded. Call the SDK's opt-out (posthog.opt_out_capturing()) or reload the page.

Letting an AI assistant do it, safely

None of this is hard. It's just tedious: you have to find every tag across layouts, components and third-party snippets, and know which category each tracker belongs to. That's a good job for an AI coding assistant, if it knows what's actually wrong on the live site. Guessing from the code alone misses tags that come from a CMS, a tag manager or a plugin.

This is why I built an MCP server for Kiescy, the privacy compliance tool I work on. (Disclosure: I'm the founder.) Kiescy visits your live site in a real browser as a first-time visitor, records what loads before any choice, clicks Reject, and records what still loads. The MCP server hands those findings to your assistant:

Tool What the assistant gets
list_issues / get_issue What loaded, on which page, which category it belongs to, and the suggested fix
get_fix_prompt Step-by-step instructions for your codebase, including the tagging rules above
get_consent_api How new code should wait for consent, so the next tag you add doesn't leak
run_check / get_check Re-checks the live site after you deploy, and closes the issues that are gone

It also checks accessibility (WCAG A/AA with axe-core) and security headers, and the same tools cover those.

Connecting it in Claude Code is one line (Cursor and VS Code configs are in the repo):

claude mcp add --transport http kiescy https://mcp.kiescy.com/mcp \
  --header "Authorization: Bearer YOUR_API_KEY"

Then I just say: "Fix the Kiescy issues on this site."

The part I cared most about is that the assistant doesn't get to just start editing. The server tells it to search the code, show a plan (files and changes) and wait for an explicit OK. The key only covers one website, and through MCP the assistant can't change the banner, account settings or consent records. After I deploy, it calls run_check, and the issue only closes if the live site no longer shows it.

There's a second server, https://mcp.kiescy.com/mcp/documents, for legal pages. It asks about your business a few questions at a time, drafts or imports your privacy policy, terms and cookie policy, and only publishes after a one-time confirmation in the chat. It never guesses things like where your customers are or what you collect.

Try it

  • Run the five-minute DevTools check above on your own site. It costs nothing.
  • Or run Kiescy's free check (no sign-up) to see what your site loads before a visitor agrees.
  • To connect your assistant, create a free account (Kiescy is free during early access) and copy the key from Fix with AI. Setup guide: kiescy.com/product/fix-with-ai.

I'd love feedback from anyone building MCP servers or dealing with consent on client sites. What would you want an assistant to be allowed to do here, and what should always need a human?

Kiescy's checks help you comply. They aren't legal advice.

來源:DEV Community · dev.to